fix: water heater entity add STATE_OFF

This commit is contained in:
topsworld 2024-12-17 20:50:01 +08:00 committed by Paul Shawn
parent 8a1e464cdd
commit bf435374d2

View File

@ -53,6 +53,7 @@ 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_OFF,
ATTR_TEMPERATURE, ATTR_TEMPERATURE,
WaterHeaterEntity, WaterHeaterEntity,
WaterHeaterEntityFeature WaterHeaterEntityFeature
@ -116,6 +117,7 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
if isinstance(prop.value_range, dict): if isinstance(prop.value_range, dict):
self._attr_min_temp = prop.value_range['min'] self._attr_min_temp = prop.value_range['min']
self._attr_max_temp = prop.value_range['max'] self._attr_max_temp = prop.value_range['max']
self._attr_precision = prop.value_range['step']
if ( if (
self._attr_temperature_unit is None self._attr_temperature_unit is None
and prop.external_unit and prop.external_unit
@ -149,6 +151,7 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
self._attr_supported_features |= ( self._attr_supported_features |= (
WaterHeaterEntityFeature.OPERATION_MODE) WaterHeaterEntityFeature.OPERATION_MODE)
self._prop_mode = prop self._prop_mode = prop
self._attr_operation_list.append(STATE_OFF)
async def async_turn_on(self) -> None: async def async_turn_on(self) -> None:
"""Turn the water heater on.""" """Turn the water heater on."""
@ -167,6 +170,12 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
"""Set the operation mode of the water heater. """Set the operation mode of the water heater.
Must be in the operation_list. Must be in the operation_list.
""" """
if operation_mode == STATE_OFF:
await self.set_property_async(prop=self._prop_on, value=False)
return
if self.get_prop_value(prop=self._prop_on) is False:
await self.set_property_async(
prop=self._prop_on, value=True, update=False)
await self.set_property_async( await self.set_property_async(
prop=self._prop_mode, prop=self._prop_mode,
value=self.__get_mode_value(description=operation_mode)) value=self.__get_mode_value(description=operation_mode))
@ -188,6 +197,8 @@ class WaterHeater(MIoTServiceEntity, WaterHeaterEntity):
@property @property
def current_operation(self) -> Optional[str]: def current_operation(self) -> Optional[str]:
"""Return the current mode.""" """Return the current mode."""
if self.get_prop_value(prop=self._prop_on) is False:
return STATE_OFF
return self.__get_mode_description( return self.__get_mode_description(
key=self.get_prop_value(prop=self._prop_mode)) key=self.get_prop_value(prop=self._prop_mode))