Define climate entity attributes as class variables (#51006)

This commit is contained in:
Franck Nijhof 2021-05-27 17:39:43 +02:00 committed by GitHub
parent d1c4d0de49
commit 7dff4d6ad7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 83 additions and 69 deletions

View File

@ -1,7 +1,6 @@
"""Provides functionality to interact with climate devices.""" """Provides functionality to interact with climate devices."""
from __future__ import annotations from __future__ import annotations
from abc import abstractmethod
from datetime import timedelta from datetime import timedelta
import functools as ft import functools as ft
import logging import logging
@ -171,6 +170,31 @@ async def async_unload_entry(hass: HomeAssistant, entry):
class ClimateEntity(Entity): class ClimateEntity(Entity):
"""Base class for climate entities.""" """Base class for climate entities."""
_attr_current_humidity: int | None = None
_attr_current_temperature: float | None = None
_attr_fan_mode: str | None
_attr_fan_modes: list[str] | None
_attr_hvac_action: str | None = None
_attr_hvac_mode: str
_attr_hvac_modes: list[str]
_attr_is_aux_heat: bool | None
_attr_max_humidity: int = DEFAULT_MAX_HUMIDITY
_attr_max_temp: float
_attr_min_humidity: int = DEFAULT_MIN_HUMIDITY
_attr_min_temp: float
_attr_precision: float
_attr_preset_mode: str | None
_attr_preset_modes: list[str] | None
_attr_supported_features: int
_attr_swing_mode: str | None
_attr_swing_modes: list[str] | None
_attr_target_humidity: int | None = None
_attr_target_temperature_high: float | None
_attr_target_temperature_low: float | None
_attr_target_temperature_step: float | None = None
_attr_target_temperature: float | None = None
_attr_temperature_unit: str
@property @property
def state(self) -> str: def state(self) -> str:
"""Return the current state.""" """Return the current state."""
@ -179,6 +203,8 @@ class ClimateEntity(Entity):
@property @property
def precision(self) -> float: 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
@ -277,33 +303,33 @@ class ClimateEntity(Entity):
@property @property
def temperature_unit(self) -> str: 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_humidity(self) -> int | None: def current_humidity(self) -> int | None:
"""Return the current humidity.""" """Return the current humidity."""
return None return self._attr_current_humidity
@property @property
def target_humidity(self) -> int | None: def target_humidity(self) -> int | None:
"""Return the humidity we try to reach.""" """Return the humidity we try to reach."""
return None return self._attr_target_humidity
@property @property
@abstractmethod
def hvac_mode(self) -> str: def hvac_mode(self) -> str:
"""Return hvac operation ie. heat, cool mode. """Return hvac operation ie. heat, cool mode.
Need to be one of HVAC_MODE_*. Need to be one of HVAC_MODE_*.
""" """
return self._attr_hvac_mode
@property @property
@abstractmethod
def hvac_modes(self) -> list[str]: def hvac_modes(self) -> list[str]:
"""Return the list of available hvac operation modes. """Return the list of available hvac operation modes.
Need to be a subset of HVAC_MODES. Need to be a subset of HVAC_MODES.
""" """
return self._attr_hvac_modes
@property @property
def hvac_action(self) -> str | None: def hvac_action(self) -> str | None:
@ -311,22 +337,22 @@ class ClimateEntity(Entity):
Need to be one of CURRENT_HVAC_*. Need to be one of CURRENT_HVAC_*.
""" """
return None return self._attr_hvac_action
@property @property
def current_temperature(self) -> float | None: 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) -> float | None: 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_step(self) -> float | None: def target_temperature_step(self) -> float | None:
"""Return the supported step of target temperature.""" """Return the supported step of target temperature."""
return None return self._attr_target_temperature_step
@property @property
def target_temperature_high(self) -> float | None: def target_temperature_high(self) -> float | None:
@ -334,7 +360,7 @@ class ClimateEntity(Entity):
Requires SUPPORT_TARGET_TEMPERATURE_RANGE. Requires SUPPORT_TARGET_TEMPERATURE_RANGE.
""" """
raise NotImplementedError return self._attr_target_temperature_high
@property @property
def target_temperature_low(self) -> float | None: def target_temperature_low(self) -> float | None:
@ -342,7 +368,7 @@ class ClimateEntity(Entity):
Requires SUPPORT_TARGET_TEMPERATURE_RANGE. Requires SUPPORT_TARGET_TEMPERATURE_RANGE.
""" """
raise NotImplementedError return self._attr_target_temperature_low
@property @property
def preset_mode(self) -> str | None: def preset_mode(self) -> str | None:
@ -350,7 +376,7 @@ class ClimateEntity(Entity):
Requires SUPPORT_PRESET_MODE. Requires SUPPORT_PRESET_MODE.
""" """
raise NotImplementedError return self._attr_preset_mode
@property @property
def preset_modes(self) -> list[str] | None: def preset_modes(self) -> list[str] | None:
@ -358,7 +384,7 @@ class ClimateEntity(Entity):
Requires SUPPORT_PRESET_MODE. Requires SUPPORT_PRESET_MODE.
""" """
raise NotImplementedError return self._attr_preset_modes
@property @property
def is_aux_heat(self) -> bool | None: def is_aux_heat(self) -> bool | None:
@ -366,7 +392,7 @@ class ClimateEntity(Entity):
Requires SUPPORT_AUX_HEAT. Requires SUPPORT_AUX_HEAT.
""" """
raise NotImplementedError return self._attr_is_aux_heat
@property @property
def fan_mode(self) -> str | None: def fan_mode(self) -> str | None:
@ -374,7 +400,7 @@ class ClimateEntity(Entity):
Requires SUPPORT_FAN_MODE. Requires SUPPORT_FAN_MODE.
""" """
raise NotImplementedError return self._attr_fan_mode
@property @property
def fan_modes(self) -> list[str] | None: def fan_modes(self) -> list[str] | None:
@ -382,7 +408,7 @@ class ClimateEntity(Entity):
Requires SUPPORT_FAN_MODE. Requires SUPPORT_FAN_MODE.
""" """
raise NotImplementedError return self._attr_fan_modes
@property @property
def swing_mode(self) -> str | None: def swing_mode(self) -> str | None:
@ -390,7 +416,7 @@ class ClimateEntity(Entity):
Requires SUPPORT_SWING_MODE. Requires SUPPORT_SWING_MODE.
""" """
raise NotImplementedError return self._attr_swing_mode
@property @property
def swing_modes(self) -> list[str] | None: def swing_modes(self) -> list[str] | None:
@ -398,7 +424,7 @@ class ClimateEntity(Entity):
Requires SUPPORT_SWING_MODE. Requires SUPPORT_SWING_MODE.
""" """
raise NotImplementedError return self._attr_swing_modes
def set_temperature(self, **kwargs) -> None: def set_temperature(self, **kwargs) -> None:
"""Set new target temperature.""" """Set new target temperature."""
@ -494,31 +520,35 @@ class ClimateEntity(Entity):
@property @property
def supported_features(self) -> int: def supported_features(self) -> int:
"""Return the list of supported features.""" """Return the list of supported features."""
raise NotImplementedError() return self._attr_supported_features
@property @property
def min_temp(self) -> float: def min_temp(self) -> float:
"""Return the minimum temperature.""" """Return the minimum temperature."""
return convert_temperature( if not hasattr(self, "_attr_min_temp"):
DEFAULT_MIN_TEMP, TEMP_CELSIUS, self.temperature_unit return convert_temperature(
) DEFAULT_MIN_TEMP, TEMP_CELSIUS, self.temperature_unit
)
return self._attr_min_temp
@property @property
def max_temp(self) -> float: def max_temp(self) -> float:
"""Return the maximum temperature.""" """Return the maximum temperature."""
return convert_temperature( if not hasattr(self, "_attr_max_temp"):
DEFAULT_MAX_TEMP, TEMP_CELSIUS, self.temperature_unit return convert_temperature(
) DEFAULT_MAX_TEMP, TEMP_CELSIUS, self.temperature_unit
)
return self._attr_max_temp
@property @property
def min_humidity(self) -> int: def min_humidity(self) -> int:
"""Return the minimum humidity.""" """Return the minimum humidity."""
return DEFAULT_MIN_HUMIDITY return self._attr_min_humidity
@property @property
def max_humidity(self) -> int: def max_humidity(self) -> int:
"""Return the maximum humidity.""" """Return the maximum humidity."""
return DEFAULT_MAX_HUMIDITY return self._attr_max_humidity
async def async_service_aux_heat( async def async_service_aux_heat(

View File

@ -26,6 +26,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS from homeassistant.const import ATTR_TEMPERATURE, TEMP_CELSIUS
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from . import ToonDataUpdateCoordinator
from .const import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP, DOMAIN from .const import DEFAULT_MAX_TEMP, DEFAULT_MIN_TEMP, DOMAIN
from .helpers import toon_exception_handler from .helpers import toon_exception_handler
from .models import ToonDisplayDeviceEntity from .models import ToonDisplayDeviceEntity
@ -44,28 +45,31 @@ async def async_setup_entry(
class ToonThermostatDevice(ToonDisplayDeviceEntity, ClimateEntity): class ToonThermostatDevice(ToonDisplayDeviceEntity, ClimateEntity):
"""Representation of a Toon climate device.""" """Representation of a Toon climate device."""
@property _attr_hvac_mode = HVAC_MODE_HEAT
def unique_id(self) -> str: _attr_max_temp = DEFAULT_MAX_TEMP
"""Return the unique ID for this thermostat.""" _attr_min_temp = DEFAULT_MIN_TEMP
agreement_id = self.coordinator.data.agreement.agreement_id _attr_supported_features = SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE
# This unique ID is a bit ugly and contains unneeded information. _attr_temperature_unit = TEMP_CELSIUS
# It is here for lecagy / backward compatible reasons.
return f"{DOMAIN}_{agreement_id}_climate"
@property def __init__(
def supported_features(self) -> int: self,
"""Return the list of supported features.""" coordinator: ToonDataUpdateCoordinator,
return SUPPORT_TARGET_TEMPERATURE | SUPPORT_PRESET_MODE *,
name: str,
@property icon: str,
def hvac_mode(self) -> str: ) -> None:
"""Return hvac operation ie. heat, cool mode.""" """Initialize Toon climate entity."""
return HVAC_MODE_HEAT super().__init__(coordinator, name=name, icon=icon)
self._attr_hvac_modes = [HVAC_MODE_HEAT]
@property self._attr_preset_modes = [
def hvac_modes(self) -> list[str]: PRESET_AWAY,
"""Return the list of available hvac operation modes.""" PRESET_COMFORT,
return [HVAC_MODE_HEAT] PRESET_HOME,
PRESET_SLEEP,
]
self._attr_unique_id = (
f"{DOMAIN}_{coordinator.data.agreement.agreement_id}_climate"
)
@property @property
def hvac_action(self) -> str | None: def hvac_action(self) -> str | None:
@ -74,11 +78,6 @@ class ToonThermostatDevice(ToonDisplayDeviceEntity, ClimateEntity):
return CURRENT_HVAC_HEAT return CURRENT_HVAC_HEAT
return CURRENT_HVAC_IDLE return CURRENT_HVAC_IDLE
@property
def temperature_unit(self) -> str:
"""Return the unit of measurement."""
return TEMP_CELSIUS
@property @property
def preset_mode(self) -> str | None: def preset_mode(self) -> str | None:
"""Return the current preset mode, e.g., home, away, temp.""" """Return the current preset mode, e.g., home, away, temp."""
@ -90,11 +89,6 @@ class ToonThermostatDevice(ToonDisplayDeviceEntity, ClimateEntity):
} }
return mapping.get(self.coordinator.data.thermostat.active_state) return mapping.get(self.coordinator.data.thermostat.active_state)
@property
def preset_modes(self) -> list[str]:
"""Return a list of available preset modes."""
return [PRESET_AWAY, PRESET_COMFORT, PRESET_HOME, PRESET_SLEEP]
@property @property
def current_temperature(self) -> float | None: def current_temperature(self) -> float | None:
"""Return the current temperature.""" """Return the current temperature."""
@ -105,16 +99,6 @@ class ToonThermostatDevice(ToonDisplayDeviceEntity, ClimateEntity):
"""Return the temperature we try to reach.""" """Return the temperature we try to reach."""
return self.coordinator.data.thermostat.current_setpoint return self.coordinator.data.thermostat.current_setpoint
@property
def min_temp(self) -> float:
"""Return the minimum temperature."""
return DEFAULT_MIN_TEMP
@property
def max_temp(self) -> float:
"""Return the maximum temperature."""
return DEFAULT_MAX_TEMP
@property @property
def extra_state_attributes(self) -> dict[str, Any]: def extra_state_attributes(self) -> dict[str, Any]:
"""Return the current state of the burner.""" """Return the current state of the burner."""