1 """Component to wrap switch entities in entities of other domains."""
3 from __future__
import annotations
7 import voluptuous
as vol
16 from .const
import CONF_INVERT, CONF_TARGET_DOMAIN
17 from .light
import LightSwitch
19 __all__ = [
"LightSwitch"]
21 _LOGGER = logging.getLogger(__name__)
26 hass: HomeAssistant, entry: ConfigEntry, entity_id: str
28 """Add our config entry to the tracked entity's device."""
29 registry = er.async_get(hass)
30 device_registry = dr.async_get(hass)
34 not (wrapped_switch := registry.async_get(entity_id))
35 or not (device_id := wrapped_switch.device_id)
36 or not (device_registry.async_get(device_id))
40 device_registry.async_update_device(device_id, add_config_entry_id=entry.entry_id)
46 """Set up a config entry."""
47 registry = er.async_get(hass)
48 device_registry = dr.async_get(hass)
50 entity_id = er.async_validate_entity_id(registry, entry.options[CONF_ENTITY_ID])
54 "Failed to setup switch_as_x for unknown entity %s",
55 entry.options[CONF_ENTITY_ID],
59 async
def async_registry_updated(
60 event: Event[er.EventEntityRegistryUpdatedData],
62 """Handle entity registry update."""
64 if data[
"action"] ==
"remove":
65 await hass.config_entries.async_remove(entry.entry_id)
67 if data[
"action"] !=
"update":
70 if "entity_id" in data[
"changes"]:
72 await hass.config_entries.async_reload(entry.entry_id)
74 if device_id
and "device_id" in data[
"changes"]:
78 not (entity_entry := registry.async_get(data[CONF_ENTITY_ID]))
79 or not device_registry.async_get(device_id)
80 or entity_entry.device_id == device_id
85 device_registry.async_update_device(
86 device_id, remove_config_entry_id=entry.entry_id
89 entry.async_on_unload(
91 hass, entity_id, async_registry_updated
94 entry.async_on_unload(entry.add_update_listener(config_entry_update_listener))
98 await hass.config_entries.async_forward_entry_setups(
99 entry, (entry.options[CONF_TARGET_DOMAIN],)
105 """Migrate old entry."""
107 "Migrating from version %s.%s", config_entry.version, config_entry.minor_version
110 if config_entry.version > 1:
113 if config_entry.version == 1:
114 options = {**config_entry.options}
115 if config_entry.minor_version < 2:
116 options.setdefault(CONF_INVERT,
False)
117 hass.config_entries.async_update_entry(
118 config_entry, options=options, minor_version=2
122 "Migration to version %s.%s successful",
123 config_entry.version,
124 config_entry.minor_version,
131 """Update listener, called when the config entry options are changed."""
132 await hass.config_entries.async_reload(entry.entry_id)
136 """Unload a config entry."""
137 return await hass.config_entries.async_unload_platforms(
138 entry, (entry.options[CONF_TARGET_DOMAIN],)
143 """Unload a config entry.
145 This will unhide the wrapped entity and restore assistant expose settings.
147 registry = er.async_get(hass)
149 switch_entity_id = er.async_validate_entity_id(
150 registry, entry.options[CONF_ENTITY_ID]
156 if not (switch_entity_entry := registry.async_get(switch_entity_id)):
160 if switch_entity_entry.hidden_by == er.RegistryEntryHider.INTEGRATION:
161 registry.async_update_entity(switch_entity_id, hidden_by=
None)
163 switch_as_x_entries = er.async_entries_for_config_entry(registry, entry.entry_id)
164 if not switch_as_x_entries:
167 switch_as_x_entry = switch_as_x_entries[0]
170 expose_settings = exposed_entities.async_get_entity_settings(
171 hass, switch_as_x_entry.entity_id
173 for assistant, settings
in expose_settings.items():
174 if (should_expose := settings.get(
"should_expose"))
is None:
176 exposed_entities.async_expose_entity(
177 hass, assistant, switch_entity_id, should_expose
str|None async_add_to_device(HomeAssistant hass, ConfigEntry entry, str entity_id)
None async_remove_entry(HomeAssistant hass, ConfigEntry entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_migrate_entry(HomeAssistant hass, ConfigEntry config_entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
None config_entry_update_listener(HomeAssistant hass, ConfigEntry entry)
CALLBACK_TYPE async_track_entity_registry_updated_event(HomeAssistant hass, str|Iterable[str] entity_ids, Callable[[Event[EventEntityRegistryUpdatedData]], Any] action, HassJobType|None job_type=None)