From 37c55d81e38d9e3359af2d8bec7dcc51fd682721 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 15 May 2024 19:08:24 +0900 Subject: [PATCH] Fix non-thread-safe state write in tellduslive (#117487) --- homeassistant/components/tellduslive/const.py | 1 - homeassistant/components/tellduslive/cover.py | 6 +++--- homeassistant/components/tellduslive/entry.py | 18 ++++-------------- homeassistant/components/tellduslive/light.py | 2 +- homeassistant/components/tellduslive/switch.py | 4 ++-- 5 files changed, 10 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/tellduslive/const.py b/homeassistant/components/tellduslive/const.py index 3a24f6b033a..eee36879ba9 100644 --- a/homeassistant/components/tellduslive/const.py +++ b/homeassistant/components/tellduslive/const.py @@ -24,7 +24,6 @@ SCAN_INTERVAL = timedelta(minutes=1) ATTR_LAST_UPDATED = "time_last_updated" -SIGNAL_UPDATE_ENTITY = "tellduslive_update" TELLDUS_DISCOVERY_NEW = "telldus_new_{}_{}" CLOUD_NAME = "Cloud API" diff --git a/homeassistant/components/tellduslive/cover.py b/homeassistant/components/tellduslive/cover.py index 57c6ae9e7eb..de962041333 100644 --- a/homeassistant/components/tellduslive/cover.py +++ b/homeassistant/components/tellduslive/cover.py @@ -46,14 +46,14 @@ class TelldusLiveCover(TelldusLiveEntity, CoverEntity): def close_cover(self, **kwargs: Any) -> None: """Close the cover.""" self.device.down() - self._update_callback() + self.schedule_update_ha_state() def open_cover(self, **kwargs: Any) -> None: """Open the cover.""" self.device.up() - self._update_callback() + self.schedule_update_ha_state() def stop_cover(self, **kwargs: Any) -> None: """Stop the cover.""" self.device.stop() - self._update_callback() + self.schedule_update_ha_state() diff --git a/homeassistant/components/tellduslive/entry.py b/homeassistant/components/tellduslive/entry.py index 77a04fabd06..a71fcb685c0 100644 --- a/homeassistant/components/tellduslive/entry.py +++ b/homeassistant/components/tellduslive/entry.py @@ -11,7 +11,6 @@ from homeassistant.const import ( ATTR_MODEL, ATTR_VIA_DEVICE, ) -from homeassistant.core import callback from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity @@ -33,25 +32,16 @@ class TelldusLiveEntity(Entity): """Initialize the entity.""" self._id = device_id self._client = client - self._async_unsub_dispatcher_connect = None async def async_added_to_hass(self): """Call when entity is added to hass.""" _LOGGER.debug("Created device %s", self) - self._async_unsub_dispatcher_connect = async_dispatcher_connect( - self.hass, SIGNAL_UPDATE_ENTITY, self._update_callback + self.async_on_remove( + async_dispatcher_connect( + self.hass, SIGNAL_UPDATE_ENTITY, self.async_write_ha_state + ) ) - async def async_will_remove_from_hass(self): - """Disconnect dispatcher listener when removed.""" - if self._async_unsub_dispatcher_connect: - self._async_unsub_dispatcher_connect() - - @callback - def _update_callback(self): - """Return the property of the device might have changed.""" - self.async_write_ha_state() - @property def device_id(self): """Return the id of the device.""" diff --git a/homeassistant/components/tellduslive/light.py b/homeassistant/components/tellduslive/light.py index 63af8a32527..101ccb0dab0 100644 --- a/homeassistant/components/tellduslive/light.py +++ b/homeassistant/components/tellduslive/light.py @@ -50,7 +50,7 @@ class TelldusLiveLight(TelldusLiveEntity, LightEntity): def changed(self): """Define a property of the device that might have changed.""" self._last_brightness = self.brightness - self._update_callback() + self.schedule_update_ha_state() @property def brightness(self): diff --git a/homeassistant/components/tellduslive/switch.py b/homeassistant/components/tellduslive/switch.py index c26a8dcf951..cd28a170442 100644 --- a/homeassistant/components/tellduslive/switch.py +++ b/homeassistant/components/tellduslive/switch.py @@ -45,9 +45,9 @@ class TelldusLiveSwitch(TelldusLiveEntity, SwitchEntity): def turn_on(self, **kwargs: Any) -> None: """Turn the switch on.""" self.device.turn_on() - self._update_callback() + self.schedule_update_ha_state() def turn_off(self, **kwargs: Any) -> None: """Turn the switch off.""" self.device.turn_off() - self._update_callback() + self.schedule_update_ha_state()