Skip to content

Commit fd605e0

Browse files
emontnemeryfrenck
authored andcommitted
Handle changes to source entities in generic_hygrostat helper (home-assistant#146538)
1 parent e73bcc7 commit fd605e0

File tree

2 files changed

+480
-7
lines changed

2 files changed

+480
-7
lines changed

homeassistant/components/generic_hygrostat/__init__.py

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
from homeassistant.components.humidifier import HumidifierDeviceClass
66
from homeassistant.config_entries import ConfigEntry
77
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+
)
1014
from homeassistant.helpers.device import (
15+
async_entity_id_to_device_id,
1116
async_remove_stale_devices_links_keep_entity_device,
1217
)
18+
from homeassistant.helpers.event import async_track_entity_registry_updated_event
19+
from homeassistant.helpers.helper_integration import async_handle_source_entity_changes
1320
from homeassistant.helpers.typing import ConfigType
1421

1522
DOMAIN = "generic_hygrostat"
@@ -88,6 +95,54 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
8895
entry.options[CONF_HUMIDIFIER],
8996
)
9097

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+
91146
await hass.config_entries.async_forward_entry_setups(entry, (Platform.HUMIDIFIER,))
92147
entry.async_on_unload(entry.add_update_listener(config_entry_update_listener))
93148
return True

0 commit comments

Comments
 (0)