Plugwise prepare native_value_fn and companions for number (#93416)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
Co-authored-by: Bouwe <bouwe.s.westerdijk@gmail.com>
Co-authored-by: Bouwe Westerdijk <11290930+bouwew@users.noreply.github.com>
This commit is contained in:
Tom 2023-07-18 20:48:15 +02:00 committed by GitHub
parent 344f349371
commit 499c7491af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from collections.abc import Awaitable, Callable from collections.abc import Awaitable, Callable
from dataclasses import dataclass from dataclasses import dataclass
from plugwise import Smile from plugwise import ActuatorData, Smile
from homeassistant.components.number import ( from homeassistant.components.number import (
NumberDeviceClass, NumberDeviceClass,
@ -24,13 +24,13 @@ from .entity import PlugwiseEntity
@dataclass @dataclass
class PlugwiseEntityDescriptionMixin: class PlugwiseEntityDescriptionMixin:
"""Mixin values for Plugwse entities.""" """Mixin values for Plugwise entities."""
command: Callable[[Smile, str, float], Awaitable[None]] command: Callable[[Smile, str, float], Awaitable[None]]
native_max_value_key: str native_max_value_fn: Callable[[ActuatorData], float]
native_min_value_key: str native_min_value_fn: Callable[[ActuatorData], float]
native_step_key: str native_step_fn: Callable[[ActuatorData], float]
native_value_key: str native_value_fn: Callable[[ActuatorData], float]
@dataclass @dataclass
@ -47,11 +47,11 @@ NUMBER_TYPES = (
command=lambda api, number, value: api.set_number_setpoint(number, value), command=lambda api, number, value: api.set_number_setpoint(number, value),
device_class=NumberDeviceClass.TEMPERATURE, device_class=NumberDeviceClass.TEMPERATURE,
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
native_max_value_key="upper_bound",
native_min_value_key="lower_bound",
native_step_key="resolution",
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
native_value_key="setpoint", native_max_value_fn=lambda data: data["upper_bound"],
native_min_value_fn=lambda data: data["lower_bound"],
native_step_fn=lambda data: data["resolution"],
native_value_fn=lambda data: data["setpoint"],
), ),
) )
@ -70,7 +70,7 @@ async def async_setup_entry(
entities: list[PlugwiseNumberEntity] = [] entities: list[PlugwiseNumberEntity] = []
for device_id, device in coordinator.data.devices.items(): for device_id, device in coordinator.data.devices.items():
for description in NUMBER_TYPES: for description in NUMBER_TYPES:
if description.key in device and "setpoint" in device[description.key]: if (actuator := device.get(description.key)) and "setpoint" in actuator:
entities.append( entities.append(
PlugwiseNumberEntity(coordinator, device_id, description) PlugwiseNumberEntity(coordinator, device_id, description)
) )
@ -91,40 +91,30 @@ class PlugwiseNumberEntity(PlugwiseEntity, NumberEntity):
) -> None: ) -> None:
"""Initiate Plugwise Number.""" """Initiate Plugwise Number."""
super().__init__(coordinator, device_id) super().__init__(coordinator, device_id)
self.actuator = self.device[description.key]
self.entity_description = description self.entity_description = description
self._attr_unique_id = f"{device_id}-{description.key}" self._attr_unique_id = f"{device_id}-{description.key}"
self._attr_mode = NumberMode.BOX self._attr_mode = NumberMode.BOX
@property @property
def native_step(self) -> float: def native_max_value(self) -> float:
"""Return the setpoint step value.""" """Return the setpoint max. value."""
return max( return self.entity_description.native_max_value_fn(self.actuator)
self.device[self.entity_description.key][
self.entity_description.native_step_key
],
1,
)
@property
def native_value(self) -> float:
"""Return the present setpoint value."""
return self.device[self.entity_description.key][
self.entity_description.native_value_key
]
@property @property
def native_min_value(self) -> float: def native_min_value(self) -> float:
"""Return the setpoint min. value.""" """Return the setpoint min. value."""
return self.device[self.entity_description.key][ return self.entity_description.native_min_value_fn(self.actuator)
self.entity_description.native_min_value_key
]
@property @property
def native_max_value(self) -> float: def native_step(self) -> float:
"""Return the setpoint max. value.""" """Return the setpoint step value."""
return self.device[self.entity_description.key][ return max(self.entity_description.native_step_fn(self.actuator), 1)
self.entity_description.native_max_value_key
] @property
def native_value(self) -> float:
"""Return the present setpoint value."""
return self.entity_description.native_value_fn(self.actuator)
async def async_set_native_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
"""Change to the new setpoint value.""" """Change to the new setpoint value."""