1 """Support for Rflink lights."""
3 from __future__
import annotations
9 import voluptuous
as vol
13 PLATFORM_SCHEMA
as LIGHT_PLATFORM_SCHEMA,
31 CONF_SIGNAL_REPETITIONS,
33 DEVICE_DEFAULTS_SCHEMA,
37 from .entity
import SwitchableRflinkDevice
38 from .utils
import brightness_to_rflink, rflink_to_brightness
40 _LOGGER = logging.getLogger(__name__)
44 TYPE_DIMMABLE =
"dimmable"
45 TYPE_SWITCHABLE =
"switchable"
46 TYPE_HYBRID =
"hybrid"
47 TYPE_TOGGLE =
"toggle"
49 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
53 ): DEVICE_DEFAULTS_SCHEMA,
54 vol.Optional(CONF_AUTOMATIC_ADD, default=
True): cv.boolean,
55 vol.Optional(CONF_DEVICES, default={}): {
56 cv.string: vol.Schema(
58 vol.Optional(CONF_NAME): cv.string,
59 vol.Optional(CONF_TYPE): vol.Any(
60 TYPE_DIMMABLE, TYPE_SWITCHABLE, TYPE_HYBRID, TYPE_TOGGLE
62 vol.Optional(CONF_ALIASES, default=[]): vol.All(
63 cv.ensure_list, [cv.string]
65 vol.Optional(CONF_GROUP_ALIASES, default=[]): vol.All(
66 cv.ensure_list, [cv.string]
68 vol.Optional(CONF_NOGROUP_ALIASES, default=[]): vol.All(
69 cv.ensure_list, [cv.string]
71 vol.Optional(CONF_FIRE_EVENT): cv.boolean,
72 vol.Optional(CONF_SIGNAL_REPETITIONS): vol.Coerce(int),
73 vol.Optional(CONF_GROUP, default=
True): cv.boolean,
78 extra=vol.ALLOW_EXTRA,
83 """Return entity class for protocol of a given device_id.
87 entity_type_mapping = {
90 "newkaku": TYPE_HYBRID
92 protocol = device_id.split(
"_")[0]
93 return entity_type_mapping.get(protocol)
97 """Translate entity type to entity class.
101 entity_device_mapping = {
103 TYPE_DIMMABLE: DimmableRflinkLight,
106 TYPE_SWITCHABLE: RflinkLight,
109 TYPE_HYBRID: HybridRflinkLight,
112 TYPE_TOGGLE: ToggleRflinkLight,
115 return entity_device_mapping.get(entity_type, RflinkLight)
119 """Parse configuration and add Rflink light devices."""
121 for device_id, config
in domain_config[CONF_DEVICES].items():
123 if CONF_TYPE
in config:
126 entity_type = config.pop(CONF_TYPE)
131 device_config =
dict(domain_config[CONF_DEVICE_DEFAULTS], **config)
133 is_hybrid = entity_class
is HybridRflinkLight
136 repetitions_enabled = device_config[CONF_SIGNAL_REPETITIONS] != 1
137 if is_hybrid
and repetitions_enabled:
140 "Hybrid type for %s not compatible with signal "
141 "repetitions. Please set 'dimmable' or 'switchable' "
142 "type explicitly in configuration"
147 device = entity_class(device_id, **device_config)
148 devices.append(device)
156 async_add_entities: AddEntitiesCallback,
157 discovery_info: DiscoveryInfoType |
None =
None,
159 """Set up the Rflink light platform."""
163 """Check if device is known, otherwise add to list of known devices."""
164 device_id = event[EVENT_KEY_ID]
169 device_config = config[CONF_DEVICE_DEFAULTS]
170 device = entity_class(device_id, initial_event=event, **device_config)
173 if config[CONF_AUTOMATIC_ADD]:
174 hass.data[DATA_DEVICE_REGISTER][EVENT_KEY_COMMAND] = add_new_device
178 """Representation of a Rflink light."""
180 _attr_color_mode = ColorMode.ONOFF
181 _attr_supported_color_modes = {ColorMode.ONOFF}
185 """Rflink light device that support dimming."""
187 _attr_color_mode = ColorMode.BRIGHTNESS
188 _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
192 """Restore RFLink light brightness attribute."""
197 old_state
is not None
198 and old_state.attributes.get(ATTR_BRIGHTNESS)
is not None
204 """Turn the device on."""
205 if ATTR_BRIGHTNESS
in kwargs:
215 """Adjust state if Rflink picks up a remote command for this device."""
218 command = event[
"command"]
219 if command
in [
"on",
"allon"]:
221 elif command
in [
"off",
"alloff"]:
224 elif re.search(
"^set_level=(0?[0-9]|1[0-5])$", command, re.IGNORECASE):
230 """Return the brightness of this light between 0..255."""
235 """Rflink light device that sends out both dim and on/off commands.
237 Used for protocols which support lights that are not exclusively on/off
238 style. For example KlikAanKlikUit supports both on/off and dimmable light
239 switches using the same protocol. This type allows unconfigured
240 KlikAanKlikUit devices to support dimming without breaking support for
243 This type is not compatible with signal repetitions as the 'dim' and 'on'
244 command are send sequential and multiple 'on' commands to a dimmable
245 device can cause the dimmer to switch into a pulsating brightness mode.
246 Which results in a nice house disco :)
250 """Turn the device on and set dim level."""
259 """Rflink light device which sends out only 'on' commands.
261 Some switches like for example Livolo light switches use the
262 same 'on' command to switch on and switch off the lights.
263 If the light is on and 'on' gets sent, the light will turn off
264 and if the light is off and 'on' gets sent, the light will turn on.
268 """Adjust state if Rflink picks up a remote command for this device."""
271 if event[
"command"] ==
"on":
277 """Turn the device on."""
281 """Turn the device off."""
int|None brightness(self)
def _async_handle_command(self, command, *args)
def cancel_queued_send_commands(self)
None async_added_to_hass(self)
def _handle_event(self, event)
None async_turn_on(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
def _handle_event(self, event)
None async_turn_on(self, **Any kwargs)
None async_turn_off(self, **Any kwargs)
State|None async_get_last_state(self)
def devices_from_config(domain_config)
None async_setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback async_add_entities, DiscoveryInfoType|None discovery_info=None)
def entity_class_for_type(entity_type)
def entity_type_for_device_id(device_id)
int brightness_to_rflink(int brightness)
int rflink_to_brightness(int dim_level)
def add_new_device(new_device)