@@ -21,6 +21,27 @@ def on_after_startup(self):
21
21
self ._logger .info ("ExternalTempReader plugin loaded. Loading configuration..." )
22
22
self .load_configuration ()
23
23
self .start_polling_thread ()
24
+
25
+ def on_settings_save (self , data ):
26
+ """Handle settings save and apply changes immediately."""
27
+ # Get the old interval before saving
28
+ old_interval = self .polling_interval
29
+
30
+ # Save the settings using the parent class method
31
+ super ().on_settings_save (data )
32
+
33
+ # Reload configuration with new values
34
+ self .load_configuration ()
35
+
36
+ # Check if polling interval changed
37
+ if old_interval != self .polling_interval :
38
+ self ._logger .info (f"Polling interval changed from { old_interval } s to { self .polling_interval } s, restarting polling thread" )
39
+ # Restart the polling thread with new interval
40
+ self .restart_polling_thread ()
41
+
42
+ # Check if URL or UUID changed
43
+ if 'tag_uuid' in data or 'base_url' in data :
44
+ self ._logger .info ("API configuration changed, next poll will use new settings" )
24
45
25
46
def on_shutdown (self ):
26
47
self ._logger .info ("ExternalTempReader plugin unloaded. Stopping polling thread..." )
@@ -114,8 +135,17 @@ def stop_polling_thread(self):
114
135
"""Stop the polling thread."""
115
136
self .stop_polling = True
116
137
if self .polling_thread :
117
- self .polling_thread .join ()
138
+ self .polling_thread .join (timeout = 5 ) # Wait max 5 seconds
118
139
self .polling_thread = None
140
+
141
+ def restart_polling_thread (self ):
142
+ """Restart the polling thread with new settings."""
143
+ self ._logger .info ("Restarting temperature polling thread..." )
144
+ self .stop_polling_thread ()
145
+ # Small delay to ensure thread is fully stopped
146
+ time .sleep (0.5 )
147
+ self .start_polling_thread ()
148
+ self ._logger .info ("Temperature polling thread restarted with new settings" )
119
149
120
150
def get_settings_defaults (self ):
121
151
"""Default plugin settings."""
0 commit comments