29
29
AddConfigEntryEntitiesCallback ,
30
30
AddEntitiesCallback ,
31
31
)
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
+ )
33
36
from homeassistant .helpers .reload import async_setup_reload_service
34
37
from homeassistant .helpers .restore_state import RestoreEntity
35
38
from homeassistant .helpers .typing import ConfigType , DiscoveryInfoType , StateType
@@ -72,16 +75,22 @@ def async_add_to_device(
72
75
return device_id
73
76
74
77
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
+
75
83
async def async_setup_entry (
76
84
hass : HomeAssistant ,
77
85
config_entry : ConfigEntry ,
78
86
async_add_entities : AddConfigEntryEntitiesCallback ,
79
87
) -> None :
80
88
"""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 )
82
91
try :
83
92
entity_id = er .async_validate_entity_id (
84
- registry , config_entry .options [CONF_ENTITY_ID ]
93
+ entity_registry , config_entry .options [CONF_ENTITY_ID ]
85
94
)
86
95
except vol .Invalid :
87
96
# The entity is identified by an unknown entity registry ID
@@ -93,7 +102,46 @@ async def async_setup_entry(
93
102
94
103
sensor_type = config_entry .options [CONF_TYPE ]
95
104
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 )
97
145
98
146
async_add_entities (
99
147
[
0 commit comments