diff --git a/homeassistant/components/tradfri/base_class.py b/homeassistant/components/tradfri/base_class.py index a03f16a1f09..a54c9f7d500 100644 --- a/homeassistant/components/tradfri/base_class.py +++ b/homeassistant/components/tradfri/base_class.py @@ -59,31 +59,38 @@ class TradfriBaseClass(Entity): ) -> None: """Initialize a device.""" self._api = handle_error(api) + self._attr_name = device.name + self._attr_available = device.reachable self._device: Device = device self._device_control: BlindControl | LightControl | SocketControl | SignalRepeaterControl | AirPurifierControl | None = ( None ) self._device_data: Socket | Light | Blind | AirPurifier | None = None self._gateway_id = gateway_id - self._refresh(device) + + async def _async_run_observe(self, cmd: Command) -> None: + """Run observe in a coroutine.""" + try: + await self._api(cmd) + except PytradfriError as err: + self._attr_available = False + self.async_write_ha_state() + _LOGGER.warning("Observation failed, trying again", exc_info=err) + self._async_start_observe() @callback def _async_start_observe(self, exc: Exception | None = None) -> None: """Start observation of device.""" if exc: + self._attr_available = False self.async_write_ha_state() _LOGGER.warning("Observation failed for %s", self._attr_name, exc_info=exc) - - try: - cmd = self._device.observe( - callback=self._observe_update, - err_callback=self._async_start_observe, - duration=0, - ) - self.hass.async_create_task(self._api(cmd)) - except PytradfriError as err: - _LOGGER.warning("Observation failed, trying again", exc_info=err) - self._async_start_observe() + cmd = self._device.observe( + callback=self._observe_update, + err_callback=self._async_start_observe, + duration=0, + ) + self.hass.async_create_task(self._async_run_observe(cmd)) async def async_added_to_hass(self) -> None: """Start thread when added to hass.""" @@ -93,12 +100,14 @@ class TradfriBaseClass(Entity): def _observe_update(self, device: Device) -> None: """Receive new state data for this device.""" self._refresh(device) - self.async_write_ha_state() - def _refresh(self, device: Device) -> None: + def _refresh(self, device: Device, write_ha: bool = True) -> None: """Refresh the device data.""" self._device = device self._attr_name = device.name + self._attr_available = device.reachable + if write_ha: + self.async_write_ha_state() class TradfriBaseDevice(TradfriBaseClass): @@ -119,8 +128,3 @@ class TradfriBaseDevice(TradfriBaseClass): sw_version=info.firmware_version, via_device=(DOMAIN, self._gateway_id), ) - - def _refresh(self, device: Device) -> None: - """Refresh the device data.""" - super()._refresh(device) - self._attr_available = device.reachable diff --git a/homeassistant/components/tradfri/cover.py b/homeassistant/components/tradfri/cover.py index 7bcbf5af5e1..074b6ddd726 100644 --- a/homeassistant/components/tradfri/cover.py +++ b/homeassistant/components/tradfri/cover.py @@ -41,10 +41,9 @@ class TradfriCover(TradfriBaseDevice, CoverEntity): gateway_id: str, ) -> None: """Initialize a cover.""" - super().__init__(device, api, gateway_id) self._attr_unique_id = f"{gateway_id}-{device.id}" - - self._refresh(device) + super().__init__(device, api, gateway_id) + self._refresh(device, write_ha=False) @property def extra_state_attributes(self) -> dict[str, str] | None: @@ -90,11 +89,10 @@ class TradfriCover(TradfriBaseDevice, CoverEntity): """Return if the cover is closed or not.""" return self.current_cover_position == 0 - def _refresh(self, device: Command) -> None: + def _refresh(self, device: Command, write_ha: bool = True) -> None: """Refresh the cover data.""" - super()._refresh(device) - self._device = device - # Caching of BlindControl and cover object + self._device = device self._device_control = device.blind_control self._device_data = device.blind_control.blinds[0] + super()._refresh(device, write_ha=write_ha) diff --git a/homeassistant/components/tradfri/fan.py b/homeassistant/components/tradfri/fan.py index b2f8641addb..d1b5de8b7cc 100644 --- a/homeassistant/components/tradfri/fan.py +++ b/homeassistant/components/tradfri/fan.py @@ -166,10 +166,9 @@ class TradfriAirPurifierFan(TradfriBaseDevice, FanEntity): return await self._api(self._device_control.set_mode(0)) - def _refresh(self, device: Command) -> None: + def _refresh(self, device: Command, write_ha: bool = True) -> None: """Refresh the purifier data.""" - super()._refresh(device) - # Caching of air purifier control and purifier object self._device_control = device.air_purifier_control self._device_data = device.air_purifier_control.air_purifiers[0] + super()._refresh(device, write_ha=write_ha) diff --git a/homeassistant/components/tradfri/light.py b/homeassistant/components/tradfri/light.py index 53309e144ff..5a2da96152f 100644 --- a/homeassistant/components/tradfri/light.py +++ b/homeassistant/components/tradfri/light.py @@ -73,7 +73,7 @@ class TradfriGroup(TradfriBaseClass, LightEntity): self._attr_unique_id = f"group-{gateway_id}-{device.id}" self._attr_should_poll = True - self._refresh(device) + self._refresh(device, write_ha=False) async def async_update(self) -> None: """Fetch new state data for the group. @@ -134,8 +134,7 @@ class TradfriLight(TradfriBaseDevice, LightEntity): if device.light_control.can_set_temp: _features |= SUPPORT_COLOR_TEMP self._attr_supported_features = _features - - self._refresh(device) + self._refresh(device, write_ha=False) if self._device_control: self._attr_min_mireds = self._device_control.min_mireds self._attr_max_mireds = self._device_control.max_mireds @@ -270,10 +269,9 @@ class TradfriLight(TradfriBaseDevice, LightEntity): if command is not None: await self._api(command) - def _refresh(self, device: Command) -> None: + def _refresh(self, device: Command, write_ha: bool = True) -> None: """Refresh the light data.""" - super()._refresh(device) - # Caching of LightControl and light object self._device_control = device.light_control self._device_data = device.light_control.lights[0] + super()._refresh(device, write_ha=write_ha) diff --git a/homeassistant/components/tradfri/switch.py b/homeassistant/components/tradfri/switch.py index b7051989265..d308e78c3d1 100644 --- a/homeassistant/components/tradfri/switch.py +++ b/homeassistant/components/tradfri/switch.py @@ -46,13 +46,12 @@ class TradfriSwitch(TradfriBaseDevice, SwitchEntity): super().__init__(device, api, gateway_id) self._attr_unique_id = f"{gateway_id}-{device.id}" - def _refresh(self, device: Command) -> None: + def _refresh(self, device: Command, write_ha: bool = True) -> None: """Refresh the switch data.""" - super()._refresh(device) - # Caching of switch control and switch object self._device_control = device.socket_control self._device_data = device.socket_control.sockets[0] + super()._refresh(device, write_ha=write_ha) @property def is_on(self) -> bool: