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):
"""Representation of a demo water_heater device."""
_attr_should_poll = False
_attr_supported_features = SUPPORT_FLAGS_HEATER
def __init__(
self, name, target_temperature, unit_of_measurement, away, current_operation
):
"""Initialize the water_heater device."""
self._name = name
self._support_flags = SUPPORT_FLAGS_HEATER
self._attr_name = name
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:
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:
self._support_flags = self._support_flags | SUPPORT_OPERATION_MODE
self._target_temperature = target_temperature
self._unit_of_measurement = unit_of_measurement
self._away = away
self._current_operation = current_operation
self._operation_list = [
self._attr_supported_features = (
self.supported_features | SUPPORT_OPERATION_MODE
)
self._attr_target_temperature = target_temperature
self._attr_temperature_unit = unit_of_measurement
self._attr_is_away_mode_on = away
self._attr_current_operation = current_operation
self._attr_operation_list = [
"eco",
"electric",
"performance",
@ -56,62 +62,22 @@ class DemoWaterHeater(WaterHeaterEntity):
"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):
"""Set new target temperatures."""
self._target_temperature = kwargs.get(ATTR_TEMPERATURE)
self._attr_target_temperature = kwargs.get(ATTR_TEMPERATURE)
self.schedule_update_ha_state()
def set_operation_mode(self, operation_mode):
"""Set new operation mode."""
self._current_operation = operation_mode
self._attr_current_operation = operation_mode
self.schedule_update_ha_state()
def turn_away_mode_on(self):
"""Turn away mode on."""
self._away = True
self._attr_is_away_mode_on = True
self.schedule_update_ha_state()
def turn_away_mode_off(self):
"""Turn away mode off."""
self._away = False
self._attr_is_away_mode_on = False
self.schedule_update_ha_state()

View File

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