1 """Support for Decora dimmers."""
3 from __future__
import annotations
5 from collections.abc
import Callable
7 from functools
import wraps
10 from typing
import TYPE_CHECKING, Any, Concatenate
12 from bluepy.btle
import BTLEException
14 import voluptuous
as vol
16 from homeassistant
import util
19 PLATFORM_SCHEMA
as LIGHT_PLATFORM_SCHEMA,
32 _LOGGER = logging.getLogger(__name__)
36 """Validate the name."""
37 config = copy.deepcopy(config)
38 for address, device_config
in config[CONF_DEVICES].items():
39 if CONF_NAME
not in device_config:
40 device_config[CONF_NAME] = util.slugify(address)
45 DEVICE_SCHEMA = vol.Schema(
46 {vol.Optional(CONF_NAME): cv.string, vol.Required(CONF_API_KEY): cv.string}
49 PLATFORM_SCHEMA = vol.Schema(
51 LIGHT_PLATFORM_SCHEMA.extend(
52 {vol.Optional(CONF_DEVICES, default={}): {cv.string: DEVICE_SCHEMA}}
59 def retry[_DecoraLightT: DecoraLight, **_P, _R](
60 method: Callable[Concatenate[_DecoraLightT, _P], _R],
61 ) -> Callable[Concatenate[_DecoraLightT, _P], _R |
None]:
62 """Retry bluetooth commands."""
66 device: _DecoraLightT, *args: _P.args, **kwargs: _P.kwargs
68 """Try send command and retry on error."""
70 initial = time.monotonic()
72 if time.monotonic() - initial >= 10:
75 return method(device, *args, **kwargs)
76 except (decora.decoraException, AttributeError, BTLEException):
78 "Decora connect error for device %s. Reconnecting",
81 device._switch.connect()
89 add_entities: AddEntitiesCallback,
90 discovery_info: DiscoveryInfoType |
None =
None,
92 """Set up an Decora switch."""
94 for address, device_config
in config[CONF_DEVICES].items():
96 device[
"name"] = device_config[CONF_NAME]
97 device[
"key"] = device_config[CONF_API_KEY]
98 device[
"address"] = address
106 """Representation of an Decora light."""
108 _attr_color_mode = ColorMode.BRIGHTNESS
109 _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
111 def __init__(self, device: dict[str, Any]) ->
None:
112 """Initialize the light."""
117 self.
_switch_switch = decora.decora(device[
"address"], self.
_key_key)
123 """Set the state of this lamp to the provided brightness."""
124 self.
_switch_switch.set_brightness(
int(brightness / 2.55))
129 """Turn the specified or all lights on."""
130 brightness = kwargs.get(ATTR_BRIGHTNESS)
134 if brightness
is not None:
139 """Turn the specified or all lights off."""
145 """Synchronise internal state with the actual light state."""
None set_state(self, int brightness)
None __init__(self, dict[str, Any] device)
None turn_on(self, **Any kwargs)
None turn_off(self, **Any kwargs)
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
def _name_validator(config)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)