Add unique_id to the light switch (#40603)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/40789/head
Thibaut 2020-09-30 13:05:18 +02:00 committed by GitHub
parent 205cf57a77
commit 729ed1cab6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 2 deletions

View File

@ -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}