Use value_fn in WLED number (#79865)

This commit is contained in:
Marc Mueller 2022-10-08 12:34:41 +02:00 committed by GitHub
parent e7b550685e
commit c6df823b35
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,12 @@
"""Support for LED numbers.""" """Support for LED numbers."""
from __future__ import annotations from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
from functools import partial from functools import partial
from wled import Segment
from homeassistant.components.number import NumberEntity, NumberEntityDescription from homeassistant.components.number import NumberEntity, NumberEntityDescription
from homeassistant.config_entries import ConfigEntry from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
@ -35,8 +39,20 @@ async def async_setup_entry(
update_segments() update_segments()
@dataclass
class WLEDNumberDescriptionMixin:
"""Mixin for WLED number."""
value_fn: Callable[[Segment], float | None]
@dataclass
class WLEDNumberEntityDescription(NumberEntityDescription, WLEDNumberDescriptionMixin):
"""Class describing WLED number entities."""
NUMBERS = [ NUMBERS = [
NumberEntityDescription( WLEDNumberEntityDescription(
key=ATTR_SPEED, key=ATTR_SPEED,
name="Speed", name="Speed",
icon="mdi:speedometer", icon="mdi:speedometer",
@ -44,14 +60,16 @@ NUMBERS = [
native_step=1, native_step=1,
native_min_value=0, native_min_value=0,
native_max_value=255, native_max_value=255,
value_fn=lambda segment: segment.speed,
), ),
NumberEntityDescription( WLEDNumberEntityDescription(
key=ATTR_INTENSITY, key=ATTR_INTENSITY,
name="Intensity", name="Intensity",
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
native_step=1, native_step=1,
native_min_value=0, native_min_value=0,
native_max_value=255, native_max_value=255,
value_fn=lambda segment: segment.intensity,
), ),
] ]
@ -59,11 +77,13 @@ NUMBERS = [
class WLEDNumber(WLEDEntity, NumberEntity): class WLEDNumber(WLEDEntity, NumberEntity):
"""Defines a WLED speed number.""" """Defines a WLED speed number."""
entity_description: WLEDNumberEntityDescription
def __init__( def __init__(
self, self,
coordinator: WLEDDataUpdateCoordinator, coordinator: WLEDDataUpdateCoordinator,
segment: int, segment: int,
description: NumberEntityDescription, description: WLEDNumberEntityDescription,
) -> None: ) -> None:
"""Initialize WLED .""" """Initialize WLED ."""
super().__init__(coordinator=coordinator) super().__init__(coordinator=coordinator)
@ -92,9 +112,8 @@ class WLEDNumber(WLEDEntity, NumberEntity):
@property @property
def native_value(self) -> float | None: def native_value(self) -> float | None:
"""Return the current WLED segment number value.""" """Return the current WLED segment number value."""
return getattr( # type: ignore[no-any-return] return self.entity_description.value_fn(
self.coordinator.data.state.segments[self._segment], self.coordinator.data.state.segments[self._segment]
self.entity_description.key,
) )
@wled_exception_handler @wled_exception_handler