Add max/min/step to NumberEntityDescription (#61100)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-12-06 18:56:46 +01:00 committed by GitHub
parent e33384d8b9
commit b6dc89b4b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 23 deletions

View File

@ -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:

View File

@ -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."""
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."""
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:

View File

@ -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."""

View File

@ -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:

View File

@ -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,

View File

@ -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
)