1 """Base entity for the Switch as X integration."""
3 from __future__
import annotations
22 from .const
import DOMAIN
as SWITCH_AS_X_DOMAIN
26 """Represents a Switch as an X."""
28 _attr_should_poll =
False
34 config_entry_title: str,
36 switch_entity_id: str,
39 """Initialize Switch as an X."""
40 registry = er.async_get(hass)
41 device_registry = dr.async_get(hass)
42 wrapped_switch = registry.async_get(switch_entity_id)
43 device_id = wrapped_switch.device_id
if wrapped_switch
else None
44 entity_category = wrapped_switch.entity_category
if wrapped_switch
else None
45 has_entity_name = wrapped_switch.has_entity_name
if wrapped_switch
else False
47 name: str |
None = config_entry_title
49 name = wrapped_switch.original_name
52 if device_id
and (device := device_registry.async_get(device_id)):
54 connections=device.connections,
55 identifiers=device.identifiers,
64 registry.async_get_entity_id(domain, SWITCH_AS_X_DOMAIN, unique_id)
is None
69 self, event: Event[EventStateChangedData] |
None =
None
71 """Handle child updates."""
74 )
is None or state.state == STATE_UNAVAILABLE:
81 """Register callbacks and copy the wrapped entity's custom name if set."""
84 def _async_state_changed_listener(
85 event: Event[EventStateChangedData] |
None =
None,
87 """Handle child updates."""
98 _async_state_changed_listener()
101 registry = er.async_get(self.
hasshass)
102 if registry.async_get(self.
entity_identity_id)
is not None:
103 registry.async_update_entity_options(
114 def copy_custom_name(wrapped_switch: er.RegistryEntry) ->
None:
115 """Copy the name set by user from the wrapped entity."""
116 if wrapped_switch.name
is None:
118 registry.async_update_entity(self.
entity_identity_id, name=wrapped_switch.name)
120 def copy_expose_settings() -> None:
121 """Copy assistant expose settings from the wrapped entity.
123 Also unexpose the wrapped entity if exposed.
125 expose_settings = exposed_entities.async_get_entity_settings(
128 for assistant, settings
in expose_settings.items():
129 if (should_expose := settings.get(
"should_expose"))
is None:
131 exposed_entities.async_expose_entity(
132 self.
hasshass, assistant, self.
entity_identity_id, should_expose
134 exposed_entities.async_expose_entity(
138 copy_custom_name(wrapped_switch)
139 copy_expose_settings()
143 """Generate entity options."""
148 """Represents a Switch as a ToggleEntity."""
151 """Forward the turn_on command to the switch in this light switch."""
152 await self.
hasshass.services.async_call(
161 """Forward the turn_off command to the switch in this light switch."""
162 await self.
hasshass.services.async_call(
172 self, event: Event[EventStateChangedData] |
None =
None
174 """Handle child updates."""
186 """Represents a Switch as an X."""
191 config_entry_title: str,
194 switch_entity_id: str,
197 """Initialize Switch as an X."""
198 super().
__init__(hass, config_entry_title, domain, switch_entity_id, unique_id)
203 """Generate entity options."""
None async_added_to_hass(self)
dict[str, Any] async_generate_entity_options(self)
None __init__(self, HomeAssistant hass, str config_entry_title, str domain, str switch_entity_id, str unique_id)
None async_state_changed_listener(self, Event[EventStateChangedData]|None event=None)
None __init__(self, HomeAssistant hass, str config_entry_title, str domain, bool invert, str switch_entity_id, str unique_id)
dict[str, Any] async_generate_entity_options(self)
None async_state_changed_listener(self, Event[EventStateChangedData]|None event=None)
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None async_write_ha_state(self)
None async_on_remove(self, CALLBACK_TYPE func)
CALLBACK_TYPE async_track_state_change_event(HomeAssistant hass, str|Iterable[str] entity_ids, Callable[[Event[EventStateChangedData]], Any] action, HassJobType|None job_type=None)