fix: fix water heater error & some type error (#684)

* fix: fix water heater target-temp error

* feat: update miot device logic

* fix: fix some type error
This commit is contained in:
Paul Shawn 2025-01-22 19:55:41 +08:00 committed by GitHub
parent 8778b00c3a
commit 52fd6371ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 87 additions and 78 deletions

View File

@ -296,39 +296,37 @@ class AirConditioner(MIoTServiceEntity, ClimateEntity):
async def async_set_swing_mode(self, swing_mode): async def async_set_swing_mode(self, swing_mode):
"""Set new target swing operation.""" """Set new target swing operation."""
if swing_mode == SWING_BOTH: if swing_mode == SWING_BOTH:
if await self.set_property_async( await self.set_property_async(
prop=self._prop_horizontal_swing, value=True, update=False): prop=self._prop_horizontal_swing, value=True,
self.set_prop_value(self._prop_horizontal_swing, value=True) write_ha_state=False)
if await self.set_property_async( await self.set_property_async(
prop=self._prop_vertical_swing, value=True, update=False): prop=self._prop_vertical_swing, value=True)
self.set_prop_value(self._prop_vertical_swing, value=True)
elif swing_mode == SWING_HORIZONTAL: elif swing_mode == SWING_HORIZONTAL:
if await self.set_property_async( await self.set_property_async(
prop=self._prop_horizontal_swing, value=True, update=False): prop=self._prop_horizontal_swing, value=True)
self.set_prop_value(self._prop_horizontal_swing, value=True)
elif swing_mode == SWING_VERTICAL: elif swing_mode == SWING_VERTICAL:
if await self.set_property_async( await self.set_property_async(
prop=self._prop_vertical_swing, value=True, update=False): prop=self._prop_vertical_swing, value=True)
self.set_prop_value(self._prop_vertical_swing, value=True)
elif swing_mode == SWING_ON: elif swing_mode == SWING_ON:
if await self.set_property_async( await self.set_property_async(
prop=self._prop_fan_on, value=True, update=False): prop=self._prop_fan_on, value=True)
self.set_prop_value(self._prop_fan_on, value=True)
elif swing_mode == SWING_OFF: elif swing_mode == SWING_OFF:
if self._prop_fan_on and await self.set_property_async( if self._prop_fan_on:
prop=self._prop_fan_on, value=False, update=False): await self.set_property_async(
self.set_prop_value(self._prop_fan_on, value=False) prop=self._prop_fan_on, value=False,
if self._prop_horizontal_swing and await self.set_property_async( write_ha_state=False)
if self._prop_horizontal_swing:
await self.set_property_async(
prop=self._prop_horizontal_swing, value=False, prop=self._prop_horizontal_swing, value=False,
update=False): write_ha_state=False)
self.set_prop_value(self._prop_horizontal_swing, value=False) if self._prop_vertical_swing:
if self._prop_vertical_swing and await self.set_property_async( await self.set_property_async(
prop=self._prop_vertical_swing, value=False, update=False): prop=self._prop_vertical_swing, value=False,
self.set_prop_value(self._prop_vertical_swing, value=False) write_ha_state=False)
self.async_write_ha_state()
else: else:
raise RuntimeError( raise RuntimeError(
f'unknown swing_mode, {swing_mode}, {self.entity_id}') f'unknown swing_mode, {swing_mode}, {self.entity_id}')
self.async_write_ha_state()
async def async_set_fan_mode(self, fan_mode): async def async_set_fan_mode(self, fan_mode):
"""Set new target fan mode.""" """Set new target fan mode."""
@ -389,12 +387,10 @@ class AirConditioner(MIoTServiceEntity, ClimateEntity):
Requires ClimateEntityFeature.SWING_MODE. Requires ClimateEntityFeature.SWING_MODE.
""" """
horizontal: bool = ( horizontal = (
self.get_prop_value(prop=self._prop_horizontal_swing) self.get_prop_value(prop=self._prop_horizontal_swing))
if self._prop_horizontal_swing else None) vertical = (
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 None)
if horizontal and vertical: if horizontal and vertical:
return SWING_BOTH return SWING_BOTH
if horizontal: if horizontal:
@ -450,7 +446,11 @@ class AirConditioner(MIoTServiceEntity, ClimateEntity):
self.set_prop_value(prop=self._prop_fan_level, self.set_prop_value(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 len(self._attr_swing_modes) == 2
):
if ( if (
SWING_HORIZONTAL in self._attr_swing_modes SWING_HORIZONTAL in self._attr_swing_modes
and self._prop_horizontal_swing and self._prop_horizontal_swing
@ -465,10 +465,10 @@ class AirConditioner(MIoTServiceEntity, ClimateEntity):
self.set_prop_value( self.set_prop_value(
prop=self._prop_vertical_swing, prop=self._prop_vertical_swing,
value=v_ac_state['D'] == 0) value=v_ac_state['D'] == 0)
if self._value_ac_state:
self._value_ac_state.update(v_ac_state) self._value_ac_state.update(v_ac_state)
_LOGGER.debug( _LOGGER.debug(
'ac_state update, %s', self._value_ac_state) 'ac_state update, %s', self._value_ac_state)
class Heater(MIoTServiceEntity, ClimateEntity): class Heater(MIoTServiceEntity, ClimateEntity):

View File

@ -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

View File

@ -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:

View File

@ -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)

View File

@ -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

View File

@ -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