mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Add unique_id to the light switch (#40603)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
205cf57a77
commit
729ed1cab6
@ -44,18 +44,31 @@ async def async_setup_platform(
|
|||||||
discovery_info: Optional[DiscoveryInfoType] = None,
|
discovery_info: Optional[DiscoveryInfoType] = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize Light Switch platform."""
|
"""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(
|
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):
|
class LightSwitch(LightEntity):
|
||||||
"""Represents a Switch as a Light."""
|
"""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."""
|
"""Initialize Light Switch."""
|
||||||
self._name = name
|
self._name = name
|
||||||
self._switch_entity_id = switch_entity_id
|
self._switch_entity_id = switch_entity_id
|
||||||
|
self._unique_id = unique_id
|
||||||
self._is_on = False
|
self._is_on = False
|
||||||
self._available = False
|
self._available = False
|
||||||
self._async_unsub_state_changed: Optional[CALLBACK_TYPE] = None
|
self._async_unsub_state_changed: Optional[CALLBACK_TYPE] = None
|
||||||
@ -80,6 +93,11 @@ class LightSwitch(LightEntity):
|
|||||||
"""No polling needed for a light switch."""
|
"""No polling needed for a light switch."""
|
||||||
return False
|
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):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Forward the turn_on command to the switch in this light switch."""
|
"""Forward the turn_on command to the switch in this light switch."""
|
||||||
data = {ATTR_ENTITY_ID: self._switch_entity_id}
|
data = {ATTR_ENTITY_ID: self._switch_entity_id}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user