mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Add max/min/step to NumberEntityDescription (#61100)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
e33384d8b9
commit
b6dc89b4b7
@ -29,9 +29,6 @@ class DeconzNumberEntityDescriptionBase:
|
|||||||
device_property: str
|
device_property: str
|
||||||
suffix: str
|
suffix: str
|
||||||
update_key: str
|
update_key: str
|
||||||
max_value: int
|
|
||||||
min_value: int
|
|
||||||
step: int
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@ -122,9 +119,6 @@ class DeconzNumber(DeconzDevice, NumberEntity):
|
|||||||
super().__init__(device, gateway)
|
super().__init__(device, gateway)
|
||||||
|
|
||||||
self._attr_name = f"{device.name} {description.suffix}"
|
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
|
@callback
|
||||||
def async_update_callback(self) -> None:
|
def async_update_callback(self) -> None:
|
||||||
|
@ -91,13 +91,17 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||||||
class NumberEntityDescription(EntityDescription):
|
class NumberEntityDescription(EntityDescription):
|
||||||
"""A class that describes number entities."""
|
"""A class that describes number entities."""
|
||||||
|
|
||||||
|
max_value: float | None = None
|
||||||
|
min_value: float | None = None
|
||||||
|
step: float | None = None
|
||||||
|
|
||||||
|
|
||||||
class NumberEntity(Entity):
|
class NumberEntity(Entity):
|
||||||
"""Representation of a Number entity."""
|
"""Representation of a Number entity."""
|
||||||
|
|
||||||
entity_description: NumberEntityDescription
|
entity_description: NumberEntityDescription
|
||||||
_attr_max_value: float = DEFAULT_MAX_VALUE
|
_attr_max_value: float
|
||||||
_attr_min_value: float = DEFAULT_MIN_VALUE
|
_attr_min_value: float
|
||||||
_attr_state: None = None
|
_attr_state: None = None
|
||||||
_attr_step: float
|
_attr_step: float
|
||||||
_attr_mode: NumberMode = NumberMode.AUTO
|
_attr_mode: NumberMode = NumberMode.AUTO
|
||||||
@ -116,18 +120,37 @@ class NumberEntity(Entity):
|
|||||||
@property
|
@property
|
||||||
def min_value(self) -> float:
|
def min_value(self) -> float:
|
||||||
"""Return the minimum value."""
|
"""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
|
@property
|
||||||
def max_value(self) -> float:
|
def max_value(self) -> float:
|
||||||
"""Return the maximum value."""
|
"""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
|
@property
|
||||||
def step(self) -> float:
|
def step(self) -> float:
|
||||||
"""Return the increment/decrement step."""
|
"""Return the increment/decrement step."""
|
||||||
if hasattr(self, "_attr_step"):
|
if hasattr(self, "_attr_step"):
|
||||||
return 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
|
step = DEFAULT_STEP
|
||||||
value_range = abs(self.max_value - self.min_value)
|
value_range = abs(self.max_value - self.min_value)
|
||||||
if value_range != 0:
|
if value_range != 0:
|
||||||
|
@ -149,6 +149,8 @@ class TemplateNumber(TemplateEntity, NumberEntity):
|
|||||||
self._attr_unique_id = unique_id
|
self._attr_unique_id = unique_id
|
||||||
self._attr_value = None
|
self._attr_value = None
|
||||||
self._attr_step = None
|
self._attr_step = None
|
||||||
|
self._attr_min_value = None
|
||||||
|
self._attr_max_value = None
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
|
@ -19,8 +19,6 @@ from .const import CONF_MAX_AVAILABLE_POWER_KEY, CONF_MAX_CHARGING_CURRENT_KEY,
|
|||||||
class WallboxNumberEntityDescription(NumberEntityDescription):
|
class WallboxNumberEntityDescription(NumberEntityDescription):
|
||||||
"""Describes Wallbox sensor entity."""
|
"""Describes Wallbox sensor entity."""
|
||||||
|
|
||||||
min_value: float = 0
|
|
||||||
|
|
||||||
|
|
||||||
NUMBER_TYPES: dict[str, WallboxNumberEntityDescription] = {
|
NUMBER_TYPES: dict[str, WallboxNumberEntityDescription] = {
|
||||||
CONF_MAX_CHARGING_CURRENT_KEY: WallboxNumberEntityDescription(
|
CONF_MAX_CHARGING_CURRENT_KEY: WallboxNumberEntityDescription(
|
||||||
@ -71,7 +69,6 @@ class WallboxNumber(CoordinatorEntity, NumberEntity):
|
|||||||
self.entity_description = description
|
self.entity_description = description
|
||||||
self._coordinator = coordinator
|
self._coordinator = coordinator
|
||||||
self._attr_name = f"{entry.title} {description.name}"
|
self._attr_name = f"{entry.title} {description.name}"
|
||||||
self._attr_min_value = description.min_value
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def max_value(self) -> float:
|
def max_value(self) -> float:
|
||||||
|
@ -41,11 +41,17 @@ NUMBERS = [
|
|||||||
name="Speed",
|
name="Speed",
|
||||||
icon="mdi:speedometer",
|
icon="mdi:speedometer",
|
||||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||||
|
step=1,
|
||||||
|
min_value=0,
|
||||||
|
max_value=255,
|
||||||
),
|
),
|
||||||
NumberEntityDescription(
|
NumberEntityDescription(
|
||||||
key=ATTR_INTENSITY,
|
key=ATTR_INTENSITY,
|
||||||
name="Intensity",
|
name="Intensity",
|
||||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||||
|
step=1,
|
||||||
|
min_value=0,
|
||||||
|
max_value=255,
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -53,10 +59,6 @@ NUMBERS = [
|
|||||||
class WLEDNumber(WLEDEntity, NumberEntity):
|
class WLEDNumber(WLEDEntity, NumberEntity):
|
||||||
"""Defines a WLED speed number."""
|
"""Defines a WLED speed number."""
|
||||||
|
|
||||||
_attr_step = 1
|
|
||||||
_attr_min_value = 0
|
|
||||||
_attr_max_value = 255
|
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: WLEDDataUpdateCoordinator,
|
coordinator: WLEDDataUpdateCoordinator,
|
||||||
|
@ -82,9 +82,6 @@ ATTR_VOLUME = "volume"
|
|||||||
class XiaomiMiioNumberDescription(NumberEntityDescription):
|
class XiaomiMiioNumberDescription(NumberEntityDescription):
|
||||||
"""A class that describes number entities."""
|
"""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
|
available_with_device_off: bool = True
|
||||||
method: str | None = None
|
method: str | None = None
|
||||||
|
|
||||||
@ -278,9 +275,6 @@ class XiaomiNumberEntity(XiaomiCoordinatedMiioEntity, NumberEntity):
|
|||||||
"""Initialize the generic Xiaomi attribute selector."""
|
"""Initialize the generic Xiaomi attribute selector."""
|
||||||
super().__init__(name, device, entry, unique_id, coordinator)
|
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(
|
self._attr_value = self._extract_value_from_attribute(
|
||||||
coordinator.data, description.key
|
coordinator.data, description.key
|
||||||
)
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user