Define HumidifierEntity entity attributes as class variables (#51841)

This commit is contained in:
Franck Nijhof 2021-06-14 18:04:46 +02:00 committed by GitHub
parent c8755cd896
commit 32409a2c93
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 64 deletions

View File

@ -1,4 +1,6 @@
"""Demo platform that offers a fake humidifier device.""" """Demo platform that offers a fake humidifier device."""
from __future__ import annotations
from homeassistant.components.humidifier import HumidifierEntity from homeassistant.components.humidifier import HumidifierEntity
from homeassistant.components.humidifier.const import ( from homeassistant.components.humidifier.const import (
DEVICE_CLASS_DEHUMIDIFIER, DEVICE_CLASS_DEHUMIDIFIER,
@ -43,82 +45,46 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class DemoHumidifier(HumidifierEntity): class DemoHumidifier(HumidifierEntity):
"""Representation of a demo humidifier device.""" """Representation of a demo humidifier device."""
_attr_should_poll = False
def __init__( def __init__(
self, self,
name, name: str,
mode, mode: str | None,
target_humidity, target_humidity: int,
available_modes=None, available_modes: list[str] | None = None,
is_on=True, is_on: bool = True,
device_class=None, device_class: str | None = None,
): ) -> None:
"""Initialize the humidifier device.""" """Initialize the humidifier device."""
self._name = name self._attr_name = name
self._state = is_on self._attr_is_on = is_on
self._support_flags = SUPPORT_FLAGS self._attr_supported_features = SUPPORT_FLAGS
if mode is not None: if mode is not None:
self._support_flags = self._support_flags | SUPPORT_MODES self._attr_supported_features = (
self._target_humidity = target_humidity self._attr_supported_features | SUPPORT_MODES
self._mode = mode )
self._available_modes = available_modes self._attr_target_humidity = target_humidity
self._device_class = device_class self._attr_mode = mode
self._attr_available_modes = available_modes
@property self._attr_device_class = device_class
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 humidity device."""
return self._name
@property
def target_humidity(self):
"""Return the humidity we try to reach."""
return self._target_humidity
@property
def mode(self):
"""Return current mode."""
return self._mode
@property
def available_modes(self):
"""Return available modes."""
return self._available_modes
@property
def is_on(self):
"""Return true if the humidifier is on."""
return self._state
@property
def device_class(self):
"""Return the device class of the humidifier."""
return self._device_class
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
"""Turn the device on.""" """Turn the device on."""
self._state = True self._attr_is_on = True
self.async_write_ha_state() self.async_write_ha_state()
async def async_turn_off(self, **kwargs): async def async_turn_off(self, **kwargs):
"""Turn the device off.""" """Turn the device off."""
self._state = False self._attr_is_on = False
self.async_write_ha_state() self.async_write_ha_state()
async def async_set_humidity(self, humidity): async def async_set_humidity(self, humidity):
"""Set new humidity level.""" """Set new humidity level."""
self._target_humidity = humidity self._attr_target_humidity = humidity
self.async_write_ha_state() self.async_write_ha_state()
async def async_set_mode(self, mode): async def async_set_mode(self, mode):
"""Update mode.""" """Update mode."""
self._mode = mode self._attr_mode = mode
self.async_write_ha_state() self.async_write_ha_state()

View File

@ -102,6 +102,12 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
class HumidifierEntity(ToggleEntity): class HumidifierEntity(ToggleEntity):
"""Base class for humidifier entities.""" """Base class for humidifier entities."""
_attr_available_modes: list[str] | None
_attr_max_humidity: int = DEFAULT_MAX_HUMIDITY
_attr_min_humidity: int = DEFAULT_MIN_HUMIDITY
_attr_mode: str | None
_attr_target_humidity: int | None = None
@property @property
def capability_attributes(self) -> dict[str, Any]: def capability_attributes(self) -> dict[str, Any]:
"""Return capability attributes.""" """Return capability attributes."""
@ -134,7 +140,7 @@ class HumidifierEntity(ToggleEntity):
@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
def mode(self) -> str | None: def mode(self) -> str | None:
@ -142,7 +148,7 @@ class HumidifierEntity(ToggleEntity):
Requires SUPPORT_MODES. Requires SUPPORT_MODES.
""" """
raise NotImplementedError return self._attr_mode
@property @property
def available_modes(self) -> list[str] | None: def available_modes(self) -> list[str] | None:
@ -150,7 +156,7 @@ class HumidifierEntity(ToggleEntity):
Requires SUPPORT_MODES. Requires SUPPORT_MODES.
""" """
raise NotImplementedError return self._attr_available_modes
def set_humidity(self, humidity: int) -> None: def set_humidity(self, humidity: int) -> None:
"""Set new target humidity.""" """Set new target humidity."""
@ -171,9 +177,9 @@ class HumidifierEntity(ToggleEntity):
@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