Skip to content

Commit 9e2b0fc

Browse files
authored
Merge pull request #89 from SB2DD/fix/uncancelled-update-task
2 parents 8629403 + 9312825 commit 9e2b0fc

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

custom_components/divoom_pixoo/sensor.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,12 @@ async def async_added_to_hass(self):
8787

8888
# Continue with the setup
8989
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
9191
await self._async_next_page()
9292

9393
async def async_will_remove_from_hass(self):
9494
"""When entity is being removed from hass."""
95-
pass
95+
self.cancel_update_task()
9696

9797
async def async_schedule_next_page(self, wait_time: float):
9898
_LOGGER.debug("Scheduling next page in %s seconds for %s", wait_time, self._pixoo.address)
@@ -104,7 +104,7 @@ async def task():
104104
except asyncio.CancelledError:
105105
_LOGGER.debug('Next page timer cancelled for %s', self._pixoo.address)
106106
# 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)
108108
self._update_task = self._config_entry.async_create_background_task(self.hass, task(), "pixoo-next-page-timer")
109109

110110
async def _async_next_page(self):
@@ -300,7 +300,7 @@ def draw():
300300

301301
await self.hass.async_add_executor_job(draw)
302302
if self._update_task:
303-
self._update_task.cancel()
303+
self.cancel_update_task()
304304
await self.async_schedule_next_page(duration.total_seconds())
305305

306306
# Service to play the buzzer
@@ -326,6 +326,11 @@ def update_current_page():
326326

327327
await self.hass.async_add_executor_job(update_current_page)
328328

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+
329334
@property
330335
def state(self):
331336
return self._current_page_index+1

0 commit comments

Comments
 (0)