feat: add an alongside switch entity for the water heater (#1115)

This commit is contained in:
Li Shuzhen 2025-05-23 09:10:11 +08:00 committed by GitHub
parent 1bd338639b
commit 62dd32a132
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 53 deletions

View File

@ -601,7 +601,7 @@ class MIoTSpecProperty(_MIoTSpecBase):
if value is None: if value is None:
return None return None
if self.format_ == int: if self.format_ == int:
return int(value) return int(round(value))
if self.format_ == float: if self.format_ == float:
return round(value, self.precision) return round(value, self.precision)
if self.format_ == bool: if self.format_ == bool:

View File

@ -18,5 +18,45 @@
} }
] ]
} }
],
"urn:miot-spec-v2:device:water-heater:0000A02A:xiaomi-yms2:1": [
{
"iid": 2,
"type": "urn:miot-spec-v2:service:switch:0000780C:xiaomi-yms2:1",
"description": "Switch",
"properties": [
{
"iid": 6,
"type": "urn:miot-spec-v2:property:on:00000006:xiaomi-yms2:1",
"description": "Switch Status",
"format": "bool",
"access": [
"read",
"write",
"notify"
]
}
]
}
],
"urn:miot-spec-v2:device:water-heater:0000A02A:zimi-h03:1": [
{
"iid": 2,
"type": "urn:miot-spec-v2:service:switch:0000780C:zimi-h03:1",
"description": "Switch",
"properties": [
{
"iid": 6,
"type": "urn:miot-spec-v2:property:on:00000006:zimi-h03:1",
"description": "Switch Status",
"format": "bool",
"access": [
"read",
"write",
"notify"
]
}
]
}
] ]
} }

View File

@ -397,7 +397,7 @@ SPEC_SERVICE_TRANS_MAP: dict = {
} }
}, },
'optional': { 'optional': {
'properties': {'on', 'temperature', 'target-temperature', 'mode'} 'properties': {'temperature', 'target-temperature', 'mode'}
}, },
'entity': 'water_heater' 'entity': 'water_heater'
}, },

View File

@ -52,25 +52,22 @@ from typing import Any, Optional
from homeassistant.config_entries import ConfigEntry 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.water_heater import ( from homeassistant.components.water_heater import (STATE_ON, STATE_OFF,
STATE_ON, ATTR_TEMPERATURE,
STATE_OFF, WaterHeaterEntity,
ATTR_TEMPERATURE, WaterHeaterEntityFeature)
WaterHeaterEntity,
WaterHeaterEntityFeature
)
from .miot.const import DOMAIN from .miot.const import DOMAIN
from .miot.miot_device import MIoTDevice, MIoTEntityData, MIoTServiceEntity from .miot.miot_device import MIoTDevice, MIoTEntityData, MIoTServiceEntity
from .miot.miot_spec import MIoTSpecProperty 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, hass: HomeAssistant,
config_entry: ConfigEntry, config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> 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'][
@ -79,8 +76,8 @@ async def async_setup_entry(
new_entities = [] new_entities = []
for miot_device in device_list: for miot_device in device_list:
for data in miot_device.entity_list.get('water_heater', []): for data in miot_device.entity_list.get('water_heater', []):
new_entities.append(WaterHeater( new_entities.append(
miot_device=miot_device, entity_data=data)) WaterHeater(miot_device=miot_device, entity_data=data))
if new_entities: if new_entities:
async_add_entities(new_entities) async_add_entities(new_entities)
@ -95,12 +92,11 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
_mode_map: Optional[dict[Any, Any]] _mode_map: Optional[dict[Any, Any]]
def __init__( def __init__(self, miot_device: MIoTDevice,
self, miot_device: MIoTDevice, entity_data: MIoTEntityData entity_data: MIoTEntityData) -> 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 # type: ignore self._attr_temperature_unit = None
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
@ -117,14 +113,11 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
# temperature # temperature
if prop.name == 'temperature': if prop.name == 'temperature':
if not prop.value_range: if not prop.value_range:
_LOGGER.error( _LOGGER.error('invalid temperature value_range format, %s',
'invalid temperature value_range format, %s', self.entity_id)
self.entity_id)
continue continue
if prop.external_unit: if prop.external_unit:
self._attr_temperature_unit = 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 self._prop_temp = prop
# target-temperature # target-temperature
if prop.name == 'target-temperature': if prop.name == 'target-temperature':
@ -133,9 +126,9 @@ 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_target_temperature_low = prop.value_range.min_ self._attr_min_temp = prop.value_range.min_
self._attr_target_temperature_high = prop.value_range.max_ self._attr_max_temp = prop.value_range.max_
self._attr_precision = prop.value_range.step self._attr_target_temperature_step = 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
self._attr_supported_features |= ( self._attr_supported_features |= (
@ -144,8 +137,7 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
# mode # mode
if prop.name == 'mode': if prop.name == 'mode':
if not prop.value_list: if not prop.value_list:
_LOGGER.error( _LOGGER.error('mode value_list is None, %s', self.entity_id)
'mode value_list is None, %s', self.entity_id)
continue continue
self._mode_map = prop.value_list.to_map() self._mode_map = prop.value_list.to_map()
self._attr_operation_list = list(self._mode_map.values()) self._attr_operation_list = list(self._mode_map.values())
@ -165,16 +157,12 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
await self.set_property_async(prop=self._prop_on, value=False) await self.set_property_async(prop=self._prop_on, value=False)
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 target temperature."""
if not self._prop_target_temp: await self.set_property_async(prop=self._prop_target_temp,
return value=kwargs[ATTR_TEMPERATURE])
await self.set_property_async(
prop=self._prop_target_temp, value=kwargs[ATTR_TEMPERATURE])
async def async_set_operation_mode(self, operation_mode: str) -> None: async def async_set_operation_mode(self, operation_mode: str) -> None:
"""Set the operation mode of the water heater. """Set the operation mode of the water heater."""
Must be in the operation_list.
"""
if operation_mode == STATE_OFF: if operation_mode == STATE_OFF:
await self.set_property_async(prop=self._prop_on, value=False) await self.set_property_async(prop=self._prop_on, value=False)
return return
@ -182,32 +170,32 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
await self.set_property_async(prop=self._prop_on, value=True) await self.set_property_async(prop=self._prop_on, value=True)
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,
prop=self._prop_on, value=True, write_ha_state=False) value=True,
await self.set_property_async( write_ha_state=False)
prop=self._prop_mode, await self.set_property_async(prop=self._prop_mode,
value=self.get_map_key( value=self.get_map_key(
map_=self._mode_map, value=operation_mode)) map_=self._mode_map,
value=operation_mode))
@property @property
def current_temperature(self) -> Optional[float]: def current_temperature(self) -> Optional[float]:
"""Return the current temperature.""" """The current temperature."""
return self.get_prop_value(prop=self._prop_temp) return (None if self._prop_temp is None else self.get_prop_value(
prop=self._prop_temp))
@property @property
def target_temperature(self) -> Optional[float]: def target_temperature(self) -> Optional[float]:
"""Return the target temperature.""" """The target temperature."""
if not self._prop_target_temp: return (None if self._prop_target_temp is None else self.get_prop_value(
return None prop=self._prop_target_temp))
return self.get_prop_value(prop=self._prop_target_temp)
@property @property
def current_operation(self) -> Optional[str]: def current_operation(self) -> Optional[str]:
"""Return the current mode.""" """The current mode."""
if self.get_prop_value(prop=self._prop_on) is False: if self.get_prop_value(prop=self._prop_on) is False:
return STATE_OFF return STATE_OFF
if not self._prop_mode and self.get_prop_value(prop=self._prop_on): if not self._prop_mode and self.get_prop_value(prop=self._prop_on):
return STATE_ON return STATE_ON
return self.get_map_value( return self.get_map_value(map_=self._mode_map,
map_=self._mode_map, key=self.get_prop_value(prop=self._prop_mode))
key=self.get_prop_value(prop=self._prop_mode))