Define WaterHeaterEntity entity attributes as class variables (#51903)

This commit is contained in:
Franck Nijhof 2021-06-17 10:19:29 +02:00 committed by GitHub
parent 3b00e87ebc
commit 9f17b8856a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 61 additions and 77 deletions

View File

@ -30,23 +30,29 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class DemoWaterHeater(WaterHeaterEntity): class DemoWaterHeater(WaterHeaterEntity):
"""Representation of a demo water_heater device.""" """Representation of a demo water_heater device."""
_attr_should_poll = False
_attr_supported_features = SUPPORT_FLAGS_HEATER
def __init__( def __init__(
self, name, target_temperature, unit_of_measurement, away, current_operation self, name, target_temperature, unit_of_measurement, away, current_operation
): ):
"""Initialize the water_heater device.""" """Initialize the water_heater device."""
self._name = name self._attr_name = name
self._support_flags = SUPPORT_FLAGS_HEATER
if target_temperature is not None: if target_temperature is not None:
self._support_flags = self._support_flags | SUPPORT_TARGET_TEMPERATURE self._attr_supported_features = (
self.supported_features | SUPPORT_TARGET_TEMPERATURE
)
if away is not None: if away is not None:
self._support_flags = self._support_flags | SUPPORT_AWAY_MODE self._attr_supported_features = self.supported_features | SUPPORT_AWAY_MODE
if current_operation is not None: if current_operation is not None:
self._support_flags = self._support_flags | SUPPORT_OPERATION_MODE self._attr_supported_features = (
self._target_temperature = target_temperature self.supported_features | SUPPORT_OPERATION_MODE
self._unit_of_measurement = unit_of_measurement )
self._away = away self._attr_target_temperature = target_temperature
self._current_operation = current_operation self._attr_temperature_unit = unit_of_measurement
self._operation_list = [ self._attr_is_away_mode_on = away
self._attr_current_operation = current_operation
self._attr_operation_list = [
"eco", "eco",
"electric", "electric",
"performance", "performance",
@ -56,62 +62,22 @@ class DemoWaterHeater(WaterHeaterEntity):
"off", "off",
] ]
@property
def supported_features(self):
"""Return the list of supported features."""
return self._support_flags
@property
def should_poll(self):
"""Return the polling state."""
return False
@property
def name(self):
"""Return the name of the water_heater device."""
return self._name
@property
def temperature_unit(self):
"""Return the unit of measurement."""
return self._unit_of_measurement
@property
def target_temperature(self):
"""Return the temperature we try to reach."""
return self._target_temperature
@property
def current_operation(self):
"""Return current operation ie. heat, cool, idle."""
return self._current_operation
@property
def operation_list(self):
"""Return the list of available operation modes."""
return self._operation_list
@property
def is_away_mode_on(self):
"""Return if away mode is on."""
return self._away
def set_temperature(self, **kwargs): def set_temperature(self, **kwargs):
"""Set new target temperatures.""" """Set new target temperatures."""
self._target_temperature = kwargs.get(ATTR_TEMPERATURE) self._attr_target_temperature = kwargs.get(ATTR_TEMPERATURE)
self.schedule_update_ha_state() self.schedule_update_ha_state()
def set_operation_mode(self, operation_mode): def set_operation_mode(self, operation_mode):
"""Set new operation mode.""" """Set new operation mode."""
self._current_operation = operation_mode self._attr_current_operation = operation_mode
self.schedule_update_ha_state() self.schedule_update_ha_state()
def turn_away_mode_on(self): def turn_away_mode_on(self):
"""Turn away mode on.""" """Turn away mode on."""
self._away = True self._attr_is_away_mode_on = True
self.schedule_update_ha_state() self.schedule_update_ha_state()
def turn_away_mode_off(self): def turn_away_mode_off(self):
"""Turn away mode off.""" """Turn away mode off."""
self._away = False self._attr_is_away_mode_on = False
self.schedule_update_ha_state() self.schedule_update_ha_state()

View File

@ -1,4 +1,6 @@
"""Support for water heater devices.""" """Support for water heater devices."""
from __future__ import annotations
from datetime import timedelta from datetime import timedelta
import functools as ft import functools as ft
import logging import logging
@ -136,14 +138,31 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class WaterHeaterEntity(Entity): class WaterHeaterEntity(Entity):
"""Base class for water heater entities.""" """Base class for water heater entities."""
_attr_current_operation: str | None = None
_attr_current_temperature: float | None = None
_attr_is_away_mode_on: bool | None = None
_attr_max_temp: float
_attr_min_temp: float
_attr_operation_list: list[str] | None = None
_attr_precision: float
_attr_state: None = None
_attr_supported_features: int
_attr_target_temperature_high: float | None = None
_attr_target_temperature_low: float | None = None
_attr_target_temperature: float | None = None
_attr_temperature_unit: str
@final
@property @property
def state(self): def state(self) -> str | None:
"""Return the current state.""" """Return the current state."""
return self.current_operation return self.current_operation
@property @property
def precision(self): def precision(self) -> float:
"""Return the precision of the system.""" """Return the precision of the system."""
if hasattr(self, "_attr_precision"):
return self._attr_precision
if self.hass.config.units.temperature_unit == TEMP_CELSIUS: if self.hass.config.units.temperature_unit == TEMP_CELSIUS:
return PRECISION_TENTHS return PRECISION_TENTHS
return PRECISION_WHOLE return PRECISION_WHOLE
@ -210,44 +229,44 @@ class WaterHeaterEntity(Entity):
return data return data
@property @property
def temperature_unit(self): def temperature_unit(self) -> str:
"""Return the unit of measurement used by the platform.""" """Return the unit of measurement used by the platform."""
raise NotImplementedError return self._attr_temperature_unit
@property @property
def current_operation(self): def current_operation(self) -> str | None:
"""Return current operation ie. eco, electric, performance, ...""" """Return current operation ie. eco, electric, performance, ..."""
return None return self._attr_current_operation
@property @property
def operation_list(self): def operation_list(self) -> list[str] | None:
"""Return the list of available operation modes.""" """Return the list of available operation modes."""
return None return self._attr_operation_list
@property @property
def current_temperature(self): def current_temperature(self) -> float | None:
"""Return the current temperature.""" """Return the current temperature."""
return None return self._attr_current_temperature
@property @property
def target_temperature(self): def target_temperature(self) -> float | None:
"""Return the temperature we try to reach.""" """Return the temperature we try to reach."""
return None return self._attr_target_temperature
@property @property
def target_temperature_high(self): def target_temperature_high(self) -> float | None:
"""Return the highbound target temperature we try to reach.""" """Return the highbound target temperature we try to reach."""
return None return self._attr_target_temperature_high
@property @property
def target_temperature_low(self): def target_temperature_low(self) -> float | None:
"""Return the lowbound target temperature we try to reach.""" """Return the lowbound target temperature we try to reach."""
return None return self._attr_target_temperature_low
@property @property
def is_away_mode_on(self): def is_away_mode_on(self) -> bool | None:
"""Return true if away mode is on.""" """Return true if away mode is on."""
return None return self._attr_is_away_mode_on
def set_temperature(self, **kwargs): def set_temperature(self, **kwargs):
"""Set new target temperature.""" """Set new target temperature."""
@ -283,14 +302,11 @@ class WaterHeaterEntity(Entity):
"""Turn away mode off.""" """Turn away mode off."""
await self.hass.async_add_executor_job(self.turn_away_mode_off) await self.hass.async_add_executor_job(self.turn_away_mode_off)
@property
def supported_features(self):
"""Return the list of supported features."""
raise NotImplementedError()
@property @property
def min_temp(self): def min_temp(self):
"""Return the minimum temperature.""" """Return the minimum temperature."""
if hasattr(self, "_attr_min_temp"):
return self._attr_min_temp
return convert_temperature( return convert_temperature(
DEFAULT_MIN_TEMP, TEMP_FAHRENHEIT, self.temperature_unit DEFAULT_MIN_TEMP, TEMP_FAHRENHEIT, self.temperature_unit
) )
@ -298,6 +314,8 @@ class WaterHeaterEntity(Entity):
@property @property
def max_temp(self): def max_temp(self):
"""Return the maximum temperature.""" """Return the maximum temperature."""
if hasattr(self, "_attr_max_temp"):
return self._attr_max_temp
return convert_temperature( return convert_temperature(
DEFAULT_MAX_TEMP, TEMP_FAHRENHEIT, self.temperature_unit DEFAULT_MAX_TEMP, TEMP_FAHRENHEIT, self.temperature_unit
) )