2021-10-26 07:04:19 +00:00
|
|
|
"""Support for LED numbers."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-10-08 10:34:41 +00:00
|
|
|
from collections.abc import Callable
|
|
|
|
from dataclasses import dataclass
|
2021-10-26 07:04:19 +00:00
|
|
|
from functools import partial
|
|
|
|
|
2022-10-08 10:34:41 +00:00
|
|
|
from wled import Segment
|
|
|
|
|
2021-10-26 07:04:19 +00:00
|
|
|
from homeassistant.components.number import NumberEntity, NumberEntityDescription
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2021-12-18 15:17:55 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-10-26 07:04:19 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .const import ATTR_INTENSITY, ATTR_SPEED, DOMAIN
|
|
|
|
from .coordinator import WLEDDataUpdateCoordinator
|
|
|
|
from .helpers import wled_exception_handler
|
|
|
|
from .models import WLEDEntity
|
|
|
|
|
|
|
|
PARALLEL_UPDATES = 1
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up WLED number based on a config entry."""
|
|
|
|
coordinator: WLEDDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
|
|
|
|
|
|
|
update_segments = partial(
|
|
|
|
async_update_segments,
|
|
|
|
coordinator,
|
|
|
|
set(),
|
|
|
|
async_add_entities,
|
|
|
|
)
|
|
|
|
coordinator.async_add_listener(update_segments)
|
|
|
|
update_segments()
|
|
|
|
|
|
|
|
|
2022-10-08 10:34:41 +00:00
|
|
|
@dataclass
|
|
|
|
class WLEDNumberDescriptionMixin:
|
|
|
|
"""Mixin for WLED number."""
|
|
|
|
|
|
|
|
value_fn: Callable[[Segment], float | None]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class WLEDNumberEntityDescription(NumberEntityDescription, WLEDNumberDescriptionMixin):
|
|
|
|
"""Class describing WLED number entities."""
|
|
|
|
|
|
|
|
|
2021-10-26 07:04:19 +00:00
|
|
|
NUMBERS = [
|
2022-10-08 10:34:41 +00:00
|
|
|
WLEDNumberEntityDescription(
|
2021-10-26 07:04:19 +00:00
|
|
|
key=ATTR_SPEED,
|
|
|
|
name="Speed",
|
|
|
|
icon="mdi:speedometer",
|
2021-12-18 15:17:55 +00:00
|
|
|
entity_category=EntityCategory.CONFIG,
|
2022-06-14 18:15:56 +00:00
|
|
|
native_step=1,
|
|
|
|
native_min_value=0,
|
|
|
|
native_max_value=255,
|
2022-10-08 10:34:41 +00:00
|
|
|
value_fn=lambda segment: segment.speed,
|
2021-10-26 07:04:19 +00:00
|
|
|
),
|
2022-10-08 10:34:41 +00:00
|
|
|
WLEDNumberEntityDescription(
|
2021-10-26 07:04:19 +00:00
|
|
|
key=ATTR_INTENSITY,
|
|
|
|
name="Intensity",
|
2021-12-18 15:17:55 +00:00
|
|
|
entity_category=EntityCategory.CONFIG,
|
2022-06-14 18:15:56 +00:00
|
|
|
native_step=1,
|
|
|
|
native_min_value=0,
|
|
|
|
native_max_value=255,
|
2022-10-08 10:34:41 +00:00
|
|
|
value_fn=lambda segment: segment.intensity,
|
2021-10-26 07:04:19 +00:00
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class WLEDNumber(WLEDEntity, NumberEntity):
|
|
|
|
"""Defines a WLED speed number."""
|
|
|
|
|
2022-10-08 10:34:41 +00:00
|
|
|
entity_description: WLEDNumberEntityDescription
|
|
|
|
|
2021-10-26 07:04:19 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: WLEDDataUpdateCoordinator,
|
|
|
|
segment: int,
|
2022-10-08 10:34:41 +00:00
|
|
|
description: WLEDNumberEntityDescription,
|
2021-10-26 07:04:19 +00:00
|
|
|
) -> None:
|
|
|
|
"""Initialize WLED ."""
|
|
|
|
super().__init__(coordinator=coordinator)
|
|
|
|
self.entity_description = description
|
|
|
|
|
|
|
|
# Segment 0 uses a simpler name, which is more natural for when using
|
|
|
|
# a single segment / using WLED with one big LED strip.
|
2022-07-10 21:56:48 +00:00
|
|
|
if segment != 0:
|
|
|
|
self._attr_name = f"Segment {segment} {description.name}"
|
2021-10-26 07:04:19 +00:00
|
|
|
|
|
|
|
self._attr_unique_id = (
|
|
|
|
f"{coordinator.data.info.mac_address}_{description.key}_{segment}"
|
|
|
|
)
|
|
|
|
self._segment = segment
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
try:
|
|
|
|
self.coordinator.data.state.segments[self._segment]
|
|
|
|
except IndexError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return super().available
|
|
|
|
|
|
|
|
@property
|
2022-06-14 18:15:56 +00:00
|
|
|
def native_value(self) -> float | None:
|
2021-10-26 07:04:19 +00:00
|
|
|
"""Return the current WLED segment number value."""
|
2022-10-08 10:34:41 +00:00
|
|
|
return self.entity_description.value_fn(
|
|
|
|
self.coordinator.data.state.segments[self._segment]
|
2021-10-26 07:04:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
@wled_exception_handler
|
2022-06-14 18:15:56 +00:00
|
|
|
async def async_set_native_value(self, value: float) -> None:
|
2021-10-26 07:04:19 +00:00
|
|
|
"""Set the WLED segment value."""
|
|
|
|
key = self.entity_description.key
|
|
|
|
if key == ATTR_SPEED:
|
|
|
|
await self.coordinator.wled.segment(
|
|
|
|
segment_id=self._segment, speed=int(value)
|
|
|
|
)
|
|
|
|
elif key == ATTR_INTENSITY:
|
2021-10-27 11:16:07 +00:00
|
|
|
await self.coordinator.wled.segment(
|
2021-10-26 07:04:19 +00:00
|
|
|
segment_id=self._segment, intensity=int(value)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_update_segments(
|
|
|
|
coordinator: WLEDDataUpdateCoordinator,
|
|
|
|
current_ids: set[int],
|
2022-08-26 08:25:33 +00:00
|
|
|
async_add_entities: AddEntitiesCallback,
|
2021-10-26 07:04:19 +00:00
|
|
|
) -> None:
|
|
|
|
"""Update segments."""
|
|
|
|
segment_ids = {segment.segment_id for segment in coordinator.data.state.segments}
|
|
|
|
|
2022-08-26 08:25:33 +00:00
|
|
|
new_entities: list[WLEDNumber] = []
|
2021-10-26 07:04:19 +00:00
|
|
|
|
|
|
|
# Process new segments, add them to Home Assistant
|
|
|
|
for segment_id in segment_ids - current_ids:
|
|
|
|
current_ids.add(segment_id)
|
|
|
|
for desc in NUMBERS:
|
|
|
|
new_entities.append(WLEDNumber(coordinator, segment_id, desc))
|
|
|
|
|
2022-10-17 19:11:58 +00:00
|
|
|
async_add_entities(new_entities)
|