From b6dc89b4b79d0ba7e7b267af54a09f5f01ffca02 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 6 Dec 2021 18:56:46 +0100 Subject: [PATCH] Add max/min/step to NumberEntityDescription (#61100) Co-authored-by: epenet --- homeassistant/components/deconz/number.py | 6 ---- homeassistant/components/number/__init__.py | 31 ++++++++++++++++--- homeassistant/components/template/number.py | 2 ++ homeassistant/components/wallbox/number.py | 3 -- homeassistant/components/wled/number.py | 10 +++--- .../components/xiaomi_miio/number.py | 6 ---- 6 files changed, 35 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/deconz/number.py b/homeassistant/components/deconz/number.py index 6c3ca08710c..300a10f9a27 100644 --- a/homeassistant/components/deconz/number.py +++ b/homeassistant/components/deconz/number.py @@ -29,9 +29,6 @@ class DeconzNumberEntityDescriptionBase: device_property: str suffix: str update_key: str - max_value: int - min_value: int - step: int @dataclass @@ -122,9 +119,6 @@ class DeconzNumber(DeconzDevice, NumberEntity): super().__init__(device, gateway) self._attr_name = f"{device.name} {description.suffix}" - self._attr_max_value = description.max_value - self._attr_min_value = description.min_value - self._attr_step = description.step @callback def async_update_callback(self) -> None: diff --git a/homeassistant/components/number/__init__.py b/homeassistant/components/number/__init__.py index af88b5c86b5..47a80f00561 100644 --- a/homeassistant/components/number/__init__.py +++ b/homeassistant/components/number/__init__.py @@ -91,13 +91,17 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: class NumberEntityDescription(EntityDescription): """A class that describes number entities.""" + max_value: float | None = None + min_value: float | None = None + step: float | None = None + class NumberEntity(Entity): """Representation of a Number entity.""" entity_description: NumberEntityDescription - _attr_max_value: float = DEFAULT_MAX_VALUE - _attr_min_value: float = DEFAULT_MIN_VALUE + _attr_max_value: float + _attr_min_value: float _attr_state: None = None _attr_step: float _attr_mode: NumberMode = NumberMode.AUTO @@ -116,18 +120,37 @@ class NumberEntity(Entity): @property def min_value(self) -> float: """Return the minimum value.""" - return self._attr_min_value + if hasattr(self, "_attr_min_value"): + return self._attr_min_value + if ( + hasattr(self, "entity_description") + and self.entity_description.min_value is not None + ): + return self.entity_description.min_value + return DEFAULT_MIN_VALUE @property def max_value(self) -> float: """Return the maximum value.""" - return self._attr_max_value + if hasattr(self, "_attr_max_value"): + return self._attr_max_value + if ( + hasattr(self, "entity_description") + and self.entity_description.max_value is not None + ): + return self.entity_description.max_value + return DEFAULT_MAX_VALUE @property def step(self) -> float: """Return the increment/decrement step.""" if hasattr(self, "_attr_step"): return self._attr_step + if ( + hasattr(self, "entity_description") + and self.entity_description.step is not None + ): + return self.entity_description.step step = DEFAULT_STEP value_range = abs(self.max_value - self.min_value) if value_range != 0: diff --git a/homeassistant/components/template/number.py b/homeassistant/components/template/number.py index 0f5e8e4bee8..90a944a2d33 100644 --- a/homeassistant/components/template/number.py +++ b/homeassistant/components/template/number.py @@ -149,6 +149,8 @@ class TemplateNumber(TemplateEntity, NumberEntity): self._attr_unique_id = unique_id self._attr_value = None self._attr_step = None + self._attr_min_value = None + self._attr_max_value = None async def async_added_to_hass(self) -> None: """Register callbacks.""" diff --git a/homeassistant/components/wallbox/number.py b/homeassistant/components/wallbox/number.py index 64c1f1e1abb..d8a677c147f 100644 --- a/homeassistant/components/wallbox/number.py +++ b/homeassistant/components/wallbox/number.py @@ -19,8 +19,6 @@ from .const import CONF_MAX_AVAILABLE_POWER_KEY, CONF_MAX_CHARGING_CURRENT_KEY, class WallboxNumberEntityDescription(NumberEntityDescription): """Describes Wallbox sensor entity.""" - min_value: float = 0 - NUMBER_TYPES: dict[str, WallboxNumberEntityDescription] = { CONF_MAX_CHARGING_CURRENT_KEY: WallboxNumberEntityDescription( @@ -71,7 +69,6 @@ class WallboxNumber(CoordinatorEntity, NumberEntity): self.entity_description = description self._coordinator = coordinator self._attr_name = f"{entry.title} {description.name}" - self._attr_min_value = description.min_value @property def max_value(self) -> float: diff --git a/homeassistant/components/wled/number.py b/homeassistant/components/wled/number.py index 5167ee8a37a..48c92cf839c 100644 --- a/homeassistant/components/wled/number.py +++ b/homeassistant/components/wled/number.py @@ -41,11 +41,17 @@ NUMBERS = [ name="Speed", icon="mdi:speedometer", entity_category=ENTITY_CATEGORY_CONFIG, + step=1, + min_value=0, + max_value=255, ), NumberEntityDescription( key=ATTR_INTENSITY, name="Intensity", entity_category=ENTITY_CATEGORY_CONFIG, + step=1, + min_value=0, + max_value=255, ), ] @@ -53,10 +59,6 @@ NUMBERS = [ class WLEDNumber(WLEDEntity, NumberEntity): """Defines a WLED speed number.""" - _attr_step = 1 - _attr_min_value = 0 - _attr_max_value = 255 - def __init__( self, coordinator: WLEDDataUpdateCoordinator, diff --git a/homeassistant/components/xiaomi_miio/number.py b/homeassistant/components/xiaomi_miio/number.py index 2823c6c2582..f8516c66e84 100644 --- a/homeassistant/components/xiaomi_miio/number.py +++ b/homeassistant/components/xiaomi_miio/number.py @@ -82,9 +82,6 @@ ATTR_VOLUME = "volume" class XiaomiMiioNumberDescription(NumberEntityDescription): """A class that describes number entities.""" - min_value: float | None = None - max_value: float | None = None - step: float | None = None available_with_device_off: bool = True method: str | None = None @@ -278,9 +275,6 @@ class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity): """Initialize the generic Xiaomi attribute selector.""" super().__init__(name, device, entry, unique_id, coordinator) - self._attr_min_value = description.min_value - self._attr_max_value = description.max_value - self._attr_step = description.step self._attr_value = self._extract_value_from_attribute( coordinator.data, description.key )