From 729ed1cab686252f78de0b008d748e10741d4cc3 Mon Sep 17 00:00:00 2001 From: Thibaut Date: Wed, 30 Sep 2020 13:05:18 +0200 Subject: [PATCH] Add unique_id to the light switch (#40603) Co-authored-by: Martin Hjelmare --- homeassistant/components/switch/light.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/switch/light.py b/homeassistant/components/switch/light.py index 8b751871014..c9dd3553928 100644 --- a/homeassistant/components/switch/light.py +++ b/homeassistant/components/switch/light.py @@ -44,18 +44,31 @@ async def async_setup_platform( discovery_info: Optional[DiscoveryInfoType] = None, ) -> None: """Initialize Light Switch platform.""" + + registry = await hass.helpers.entity_registry.async_get_registry() + wrapped_switch = registry.async_get(config[CONF_ENTITY_ID]) + unique_id = wrapped_switch.unique_id if wrapped_switch else None + async_add_entities( - [LightSwitch(cast(str, config.get(CONF_NAME)), config[CONF_ENTITY_ID])], True + [ + LightSwitch( + cast(str, config.get(CONF_NAME)), + config[CONF_ENTITY_ID], + unique_id, + ) + ], + True, ) class LightSwitch(LightEntity): """Represents a Switch as a Light.""" - def __init__(self, name: str, switch_entity_id: str) -> None: + def __init__(self, name: str, switch_entity_id: str, unique_id: str) -> None: """Initialize Light Switch.""" self._name = name self._switch_entity_id = switch_entity_id + self._unique_id = unique_id self._is_on = False self._available = False self._async_unsub_state_changed: Optional[CALLBACK_TYPE] = None @@ -80,6 +93,11 @@ class LightSwitch(LightEntity): """No polling needed for a light switch.""" return False + @property + def unique_id(self): + """Return the unique id of the light switch.""" + return self._unique_id + async def async_turn_on(self, **kwargs): """Forward the turn_on command to the switch in this light switch.""" data = {ATTR_ENTITY_ID: self._switch_entity_id}