@@ -87,12 +87,12 @@ async def async_added_to_hass(self):
87
87
88
88
# Continue with the setup
89
89
if DOMAIN in self .hass .data :
90
- self .hass .data [DOMAIN ].setdefault ('entities' , []). append ( self )
90
+ self .hass .data [DOMAIN ].setdefault (self . _config_entry . entry_id , {})[ 'sensor' ] = self
91
91
await self ._async_next_page ()
92
92
93
93
async def async_will_remove_from_hass (self ):
94
94
"""When entity is being removed from hass."""
95
- pass
95
+ self . cancel_update_task ()
96
96
97
97
async def async_schedule_next_page (self , wait_time : float ):
98
98
_LOGGER .debug ("Scheduling next page in %s seconds for %s" , wait_time , self ._pixoo .address )
@@ -104,7 +104,7 @@ async def task():
104
104
except asyncio .CancelledError :
105
105
_LOGGER .debug ('Next page timer cancelled for %s' , self ._pixoo .address )
106
106
# Using HA's async_create_task instead of asyncio.create_task because it's better for HA.
107
- # (Also, from the docs, it automatically cancels the task when the entry is unloaded. )
107
+ # (canceled in the async_will_remove_from_hass method of this file )
108
108
self ._update_task = self ._config_entry .async_create_background_task (self .hass , task (), "pixoo-next-page-timer" )
109
109
110
110
async def _async_next_page (self ):
@@ -300,7 +300,7 @@ def draw():
300
300
301
301
await self .hass .async_add_executor_job (draw )
302
302
if self ._update_task :
303
- self ._update_task . cancel ()
303
+ self .cancel_update_task ()
304
304
await self .async_schedule_next_page (duration .total_seconds ())
305
305
306
306
# Service to play the buzzer
@@ -326,6 +326,11 @@ def update_current_page():
326
326
327
327
await self .hass .async_add_executor_job (update_current_page )
328
328
329
+ def cancel_update_task (self ):
330
+ if self ._update_task :
331
+ self ._update_task .cancel ()
332
+ _LOGGER .debug ("Successfully canceled update task for %s" , self ._pixoo .address )
333
+
329
334
@property
330
335
def state (self ):
331
336
return self ._current_page_index + 1
0 commit comments