mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2026-01-16 06:30:44 +08:00
Compare commits
7 Commits
ee91c30ecc
...
18fb6bb864
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
18fb6bb864 | ||
|
|
ae1839bf9a | ||
|
|
ad8ca02fa1 | ||
|
|
3053099fd5 | ||
|
|
0c0cc8d528 | ||
|
|
2e60962e94 | ||
|
|
52fd6371ab |
@ -53,17 +53,8 @@ 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
|
||||
@ -72,11 +63,8 @@ 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]
|
||||
@ -104,9 +92,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:
|
||||
def __init__(self, miot_device: MIoTDevice,
|
||||
entity_data: MIoTEntityData) -> None:
|
||||
"""Initialize the feature class."""
|
||||
self._prop_on = None
|
||||
|
||||
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
||||
@ -114,12 +102,11 @@ 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 |= (
|
||||
@ -139,9 +126,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:
|
||||
def __init__(self, miot_device: MIoTDevice,
|
||||
entity_data: MIoTEntityData) -> None:
|
||||
"""Initialize the feature class."""
|
||||
self._prop_target_temp = None
|
||||
|
||||
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
||||
@ -162,7 +149,7 @@ class FeatureTargetTemperature(MIoTServiceEntity, ClimateEntity):
|
||||
self._prop_target_temp = prop
|
||||
|
||||
async def async_set_temperature(self, **kwargs):
|
||||
"""Set new target temperature."""
|
||||
"""Set the target temperature."""
|
||||
if ATTR_TEMPERATURE in kwargs:
|
||||
temp = kwargs[ATTR_TEMPERATURE]
|
||||
if temp > self._attr_max_temp:
|
||||
@ -170,15 +157,14 @@ 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]:
|
||||
"""Return the target temperature."""
|
||||
return (
|
||||
self.get_prop_value(prop=self._prop_target_temp)
|
||||
if self._prop_target_temp else None)
|
||||
"""The current target temperature."""
|
||||
return (self.get_prop_value(
|
||||
prop=self._prop_target_temp) if self._prop_target_temp else None)
|
||||
|
||||
|
||||
class FeaturePresetMode(MIoTServiceEntity, ClimateEntity):
|
||||
@ -186,9 +172,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:
|
||||
def __init__(self, miot_device: MIoTDevice,
|
||||
entity_data: MIoTEntityData) -> None:
|
||||
"""Initialize the feature class."""
|
||||
self._prop_mode = None
|
||||
self._mode_map = None
|
||||
|
||||
@ -197,9 +183,8 @@ 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
|
||||
@ -209,17 +194,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]:
|
||||
return (
|
||||
self.get_map_value(
|
||||
map_=self._mode_map,
|
||||
key=self.get_prop_value(prop=self._prop_mode))
|
||||
if self._prop_mode else None)
|
||||
"""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)
|
||||
|
||||
|
||||
class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
|
||||
@ -228,9 +213,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:
|
||||
def __init__(self, miot_device: MIoTDevice,
|
||||
entity_data: MIoTEntityData) -> None:
|
||||
"""Initialize the feature class."""
|
||||
self._prop_fan_on = None
|
||||
self._prop_fan_level = None
|
||||
self._fan_mode_map = None
|
||||
@ -240,8 +225,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
|
||||
@ -265,14 +250,11 @@ 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]:
|
||||
@ -280,9 +262,8 @@ 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))
|
||||
@ -293,9 +274,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:
|
||||
def __init__(self, miot_device: MIoTDevice,
|
||||
entity_data: MIoTEntityData) -> None:
|
||||
"""Initialize the feature class."""
|
||||
self._prop_horizontal_swing = None
|
||||
self._prop_vertical_swing = None
|
||||
|
||||
@ -320,23 +301,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}')
|
||||
@ -344,17 +325,14 @@ 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:
|
||||
@ -369,9 +347,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:
|
||||
def __init__(self, miot_device: MIoTDevice,
|
||||
entity_data: MIoTEntityData) -> None:
|
||||
"""Initialize the feature class."""
|
||||
self._prop_env_temperature = None
|
||||
|
||||
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
||||
@ -383,18 +361,17 @@ 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:
|
||||
def __init__(self, miot_device: MIoTDevice,
|
||||
entity_data: MIoTEntityData) -> None:
|
||||
"""Initialize the feature class."""
|
||||
self._prop_env_humidity = None
|
||||
|
||||
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
||||
@ -406,18 +383,17 @@ 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:
|
||||
def __init__(self, miot_device: MIoTDevice,
|
||||
entity_data: MIoTEntityData) -> None:
|
||||
"""Initialize the feature class."""
|
||||
self._prop_target_humidity = None
|
||||
|
||||
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
||||
@ -441,30 +417,23 @@ 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'
|
||||
@ -480,30 +449,22 @@ class Heater(
|
||||
@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 Heater."""
|
||||
def __init__(self, miot_device: MIoTDevice,
|
||||
entity_data: MIoTEntityData) -> None:
|
||||
"""Initialize the air conditioner."""
|
||||
self._prop_mode = None
|
||||
self._hvac_mode_map = None
|
||||
self._prop_ac_state = None
|
||||
@ -512,11 +473,11 @@ class AirConditioner(
|
||||
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:
|
||||
@ -537,8 +498,8 @@ class AirConditioner(
|
||||
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]
|
||||
@ -549,24 +510,22 @@ class AirConditioner(
|
||||
"""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
|
||||
elif self.get_prop_value(prop=self._prop_on) is False:
|
||||
await self.set_property_async(prop=self._prop_on, value=True)
|
||||
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)
|
||||
# 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}')
|
||||
|
||||
@ -575,11 +534,10 @@ class AirConditioner(
|
||||
"""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
|
||||
@ -609,51 +567,41 @@ class AirConditioner(
|
||||
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 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 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)
|
||||
|
||||
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
|
||||
@ -664,8 +612,7 @@ class PtcBathHeater(
|
||||
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:
|
||||
@ -688,38 +635,29 @@ class PtcBathHeater(
|
||||
"""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
|
||||
@ -727,11 +665,11 @@ class Thermostat(
|
||||
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:
|
||||
@ -756,15 +694,13 @@ class Thermostat(
|
||||
self._attr_hvac_modes.insert(0, HVACMode.OFF)
|
||||
|
||||
async def async_set_hvac_mode(self, hvac_mode: HVACMode) -> None:
|
||||
"""Set new target hvac mode."""
|
||||
"""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
|
||||
elif self.get_prop_value(prop=self._prop_on) is False:
|
||||
@ -772,12 +708,9 @@ class Thermostat(
|
||||
# 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}')
|
||||
|
||||
@ -786,8 +719,7 @@ class Thermostat(
|
||||
"""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)
|
||||
|
||||
@ -200,7 +200,7 @@ class Cover(MIoTServiceEntity, CoverEntity):
|
||||
if pos is None:
|
||||
return None
|
||||
pos = round(pos*self._prop_position_value_range/100)
|
||||
return await self.set_property_async(
|
||||
await self.set_property_async(
|
||||
prop=self._prop_target_position, value=pos)
|
||||
|
||||
@property
|
||||
|
||||
@ -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:
|
||||
if self._speed_names and self._speed_name_map:
|
||||
return ordered_list_item_to_percentage(
|
||||
self._speed_names, self._speed_name_map[fan_level])
|
||||
else:
|
||||
|
||||
@ -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: MIoTSpecProperty
|
||||
_prop_on: Optional[MIoTSpecProperty]
|
||||
_prop_brightness: Optional[MIoTSpecProperty]
|
||||
_prop_color_temp: Optional[MIoTSpecProperty]
|
||||
_prop_color: Optional[MIoTSpecProperty]
|
||||
@ -250,23 +250,25 @@ class Light(MIoTServiceEntity, LightEntity):
|
||||
|
||||
Shall set attributes in kwargs if applicable.
|
||||
"""
|
||||
result: bool = False
|
||||
# on
|
||||
# Dirty logic for lumi.gateway.mgl03 indicator light
|
||||
value_on = True if self._prop_on.format_ == bool else 1
|
||||
result = await self.set_property_async(
|
||||
prop=self._prop_on, value=value_on)
|
||||
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)
|
||||
# brightness
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
brightness = brightness_to_value(
|
||||
self._brightness_scale, kwargs[ATTR_BRIGHTNESS])
|
||||
result = await self.set_property_async(
|
||||
prop=self._prop_brightness, value=brightness)
|
||||
await self.set_property_async(
|
||||
prop=self._prop_brightness, value=brightness,
|
||||
write_ha_state=False)
|
||||
# color-temperature
|
||||
if ATTR_COLOR_TEMP_KELVIN in kwargs:
|
||||
result = await self.set_property_async(
|
||||
await self.set_property_async(
|
||||
prop=self._prop_color_temp,
|
||||
value=kwargs[ATTR_COLOR_TEMP_KELVIN])
|
||||
value=kwargs[ATTR_COLOR_TEMP_KELVIN],
|
||||
write_ha_state=False)
|
||||
self._attr_color_mode = ColorMode.COLOR_TEMP
|
||||
# rgb color
|
||||
if ATTR_RGB_COLOR in kwargs:
|
||||
@ -274,19 +276,23 @@ class Light(MIoTServiceEntity, LightEntity):
|
||||
g = kwargs[ATTR_RGB_COLOR][1]
|
||||
b = kwargs[ATTR_RGB_COLOR][2]
|
||||
rgb = (r << 16) | (g << 8) | b
|
||||
result = await self.set_property_async(
|
||||
prop=self._prop_color, value=rgb)
|
||||
await self.set_property_async(
|
||||
prop=self._prop_color, value=rgb,
|
||||
write_ha_state=False)
|
||||
self._attr_color_mode = ColorMode.RGB
|
||||
# mode
|
||||
if ATTR_EFFECT in kwargs:
|
||||
result = await self.set_property_async(
|
||||
await self.set_property_async(
|
||||
prop=self._prop_mode,
|
||||
value=self.get_map_key(
|
||||
map_=self._mode_map, value=kwargs[ATTR_EFFECT]))
|
||||
return result
|
||||
map_=self._mode_map, value=kwargs[ATTR_EFFECT]),
|
||||
write_ha_state=False)
|
||||
self.async_write_ha_state()
|
||||
|
||||
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
|
||||
return await self.set_property_async(prop=self._prop_on, value=value_on)
|
||||
await self.set_property_async(prop=self._prop_on, value=value_on)
|
||||
|
||||
@ -992,14 +992,14 @@ class MIoTServiceEntity(Entity):
|
||||
siid=event.service.iid, eiid=event.iid, sub_id=sub_id)
|
||||
|
||||
def get_map_value(
|
||||
self, map_: dict[int, Any], key: int
|
||||
self, map_: Optional[dict[int, Any]], key: int
|
||||
) -> Any:
|
||||
if map_ is None:
|
||||
return None
|
||||
return map_.get(key, None)
|
||||
|
||||
def get_map_key(
|
||||
self, map_: dict[int, Any], value: Any
|
||||
self, map_: Optional[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: MIoTSpecProperty) -> Any:
|
||||
def get_prop_value(self, prop: Optional[MIoTSpecProperty]) -> Any:
|
||||
if not prop:
|
||||
_LOGGER.error(
|
||||
'get_prop_value error, property is None, %s, %s',
|
||||
@ -1016,7 +1016,9 @@ class MIoTServiceEntity(Entity):
|
||||
return None
|
||||
return self._prop_value_map.get(prop, None)
|
||||
|
||||
def set_prop_value(self, prop: MIoTSpecProperty, value: Any) -> None:
|
||||
def set_prop_value(
|
||||
self, prop: Optional[MIoTSpecProperty], value: Any
|
||||
) -> None:
|
||||
if not prop:
|
||||
_LOGGER.error(
|
||||
'set_prop_value error, property is None, %s, %s',
|
||||
@ -1025,13 +1027,14 @@ class MIoTServiceEntity(Entity):
|
||||
self._prop_value_map[prop] = value
|
||||
|
||||
async def set_property_async(
|
||||
self, prop: MIoTSpecProperty, value: Any, update: bool = True
|
||||
self, prop: Optional[MIoTSpecProperty], value: Any,
|
||||
update_value: bool = True, write_ha_state: 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, '
|
||||
@ -1047,8 +1050,9 @@ class MIoTServiceEntity(Entity):
|
||||
except MIoTClientError as e:
|
||||
raise RuntimeError(
|
||||
f'{e}, {self.entity_id}, {self.name}, {prop.name}') from e
|
||||
if update:
|
||||
if update_value:
|
||||
self._prop_value_map[prop] = value
|
||||
if write_ha_state:
|
||||
self.async_write_ha_state()
|
||||
return True
|
||||
|
||||
|
||||
@ -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
|
||||
self._attr_temperature_unit = None # type: ignore
|
||||
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 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:
|
||||
if not prop.value_range:
|
||||
_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_min_temp = prop.value_range.min_
|
||||
self._attr_max_temp = prop.value_range.max_
|
||||
self._attr_target_temperature_low = prop.value_range.min_
|
||||
self._attr_target_temperature_high = 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,6 +166,8 @@ 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])
|
||||
|
||||
@ -181,16 +183,11 @@ 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, update=False)
|
||||
prop=self._prop_on, value=True, write_ha_state=False)
|
||||
await self.set_property_async(
|
||||
prop=self._prop_mode,
|
||||
value=self.get_map_key(
|
||||
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)
|
||||
map_=self._mode_map, value=operation_mode))
|
||||
|
||||
@property
|
||||
def current_temperature(self) -> Optional[float]:
|
||||
@ -200,6 +197,8 @@ 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
|
||||
|
||||
Loading…
Reference in New Issue
Block a user