mirror of
https://github.com/XiaoMi/ha_xiaomi_home.git
synced 2026-01-16 23:00:43 +08:00
style: format the file based on google style
This commit is contained in:
parent
3053099fd5
commit
ad8ca02fa1
@ -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
|
||||||
@ -524,8 +476,7 @@ class AirConditioner(
|
|||||||
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 +497,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,25 +509,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
|
||||||
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(prop=self._prop_on, value=True,
|
await self.set_property_async(prop=self._prop_on,
|
||||||
|
value=True,
|
||||||
write_ha_state=False)
|
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}')
|
||||||
|
|
||||||
@ -585,11 +533,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
|
||||||
@ -619,55 +566,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 (
|
if ('D' in v_ac_state and self._attr_swing_modes and
|
||||||
'D' in v_ac_state
|
len(self._attr_swing_modes) == 2):
|
||||||
and self._attr_swing_modes
|
if (SWING_HORIZONTAL in self._attr_swing_modes and
|
||||||
and len(self._attr_swing_modes) == 2
|
self._prop_horizontal_swing):
|
||||||
):
|
self.set_prop_value(prop=self._prop_horizontal_swing,
|
||||||
if (
|
value=v_ac_state['D'] == 0)
|
||||||
SWING_HORIZONTAL in self._attr_swing_modes
|
elif (SWING_VERTICAL in self._attr_swing_modes and
|
||||||
and self._prop_horizontal_swing
|
self._prop_vertical_swing):
|
||||||
):
|
self.set_prop_value(prop=self._prop_vertical_swing,
|
||||||
self.set_prop_value(
|
value=v_ac_state['D'] == 0)
|
||||||
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)
|
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
|
||||||
@ -678,8 +611,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:
|
||||||
@ -702,38 +634,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
|
||||||
@ -744,8 +667,7 @@ class Thermostat(
|
|||||||
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:
|
||||||
@ -773,12 +695,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:
|
||||||
@ -786,12 +706,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}')
|
||||||
|
|
||||||
@ -800,8 +717,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)
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user