Skip to content

Commit b58b8da

Browse files
Handle source entity changes
1 parent 65559b3 commit b58b8da

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

custom_components/periodic_min_max/sensor.py

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
AddConfigEntryEntitiesCallback,
3030
AddEntitiesCallback,
3131
)
32-
from homeassistant.helpers.event import async_track_state_change_event
32+
from homeassistant.helpers.event import (
33+
async_track_entity_registry_updated_event,
34+
async_track_state_change_event,
35+
)
3336
from homeassistant.helpers.reload import async_setup_reload_service
3437
from homeassistant.helpers.restore_state import RestoreEntity
3538
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, StateType
@@ -72,16 +75,22 @@ def async_add_to_device(
7275
return device_id
7376

7477

78+
async def config_entry_update_listener(hass: HomeAssistant, entry: ConfigEntry) -> None:
79+
"""Update listener, called when the config entry options are changed."""
80+
await hass.config_entries.async_reload(entry.entry_id)
81+
82+
7583
async def async_setup_entry(
7684
hass: HomeAssistant,
7785
config_entry: ConfigEntry,
7886
async_add_entities: AddConfigEntryEntitiesCallback,
7987
) -> None:
8088
"""Initialize min/max/mean config entry."""
81-
registry = er.async_get(hass)
89+
entity_registry = er.async_get(hass)
90+
device_registry = dr.async_get(hass)
8291
try:
8392
entity_id = er.async_validate_entity_id(
84-
registry, config_entry.options[CONF_ENTITY_ID]
93+
entity_registry, config_entry.options[CONF_ENTITY_ID]
8594
)
8695
except vol.Invalid:
8796
# The entity is identified by an unknown entity registry ID
@@ -93,7 +102,46 @@ async def async_setup_entry(
93102

94103
sensor_type = config_entry.options[CONF_TYPE]
95104

96-
async_add_to_device(hass, config_entry, entity_id)
105+
async def async_registry_updated(
106+
event: Event[er.EventEntityRegistryUpdatedData],
107+
) -> None:
108+
"""Handle entity registry update."""
109+
data = event.data
110+
if data["action"] == "remove":
111+
await hass.config_entries.async_remove(config_entry.entry_id)
112+
113+
if data["action"] != "update":
114+
return
115+
116+
if "entity_id" in data["changes"]:
117+
# Entity_id changed, reload the config entry
118+
await hass.config_entries.async_reload(config_entry.entry_id)
119+
120+
if device_id and "device_id" in data["changes"]:
121+
# If the tracked entity is no longer in the device, remove our config entry
122+
# from the device
123+
if (
124+
not (entity_entry := entity_registry.async_get(data[CONF_ENTITY_ID]))
125+
or not device_registry.async_get(device_id)
126+
or entity_entry.device_id == device_id
127+
):
128+
# No need to do any cleanup
129+
return
130+
131+
device_registry.async_update_device(
132+
device_id, remove_config_entry_id=config_entry.entry_id
133+
)
134+
135+
config_entry.async_on_unload(
136+
async_track_entity_registry_updated_event(
137+
hass, entity_id, async_registry_updated
138+
)
139+
)
140+
config_entry.async_on_unload(
141+
config_entry.add_update_listener(config_entry_update_listener)
142+
)
143+
144+
device_id = async_add_to_device(hass, config_entry, entity_id)
97145

98146
async_add_entities(
99147
[

0 commit comments

Comments
 (0)