Skip to content

Commit

Permalink
🔧 replace deprecated constants (#1461)
Browse files Browse the repository at this point in the history
* Required hass v2023.1.0
  • Loading branch information
caibinqing authored Dec 31, 2023
1 parent 987eb49 commit b8c3960
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 74 deletions.
106 changes: 53 additions & 53 deletions custom_components/xiaomi_miot/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def update_bind_sensor(self):
if num is not None:
cls = sta.attributes.get(ATTR_DEVICE_CLASS)
unit = sta.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
if cls == SensorDeviceClass.TEMPERATURE.value or unit in [TEMP_CELSIUS, TEMP_KELVIN, TEMP_FAHRENHEIT]:
if cls == SensorDeviceClass.TEMPERATURE.value or unit in [UnitOfTemperature.CELSIUS, UnitOfTemperature.KELVIN, UnitOfTemperature.FAHRENHEIT]:
ext[ATTR_CURRENT_TEMPERATURE] = self.hass.config.units.temperature(num, unit)
elif cls == SensorDeviceClass.HUMIDITY.value:
ext[ATTR_CURRENT_HUMIDITY] = num
Expand Down Expand Up @@ -158,12 +158,12 @@ def __init__(self, config: dict, miot_service: MiotService):
if miot_service.get_property('heat_level'):
self._power_modes.append('heater')
self._hvac_modes = {
HVAC_MODE_OFF: {'list': ['Off', 'Idle', 'None'], 'action': CURRENT_HVAC_OFF},
HVAC_MODE_AUTO: {'list': ['Auto', 'Manual', 'Normal']},
HVAC_MODE_COOL: {'list': ['Cool'], 'action': CURRENT_HVAC_COOL},
HVAC_MODE_HEAT: {'list': ['Heat'], 'action': CURRENT_HVAC_HEAT},
HVAC_MODE_DRY: {'list': ['Dry'], 'action': CURRENT_HVAC_DRY},
HVAC_MODE_FAN_ONLY: {'list': ['Fan'], 'action': CURRENT_HVAC_FAN},
HVACMode.OFF: {'list': ['Off', 'Idle', 'None'], 'action': HVACAction.OFF},
HVACMode.AUTO: {'list': ['Auto', 'Manual', 'Normal']},
HVACMode.COOL: {'list': ['Cool'], 'action': HVACAction.COOLING},
HVACMode.HEAT: {'list': ['Heat'], 'action': HVACAction.HEATING},
HVACMode.DRY: {'list': ['Dry'], 'action': HVACAction.DRYING},
HVACMode.FAN_ONLY: {'list': ['Fan'], 'action': HVACAction.FAN},
}
self._preset_modes = {}

Expand All @@ -185,7 +185,7 @@ async def async_added_to_hass(self):
if val is not None:
self._hvac_modes[mk]['value'] = val
mvs.append(val)
elif mk != HVAC_MODE_OFF:
elif mk != HVACMode.OFF:
dls.append(mk)
for k in dls:
self._hvac_modes.pop(k, None)
Expand All @@ -201,7 +201,7 @@ async def async_added_to_hass(self):
des = self._prop_mode.get_translation(val)
self._preset_modes[val] = des
if fst and len(self._hvac_modes) <= 1:
self._hvac_modes[HVAC_MODE_AUTO] = {
self._hvac_modes[HVACMode.AUTO] = {
'list': [fst.get('description')],
'value': fst.get('value'),
}
Expand All @@ -221,7 +221,7 @@ async def async_update(self):
elif add_fans and p:
self._subs[m] = ClimateModeSubEntity(self, p)
add_fans([self._subs[m]], update_before_add=True)
off = self._hvac_modes.get(HVAC_MODE_OFF, {}).get('value')
off = self._hvac_modes.get(HVACMode.OFF, {}).get('value')
for val, des in self._preset_modes.items():
if des in self._subs:
self._subs[des].update_from_parent()
Expand Down Expand Up @@ -274,7 +274,7 @@ def is_on(self):
if self._state_attrs.get(p.full_name):
return True
if self._prop_mode:
off = self._hvac_modes.get(HVAC_MODE_OFF, {}).get('value')
off = self._hvac_modes.get(HVACMode.OFF, {}).get('value')
if off is not None:
return off != self._prop_mode.from_dict(self._state_attrs)
power = self._state_attrs.get('power')
Expand Down Expand Up @@ -304,7 +304,7 @@ def turn_on(self, **kwargs):
if not p:
continue
return self.set_property(p, True)
for mode in (HVAC_MODE_HEAT_COOL, HVAC_MODE_AUTO, HVAC_MODE_HEAT, HVAC_MODE_COOL):
for mode in (HVACMode.HEAT_COOL, HVACMode.AUTO, HVACMode.HEAT, HVACMode.COOL):
if mode not in self.hvac_modes:
continue
return self.set_hvac_mode(mode)
Expand All @@ -316,7 +316,7 @@ def turn_off(self, **kwargs):
self.set_property(self._prop_fan_power, False)
return self.set_property(self._prop_power, False)
if self._prop_mode:
off = self._hvac_modes.get(HVAC_MODE_OFF, {}).get('value')
off = self._hvac_modes.get(HVACMode.OFF, {}).get('value')
if off is not None:
return self.set_property(self._prop_mode, off)
act = self._miot_service.get_action('stop_working', 'power_off')
Expand Down Expand Up @@ -351,7 +351,7 @@ def state(self):
@property
def hvac_mode(self):
if not self.is_on:
return HVAC_MODE_OFF
return HVACMode.OFF
if self._prop_mode:
acm = self._prop_mode.from_dict(self._state_attrs)
try:
Expand All @@ -364,7 +364,7 @@ def hvac_mode(self):
if self._prop_power:
if mod := self._vars.get('turn_on_hvac'):
return mod
return HVAC_MODE_AUTO
return HVACMode.AUTO
return None

@property
Expand All @@ -376,11 +376,11 @@ def hvac_modes(self):
continue
hms.append(mk)
if self._prop_power:
mod = self._vars.get('turn_on_hvac') or HVAC_MODE_AUTO
mod = self._vars.get('turn_on_hvac') or HVACMode.AUTO
if mod and mod not in hms:
hms.append(mod)
if HVAC_MODE_OFF not in hms:
hms.append(HVAC_MODE_OFF)
if HVACMode.OFF not in hms:
hms.append(HVACMode.OFF)
return hms

def set_hvac_mode(self, mode: str):
Expand All @@ -389,21 +389,21 @@ def set_hvac_mode(self, mode: str):
@property
def hvac_action(self):
"""Return the current running hvac operation if supported.
Need to be one of CURRENT_HVAC_*.
Need to be one of HVACAction.*.
"""
if not self.is_on:
return CURRENT_HVAC_OFF
return HVACAction.OFF
if self._miot_service.name in ['heater']:
return CURRENT_HVAC_HEAT
return HVACAction.HEATING
hvac = self.hvac_mode
if hvac is None:
return CURRENT_HVAC_IDLE
return HVACAction.IDLE
return self._hvac_modes.get(hvac, {}).get('action')

@property
def preset_mode(self):
if not self.is_on:
return HVAC_MODE_OFF
return HVACMode.OFF
if self._preset_modes and self._prop_mode:
acm = self._prop_mode.from_dict(self._state_attrs)
acm = -1 if acm is None else acm
Expand All @@ -416,12 +416,12 @@ def preset_modes(self):
if self._preset_modes:
for mk, mv in self._preset_modes.items():
pms.append(mv)
if HVAC_MODE_OFF not in pms:
pms.append(HVAC_MODE_OFF)
if HVACMode.OFF not in pms:
pms.append(HVACMode.OFF)
return pms

def set_preset_mode(self, mode: str):
if mode == HVAC_MODE_OFF:
if mode == HVACMode.OFF:
return self.turn_off()
if not self.is_on:
self.turn_on(without_modes=True)
Expand Down Expand Up @@ -450,13 +450,13 @@ def set_preset_mode(self, mode: str):
def temperature_unit(self):
prop = self._prop_temperature or self._prop_target_temp
if prop:
if prop.unit in ['celsius', TEMP_CELSIUS]:
return TEMP_CELSIUS
if prop.unit in ['fahrenheit', TEMP_FAHRENHEIT]:
return TEMP_FAHRENHEIT
if prop.unit in ['kelvin', TEMP_KELVIN]:
return TEMP_KELVIN
return TEMP_CELSIUS
if prop.unit in ['celsius', UnitOfTemperature.CELSIUS]:
return UnitOfTemperature.CELSIUS
if prop.unit in ['fahrenheit', UnitOfTemperature.FAHRENHEIT]:
return UnitOfTemperature.FAHRENHEIT
if prop.unit in ['kelvin', UnitOfTemperature.KELVIN]:
return UnitOfTemperature.KELVIN
return UnitOfTemperature.CELSIUS

@property
def current_temperature(self):
Expand Down Expand Up @@ -575,8 +575,8 @@ def fan_modes(self):
return None

def set_fan_mode(self, fan_mode: str):
if not self.is_on and HVAC_MODE_FAN_ONLY in self._hvac_modes:
self.set_hvac_mode(HVAC_MODE_FAN_ONLY)
if not self.is_on and HVACMode.FAN_ONLY in self._hvac_modes:
self.set_hvac_mode(HVACMode.FAN_ONLY)
if self._prop_fan_level:
val = self._prop_fan_level.list_value(fan_mode)
return self.set_property(self._prop_fan_level, val)
Expand Down Expand Up @@ -641,9 +641,9 @@ def is_aux_heat(self):
"""Return true if aux heater."""
if self._prop_heater:
return self._prop_heater.from_dict(self._state_attrs) and self.hvac_mode in [
HVAC_MODE_AUTO,
HVAC_MODE_HEAT,
HVAC_MODE_HEAT_COOL,
HVACMode.AUTO,
HVACMode.HEAT,
HVACMode.HEAT_COOL,
]
raise NotImplementedError

Expand Down Expand Up @@ -736,12 +736,12 @@ def __init__(self, config: dict, miot_service: MiotService):

self._attr_hvac_mode = None
self._hvac_modes = {
HVAC_MODE_OFF: {'list': ['Off', 'Idle', 'None'], 'action': CURRENT_HVAC_OFF},
HVAC_MODE_AUTO: {'list': ['Auto', 'Manual', 'Normal']},
HVAC_MODE_COOL: {'list': ['Cool'], 'action': CURRENT_HVAC_COOL},
HVAC_MODE_HEAT: {'list': ['Heat'], 'action': CURRENT_HVAC_HEAT},
HVAC_MODE_DRY: {'list': ['Dry'], 'action': CURRENT_HVAC_DRY},
HVAC_MODE_FAN_ONLY: {'list': ['Fan'], 'action': CURRENT_HVAC_FAN},
HVACMode.OFF: {'list': ['Off', 'Idle', 'None'], 'action': HVACAction.OFF},
HVACMode.AUTO: {'list': ['Auto', 'Manual', 'Normal']},
HVACMode.COOL: {'list': ['Cool'], 'action': HVACAction.COOLING},
HVACMode.HEAT: {'list': ['Heat'], 'action': HVACAction.HEATING},
HVACMode.DRY: {'list': ['Dry'], 'action': HVACAction.DRYING},
HVACMode.FAN_ONLY: {'list': ['Fan'], 'action': HVACAction.FAN},
}
self._attr_hvac_modes = []
self._prop_mode = miot_service.get_property('ir_mode')
Expand All @@ -751,11 +751,11 @@ def __init__(self, config: dict, miot_service: MiotService):
if val is not None:
self._hvac_modes[mk]['value'] = val
self._attr_hvac_modes.append(mk)
elif mk == HVAC_MODE_OFF:
elif mk == HVACMode.OFF:
self._attr_hvac_modes.append(mk)

self._attr_current_temperature = None
self._attr_temperature_unit = TEMP_CELSIUS
self._attr_temperature_unit = UnitOfTemperature.CELSIUS
self._attr_max_temp = DEFAULT_MAX_TEMP
self._attr_min_temp = DEFAULT_MIN_TEMP
self._attr_target_temperature_step = 1
Expand Down Expand Up @@ -799,27 +799,27 @@ async def async_update(self):

@property
def is_on(self):
return self.hvac_mode not in [None, HVAC_MODE_OFF]
return self.hvac_mode not in [None, HVACMode.OFF]

def turn_on(self, **kwargs):
"""Turn the entity on."""
if not self._act_turn_on:
raise NotImplementedError()
if ret := self.call_action(self._act_turn_on):
self._attr_hvac_mode = HVAC_MODE_AUTO
self._attr_hvac_mode = HVACMode.AUTO
return ret

def turn_off(self, **kwargs):
"""Turn the entity off."""
if not self._act_turn_off:
raise NotImplementedError()
if ret := self.call_action(self._act_turn_off):
self._attr_hvac_mode = HVAC_MODE_OFF
self._attr_hvac_mode = HVACMode.OFF
return ret

def set_hvac_mode(self, hvac_mode):
"""Set new target hvac mode."""
if hvac_mode == HVAC_MODE_OFF:
if hvac_mode == HVACMode.OFF:
return self.turn_off()
if not self._prop_mode:
raise NotImplementedError()
Expand All @@ -833,13 +833,13 @@ def set_hvac_mode(self, hvac_mode):
@property
def hvac_action(self):
"""Return the current running hvac operation if supported.
Need to be one of CURRENT_HVAC_*.
Need to be one of HVACAction.*.
"""
if not self.is_on:
return CURRENT_HVAC_OFF
return HVACAction.OFF
hvac = self.hvac_mode
if hvac is None:
return CURRENT_HVAC_IDLE
return HVACAction.IDLE
return self._hvac_modes.get(hvac, {}).get('action')

def set_temperature(self, **kwargs):
Expand Down
16 changes: 8 additions & 8 deletions custom_components/xiaomi_miot/core/miot_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,23 +745,23 @@ def unit_of_measurement(self):
name = self.name
unit = self.unit
aliases = {
'celsius': TEMP_CELSIUS,
'fahrenheit': TEMP_FAHRENHEIT,
'kelvin': TEMP_KELVIN,
'celsius': UnitOfTemperature.CELSIUS,
'fahrenheit': UnitOfTemperature.FAHRENHEIT,
'kelvin': UnitOfTemperature.KELVIN,
'percentage': PERCENTAGE,
'lux': LIGHT_LUX,
'watt': POWER_WATT,
'pascal': PRESSURE_PA,
'watt': UnitOfPower.WATT,
'pascal': UnitOfPressure.PA,
'μg/m3': CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
'mg/m3': CONCENTRATION_MILLIGRAMS_PER_CUBIC_METER,
'p/m3': CONCENTRATION_PARTS_PER_CUBIC_METER,
}
names = {
'current_step_count': 'steps',
'heart_rate': 'bpm',
'power_consumption': ENERGY_WATT_HOUR,
'electric_current': ELECTRIC_CURRENT_AMPERE,
'voltage': ELECTRIC_POTENTIAL_VOLT,
'power_consumption': UnitOfEnergy.WATT_HOUR,
'electric_current': UnitOfElectricCurrent.AMPERE,
'voltage': UnitOfElectricPotential.VOLT,
'pm2_5_density': CONCENTRATION_MICROGRAMS_PER_CUBIC_METER,
'tds_in': CONCENTRATION_PARTS_PER_MILLION,
'tds_out': CONCENTRATION_PARTS_PER_MILLION,
Expand Down
4 changes: 2 additions & 2 deletions custom_components/xiaomi_miot/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from homeassistant.components.device_tracker import (
DOMAIN as ENTITY_DOMAIN,
)
from homeassistant.components.device_tracker.const import SOURCE_TYPE_GPS
from homeassistant.components.device_tracker.const import SourceType
from homeassistant.components.device_tracker.config_entry import TrackerEntity

from . import (
Expand Down Expand Up @@ -108,7 +108,7 @@ def should_poll(self):
@property
def source_type(self):
"""Return the source type, eg gps or router, of the device."""
return SOURCE_TYPE_GPS
return SourceType.GPS

@property
def latitude(self):
Expand Down
4 changes: 2 additions & 2 deletions custom_components/xiaomi_miot/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def __init__(self, config):
self._subs = {
'tds_in': {'keys': ['tds_warn_thd'], 'unit': CONCENTRATION_PARTS_PER_MILLION, 'icon': 'mdi:water'},
'tds_out': {'keys': ['tds_warn_thd'], 'unit': CONCENTRATION_PARTS_PER_MILLION, 'icon': 'mdi:water-check'},
'temperature': {'class': SensorDeviceClass.TEMPERATURE, 'unit': TEMP_CELSIUS},
'temperature': {'class': SensorDeviceClass.TEMPERATURE, 'unit': UnitOfTemperature.CELSIUS},
}
for i in [1, 2, 3]:
self._subs.update({
Expand All @@ -546,7 +546,7 @@ def __init__(self, config):
},
f'f{i}_remain_days': {
'keys': [f'f{i}_totaltime', f'f{i}_usedtime'],
'unit': TIME_DAYS,
'unit': UnitOfTime.DAYS,
'icon': 'mdi:clock',
},
})
Expand Down
14 changes: 7 additions & 7 deletions custom_components/xiaomi_miot/water_heater.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,13 @@ def current_temperature(self):
def temperature_unit(self):
prop = self._prop_temperature or self._prop_target_temp
if prop:
if prop.unit in ['celsius', TEMP_CELSIUS]:
return TEMP_CELSIUS
if prop.unit in ['fahrenheit', TEMP_FAHRENHEIT]:
return TEMP_FAHRENHEIT
if prop.unit in ['kelvin', TEMP_KELVIN]:
return TEMP_KELVIN
return TEMP_CELSIUS
if prop.unit in ['celsius', UnitOfTemperature.CELSIUS]:
return UnitOfTemperature.CELSIUS
if prop.unit in ['fahrenheit', UnitOfTemperature.FAHRENHEIT]:
return UnitOfTemperature.FAHRENHEIT
if prop.unit in ['kelvin', UnitOfTemperature.KELVIN]:
return UnitOfTemperature.KELVIN
return UnitOfTemperature.CELSIUS

def set_temperature(self, **kwargs):
"""Set new target temperature."""
Expand Down
4 changes: 2 additions & 2 deletions hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"zip_release": true,
"filename": "xiaomi_miot.zip",
"render_readme": true,
"homeassistant": "2022.7.0"
}
"homeassistant": "2023.1.0"
}

0 comments on commit b8c3960

Please sign in to comment.