24
24
EVENT_UPDATE ,
25
25
SERVICE_RESET ,
26
26
SERVICE_UPDATE ,
27
+ COORDINATOR ,
27
28
)
28
29
29
30
_LOGGER = logging .getLogger (__name__ )
@@ -47,6 +48,44 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
47
48
unit_of_measurement = entry .data .get (CONF_UNIT_OF_MEASUREMENT )
48
49
auto_reset = entry .data .get (CONF_AUTO_RESET , DEFAULT_AUTO_RESET )
49
50
51
+ # update listener for options flow
52
+ hass_data = dict (entry .data )
53
+ unsub_options_update_listener = entry .add_update_listener (options_update_listener )
54
+ hass_data ["unsub_options_update_listener" ] = unsub_options_update_listener
55
+ hass .data [DOMAIN ][entry .entry_id ] = hass_data
56
+
57
+ # logic here is: if options are set that do not agree with the data settings, use the options
58
+ # handle options flow data
59
+ if CONF_INPUT_SENSOR in entry .options and entry .options .get (
60
+ CONF_INPUT_SENSOR
61
+ ) != entry .data .get (CONF_INPUT_SENSOR ):
62
+ input_sensor = hass .data [DOMAIN ][entry .entry_id ][
63
+ CONF_INPUT_SENSOR
64
+ ] = entry .options .get (CONF_INPUT_SENSOR )
65
+ if CONF_AUTO_RESET in entry .options and entry .options .get (
66
+ CONF_AUTO_RESET
67
+ ) != entry .data .get (CONF_AUTO_RESET ):
68
+ auto_reset = hass .data [DOMAIN ][entry .entry_id ][
69
+ CONF_AUTO_RESET
70
+ ] = entry .options .get (CONF_AUTO_RESET )
71
+ if CONF_INTERVAL in entry .options and entry .options .get (
72
+ CONF_INTERVAL
73
+ ) != entry .data .get (CONF_INTERVAL ):
74
+ interval = hass .data [DOMAIN ][entry .entry_id ][CONF_INTERVAL ] = entry .options .get (
75
+ CONF_INTERVAL
76
+ )
77
+ if CONF_OPERATION in entry .options and entry .options .get (
78
+ CONF_OPERATION
79
+ ) != entry .data .get (CONF_OPERATION ):
80
+ operation = hass .data [DOMAIN ][entry .entry_id ][
81
+ CONF_OPERATION
82
+ ] = entry .options .get (CONF_OPERATION )
83
+ if CONF_UNIT_OF_MEASUREMENT in entry .options and entry .options .get (
84
+ CONF_UNIT_OF_MEASUREMENT
85
+ ) != entry .data .get (CONF_UNIT_OF_MEASUREMENT ):
86
+ unit_of_measurement = hass .data [DOMAIN ][entry .entry_id ][
87
+ CONF_UNIT_OF_MEASUREMENT
88
+ ] = entry .options .get (CONF_UNIT_OF_MEASUREMENT )
50
89
# set up coordinator
51
90
coordinator = DailySensorUpdateCoordinator (
52
91
hass ,
@@ -63,18 +102,17 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
63
102
if not coordinator .last_update_success :
64
103
raise ConfigEntryNotReady
65
104
66
- hass .data [DOMAIN ][entry .entry_id ] = coordinator
67
-
68
105
for platform in PLATFORMS :
69
106
coordinator .platforms .append (platform )
70
107
hass .async_create_task (
71
108
hass .config_entries .async_forward_entry_setup (entry , platform )
72
109
)
73
110
74
111
# add update listener if not already added.
75
- if weakref .ref (async_reload_entry ) not in entry .update_listeners :
76
- entry .add_update_listener (async_reload_entry )
112
+ # if weakref.ref(async_reload_entry) not in entry.update_listeners:
113
+ # entry.add_update_listener(async_reload_entry)
77
114
115
+ hass .data [DOMAIN ][entry .entry_id ][COORDINATOR ] = coordinator
78
116
# register services
79
117
hass .services .async_register (
80
118
DOMAIN ,
@@ -91,28 +129,58 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
91
129
92
130
async def async_reload_entry (hass : HomeAssistant , entry : ConfigEntry ):
93
131
"""Reload config entry."""
94
- coordinator = hass .data [DOMAIN ][entry .entry_id ]
132
+ coordinator = hass .data [DOMAIN ][entry .entry_id ][ COORDINATOR ]
95
133
if coordinator .entry_setup_completed :
96
134
await async_unload_entry (hass , entry )
97
135
await async_setup_entry (hass , entry )
98
136
99
137
138
+ async def options_update_listener (hass , config_entry ):
139
+ """Handle options update."""
140
+ hass .data [DOMAIN ][config_entry .entry_id ][CONF_INTERVAL ] = config_entry .options .get (
141
+ CONF_INTERVAL
142
+ )
143
+ hass .data [DOMAIN ][config_entry .entry_id ][
144
+ CONF_INPUT_SENSOR
145
+ ] = config_entry .options .get (CONF_INPUT_SENSOR )
146
+ hass .data [DOMAIN ][config_entry .entry_id ][
147
+ CONF_AUTO_RESET
148
+ ] = config_entry .options .get (CONF_AUTO_RESET )
149
+ hass .data [DOMAIN ][config_entry .entry_id ][CONF_OPERATION ] = config_entry .options .get (
150
+ CONF_OPERATION
151
+ )
152
+ hass .data [DOMAIN ][config_entry .entry_id ][
153
+ CONF_UNIT_OF_MEASUREMENT
154
+ ] = config_entry .options .get (CONF_UNIT_OF_MEASUREMENT )
155
+ await hass .config_entries .async_reload (config_entry .entry_id )
156
+
157
+
100
158
async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ):
101
159
"""Handle removal of an entry."""
102
- coordinator = hass .data [DOMAIN ][entry .entry_id ]
103
- unloaded = all (
104
- await asyncio .gather (
105
- * [
106
- hass .config_entries .async_forward_entry_unload (entry , platform )
107
- for platform in PLATFORMS
108
- if platform in coordinator .platforms
109
- ]
160
+ if DOMAIN in hass .data and entry .entry_id in hass .data [DOMAIN ]:
161
+ coordinator = hass .data [DOMAIN ][entry .entry_id ][COORDINATOR ]
162
+ unloaded = all (
163
+ await asyncio .gather (
164
+ * [
165
+ hass .config_entries .async_forward_entry_unload (entry , platform )
166
+ for platform in PLATFORMS
167
+ if platform in coordinator .platforms
168
+ ]
169
+ )
110
170
)
111
- )
112
- if unloaded :
113
- hass .data [DOMAIN ].pop (entry .entry_id )
171
+ if unloaded :
172
+ hass .data [DOMAIN ].pop (entry .entry_id )
173
+
174
+ return unloaded
175
+ return True
176
+
114
177
115
- return unloaded
178
+ async def async_remove_entry (hass , entry ):
179
+ """Remove Daily sensor config entry."""
180
+ if DOMAIN in hass .data and entry .entry_id in hass .data [DOMAIN ]:
181
+ coordinator = hass .data [DOMAIN ][entry .entry_id ]["coordinator" ]
182
+ await coordinator .async_delete_config ()
183
+ del hass .data [DOMAIN ][entry .entry_id ]
116
184
117
185
118
186
class DailySensorUpdateCoordinator (DataUpdateCoordinator ):
0 commit comments