|
5 | 5 | from homeassistant.components.humidifier import HumidifierDeviceClass |
6 | 6 | from homeassistant.config_entries import ConfigEntry |
7 | 7 | from homeassistant.const import CONF_NAME, CONF_UNIQUE_ID, Platform |
8 | | -from homeassistant.core import HomeAssistant |
9 | | -from homeassistant.helpers import config_validation as cv, discovery |
| 8 | +from homeassistant.core import Event, HomeAssistant |
| 9 | +from homeassistant.helpers import ( |
| 10 | + config_validation as cv, |
| 11 | + discovery, |
| 12 | + entity_registry as er, |
| 13 | +) |
10 | 14 | from homeassistant.helpers.device import ( |
| 15 | + async_entity_id_to_device_id, |
11 | 16 | async_remove_stale_devices_links_keep_entity_device, |
12 | 17 | ) |
| 18 | +from homeassistant.helpers.event import async_track_entity_registry_updated_event |
| 19 | +from homeassistant.helpers.helper_integration import async_handle_source_entity_changes |
13 | 20 | from homeassistant.helpers.typing import ConfigType |
14 | 21 |
|
15 | 22 | DOMAIN = "generic_hygrostat" |
@@ -88,6 +95,54 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: |
88 | 95 | entry.options[CONF_HUMIDIFIER], |
89 | 96 | ) |
90 | 97 |
|
| 98 | + def set_humidifier_entity_id_or_uuid(source_entity_id: str) -> None: |
| 99 | + hass.config_entries.async_update_entry( |
| 100 | + entry, |
| 101 | + options={**entry.options, CONF_HUMIDIFIER: source_entity_id}, |
| 102 | + ) |
| 103 | + |
| 104 | + async def source_entity_removed() -> None: |
| 105 | + # The source entity has been removed, we need to clean the device links. |
| 106 | + async_remove_stale_devices_links_keep_entity_device(hass, entry.entry_id, None) |
| 107 | + |
| 108 | + entry.async_on_unload( |
| 109 | + # We use async_handle_source_entity_changes to track changes to the humidifer, |
| 110 | + # but not the humidity sensor because the generic_hygrostat adds itself to the |
| 111 | + # humidifier's device. |
| 112 | + async_handle_source_entity_changes( |
| 113 | + hass, |
| 114 | + helper_config_entry_id=entry.entry_id, |
| 115 | + set_source_entity_id_or_uuid=set_humidifier_entity_id_or_uuid, |
| 116 | + source_device_id=async_entity_id_to_device_id( |
| 117 | + hass, entry.options[CONF_HUMIDIFIER] |
| 118 | + ), |
| 119 | + source_entity_id_or_uuid=entry.options[CONF_HUMIDIFIER], |
| 120 | + source_entity_removed=source_entity_removed, |
| 121 | + ) |
| 122 | + ) |
| 123 | + |
| 124 | + async def async_sensor_updated( |
| 125 | + event: Event[er.EventEntityRegistryUpdatedData], |
| 126 | + ) -> None: |
| 127 | + """Handle entity registry update.""" |
| 128 | + data = event.data |
| 129 | + if data["action"] != "update": |
| 130 | + return |
| 131 | + if "entity_id" not in data["changes"]: |
| 132 | + return |
| 133 | + |
| 134 | + # Entity_id changed, update the config entry |
| 135 | + hass.config_entries.async_update_entry( |
| 136 | + entry, |
| 137 | + options={**entry.options, CONF_SENSOR: data["entity_id"]}, |
| 138 | + ) |
| 139 | + |
| 140 | + entry.async_on_unload( |
| 141 | + async_track_entity_registry_updated_event( |
| 142 | + hass, entry.options[CONF_SENSOR], async_sensor_updated |
| 143 | + ) |
| 144 | + ) |
| 145 | + |
91 | 146 | await hass.config_entries.async_forward_entry_setups(entry, (Platform.HUMIDIFIER,)) |
92 | 147 | entry.async_on_unload(entry.add_update_listener(config_entry_update_listener)) |
93 | 148 | return True |
|
0 commit comments