1 """Helpers for WLED."""
3 from __future__
import annotations
5 from collections.abc
import Callable, Coroutine
6 from typing
import Any, Concatenate
8 from wled
import WLEDConnectionError, WLEDError
12 from .entity
import WLEDEntity
15 def wled_exception_handler[_WLEDEntityT: WLEDEntity, **_P](
16 func: Callable[Concatenate[_WLEDEntityT, _P], Coroutine[Any, Any, Any]],
17 ) -> Callable[Concatenate[_WLEDEntityT, _P], Coroutine[Any, Any,
None]]:
18 """Decorate WLED calls to handle WLED exceptions.
20 A decorator that wraps the passed in function, catches WLED errors,
21 and handles the availability of the device in the data coordinator.
24 async
def handler(self: _WLEDEntityT, *args: _P.args, **kwargs: _P.kwargs) ->
None:
26 await func(self, *args, **kwargs)
27 self.coordinator.async_update_listeners()
29 except WLEDConnectionError
as error:
30 self.coordinator.last_update_success =
False
31 self.coordinator.async_update_listeners()
34 except WLEDError
as error:
41 """Map color temperature in K from minK-maxK to 0-255."""
42 return int((k - min_k) / (max_k - min_k) * 255)
46 """Map color temperature from 0-255 to minK-maxK K."""
47 return int(v / 255 * (max_k - min_k) + min_k)
int kelvin_to_255(int k, int min_k, int max_k)
int kelvin_to_255_reverse(int v, int min_k, int max_k)