Compare commits

..

1 Commits

Author SHA1 Message Date
Li Shuzhen
ee91c30ecc
Merge ed4812d166 into 8778b00c3a 2025-01-22 11:45:05 +00:00
6 changed files with 270 additions and 211 deletions

View File

@ -53,8 +53,17 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.components.climate import (
FAN_ON, FAN_OFF, SWING_OFF, SWING_BOTH, SWING_VERTICAL, SWING_HORIZONTAL,
ATTR_TEMPERATURE, HVACMode, ClimateEntity, ClimateEntityFeature)
FAN_ON,
FAN_OFF,
SWING_OFF,
SWING_BOTH,
SWING_VERTICAL,
SWING_HORIZONTAL,
ATTR_TEMPERATURE,
HVACMode,
ClimateEntity,
ClimateEntityFeature
)
from .miot.const import DOMAIN
from .miot.miot_device import MIoTDevice, MIoTServiceEntity, MIoTEntityData
@ -63,8 +72,11 @@ from .miot.miot_spec import MIoTSpecProperty
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback) -> None:
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback
) -> None:
"""Set up a config entry."""
device_list: list[MIoTDevice] = hass.data[DOMAIN]['devices'][
config_entry.entry_id]
@ -92,9 +104,9 @@ class FeatureOnOff(MIoTServiceEntity, ClimateEntity):
"""TURN_ON and TURN_OFF feature of the climate entity."""
_prop_on: Optional[MIoTSpecProperty]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
"""Initialize the feature class."""
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
self._prop_on = None
super().__init__(miot_device=miot_device, entity_data=entity_data)
@ -102,11 +114,12 @@ class FeatureOnOff(MIoTServiceEntity, ClimateEntity):
for prop in entity_data.props:
if prop.name == 'on':
if (
# The "on" property of the "fan-control" service is not
# the on/off feature of the entity.
prop.service.name == 'air-conditioner' or
prop.service.name == 'heater' or
prop.service.name == 'thermostat'):
# The "on" property of the "fan-control" service is not
# the on/off feature of the entity.
prop.service.name == 'air-conditioner'
or prop.service.name == 'heater'
or prop.service.name == 'thermostat'
):
self._attr_supported_features |= (
ClimateEntityFeature.TURN_ON)
self._attr_supported_features |= (
@ -126,9 +139,9 @@ class FeatureTargetTemperature(MIoTServiceEntity, ClimateEntity):
"""TARGET_TEMPERATURE feature of the climate entity."""
_prop_target_temp: Optional[MIoTSpecProperty]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
"""Initialize the feature class."""
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
self._prop_target_temp = None
super().__init__(miot_device=miot_device, entity_data=entity_data)
@ -149,7 +162,7 @@ class FeatureTargetTemperature(MIoTServiceEntity, ClimateEntity):
self._prop_target_temp = prop
async def async_set_temperature(self, **kwargs):
"""Set the target temperature."""
"""Set new target temperature."""
if ATTR_TEMPERATURE in kwargs:
temp = kwargs[ATTR_TEMPERATURE]
if temp > self._attr_max_temp:
@ -157,14 +170,15 @@ class FeatureTargetTemperature(MIoTServiceEntity, ClimateEntity):
elif temp < self._attr_min_temp:
temp = self._attr_min_temp
await self.set_property_async(prop=self._prop_target_temp,
value=temp)
await self.set_property_async(
prop=self._prop_target_temp, value=temp)
@property
def target_temperature(self) -> Optional[float]:
"""The current target temperature."""
return (self.get_prop_value(
prop=self._prop_target_temp) if self._prop_target_temp else None)
"""Return the target temperature."""
return (
self.get_prop_value(prop=self._prop_target_temp)
if self._prop_target_temp else None)
class FeaturePresetMode(MIoTServiceEntity, ClimateEntity):
@ -172,9 +186,9 @@ class FeaturePresetMode(MIoTServiceEntity, ClimateEntity):
_prop_mode: Optional[MIoTSpecProperty]
_mode_map: Optional[dict[int, str]]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
"""Initialize the feature class."""
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
self._prop_mode = None
self._mode_map = None
@ -183,8 +197,9 @@ class FeaturePresetMode(MIoTServiceEntity, ClimateEntity):
for prop in entity_data.props:
if prop.name == 'heat-level' and prop.service.name == 'heater':
if not prop.value_list:
_LOGGER.error('invalid heater heat-level value_list, %s',
self.entity_id)
_LOGGER.error(
'invalid heater heat-level value_list, %s',
self.entity_id)
continue
self._mode_map = prop.value_list.to_map()
self._attr_preset_modes = prop.value_list.descriptions
@ -194,17 +209,17 @@ class FeaturePresetMode(MIoTServiceEntity, ClimateEntity):
async def async_set_preset_mode(self, preset_mode: str) -> None:
"""Set the preset mode."""
await self.set_property_async(self._prop_mode,
value=self.get_map_key(
map_=self._mode_map,
value=preset_mode))
await self.set_property_async(
self._prop_mode,
value=self.get_map_key(map_=self._mode_map, value=preset_mode))
@property
def preset_mode(self) -> Optional[str]:
"""The current preset mode."""
return (self.get_map_value(
map_=self._mode_map, key=self.get_prop_value(
prop=self._prop_mode)) if self._prop_mode else None)
return (
self.get_map_value(
map_=self._mode_map,
key=self.get_prop_value(prop=self._prop_mode))
if self._prop_mode else None)
class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
@ -213,9 +228,9 @@ class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
_prop_fan_level: Optional[MIoTSpecProperty]
_fan_mode_map: Optional[dict[int, str]]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
"""Initialize the feature class."""
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
self._prop_fan_on = None
self._prop_fan_level = None
self._fan_mode_map = None
@ -225,8 +240,8 @@ class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
for prop in entity_data.props:
if prop.name == 'fan-level' and prop.service.name == 'fan-control':
if not prop.value_list:
_LOGGER.error('invalid fan-level value_list, %s',
self.entity_id)
_LOGGER.error(
'invalid fan-level value_list, %s', self.entity_id)
continue
self._fan_mode_map = prop.value_list.to_map()
self._attr_fan_modes = prop.value_list.descriptions
@ -250,11 +265,14 @@ class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
if fan_mode == FAN_ON:
await self.set_property_async(prop=self._prop_fan_on, value=True)
return
mode_value = self.get_map_key(map_=self._fan_mode_map, value=fan_mode)
mode_value = self.get_map_key(
map_=self._fan_mode_map, value=fan_mode)
if mode_value is None or not await self.set_property_async(
prop=self._prop_fan_level, value=mode_value):
raise RuntimeError(f'set climate prop.fan_mode failed, {fan_mode}, '
f'{self.entity_id}')
prop=self._prop_fan_level, value=mode_value
):
raise RuntimeError(
f'set climate prop.fan_mode failed, {fan_mode}, '
f'{self.entity_id}')
@property
def fan_mode(self) -> Optional[str]:
@ -262,8 +280,9 @@ class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
if self._prop_fan_level is None and self._prop_fan_on is None:
return None
if self._prop_fan_level is None and self._prop_fan_on:
return (FAN_ON if self.get_prop_value(
prop=self._prop_fan_on) else FAN_OFF)
return (
FAN_ON if self.get_prop_value(prop=self._prop_fan_on)
else FAN_OFF)
return self.get_map_value(
map_=self._fan_mode_map,
key=self.get_prop_value(prop=self._prop_fan_level))
@ -274,9 +293,9 @@ class FeatureSwingMode(MIoTServiceEntity, ClimateEntity):
_prop_horizontal_swing: Optional[MIoTSpecProperty]
_prop_vertical_swing: Optional[MIoTSpecProperty]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
"""Initialize the feature class."""
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
self._prop_horizontal_swing = None
self._prop_vertical_swing = None
@ -301,23 +320,23 @@ class FeatureSwingMode(MIoTServiceEntity, ClimateEntity):
async def async_set_swing_mode(self, swing_mode):
"""Set the target swing operation."""
if swing_mode == SWING_BOTH:
await self.set_property_async(prop=self._prop_horizontal_swing,
value=True)
await self.set_property_async(prop=self._prop_vertical_swing,
value=True)
await self.set_property_async(
prop=self._prop_horizontal_swing, value=True)
await self.set_property_async(
prop=self._prop_vertical_swing, value=True)
elif swing_mode == SWING_HORIZONTAL:
await self.set_property_async(prop=self._prop_horizontal_swing,
value=True)
await self.set_property_async(
prop=self._prop_horizontal_swing, value=True)
elif swing_mode == SWING_VERTICAL:
await self.set_property_async(prop=self._prop_vertical_swing,
value=True)
await self.set_property_async(
prop=self._prop_vertical_swing, value=True)
elif swing_mode == SWING_OFF:
if self._prop_horizontal_swing:
await self.set_property_async(prop=self._prop_horizontal_swing,
value=False)
await self.set_property_async(
prop=self._prop_horizontal_swing, value=False)
if self._prop_vertical_swing:
await self.set_property_async(prop=self._prop_vertical_swing,
value=False)
await self.set_property_async(
prop=self._prop_vertical_swing, value=False)
else:
raise RuntimeError(
f'unknown swing_mode, {swing_mode}, {self.entity_id}')
@ -325,14 +344,17 @@ class FeatureSwingMode(MIoTServiceEntity, ClimateEntity):
@property
def swing_mode(self) -> Optional[str]:
"""The current swing mode of the fan."""
if (self._prop_horizontal_swing is None and
self._prop_vertical_swing is None):
if (
self._prop_horizontal_swing is None
and self._prop_vertical_swing is None
):
return None
horizontal: bool = (self.get_prop_value(
prop=self._prop_horizontal_swing)
if self._prop_horizontal_swing else False)
vertical: bool = (self.get_prop_value(prop=self._prop_vertical_swing)
if self._prop_vertical_swing else False)
horizontal: bool = (
self.get_prop_value(prop=self._prop_horizontal_swing)
if self._prop_horizontal_swing else False)
vertical: bool = (
self.get_prop_value(prop=self._prop_vertical_swing)
if self._prop_vertical_swing else False)
if horizontal and vertical:
return SWING_BOTH
elif horizontal:
@ -347,9 +369,9 @@ class FeatureTemperature(MIoTServiceEntity, ClimateEntity):
"""Temperature of the climate entity."""
_prop_env_temperature: Optional[MIoTSpecProperty]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
"""Initialize the feature class."""
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
self._prop_env_temperature = None
super().__init__(miot_device=miot_device, entity_data=entity_data)
@ -361,17 +383,18 @@ class FeatureTemperature(MIoTServiceEntity, ClimateEntity):
@property
def current_temperature(self) -> Optional[float]:
"""The current environment temperature."""
return (self.get_prop_value(prop=self._prop_env_temperature)
if self._prop_env_temperature else None)
return (
self.get_prop_value(prop=self._prop_env_temperature)
if self._prop_env_temperature else None)
class FeatureHumidity(MIoTServiceEntity, ClimateEntity):
"""Humidity of the climate entity."""
_prop_env_humidity: Optional[MIoTSpecProperty]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
"""Initialize the feature class."""
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
self._prop_env_humidity = None
super().__init__(miot_device=miot_device, entity_data=entity_data)
@ -383,17 +406,18 @@ class FeatureHumidity(MIoTServiceEntity, ClimateEntity):
@property
def current_humidity(self) -> Optional[float]:
"""The current environment humidity."""
return (self.get_prop_value(
prop=self._prop_env_humidity) if self._prop_env_humidity else None)
return (
self.get_prop_value(prop=self._prop_env_humidity)
if self._prop_env_humidity else None)
class FeatureTargetHumidity(MIoTServiceEntity, ClimateEntity):
"""TARGET_HUMIDITY feature of the climate entity."""
_prop_target_humidity: Optional[MIoTSpecProperty]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
"""Initialize the feature class."""
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
self._prop_target_humidity = None
super().__init__(miot_device=miot_device, entity_data=entity_data)
@ -417,23 +441,30 @@ class FeatureTargetHumidity(MIoTServiceEntity, ClimateEntity):
humidity = self._attr_max_humidity
elif humidity < self._attr_min_humidity:
humidity = self._attr_min_humidity
await self.set_property_async(prop=self._prop_target_humidity,
value=humidity)
await self.set_property_async(
prop=self._prop_target_humidity, value=humidity)
@property
def target_humidity(self) -> Optional[int]:
"""The current target humidity."""
return (self.get_prop_value(prop=self._prop_target_humidity)
if self._prop_target_humidity else None)
return (
self.get_prop_value(prop=self._prop_target_humidity)
if self._prop_target_humidity else None)
class Heater(FeatureOnOff, FeatureTargetTemperature, FeatureTemperature,
FeatureHumidity, FeaturePresetMode):
class Heater(
FeatureOnOff,
FeatureTargetTemperature,
FeatureTemperature,
FeatureHumidity,
FeaturePresetMode
):
"""Heater"""
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
"""Initialize the heater."""
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
"""Initialize the Heater."""
super().__init__(miot_device=miot_device, entity_data=entity_data)
self._attr_icon = 'mdi:radiator'
@ -449,22 +480,30 @@ class Heater(FeatureOnOff, FeatureTargetTemperature, FeatureTemperature,
@property
def hvac_mode(self) -> Optional[HVACMode]:
"""The current hvac mode."""
return (HVACMode.HEAT if self.get_prop_value(
prop=self._prop_on) else HVACMode.OFF)
return (
HVACMode.HEAT if self.get_prop_value(prop=self._prop_on)
else HVACMode.OFF)
class AirConditioner(FeatureOnOff, FeatureTargetTemperature,
FeatureTargetHumidity, FeatureTemperature, FeatureHumidity,
FeatureFanMode, FeatureSwingMode):
class AirConditioner(
FeatureOnOff,
FeatureTargetTemperature,
FeatureTargetHumidity,
FeatureTemperature,
FeatureHumidity,
FeatureFanMode,
FeatureSwingMode
):
"""Air conditioner"""
_prop_mode: Optional[MIoTSpecProperty]
_hvac_mode_map: Optional[dict[int, HVACMode]]
_prop_ac_state: Optional[MIoTSpecProperty]
_value_ac_state: Optional[dict[str, int]]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
"""Initialize the air conditioner."""
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
"""Initialize the Heater."""
self._prop_mode = None
self._hvac_mode_map = None
self._prop_ac_state = None
@ -473,11 +512,11 @@ class AirConditioner(FeatureOnOff, FeatureTargetTemperature,
super().__init__(miot_device=miot_device, entity_data=entity_data)
self._attr_icon = 'mdi:air-conditioner'
# hvac modes
self._attr_hvac_modes = None
for prop in entity_data.props:
if prop.name == 'mode':
if not prop.value_list:
_LOGGER.error('invalid mode value_list, %s', self.entity_id)
_LOGGER.error(
'invalid mode value_list, %s', self.entity_id)
continue
self._hvac_mode_map = {}
for item in prop.value_list.items:
@ -498,8 +537,8 @@ class AirConditioner(FeatureOnOff, FeatureTargetTemperature,
elif prop.name == 'ac-state':
self._prop_ac_state = prop
self._value_ac_state = {}
self.sub_prop_changed(prop=prop,
handler=self.__ac_state_changed)
self.sub_prop_changed(
prop=prop, handler=self.__ac_state_changed)
if self._attr_hvac_modes is None:
self._attr_hvac_modes = [HVACMode.OFF]
@ -510,22 +549,24 @@ class AirConditioner(FeatureOnOff, FeatureTargetTemperature,
"""Set the target hvac mode."""
# set the device off
if hvac_mode == HVACMode.OFF:
if not await self.set_property_async(prop=self._prop_on,
value=False):
raise RuntimeError(f'set climate prop.on failed, {hvac_mode}, '
f'{self.entity_id}')
if not await self.set_property_async(
prop=self._prop_on, value=False
):
raise RuntimeError(
f'set climate prop.on failed, {hvac_mode}, '
f'{self.entity_id}')
return
# set the device on
if self.get_prop_value(prop=self._prop_on) is False:
await self.set_property_async(prop=self._prop_on,
value=True,
write_ha_state=False)
elif self.get_prop_value(prop=self._prop_on) is False:
await self.set_property_async(prop=self._prop_on, value=True)
# set mode
if self._prop_mode is None:
return
mode_value = self.get_map_key(map_=self._hvac_mode_map, value=hvac_mode)
mode_value = self.get_map_key(
map_=self._hvac_mode_map, value=hvac_mode)
if mode_value is None or not await self.set_property_async(
prop=self._prop_mode, value=mode_value):
prop=self._prop_mode, value=mode_value
):
raise RuntimeError(
f'set climate prop.mode failed, {hvac_mode}, {self.entity_id}')
@ -534,10 +575,11 @@ class AirConditioner(FeatureOnOff, FeatureTargetTemperature,
"""The current hvac mode."""
if self.get_prop_value(prop=self._prop_on) is False:
return HVACMode.OFF
return (self.get_map_value(map_=self._hvac_mode_map,
key=self.get_prop_value(
prop=self._prop_mode))
if self._prop_mode else None)
return (
self.get_map_value(
map_=self._hvac_mode_map,
key=self.get_prop_value(prop=self._prop_mode))
if self._prop_mode else None)
def __ac_state_changed(self, prop: MIoTSpecProperty, value: Any) -> None:
del prop
@ -567,41 +609,51 @@ class AirConditioner(FeatureOnOff, FeatureTargetTemperature,
4: HVACMode.DRY,
}.get(v_ac_state['M'], None)
if mode:
self.set_prop_value(prop=self._prop_mode,
value=self.get_map_key(
map_=self._hvac_mode_map, value=mode))
self.set_prop_value(
prop=self._prop_mode,
value=self.get_map_key(
map_=self._hvac_mode_map, value=mode))
# T: target temperature
if 'T' in v_ac_state and self._prop_target_temp:
self.set_prop_value(prop=self._prop_target_temp,
value=v_ac_state['T'])
self.set_prop_value(
prop=self._prop_target_temp, value=v_ac_state['T'])
# S: fan level. 0: auto, 1: low, 2: media, 3: high
if 'S' in v_ac_state and self._prop_fan_level:
self.set_prop_value(prop=self._prop_fan_level,
value=v_ac_state['S'])
self.set_prop_value(
prop=self._prop_fan_level, value=v_ac_state['S'])
# D: swing mode. 0: on, 1: off
if ('D' in v_ac_state and self._attr_swing_modes and
len(self._attr_swing_modes) == 2):
if (SWING_HORIZONTAL in self._attr_swing_modes and
self._prop_horizontal_swing):
self.set_prop_value(prop=self._prop_horizontal_swing,
value=v_ac_state['D'] == 0)
elif (SWING_VERTICAL in self._attr_swing_modes and
self._prop_vertical_swing):
self.set_prop_value(prop=self._prop_vertical_swing,
value=v_ac_state['D'] == 0)
if 'D' in v_ac_state and len(self._attr_swing_modes) == 2:
if (
SWING_HORIZONTAL in self._attr_swing_modes
and self._prop_horizontal_swing
):
self.set_prop_value(
prop=self._prop_horizontal_swing, value=v_ac_state[
'D'] == 0)
elif (
SWING_VERTICAL in self._attr_swing_modes
and self._prop_vertical_swing
):
self.set_prop_value(
prop=self._prop_vertical_swing, value=v_ac_state['D'] == 0)
self._value_ac_state.update(v_ac_state)
_LOGGER.debug('ac_state update, %s', self._value_ac_state)
class PtcBathHeater(FeatureTargetTemperature, FeatureTemperature,
FeatureFanMode, FeatureSwingMode):
class PtcBathHeater(
FeatureTargetTemperature,
FeatureTemperature,
FeatureFanMode,
FeatureSwingMode
):
"""Ptc bath heater"""
_prop_mode: Optional[MIoTSpecProperty]
_hvac_mode_map: Optional[dict[int, HVACMode]]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
"""Initialize the ptc bath heater."""
self._prop_mode = None
self._hvac_mode_map = None
@ -612,7 +664,8 @@ class PtcBathHeater(FeatureTargetTemperature, FeatureTemperature,
for prop in entity_data.props:
if prop.name == 'mode':
if not prop.value_list:
_LOGGER.error('invalid mode value_list, %s', self.entity_id)
_LOGGER.error(
'invalid mode value_list, %s', self.entity_id)
continue
self._hvac_mode_map = {}
for item in prop.value_list.items:
@ -635,29 +688,38 @@ class PtcBathHeater(FeatureTargetTemperature, FeatureTemperature,
"""Set the target hvac mode."""
if self._prop_mode is None:
return
mode_value = self.get_map_key(map_=self._hvac_mode_map, value=hvac_mode)
mode_value = self.get_map_key(
map_=self._hvac_mode_map, value=hvac_mode)
if mode_value is None or not await self.set_property_async(
prop=self._prop_mode, value=mode_value):
prop=self._prop_mode, value=mode_value
):
raise RuntimeError(
f'set climate prop.mode failed, {hvac_mode}, {self.entity_id}')
@property
def hvac_mode(self) -> Optional[HVACMode]:
"""The current hvac mode."""
return (self.get_map_value(map_=self._hvac_mode_map,
key=self.get_prop_value(
prop=self._prop_mode))
if self._prop_mode else None)
return (
self.get_map_value(
map_=self._hvac_mode_map,
key=self.get_prop_value(prop=self._prop_mode))
if self._prop_mode else None)
class Thermostat(FeatureOnOff, FeatureTargetTemperature, FeatureTemperature,
FeatureHumidity, FeatureFanMode):
class Thermostat(
FeatureOnOff,
FeatureTargetTemperature,
FeatureTemperature,
FeatureHumidity,
FeatureFanMode
):
"""Thermostat"""
_prop_mode: Optional[MIoTSpecProperty]
_hvac_mode_map: Optional[dict[int, HVACMode]]
def __init__(self, miot_device: MIoTDevice,
entity_data: MIoTEntityData) -> None:
def __init__(
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
) -> None:
"""Initialize the thermostat."""
self._prop_mode = None
self._hvac_mode_map = None
@ -665,11 +727,11 @@ class Thermostat(FeatureOnOff, FeatureTargetTemperature, FeatureTemperature,
super().__init__(miot_device=miot_device, entity_data=entity_data)
self._attr_icon = 'mdi:thermostat'
# hvac modes
self._attr_hvac_modes = None
for prop in entity_data.props:
if prop.name == 'mode':
if not prop.value_list:
_LOGGER.error('invalid mode value_list, %s', self.entity_id)
_LOGGER.error(
'invalid mode value_list, %s', self.entity_id)
continue
self._hvac_mode_map = {}
for item in prop.value_list.items:
@ -694,13 +756,15 @@ class Thermostat(FeatureOnOff, FeatureTargetTemperature, FeatureTemperature,
self._attr_hvac_modes.insert(0, HVACMode.OFF)
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
"""Set the target hvac mode."""
"""Set new target hvac mode."""
# set the device off
if hvac_mode == HVACMode.OFF:
if not await self.set_property_async(prop=self._prop_on,
value=False):
raise RuntimeError(f'set climate prop.on failed, {hvac_mode}, '
f'{self.entity_id}')
if not await self.set_property_async(
prop=self._prop_on, value=False
):
raise RuntimeError(
f'set climate prop.on failed, {hvac_mode}, '
f'{self.entity_id}')
return
# set the device on
elif self.get_prop_value(prop=self._prop_on) is False:
@ -708,9 +772,12 @@ class Thermostat(FeatureOnOff, FeatureTargetTemperature, FeatureTemperature,
# set mode
if self._prop_mode is None:
return
mode_value = self.get_map_key(map_=self._hvac_mode_map, value=hvac_mode)
mode_value = self.get_map_key(
map_=self._hvac_mode_map, value=hvac_mode
)
if mode_value is None or not await self.set_property_async(
prop=self._prop_mode, value=mode_value):
prop=self._prop_mode, value=mode_value
):
raise RuntimeError(
f'set climate prop.mode failed, {hvac_mode}, {self.entity_id}')
@ -719,7 +786,8 @@ class Thermostat(FeatureOnOff, FeatureTargetTemperature, FeatureTemperature,
"""The current hvac mode."""
if self.get_prop_value(prop=self._prop_on) is False:
return HVACMode.OFF
return (self.get_map_value(map_=self._hvac_mode_map,
key=self.get_prop_value(
prop=self._prop_mode))
if self._prop_mode else None)
return (
self.get_map_value(
map_=self._hvac_mode_map,
key=self.get_prop_value(prop=self._prop_mode))
if self._prop_mode else None)

View File

@ -200,7 +200,7 @@ class Cover(MIoTServiceEntity, CoverEntity):
if pos is None:
return None
pos = round(pos*self._prop_position_value_range/100)
await self.set_property_async(
return await self.set_property_async(
prop=self._prop_target_position, value=pos)
@property

View File

@ -303,7 +303,7 @@ class Fan(MIoTServiceEntity, FanEntity):
fan_level = self.get_prop_value(prop=self._prop_fan_level)
if fan_level is None:
return None
if self._speed_names and self._speed_name_map:
if self._speed_names:
return ordered_list_item_to_percentage(
self._speed_names, self._speed_name_map[fan_level])
else:

View File

@ -96,7 +96,7 @@ class Light(MIoTServiceEntity, LightEntity):
"""Light entities for Xiaomi Home."""
# pylint: disable=unused-argument
_VALUE_RANGE_MODE_COUNT_MAX = 30
_prop_on: Optional[MIoTSpecProperty]
_prop_on: MIoTSpecProperty
_prop_brightness: Optional[MIoTSpecProperty]
_prop_color_temp: Optional[MIoTSpecProperty]
_prop_color: Optional[MIoTSpecProperty]
@ -250,25 +250,23 @@ class Light(MIoTServiceEntity, LightEntity):
Shall set attributes in kwargs if applicable.
"""
result: bool = False
# on
# Dirty logic for lumi.gateway.mgl03 indicator light
if self._prop_on:
value_on = True if self._prop_on.format_ == bool else 1
await self.set_property_async(
prop=self._prop_on, value=value_on)
value_on = True if self._prop_on.format_ == bool else 1
result = await self.set_property_async(
prop=self._prop_on, value=value_on)
# brightness
if ATTR_BRIGHTNESS in kwargs:
brightness = brightness_to_value(
self._brightness_scale, kwargs[ATTR_BRIGHTNESS])
await self.set_property_async(
prop=self._prop_brightness, value=brightness,
write_ha_state=False)
result = await self.set_property_async(
prop=self._prop_brightness, value=brightness)
# color-temperature
if ATTR_COLOR_TEMP_KELVIN in kwargs:
await self.set_property_async(
result = await self.set_property_async(
prop=self._prop_color_temp,
value=kwargs[ATTR_COLOR_TEMP_KELVIN],
write_ha_state=False)
value=kwargs[ATTR_COLOR_TEMP_KELVIN])
self._attr_color_mode = ColorMode.COLOR_TEMP
# rgb color
if ATTR_RGB_COLOR in kwargs:
@ -276,23 +274,19 @@ class Light(MIoTServiceEntity, LightEntity):
g = kwargs[ATTR_RGB_COLOR][1]
b = kwargs[ATTR_RGB_COLOR][2]
rgb = (r << 16) | (g << 8) | b
await self.set_property_async(
prop=self._prop_color, value=rgb,
write_ha_state=False)
result = await self.set_property_async(
prop=self._prop_color, value=rgb)
self._attr_color_mode = ColorMode.RGB
# mode
if ATTR_EFFECT in kwargs:
await self.set_property_async(
result = await self.set_property_async(
prop=self._prop_mode,
value=self.get_map_key(
map_=self._mode_map, value=kwargs[ATTR_EFFECT]),
write_ha_state=False)
self.async_write_ha_state()
map_=self._mode_map, value=kwargs[ATTR_EFFECT]))
return result
async def async_turn_off(self, **kwargs) -> None:
"""Turn the light off."""
if not self._prop_on:
return
# Dirty logic for lumi.gateway.mgl03 indicator light
value_on = False if self._prop_on.format_ == bool else 0
await self.set_property_async(prop=self._prop_on, value=value_on)
return await self.set_property_async(prop=self._prop_on, value=value_on)

View File

@ -992,14 +992,14 @@ class MIoTServiceEntity(Entity):
siid=event.service.iid, eiid=event.iid, sub_id=sub_id)
def get_map_value(
self, map_: Optional[dict[int, Any]], key: int
self, map_: dict[int, Any], key: int
) -> Any:
if map_ is None:
return None
return map_.get(key, None)
def get_map_key(
self, map_: Optional[dict[int, Any]], value: Any
self, map_: dict[int, Any], value: Any
) -> Optional[int]:
if map_ is None:
return None
@ -1008,7 +1008,7 @@ class MIoTServiceEntity(Entity):
return key
return None
def get_prop_value(self, prop: Optional[MIoTSpecProperty]) -> Any:
def get_prop_value(self, prop: MIoTSpecProperty) -> Any:
if not prop:
_LOGGER.error(
'get_prop_value error, property is None, %s, %s',
@ -1016,9 +1016,7 @@ class MIoTServiceEntity(Entity):
return None
return self._prop_value_map.get(prop, None)
def set_prop_value(
self, prop: Optional[MIoTSpecProperty], value: Any
) -> None:
def set_prop_value(self, prop: MIoTSpecProperty, value: Any) -> None:
if not prop:
_LOGGER.error(
'set_prop_value error, property is None, %s, %s',
@ -1027,14 +1025,13 @@ class MIoTServiceEntity(Entity):
self._prop_value_map[prop] = value
async def set_property_async(
self, prop: Optional[MIoTSpecProperty], value: Any,
update_value: bool = True, write_ha_state: bool = True
self, prop: MIoTSpecProperty, value: Any, update: bool = True
) -> bool:
value = prop.value_format(value)
if not prop:
raise RuntimeError(
f'set property failed, property is None, '
f'{self.entity_id}, {self.name}')
value = prop.value_format(value)
if prop not in self.entity_data.props:
raise RuntimeError(
f'set property failed, unknown property, '
@ -1050,9 +1047,8 @@ class MIoTServiceEntity(Entity):
except MIoTClientError as e:
raise RuntimeError(
f'{e}, {self.entity_id}, {self.name}, {prop.name}') from e
if update_value:
if update:
self._prop_value_map[prop] = value
if write_ha_state:
self.async_write_ha_state()
return True

View File

@ -100,7 +100,7 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
) -> None:
"""Initialize the Water heater."""
super().__init__(miot_device=miot_device, entity_data=entity_data)
self._attr_temperature_unit = None # type: ignore
self._attr_temperature_unit = None
self._attr_supported_features = WaterHeaterEntityFeature(0)
self._prop_on = None
self._prop_temp = None
@ -112,20 +112,20 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
for prop in entity_data.props:
# on
if prop.name == 'on':
self._attr_supported_features |= WaterHeaterEntityFeature.ON_OFF
self._prop_on = prop
# temperature
if prop.name == 'temperature':
if not prop.value_range:
if prop.value_range:
if (
self._attr_temperature_unit is None
and prop.external_unit
):
self._attr_temperature_unit = prop.external_unit
self._prop_temp = prop
else:
_LOGGER.error(
'invalid temperature value_range format, %s',
self.entity_id)
continue
if prop.external_unit:
self._attr_temperature_unit = prop.external_unit
self._attr_min_temp = prop.value_range.min_
self._attr_max_temp = prop.value_range.max_
self._prop_temp = prop
# target-temperature
if prop.name == 'target-temperature':
if not prop.value_range:
@ -133,8 +133,8 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
'invalid target-temperature value_range format, %s',
self.entity_id)
continue
self._attr_target_temperature_low = prop.value_range.min_
self._attr_target_temperature_high = prop.value_range.max_
self._attr_min_temp = prop.value_range.min_
self._attr_max_temp = prop.value_range.max_
self._attr_precision = prop.value_range.step
if self._attr_temperature_unit is None and prop.external_unit:
self._attr_temperature_unit = prop.external_unit
@ -166,8 +166,6 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
async def async_set_temperature(self, **kwargs: Any) -> None:
"""Set the temperature the water heater should heat water to."""
if not self._prop_target_temp:
return
await self.set_property_async(
prop=self._prop_target_temp, value=kwargs[ATTR_TEMPERATURE])
@ -183,11 +181,16 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
return
if self.get_prop_value(prop=self._prop_on) is False:
await self.set_property_async(
prop=self._prop_on, value=True, write_ha_state=False)
prop=self._prop_on, value=True, update=False)
await self.set_property_async(
prop=self._prop_mode,
value=self.get_map_key(
map_=self._mode_map, value=operation_mode))
map_=self._mode_map,
value=operation_mode))
async def async_turn_away_mode_on(self) -> None:
"""Set the water heater to away mode."""
await self.hass.async_add_executor_job(self.turn_away_mode_on)
@property
def current_temperature(self) -> Optional[float]:
@ -197,8 +200,6 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
@property
def target_temperature(self) -> Optional[float]:
"""Return the target temperature."""
if not self._prop_target_temp:
return None
return self.get_prop_value(prop=self._prop_target_temp)
@property