mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2026-01-20 17:29:38 +08:00
Compare commits
5 Commits
0c0cc8d528
...
ae1839bf9a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae1839bf9a | ||
|
|
ad8ca02fa1 | ||
|
|
3053099fd5 | ||
|
|
2e60962e94 | ||
|
|
52fd6371ab |
@ -53,17 +53,8 @@ from homeassistant.config_entries import ConfigEntry
|
|||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.components.climate import (
|
from homeassistant.components.climate import (
|
||||||
FAN_ON,
|
FAN_ON, FAN_OFF, SWING_OFF, SWING_BOTH, SWING_VERTICAL, SWING_HORIZONTAL,
|
||||||
FAN_OFF,
|
ATTR_TEMPERATURE, HVACMode, ClimateEntity, ClimateEntityFeature)
|
||||||
SWING_OFF,
|
|
||||||
SWING_BOTH,
|
|
||||||
SWING_VERTICAL,
|
|
||||||
SWING_HORIZONTAL,
|
|
||||||
ATTR_TEMPERATURE,
|
|
||||||
HVACMode,
|
|
||||||
ClimateEntity,
|
|
||||||
ClimateEntityFeature
|
|
||||||
)
|
|
||||||
|
|
||||||
from .miot.const import DOMAIN
|
from .miot.const import DOMAIN
|
||||||
from .miot.miot_device import MIoTDevice, MIoTServiceEntity, MIoTEntityData
|
from .miot.miot_device import MIoTDevice, MIoTServiceEntity, MIoTEntityData
|
||||||
@ -72,11 +63,8 @@ from .miot.miot_spec import MIoTSpecProperty
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry,
|
||||||
hass: HomeAssistant,
|
async_add_entities: AddEntitiesCallback) -> None:
|
||||||
config_entry: ConfigEntry,
|
|
||||||
async_add_entities: AddEntitiesCallback
|
|
||||||
) -> None:
|
|
||||||
"""Set up a config entry."""
|
"""Set up a config entry."""
|
||||||
device_list: list[MIoTDevice] = hass.data[DOMAIN]['devices'][
|
device_list: list[MIoTDevice] = hass.data[DOMAIN]['devices'][
|
||||||
config_entry.entry_id]
|
config_entry.entry_id]
|
||||||
@ -104,9 +92,8 @@ class FeatureOnOff(MIoTServiceEntity, ClimateEntity):
|
|||||||
"""TURN_ON and TURN_OFF feature of the climate entity."""
|
"""TURN_ON and TURN_OFF feature of the climate entity."""
|
||||||
_prop_on: Optional[MIoTSpecProperty]
|
_prop_on: Optional[MIoTSpecProperty]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the feature class."""
|
"""Initialize the feature class."""
|
||||||
self._prop_on = None
|
self._prop_on = None
|
||||||
|
|
||||||
@ -115,12 +102,11 @@ class FeatureOnOff(MIoTServiceEntity, ClimateEntity):
|
|||||||
for prop in entity_data.props:
|
for prop in entity_data.props:
|
||||||
if prop.name == 'on':
|
if prop.name == 'on':
|
||||||
if (
|
if (
|
||||||
# The "on" property of the "fan-control" service is not
|
# The "on" property of the "fan-control" service is not
|
||||||
# the on/off feature of the entity.
|
# the on/off feature of the entity.
|
||||||
prop.service.name == 'air-conditioner'
|
prop.service.name == 'air-conditioner' or
|
||||||
or prop.service.name == 'heater'
|
prop.service.name == 'heater' or
|
||||||
or prop.service.name == 'thermostat'
|
prop.service.name == 'thermostat'):
|
||||||
):
|
|
||||||
self._attr_supported_features |= (
|
self._attr_supported_features |= (
|
||||||
ClimateEntityFeature.TURN_ON)
|
ClimateEntityFeature.TURN_ON)
|
||||||
self._attr_supported_features |= (
|
self._attr_supported_features |= (
|
||||||
@ -140,9 +126,8 @@ class FeatureTargetTemperature(MIoTServiceEntity, ClimateEntity):
|
|||||||
"""TARGET_TEMPERATURE feature of the climate entity."""
|
"""TARGET_TEMPERATURE feature of the climate entity."""
|
||||||
_prop_target_temp: Optional[MIoTSpecProperty]
|
_prop_target_temp: Optional[MIoTSpecProperty]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the feature class."""
|
"""Initialize the feature class."""
|
||||||
self._prop_target_temp = None
|
self._prop_target_temp = None
|
||||||
|
|
||||||
@ -172,15 +157,14 @@ class FeatureTargetTemperature(MIoTServiceEntity, ClimateEntity):
|
|||||||
elif temp < self._attr_min_temp:
|
elif temp < self._attr_min_temp:
|
||||||
temp = self._attr_min_temp
|
temp = self._attr_min_temp
|
||||||
|
|
||||||
await self.set_property_async(
|
await self.set_property_async(prop=self._prop_target_temp,
|
||||||
prop=self._prop_target_temp, value=temp)
|
value=temp)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def target_temperature(self) -> Optional[float]:
|
def target_temperature(self) -> Optional[float]:
|
||||||
"""The current target temperature."""
|
"""The current target temperature."""
|
||||||
return (
|
return (self.get_prop_value(
|
||||||
self.get_prop_value(prop=self._prop_target_temp)
|
prop=self._prop_target_temp) if self._prop_target_temp else None)
|
||||||
if self._prop_target_temp else None)
|
|
||||||
|
|
||||||
|
|
||||||
class FeaturePresetMode(MIoTServiceEntity, ClimateEntity):
|
class FeaturePresetMode(MIoTServiceEntity, ClimateEntity):
|
||||||
@ -188,9 +172,8 @@ class FeaturePresetMode(MIoTServiceEntity, ClimateEntity):
|
|||||||
_prop_mode: Optional[MIoTSpecProperty]
|
_prop_mode: Optional[MIoTSpecProperty]
|
||||||
_mode_map: Optional[dict[int, str]]
|
_mode_map: Optional[dict[int, str]]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the feature class."""
|
"""Initialize the feature class."""
|
||||||
self._prop_mode = None
|
self._prop_mode = None
|
||||||
self._mode_map = None
|
self._mode_map = None
|
||||||
@ -200,9 +183,8 @@ class FeaturePresetMode(MIoTServiceEntity, ClimateEntity):
|
|||||||
for prop in entity_data.props:
|
for prop in entity_data.props:
|
||||||
if prop.name == 'heat-level' and prop.service.name == 'heater':
|
if prop.name == 'heat-level' and prop.service.name == 'heater':
|
||||||
if not prop.value_list:
|
if not prop.value_list:
|
||||||
_LOGGER.error(
|
_LOGGER.error('invalid heater heat-level value_list, %s',
|
||||||
'invalid heater heat-level value_list, %s',
|
self.entity_id)
|
||||||
self.entity_id)
|
|
||||||
continue
|
continue
|
||||||
self._mode_map = prop.value_list.to_map()
|
self._mode_map = prop.value_list.to_map()
|
||||||
self._attr_preset_modes = prop.value_list.descriptions
|
self._attr_preset_modes = prop.value_list.descriptions
|
||||||
@ -212,18 +194,17 @@ class FeaturePresetMode(MIoTServiceEntity, ClimateEntity):
|
|||||||
|
|
||||||
async def async_set_preset_mode(self, preset_mode: str) -> None:
|
async def async_set_preset_mode(self, preset_mode: str) -> None:
|
||||||
"""Set the preset mode."""
|
"""Set the preset mode."""
|
||||||
await self.set_property_async(
|
await self.set_property_async(self._prop_mode,
|
||||||
self._prop_mode,
|
value=self.get_map_key(
|
||||||
value=self.get_map_key(map_=self._mode_map, value=preset_mode))
|
map_=self._mode_map,
|
||||||
|
value=preset_mode))
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def preset_mode(self) -> Optional[str]:
|
def preset_mode(self) -> Optional[str]:
|
||||||
"""The current preset mode."""
|
"""The current preset mode."""
|
||||||
return (
|
return (self.get_map_value(
|
||||||
self.get_map_value(
|
map_=self._mode_map, key=self.get_prop_value(
|
||||||
map_=self._mode_map,
|
prop=self._prop_mode)) if self._prop_mode else None)
|
||||||
key=self.get_prop_value(prop=self._prop_mode))
|
|
||||||
if self._prop_mode else None)
|
|
||||||
|
|
||||||
|
|
||||||
class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
|
class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
|
||||||
@ -232,9 +213,8 @@ class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
|
|||||||
_prop_fan_level: Optional[MIoTSpecProperty]
|
_prop_fan_level: Optional[MIoTSpecProperty]
|
||||||
_fan_mode_map: Optional[dict[int, str]]
|
_fan_mode_map: Optional[dict[int, str]]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the feature class."""
|
"""Initialize the feature class."""
|
||||||
self._prop_fan_on = None
|
self._prop_fan_on = None
|
||||||
self._prop_fan_level = None
|
self._prop_fan_level = None
|
||||||
@ -245,8 +225,8 @@ class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
|
|||||||
for prop in entity_data.props:
|
for prop in entity_data.props:
|
||||||
if prop.name == 'fan-level' and prop.service.name == 'fan-control':
|
if prop.name == 'fan-level' and prop.service.name == 'fan-control':
|
||||||
if not prop.value_list:
|
if not prop.value_list:
|
||||||
_LOGGER.error(
|
_LOGGER.error('invalid fan-level value_list, %s',
|
||||||
'invalid fan-level value_list, %s', self.entity_id)
|
self.entity_id)
|
||||||
continue
|
continue
|
||||||
self._fan_mode_map = prop.value_list.to_map()
|
self._fan_mode_map = prop.value_list.to_map()
|
||||||
self._attr_fan_modes = prop.value_list.descriptions
|
self._attr_fan_modes = prop.value_list.descriptions
|
||||||
@ -270,14 +250,11 @@ class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
|
|||||||
if fan_mode == FAN_ON:
|
if fan_mode == FAN_ON:
|
||||||
await self.set_property_async(prop=self._prop_fan_on, value=True)
|
await self.set_property_async(prop=self._prop_fan_on, value=True)
|
||||||
return
|
return
|
||||||
mode_value = self.get_map_key(
|
mode_value = self.get_map_key(map_=self._fan_mode_map, value=fan_mode)
|
||||||
map_=self._fan_mode_map, value=fan_mode)
|
|
||||||
if mode_value is None or not await self.set_property_async(
|
if mode_value is None or not await self.set_property_async(
|
||||||
prop=self._prop_fan_level, value=mode_value
|
prop=self._prop_fan_level, value=mode_value):
|
||||||
):
|
raise RuntimeError(f'set climate prop.fan_mode failed, {fan_mode}, '
|
||||||
raise RuntimeError(
|
f'{self.entity_id}')
|
||||||
f'set climate prop.fan_mode failed, {fan_mode}, '
|
|
||||||
f'{self.entity_id}')
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def fan_mode(self) -> Optional[str]:
|
def fan_mode(self) -> Optional[str]:
|
||||||
@ -285,9 +262,8 @@ class FeatureFanMode(MIoTServiceEntity, ClimateEntity):
|
|||||||
if self._prop_fan_level is None and self._prop_fan_on is None:
|
if self._prop_fan_level is None and self._prop_fan_on is None:
|
||||||
return None
|
return None
|
||||||
if self._prop_fan_level is None and self._prop_fan_on:
|
if self._prop_fan_level is None and self._prop_fan_on:
|
||||||
return (
|
return (FAN_ON if self.get_prop_value(
|
||||||
FAN_ON if self.get_prop_value(prop=self._prop_fan_on)
|
prop=self._prop_fan_on) else FAN_OFF)
|
||||||
else FAN_OFF)
|
|
||||||
return self.get_map_value(
|
return self.get_map_value(
|
||||||
map_=self._fan_mode_map,
|
map_=self._fan_mode_map,
|
||||||
key=self.get_prop_value(prop=self._prop_fan_level))
|
key=self.get_prop_value(prop=self._prop_fan_level))
|
||||||
@ -298,9 +274,8 @@ class FeatureSwingMode(MIoTServiceEntity, ClimateEntity):
|
|||||||
_prop_horizontal_swing: Optional[MIoTSpecProperty]
|
_prop_horizontal_swing: Optional[MIoTSpecProperty]
|
||||||
_prop_vertical_swing: Optional[MIoTSpecProperty]
|
_prop_vertical_swing: Optional[MIoTSpecProperty]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the feature class."""
|
"""Initialize the feature class."""
|
||||||
self._prop_horizontal_swing = None
|
self._prop_horizontal_swing = None
|
||||||
self._prop_vertical_swing = None
|
self._prop_vertical_swing = None
|
||||||
@ -326,23 +301,23 @@ class FeatureSwingMode(MIoTServiceEntity, ClimateEntity):
|
|||||||
async def async_set_swing_mode(self, swing_mode):
|
async def async_set_swing_mode(self, swing_mode):
|
||||||
"""Set the target swing operation."""
|
"""Set the target swing operation."""
|
||||||
if swing_mode == SWING_BOTH:
|
if swing_mode == SWING_BOTH:
|
||||||
await self.set_property_async(
|
await self.set_property_async(prop=self._prop_horizontal_swing,
|
||||||
prop=self._prop_horizontal_swing, value=True)
|
value=True)
|
||||||
await self.set_property_async(
|
await self.set_property_async(prop=self._prop_vertical_swing,
|
||||||
prop=self._prop_vertical_swing, value=True)
|
value=True)
|
||||||
elif swing_mode == SWING_HORIZONTAL:
|
elif swing_mode == SWING_HORIZONTAL:
|
||||||
await self.set_property_async(
|
await self.set_property_async(prop=self._prop_horizontal_swing,
|
||||||
prop=self._prop_horizontal_swing, value=True)
|
value=True)
|
||||||
elif swing_mode == SWING_VERTICAL:
|
elif swing_mode == SWING_VERTICAL:
|
||||||
await self.set_property_async(
|
await self.set_property_async(prop=self._prop_vertical_swing,
|
||||||
prop=self._prop_vertical_swing, value=True)
|
value=True)
|
||||||
elif swing_mode == SWING_OFF:
|
elif swing_mode == SWING_OFF:
|
||||||
if self._prop_horizontal_swing:
|
if self._prop_horizontal_swing:
|
||||||
await self.set_property_async(
|
await self.set_property_async(prop=self._prop_horizontal_swing,
|
||||||
prop=self._prop_horizontal_swing, value=False)
|
value=False)
|
||||||
if self._prop_vertical_swing:
|
if self._prop_vertical_swing:
|
||||||
await self.set_property_async(
|
await self.set_property_async(prop=self._prop_vertical_swing,
|
||||||
prop=self._prop_vertical_swing, value=False)
|
value=False)
|
||||||
else:
|
else:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f'unknown swing_mode, {swing_mode}, {self.entity_id}')
|
f'unknown swing_mode, {swing_mode}, {self.entity_id}')
|
||||||
@ -350,17 +325,14 @@ class FeatureSwingMode(MIoTServiceEntity, ClimateEntity):
|
|||||||
@property
|
@property
|
||||||
def swing_mode(self) -> Optional[str]:
|
def swing_mode(self) -> Optional[str]:
|
||||||
"""The current swing mode of the fan."""
|
"""The current swing mode of the fan."""
|
||||||
if (
|
if (self._prop_horizontal_swing is None and
|
||||||
self._prop_horizontal_swing is None
|
self._prop_vertical_swing is None):
|
||||||
and self._prop_vertical_swing is None
|
|
||||||
):
|
|
||||||
return None
|
return None
|
||||||
horizontal: bool = (
|
horizontal: bool = (self.get_prop_value(
|
||||||
self.get_prop_value(prop=self._prop_horizontal_swing)
|
prop=self._prop_horizontal_swing)
|
||||||
if self._prop_horizontal_swing else False)
|
if self._prop_horizontal_swing else False)
|
||||||
vertical: bool = (
|
vertical: bool = (self.get_prop_value(prop=self._prop_vertical_swing)
|
||||||
self.get_prop_value(prop=self._prop_vertical_swing)
|
if self._prop_vertical_swing else False)
|
||||||
if self._prop_vertical_swing else False)
|
|
||||||
if horizontal and vertical:
|
if horizontal and vertical:
|
||||||
return SWING_BOTH
|
return SWING_BOTH
|
||||||
elif horizontal:
|
elif horizontal:
|
||||||
@ -375,9 +347,8 @@ class FeatureTemperature(MIoTServiceEntity, ClimateEntity):
|
|||||||
"""Temperature of the climate entity."""
|
"""Temperature of the climate entity."""
|
||||||
_prop_env_temperature: Optional[MIoTSpecProperty]
|
_prop_env_temperature: Optional[MIoTSpecProperty]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the feature class."""
|
"""Initialize the feature class."""
|
||||||
self._prop_env_temperature = None
|
self._prop_env_temperature = None
|
||||||
|
|
||||||
@ -390,18 +361,16 @@ class FeatureTemperature(MIoTServiceEntity, ClimateEntity):
|
|||||||
@property
|
@property
|
||||||
def current_temperature(self) -> Optional[float]:
|
def current_temperature(self) -> Optional[float]:
|
||||||
"""The current environment temperature."""
|
"""The current environment temperature."""
|
||||||
return (
|
return (self.get_prop_value(prop=self._prop_env_temperature)
|
||||||
self.get_prop_value(prop=self._prop_env_temperature)
|
if self._prop_env_temperature else None)
|
||||||
if self._prop_env_temperature else None)
|
|
||||||
|
|
||||||
|
|
||||||
class FeatureHumidity(MIoTServiceEntity, ClimateEntity):
|
class FeatureHumidity(MIoTServiceEntity, ClimateEntity):
|
||||||
"""Humidity of the climate entity."""
|
"""Humidity of the climate entity."""
|
||||||
_prop_env_humidity: Optional[MIoTSpecProperty]
|
_prop_env_humidity: Optional[MIoTSpecProperty]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the feature class."""
|
"""Initialize the feature class."""
|
||||||
self._prop_env_humidity = None
|
self._prop_env_humidity = None
|
||||||
|
|
||||||
@ -414,18 +383,16 @@ class FeatureHumidity(MIoTServiceEntity, ClimateEntity):
|
|||||||
@property
|
@property
|
||||||
def current_humidity(self) -> Optional[float]:
|
def current_humidity(self) -> Optional[float]:
|
||||||
"""The current environment humidity."""
|
"""The current environment humidity."""
|
||||||
return (
|
return (self.get_prop_value(
|
||||||
self.get_prop_value(prop=self._prop_env_humidity)
|
prop=self._prop_env_humidity) if self._prop_env_humidity else None)
|
||||||
if self._prop_env_humidity else None)
|
|
||||||
|
|
||||||
|
|
||||||
class FeatureTargetHumidity(MIoTServiceEntity, ClimateEntity):
|
class FeatureTargetHumidity(MIoTServiceEntity, ClimateEntity):
|
||||||
"""TARGET_HUMIDITY feature of the climate entity."""
|
"""TARGET_HUMIDITY feature of the climate entity."""
|
||||||
_prop_target_humidity: Optional[MIoTSpecProperty]
|
_prop_target_humidity: Optional[MIoTSpecProperty]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the feature class."""
|
"""Initialize the feature class."""
|
||||||
self._prop_target_humidity = None
|
self._prop_target_humidity = None
|
||||||
|
|
||||||
@ -450,29 +417,22 @@ class FeatureTargetHumidity(MIoTServiceEntity, ClimateEntity):
|
|||||||
humidity = self._attr_max_humidity
|
humidity = self._attr_max_humidity
|
||||||
elif humidity < self._attr_min_humidity:
|
elif humidity < self._attr_min_humidity:
|
||||||
humidity = self._attr_min_humidity
|
humidity = self._attr_min_humidity
|
||||||
await self.set_property_async(
|
await self.set_property_async(prop=self._prop_target_humidity,
|
||||||
prop=self._prop_target_humidity, value=humidity)
|
value=humidity)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def target_humidity(self) -> Optional[int]:
|
def target_humidity(self) -> Optional[int]:
|
||||||
"""The current target humidity."""
|
"""The current target humidity."""
|
||||||
return (
|
return (self.get_prop_value(prop=self._prop_target_humidity)
|
||||||
self.get_prop_value(prop=self._prop_target_humidity)
|
if self._prop_target_humidity else None)
|
||||||
if self._prop_target_humidity else None)
|
|
||||||
|
|
||||||
|
|
||||||
class Heater(
|
class Heater(FeatureOnOff, FeatureTargetTemperature, FeatureTemperature,
|
||||||
FeatureOnOff,
|
FeatureHumidity, FeaturePresetMode):
|
||||||
FeatureTargetTemperature,
|
|
||||||
FeatureTemperature,
|
|
||||||
FeatureHumidity,
|
|
||||||
FeaturePresetMode
|
|
||||||
):
|
|
||||||
"""Heater"""
|
"""Heater"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the heater."""
|
"""Initialize the heater."""
|
||||||
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
||||||
|
|
||||||
@ -489,29 +449,21 @@ class Heater(
|
|||||||
@property
|
@property
|
||||||
def hvac_mode(self) -> Optional[HVACMode]:
|
def hvac_mode(self) -> Optional[HVACMode]:
|
||||||
"""The current hvac mode."""
|
"""The current hvac mode."""
|
||||||
return (
|
return (HVACMode.HEAT if self.get_prop_value(
|
||||||
HVACMode.HEAT if self.get_prop_value(prop=self._prop_on)
|
prop=self._prop_on) else HVACMode.OFF)
|
||||||
else HVACMode.OFF)
|
|
||||||
|
|
||||||
|
|
||||||
class AirConditioner(
|
class AirConditioner(FeatureOnOff, FeatureTargetTemperature,
|
||||||
FeatureOnOff,
|
FeatureTargetHumidity, FeatureTemperature, FeatureHumidity,
|
||||||
FeatureTargetTemperature,
|
FeatureFanMode, FeatureSwingMode):
|
||||||
FeatureTargetHumidity,
|
|
||||||
FeatureTemperature,
|
|
||||||
FeatureHumidity,
|
|
||||||
FeatureFanMode,
|
|
||||||
FeatureSwingMode
|
|
||||||
):
|
|
||||||
"""Air conditioner"""
|
"""Air conditioner"""
|
||||||
_prop_mode: Optional[MIoTSpecProperty]
|
_prop_mode: Optional[MIoTSpecProperty]
|
||||||
_hvac_mode_map: Optional[dict[int, HVACMode]]
|
_hvac_mode_map: Optional[dict[int, HVACMode]]
|
||||||
_prop_ac_state: Optional[MIoTSpecProperty]
|
_prop_ac_state: Optional[MIoTSpecProperty]
|
||||||
_value_ac_state: Optional[dict[str, int]]
|
_value_ac_state: Optional[dict[str, int]]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the air conditioner."""
|
"""Initialize the air conditioner."""
|
||||||
self._prop_mode = None
|
self._prop_mode = None
|
||||||
self._hvac_mode_map = None
|
self._hvac_mode_map = None
|
||||||
@ -521,11 +473,11 @@ class AirConditioner(
|
|||||||
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
||||||
self._attr_icon = 'mdi:air-conditioner'
|
self._attr_icon = 'mdi:air-conditioner'
|
||||||
# hvac modes
|
# hvac modes
|
||||||
|
self._attr_hvac_modes = None
|
||||||
for prop in entity_data.props:
|
for prop in entity_data.props:
|
||||||
if prop.name == 'mode':
|
if prop.name == 'mode':
|
||||||
if not prop.value_list:
|
if not prop.value_list:
|
||||||
_LOGGER.error(
|
_LOGGER.error('invalid mode value_list, %s', self.entity_id)
|
||||||
'invalid mode value_list, %s', self.entity_id)
|
|
||||||
continue
|
continue
|
||||||
self._hvac_mode_map = {}
|
self._hvac_mode_map = {}
|
||||||
for item in prop.value_list.items:
|
for item in prop.value_list.items:
|
||||||
@ -546,8 +498,8 @@ class AirConditioner(
|
|||||||
elif prop.name == 'ac-state':
|
elif prop.name == 'ac-state':
|
||||||
self._prop_ac_state = prop
|
self._prop_ac_state = prop
|
||||||
self._value_ac_state = {}
|
self._value_ac_state = {}
|
||||||
self.sub_prop_changed(
|
self.sub_prop_changed(prop=prop,
|
||||||
prop=prop, handler=self.__ac_state_changed)
|
handler=self.__ac_state_changed)
|
||||||
|
|
||||||
if self._attr_hvac_modes is None:
|
if self._attr_hvac_modes is None:
|
||||||
self._attr_hvac_modes = [HVACMode.OFF]
|
self._attr_hvac_modes = [HVACMode.OFF]
|
||||||
@ -558,24 +510,22 @@ class AirConditioner(
|
|||||||
"""Set the target hvac mode."""
|
"""Set the target hvac mode."""
|
||||||
# set the device off
|
# set the device off
|
||||||
if hvac_mode == HVACMode.OFF:
|
if hvac_mode == HVACMode.OFF:
|
||||||
if not await self.set_property_async(
|
if not await self.set_property_async(prop=self._prop_on,
|
||||||
prop=self._prop_on, value=False
|
value=False):
|
||||||
):
|
raise RuntimeError(f'set climate prop.on failed, {hvac_mode}, '
|
||||||
raise RuntimeError(
|
f'{self.entity_id}')
|
||||||
f'set climate prop.on failed, {hvac_mode}, '
|
|
||||||
f'{self.entity_id}')
|
|
||||||
return
|
return
|
||||||
# set the device on
|
# set the device on
|
||||||
elif self.get_prop_value(prop=self._prop_on) is False:
|
if self.get_prop_value(prop=self._prop_on) is False:
|
||||||
await self.set_property_async(prop=self._prop_on, value=True)
|
await self.set_property_async(prop=self._prop_on,
|
||||||
|
value=True,
|
||||||
|
write_ha_state=False)
|
||||||
# set mode
|
# set mode
|
||||||
if self._prop_mode is None:
|
if self._prop_mode is None:
|
||||||
return
|
return
|
||||||
mode_value = self.get_map_key(
|
mode_value = self.get_map_key(map_=self._hvac_mode_map, value=hvac_mode)
|
||||||
map_=self._hvac_mode_map, value=hvac_mode)
|
|
||||||
if mode_value is None or not await self.set_property_async(
|
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(
|
raise RuntimeError(
|
||||||
f'set climate prop.mode failed, {hvac_mode}, {self.entity_id}')
|
f'set climate prop.mode failed, {hvac_mode}, {self.entity_id}')
|
||||||
|
|
||||||
@ -584,11 +534,10 @@ class AirConditioner(
|
|||||||
"""The current hvac mode."""
|
"""The current hvac mode."""
|
||||||
if self.get_prop_value(prop=self._prop_on) is False:
|
if self.get_prop_value(prop=self._prop_on) is False:
|
||||||
return HVACMode.OFF
|
return HVACMode.OFF
|
||||||
return (
|
return (self.get_map_value(map_=self._hvac_mode_map,
|
||||||
self.get_map_value(
|
key=self.get_prop_value(
|
||||||
map_=self._hvac_mode_map,
|
prop=self._prop_mode))
|
||||||
key=self.get_prop_value(prop=self._prop_mode))
|
if self._prop_mode else None)
|
||||||
if self._prop_mode else None)
|
|
||||||
|
|
||||||
def __ac_state_changed(self, prop: MIoTSpecProperty, value: Any) -> None:
|
def __ac_state_changed(self, prop: MIoTSpecProperty, value: Any) -> None:
|
||||||
del prop
|
del prop
|
||||||
@ -618,51 +567,41 @@ class AirConditioner(
|
|||||||
4: HVACMode.DRY,
|
4: HVACMode.DRY,
|
||||||
}.get(v_ac_state['M'], None)
|
}.get(v_ac_state['M'], None)
|
||||||
if mode:
|
if mode:
|
||||||
self.set_prop_value(
|
self.set_prop_value(prop=self._prop_mode,
|
||||||
prop=self._prop_mode,
|
value=self.get_map_key(
|
||||||
value=self.get_map_key(
|
map_=self._hvac_mode_map, value=mode))
|
||||||
map_=self._hvac_mode_map, value=mode))
|
|
||||||
# T: target temperature
|
# T: target temperature
|
||||||
if 'T' in v_ac_state and self._prop_target_temp:
|
if 'T' in v_ac_state and self._prop_target_temp:
|
||||||
self.set_prop_value(
|
self.set_prop_value(prop=self._prop_target_temp,
|
||||||
prop=self._prop_target_temp, value=v_ac_state['T'])
|
value=v_ac_state['T'])
|
||||||
# S: fan level. 0: auto, 1: low, 2: media, 3: high
|
# S: fan level. 0: auto, 1: low, 2: media, 3: high
|
||||||
if 'S' in v_ac_state and self._prop_fan_level:
|
if 'S' in v_ac_state and self._prop_fan_level:
|
||||||
self.set_prop_value(
|
self.set_prop_value(prop=self._prop_fan_level,
|
||||||
prop=self._prop_fan_level, value=v_ac_state['S'])
|
value=v_ac_state['S'])
|
||||||
# D: swing mode. 0: on, 1: off
|
# D: swing mode. 0: on, 1: off
|
||||||
if 'D' in v_ac_state and len(self._attr_swing_modes) == 2:
|
if ('D' in v_ac_state and self._attr_swing_modes and
|
||||||
if (
|
len(self._attr_swing_modes) == 2):
|
||||||
SWING_HORIZONTAL in self._attr_swing_modes
|
if (SWING_HORIZONTAL in self._attr_swing_modes and
|
||||||
and self._prop_horizontal_swing
|
self._prop_horizontal_swing):
|
||||||
):
|
self.set_prop_value(prop=self._prop_horizontal_swing,
|
||||||
self.set_prop_value(
|
value=v_ac_state['D'] == 0)
|
||||||
prop=self._prop_horizontal_swing, value=v_ac_state[
|
elif (SWING_VERTICAL in self._attr_swing_modes and
|
||||||
'D'] == 0)
|
self._prop_vertical_swing):
|
||||||
elif (
|
self.set_prop_value(prop=self._prop_vertical_swing,
|
||||||
SWING_VERTICAL in self._attr_swing_modes
|
value=v_ac_state['D'] == 0)
|
||||||
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)
|
self._value_ac_state.update(v_ac_state)
|
||||||
_LOGGER.debug('ac_state update, %s', self._value_ac_state)
|
_LOGGER.debug('ac_state update, %s', self._value_ac_state)
|
||||||
|
|
||||||
|
|
||||||
class PtcBathHeater(
|
class PtcBathHeater(FeatureTargetTemperature, FeatureTemperature,
|
||||||
FeatureTargetTemperature,
|
FeatureFanMode, FeatureSwingMode):
|
||||||
FeatureTemperature,
|
|
||||||
FeatureFanMode,
|
|
||||||
FeatureSwingMode
|
|
||||||
):
|
|
||||||
"""Ptc bath heater"""
|
"""Ptc bath heater"""
|
||||||
_prop_mode: Optional[MIoTSpecProperty]
|
_prop_mode: Optional[MIoTSpecProperty]
|
||||||
_hvac_mode_map: Optional[dict[int, HVACMode]]
|
_hvac_mode_map: Optional[dict[int, HVACMode]]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the ptc bath heater."""
|
"""Initialize the ptc bath heater."""
|
||||||
self._prop_mode = None
|
self._prop_mode = None
|
||||||
self._hvac_mode_map = None
|
self._hvac_mode_map = None
|
||||||
@ -673,8 +612,7 @@ class PtcBathHeater(
|
|||||||
for prop in entity_data.props:
|
for prop in entity_data.props:
|
||||||
if prop.name == 'mode':
|
if prop.name == 'mode':
|
||||||
if not prop.value_list:
|
if not prop.value_list:
|
||||||
_LOGGER.error(
|
_LOGGER.error('invalid mode value_list, %s', self.entity_id)
|
||||||
'invalid mode value_list, %s', self.entity_id)
|
|
||||||
continue
|
continue
|
||||||
self._hvac_mode_map = {}
|
self._hvac_mode_map = {}
|
||||||
for item in prop.value_list.items:
|
for item in prop.value_list.items:
|
||||||
@ -697,38 +635,29 @@ class PtcBathHeater(
|
|||||||
"""Set the target hvac mode."""
|
"""Set the target hvac mode."""
|
||||||
if self._prop_mode is None:
|
if self._prop_mode is None:
|
||||||
return
|
return
|
||||||
mode_value = self.get_map_key(
|
mode_value = self.get_map_key(map_=self._hvac_mode_map, value=hvac_mode)
|
||||||
map_=self._hvac_mode_map, value=hvac_mode)
|
|
||||||
if mode_value is None or not await self.set_property_async(
|
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(
|
raise RuntimeError(
|
||||||
f'set climate prop.mode failed, {hvac_mode}, {self.entity_id}')
|
f'set climate prop.mode failed, {hvac_mode}, {self.entity_id}')
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def hvac_mode(self) -> Optional[HVACMode]:
|
def hvac_mode(self) -> Optional[HVACMode]:
|
||||||
"""The current hvac mode."""
|
"""The current hvac mode."""
|
||||||
return (
|
return (self.get_map_value(map_=self._hvac_mode_map,
|
||||||
self.get_map_value(
|
key=self.get_prop_value(
|
||||||
map_=self._hvac_mode_map,
|
prop=self._prop_mode))
|
||||||
key=self.get_prop_value(prop=self._prop_mode))
|
if self._prop_mode else None)
|
||||||
if self._prop_mode else None)
|
|
||||||
|
|
||||||
|
|
||||||
class Thermostat(
|
class Thermostat(FeatureOnOff, FeatureTargetTemperature, FeatureTemperature,
|
||||||
FeatureOnOff,
|
FeatureHumidity, FeatureFanMode):
|
||||||
FeatureTargetTemperature,
|
|
||||||
FeatureTemperature,
|
|
||||||
FeatureHumidity,
|
|
||||||
FeatureFanMode
|
|
||||||
):
|
|
||||||
"""Thermostat"""
|
"""Thermostat"""
|
||||||
_prop_mode: Optional[MIoTSpecProperty]
|
_prop_mode: Optional[MIoTSpecProperty]
|
||||||
_hvac_mode_map: Optional[dict[int, HVACMode]]
|
_hvac_mode_map: Optional[dict[int, HVACMode]]
|
||||||
|
|
||||||
def __init__(
|
def __init__(self, miot_device: MIoTDevice,
|
||||||
self, miot_device: MIoTDevice, entity_data: MIoTEntityData
|
entity_data: MIoTEntityData) -> None:
|
||||||
) -> None:
|
|
||||||
"""Initialize the thermostat."""
|
"""Initialize the thermostat."""
|
||||||
self._prop_mode = None
|
self._prop_mode = None
|
||||||
self._hvac_mode_map = None
|
self._hvac_mode_map = None
|
||||||
@ -736,11 +665,11 @@ class Thermostat(
|
|||||||
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
||||||
self._attr_icon = 'mdi:thermostat'
|
self._attr_icon = 'mdi:thermostat'
|
||||||
# hvac modes
|
# hvac modes
|
||||||
|
self._attr_hvac_modes = None
|
||||||
for prop in entity_data.props:
|
for prop in entity_data.props:
|
||||||
if prop.name == 'mode':
|
if prop.name == 'mode':
|
||||||
if not prop.value_list:
|
if not prop.value_list:
|
||||||
_LOGGER.error(
|
_LOGGER.error('invalid mode value_list, %s', self.entity_id)
|
||||||
'invalid mode value_list, %s', self.entity_id)
|
|
||||||
continue
|
continue
|
||||||
self._hvac_mode_map = {}
|
self._hvac_mode_map = {}
|
||||||
for item in prop.value_list.items:
|
for item in prop.value_list.items:
|
||||||
@ -768,12 +697,10 @@ class Thermostat(
|
|||||||
"""Set the target hvac mode."""
|
"""Set the target hvac mode."""
|
||||||
# set the device off
|
# set the device off
|
||||||
if hvac_mode == HVACMode.OFF:
|
if hvac_mode == HVACMode.OFF:
|
||||||
if not await self.set_property_async(
|
if not await self.set_property_async(prop=self._prop_on,
|
||||||
prop=self._prop_on, value=False
|
value=False):
|
||||||
):
|
raise RuntimeError(f'set climate prop.on failed, {hvac_mode}, '
|
||||||
raise RuntimeError(
|
f'{self.entity_id}')
|
||||||
f'set climate prop.on failed, {hvac_mode}, '
|
|
||||||
f'{self.entity_id}')
|
|
||||||
return
|
return
|
||||||
# set the device on
|
# set the device on
|
||||||
elif self.get_prop_value(prop=self._prop_on) is False:
|
elif self.get_prop_value(prop=self._prop_on) is False:
|
||||||
@ -781,12 +708,9 @@ class Thermostat(
|
|||||||
# set mode
|
# set mode
|
||||||
if self._prop_mode is None:
|
if self._prop_mode is None:
|
||||||
return
|
return
|
||||||
mode_value = self.get_map_key(
|
mode_value = self.get_map_key(map_=self._hvac_mode_map, value=hvac_mode)
|
||||||
map_=self._hvac_mode_map, value=hvac_mode
|
|
||||||
)
|
|
||||||
if mode_value is None or not await self.set_property_async(
|
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(
|
raise RuntimeError(
|
||||||
f'set climate prop.mode failed, {hvac_mode}, {self.entity_id}')
|
f'set climate prop.mode failed, {hvac_mode}, {self.entity_id}')
|
||||||
|
|
||||||
@ -795,8 +719,7 @@ class Thermostat(
|
|||||||
"""The current hvac mode."""
|
"""The current hvac mode."""
|
||||||
if self.get_prop_value(prop=self._prop_on) is False:
|
if self.get_prop_value(prop=self._prop_on) is False:
|
||||||
return HVACMode.OFF
|
return HVACMode.OFF
|
||||||
return (
|
return (self.get_map_value(map_=self._hvac_mode_map,
|
||||||
self.get_map_value(
|
key=self.get_prop_value(
|
||||||
map_=self._hvac_mode_map,
|
prop=self._prop_mode))
|
||||||
key=self.get_prop_value(prop=self._prop_mode))
|
if self._prop_mode else None)
|
||||||
if self._prop_mode else None)
|
|
||||||
|
|||||||
@ -200,7 +200,7 @@ class Cover(MIoTServiceEntity, CoverEntity):
|
|||||||
if pos is None:
|
if pos is None:
|
||||||
return None
|
return None
|
||||||
pos = round(pos*self._prop_position_value_range/100)
|
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)
|
prop=self._prop_target_position, value=pos)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
@ -303,7 +303,7 @@ class Fan(MIoTServiceEntity, FanEntity):
|
|||||||
fan_level = self.get_prop_value(prop=self._prop_fan_level)
|
fan_level = self.get_prop_value(prop=self._prop_fan_level)
|
||||||
if fan_level is None:
|
if fan_level is None:
|
||||||
return None
|
return None
|
||||||
if self._speed_names:
|
if self._speed_names and self._speed_name_map:
|
||||||
return ordered_list_item_to_percentage(
|
return ordered_list_item_to_percentage(
|
||||||
self._speed_names, self._speed_name_map[fan_level])
|
self._speed_names, self._speed_name_map[fan_level])
|
||||||
else:
|
else:
|
||||||
|
|||||||
@ -96,7 +96,7 @@ class Light(MIoTServiceEntity, LightEntity):
|
|||||||
"""Light entities for Xiaomi Home."""
|
"""Light entities for Xiaomi Home."""
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
_VALUE_RANGE_MODE_COUNT_MAX = 30
|
_VALUE_RANGE_MODE_COUNT_MAX = 30
|
||||||
_prop_on: MIoTSpecProperty
|
_prop_on: Optional[MIoTSpecProperty]
|
||||||
_prop_brightness: Optional[MIoTSpecProperty]
|
_prop_brightness: Optional[MIoTSpecProperty]
|
||||||
_prop_color_temp: Optional[MIoTSpecProperty]
|
_prop_color_temp: Optional[MIoTSpecProperty]
|
||||||
_prop_color: Optional[MIoTSpecProperty]
|
_prop_color: Optional[MIoTSpecProperty]
|
||||||
@ -250,23 +250,25 @@ class Light(MIoTServiceEntity, LightEntity):
|
|||||||
|
|
||||||
Shall set attributes in kwargs if applicable.
|
Shall set attributes in kwargs if applicable.
|
||||||
"""
|
"""
|
||||||
result: bool = False
|
|
||||||
# on
|
# on
|
||||||
# Dirty logic for lumi.gateway.mgl03 indicator light
|
# Dirty logic for lumi.gateway.mgl03 indicator light
|
||||||
value_on = True if self._prop_on.format_ == bool else 1
|
if self._prop_on:
|
||||||
result = await self.set_property_async(
|
value_on = True if self._prop_on.format_ == bool else 1
|
||||||
prop=self._prop_on, value=value_on)
|
await self.set_property_async(
|
||||||
|
prop=self._prop_on, value=value_on)
|
||||||
# brightness
|
# brightness
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
brightness = brightness_to_value(
|
brightness = brightness_to_value(
|
||||||
self._brightness_scale, kwargs[ATTR_BRIGHTNESS])
|
self._brightness_scale, kwargs[ATTR_BRIGHTNESS])
|
||||||
result = await self.set_property_async(
|
await self.set_property_async(
|
||||||
prop=self._prop_brightness, value=brightness)
|
prop=self._prop_brightness, value=brightness,
|
||||||
|
write_ha_state=False)
|
||||||
# color-temperature
|
# color-temperature
|
||||||
if ATTR_COLOR_TEMP_KELVIN in kwargs:
|
if ATTR_COLOR_TEMP_KELVIN in kwargs:
|
||||||
result = await self.set_property_async(
|
await self.set_property_async(
|
||||||
prop=self._prop_color_temp,
|
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
|
self._attr_color_mode = ColorMode.COLOR_TEMP
|
||||||
# rgb color
|
# rgb color
|
||||||
if ATTR_RGB_COLOR in kwargs:
|
if ATTR_RGB_COLOR in kwargs:
|
||||||
@ -274,19 +276,23 @@ class Light(MIoTServiceEntity, LightEntity):
|
|||||||
g = kwargs[ATTR_RGB_COLOR][1]
|
g = kwargs[ATTR_RGB_COLOR][1]
|
||||||
b = kwargs[ATTR_RGB_COLOR][2]
|
b = kwargs[ATTR_RGB_COLOR][2]
|
||||||
rgb = (r << 16) | (g << 8) | b
|
rgb = (r << 16) | (g << 8) | b
|
||||||
result = await self.set_property_async(
|
await self.set_property_async(
|
||||||
prop=self._prop_color, value=rgb)
|
prop=self._prop_color, value=rgb,
|
||||||
|
write_ha_state=False)
|
||||||
self._attr_color_mode = ColorMode.RGB
|
self._attr_color_mode = ColorMode.RGB
|
||||||
# mode
|
# mode
|
||||||
if ATTR_EFFECT in kwargs:
|
if ATTR_EFFECT in kwargs:
|
||||||
result = await self.set_property_async(
|
await self.set_property_async(
|
||||||
prop=self._prop_mode,
|
prop=self._prop_mode,
|
||||||
value=self.get_map_key(
|
value=self.get_map_key(
|
||||||
map_=self._mode_map, value=kwargs[ATTR_EFFECT]))
|
map_=self._mode_map, value=kwargs[ATTR_EFFECT]),
|
||||||
return result
|
write_ha_state=False)
|
||||||
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs) -> None:
|
async def async_turn_off(self, **kwargs) -> None:
|
||||||
"""Turn the light off."""
|
"""Turn the light off."""
|
||||||
|
if not self._prop_on:
|
||||||
|
return
|
||||||
# Dirty logic for lumi.gateway.mgl03 indicator light
|
# Dirty logic for lumi.gateway.mgl03 indicator light
|
||||||
value_on = False if self._prop_on.format_ == bool else 0
|
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)
|
siid=event.service.iid, eiid=event.iid, sub_id=sub_id)
|
||||||
|
|
||||||
def get_map_value(
|
def get_map_value(
|
||||||
self, map_: dict[int, Any], key: int
|
self, map_: Optional[dict[int, Any]], key: int
|
||||||
) -> Any:
|
) -> Any:
|
||||||
if map_ is None:
|
if map_ is None:
|
||||||
return None
|
return None
|
||||||
return map_.get(key, None)
|
return map_.get(key, None)
|
||||||
|
|
||||||
def get_map_key(
|
def get_map_key(
|
||||||
self, map_: dict[int, Any], value: Any
|
self, map_: Optional[dict[int, Any]], value: Any
|
||||||
) -> Optional[int]:
|
) -> Optional[int]:
|
||||||
if map_ is None:
|
if map_ is None:
|
||||||
return None
|
return None
|
||||||
@ -1008,7 +1008,7 @@ class MIoTServiceEntity(Entity):
|
|||||||
return key
|
return key
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def get_prop_value(self, prop: MIoTSpecProperty) -> Any:
|
def get_prop_value(self, prop: Optional[MIoTSpecProperty]) -> Any:
|
||||||
if not prop:
|
if not prop:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
'get_prop_value error, property is None, %s, %s',
|
'get_prop_value error, property is None, %s, %s',
|
||||||
@ -1016,7 +1016,9 @@ class MIoTServiceEntity(Entity):
|
|||||||
return None
|
return None
|
||||||
return self._prop_value_map.get(prop, 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:
|
if not prop:
|
||||||
_LOGGER.error(
|
_LOGGER.error(
|
||||||
'set_prop_value error, property is None, %s, %s',
|
'set_prop_value error, property is None, %s, %s',
|
||||||
@ -1025,13 +1027,14 @@ class MIoTServiceEntity(Entity):
|
|||||||
self._prop_value_map[prop] = value
|
self._prop_value_map[prop] = value
|
||||||
|
|
||||||
async def set_property_async(
|
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:
|
) -> bool:
|
||||||
value = prop.value_format(value)
|
|
||||||
if not prop:
|
if not prop:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f'set property failed, property is None, '
|
f'set property failed, property is None, '
|
||||||
f'{self.entity_id}, {self.name}')
|
f'{self.entity_id}, {self.name}')
|
||||||
|
value = prop.value_format(value)
|
||||||
if prop not in self.entity_data.props:
|
if prop not in self.entity_data.props:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f'set property failed, unknown property, '
|
f'set property failed, unknown property, '
|
||||||
@ -1047,8 +1050,9 @@ class MIoTServiceEntity(Entity):
|
|||||||
except MIoTClientError as e:
|
except MIoTClientError as e:
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
f'{e}, {self.entity_id}, {self.name}, {prop.name}') from e
|
f'{e}, {self.entity_id}, {self.name}, {prop.name}') from e
|
||||||
if update:
|
if update_value:
|
||||||
self._prop_value_map[prop] = value
|
self._prop_value_map[prop] = value
|
||||||
|
if write_ha_state:
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
@ -100,7 +100,7 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the Water heater."""
|
"""Initialize the Water heater."""
|
||||||
super().__init__(miot_device=miot_device, entity_data=entity_data)
|
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._attr_supported_features = WaterHeaterEntityFeature(0)
|
||||||
self._prop_on = None
|
self._prop_on = None
|
||||||
self._prop_temp = None
|
self._prop_temp = None
|
||||||
@ -112,20 +112,20 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
|
|||||||
for prop in entity_data.props:
|
for prop in entity_data.props:
|
||||||
# on
|
# on
|
||||||
if prop.name == 'on':
|
if prop.name == 'on':
|
||||||
|
self._attr_supported_features |= WaterHeaterEntityFeature.ON_OFF
|
||||||
self._prop_on = prop
|
self._prop_on = prop
|
||||||
# temperature
|
# temperature
|
||||||
if prop.name == 'temperature':
|
if prop.name == 'temperature':
|
||||||
if prop.value_range:
|
if not 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(
|
_LOGGER.error(
|
||||||
'invalid temperature value_range format, %s',
|
'invalid temperature value_range format, %s',
|
||||||
self.entity_id)
|
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
|
# target-temperature
|
||||||
if prop.name == 'target-temperature':
|
if prop.name == 'target-temperature':
|
||||||
if not prop.value_range:
|
if not prop.value_range:
|
||||||
@ -133,8 +133,8 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
|
|||||||
'invalid target-temperature value_range format, %s',
|
'invalid target-temperature value_range format, %s',
|
||||||
self.entity_id)
|
self.entity_id)
|
||||||
continue
|
continue
|
||||||
self._attr_min_temp = prop.value_range.min_
|
self._attr_target_temperature_low = prop.value_range.min_
|
||||||
self._attr_max_temp = prop.value_range.max_
|
self._attr_target_temperature_high = prop.value_range.max_
|
||||||
self._attr_precision = prop.value_range.step
|
self._attr_precision = prop.value_range.step
|
||||||
if self._attr_temperature_unit is None and prop.external_unit:
|
if self._attr_temperature_unit is None and prop.external_unit:
|
||||||
self._attr_temperature_unit = 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:
|
async def async_set_temperature(self, **kwargs: Any) -> None:
|
||||||
"""Set the temperature the water heater should heat water to."""
|
"""Set the temperature the water heater should heat water to."""
|
||||||
|
if not self._prop_target_temp:
|
||||||
|
return
|
||||||
await self.set_property_async(
|
await self.set_property_async(
|
||||||
prop=self._prop_target_temp, value=kwargs[ATTR_TEMPERATURE])
|
prop=self._prop_target_temp, value=kwargs[ATTR_TEMPERATURE])
|
||||||
|
|
||||||
@ -181,16 +183,11 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
|
|||||||
return
|
return
|
||||||
if self.get_prop_value(prop=self._prop_on) is False:
|
if self.get_prop_value(prop=self._prop_on) is False:
|
||||||
await self.set_property_async(
|
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(
|
await self.set_property_async(
|
||||||
prop=self._prop_mode,
|
prop=self._prop_mode,
|
||||||
value=self.get_map_key(
|
value=self.get_map_key(
|
||||||
map_=self._mode_map,
|
map_=self._mode_map, value=operation_mode))
|
||||||
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
|
@property
|
||||||
def current_temperature(self) -> Optional[float]:
|
def current_temperature(self) -> Optional[float]:
|
||||||
@ -200,6 +197,8 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
|
|||||||
@property
|
@property
|
||||||
def target_temperature(self) -> Optional[float]:
|
def target_temperature(self) -> Optional[float]:
|
||||||
"""Return the target temperature."""
|
"""Return the target temperature."""
|
||||||
|
if not self._prop_target_temp:
|
||||||
|
return None
|
||||||
return self.get_prop_value(prop=self._prop_target_temp)
|
return self.get_prop_value(prop=self._prop_target_temp)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user