From 5a00a4db65b572cfb8f129ecd6f27d189183af7a Mon Sep 17 00:00:00 2001 From: rappenze Date: Sun, 29 Nov 2026 09:19:28 +0100 Subject: [PATCH] Fix push threading issues in Fibaro (#195249) --- .../components/fibaro/binary_sensor.py | 7 ++--- homeassistant/components/fibaro/climate.py | 7 +++-- homeassistant/components/fibaro/cover.py | 10 +++---- homeassistant/components/fibaro/entity.py | 16 ++++++++-- homeassistant/components/fibaro/event.py | 9 ++---- homeassistant/components/fibaro/light.py | 7 ++--- homeassistant/components/fibaro/lock.py | 9 ++---- homeassistant/components/fibaro/sensor.py | 10 +++---- homeassistant/components/fibaro/switch.py | 9 ++---- tests/components/fibaro/conftest.py | 2 +- tests/components/fibaro/test_switch.py | 30 +++++++++++++++++++ 11 files changed, 73 insertions(+), 43 deletions(-) diff --git a/homeassistant/components/fibaro/binary_sensor.py b/homeassistant/components/fibaro/binary_sensor.py index ea285b9f1e1a..15521c03a7ab 100644 --- a/homeassistant/components/fibaro/binary_sensor.py +++ b/homeassistant/components/fibaro/binary_sensor.py @@ -47,8 +47,7 @@ async def async_setup_entry( [ FibaroBinarySensor(device, controller) for device in controller.fibaro_devices[Platform.BINARY_SENSOR] - ], - True, + ] ) @@ -79,9 +78,9 @@ class FibaroBinarySensor(FibaroEntity, BinarySensorEntity): return {**super().extra_state_attributes, **self._own_extra_state_attributes} @override - def update(self) -> None: + def _async_update_data(self) -> None: """Get the latest data and update the state.""" - super().update() + super()._async_update_data() if self._fibaro_sensor_type == "com.fibaro.accelerometer": # Accelerator sensors have values for the three axis x, y and z moving_values = self._get_moving_values() diff --git a/homeassistant/components/fibaro/climate.py b/homeassistant/components/fibaro/climate.py index dd803c655c91..4f9f99897ef3 100644 --- a/homeassistant/components/fibaro/climate.py +++ b/homeassistant/components/fibaro/climate.py @@ -125,8 +125,7 @@ async def async_setup_entry( [ FibaroThermostat(device, controller) for device in controller.fibaro_devices[Platform.CLIMATE] - ], - True, + ] ) @@ -264,7 +263,9 @@ class FibaroThermostat(FibaroEntity, ClimateEntity): siblings = self.controller.get_siblings(self.fibaro_device) for device in siblings: if device != self.fibaro_device: - self.controller.register(device.fibaro_id, self._update_callback) + self.async_on_remove( + self.controller.register(device.fibaro_id, self._update_callback) + ) @property @override diff --git a/homeassistant/components/fibaro/cover.py b/homeassistant/components/fibaro/cover.py index 1642efe7c088..6717d6047cca 100644 --- a/homeassistant/components/fibaro/cover.py +++ b/homeassistant/components/fibaro/cover.py @@ -32,7 +32,7 @@ async def async_setup_entry( entities.append(PositionableFibaroCover(device, controller)) else: entities.append(FibaroCover(device, controller)) - async_add_entities(entities, True) + async_add_entities(entities) class PositionableFibaroCover(FibaroEntity, CoverEntity): @@ -61,9 +61,9 @@ class PositionableFibaroCover(FibaroEntity, CoverEntity): return position @override - def update(self) -> None: + def _async_update_data(self) -> None: """Update the state.""" - super().update() + super()._async_update_data() self._attr_current_cover_position = self.bound(self.level) if self._has_tilt: @@ -139,9 +139,9 @@ class FibaroCover(FibaroEntity, CoverEntity): self._attr_supported_features |= CoverEntityFeature.STOP_TILT @override - def update(self) -> None: + def _async_update_data(self) -> None: """Update the state.""" - super().update() + super()._async_update_data() device_state = self.fibaro_device.state.str_value(default="").lower() diff --git a/homeassistant/components/fibaro/entity.py b/homeassistant/components/fibaro/entity.py index d6100ebf4ef3..f4e8bbf02996 100644 --- a/homeassistant/components/fibaro/entity.py +++ b/homeassistant/components/fibaro/entity.py @@ -7,6 +7,7 @@ from typing import Any, override from pyfibaro.fibaro_device import DeviceModel from homeassistant.const import ATTR_ARMED, ATTR_BATTERY_LEVEL +from homeassistant.core import callback from homeassistant.helpers.entity import Entity from homeassistant.util import slugify @@ -56,9 +57,18 @@ class FibaroEntity(Entity): ) ) + # Calculate initial state + self._async_update_data() + def _update_callback(self, fibaro_device: DeviceModel) -> None: - """Update the state.""" - self.schedule_update_ha_state(True) + """Update and write the state by delegating to the event loop.""" + + @callback + def update_and_write() -> None: + self._async_update_data() + self.async_write_ha_state() + + self.hass.loop.call_soon_threadsafe(update_and_write) @property def level(self) -> int | None: @@ -128,7 +138,7 @@ class FibaroEntity(Entity): return attr - def update(self) -> None: + def _async_update_data(self) -> None: """Update the available state of the entity.""" if self.fibaro_device.has_dead: self._attr_available = not self.fibaro_device.dead diff --git a/homeassistant/components/fibaro/event.py b/homeassistant/components/fibaro/event.py index 36a0ea80d71f..df83de530eb3 100644 --- a/homeassistant/components/fibaro/event.py +++ b/homeassistant/components/fibaro/event.py @@ -24,12 +24,9 @@ async def async_setup_entry( # Each scene event represents a button on a device async_add_entities( - ( - FibaroEventEntity(device, scene_event, controller) - for device in controller.fibaro_devices[Platform.EVENT] - for scene_event in device.central_scene_event - ), - True, + FibaroEventEntity(device, scene_event, controller) + for device in controller.fibaro_devices[Platform.EVENT] + for scene_event in device.central_scene_event ) diff --git a/homeassistant/components/fibaro/light.py b/homeassistant/components/fibaro/light.py index c0f513b69a14..ffc3ffecd146 100644 --- a/homeassistant/components/fibaro/light.py +++ b/homeassistant/components/fibaro/light.py @@ -54,8 +54,7 @@ async def async_setup_entry( [ FibaroLight(device, controller) for device in controller.fibaro_devices[Platform.LIGHT] - ], - True, + ] ) @@ -135,9 +134,9 @@ class FibaroLight(FibaroEntity, LightEntity): self.call_turn_off() @override - def update(self) -> None: + def _async_update_data(self) -> None: """Update the state.""" - super().update() + super()._async_update_data() # Dimmable and RGB lights can be on based on different # properties, so we need to check here several values diff --git a/homeassistant/components/fibaro/lock.py b/homeassistant/components/fibaro/lock.py index 349914bc4871..ec2cb219de13 100644 --- a/homeassistant/components/fibaro/lock.py +++ b/homeassistant/components/fibaro/lock.py @@ -22,8 +22,7 @@ async def async_setup_entry( [ FibaroLock(device, controller) for device in controller.fibaro_devices[Platform.LOCK] - ], - True, + ] ) @@ -34,16 +33,14 @@ class FibaroLock(FibaroEntity, LockEntity): def lock(self, **kwargs: Any) -> None: """Lock the device.""" self.action("secure") - self._attr_is_locked = True @override def unlock(self, **kwargs: Any) -> None: """Unlock the device.""" self.action("unsecure") # codespell:ignore unsecure - self._attr_is_locked = False @override - def update(self) -> None: + def _async_update_data(self) -> None: """Update device state.""" - super().update() + super()._async_update_data() self._attr_is_locked = self.current_binary_state diff --git a/homeassistant/components/fibaro/sensor.py b/homeassistant/components/fibaro/sensor.py index 95d48823fa11..b685ec56dccf 100644 --- a/homeassistant/components/fibaro/sensor.py +++ b/homeassistant/components/fibaro/sensor.py @@ -130,7 +130,7 @@ async def async_setup_entry( if entity_description.key in device.properties ) - async_add_entities(entities, True) + async_add_entities(entities) class FibaroSensor(FibaroEntity, SensorEntity): @@ -156,9 +156,9 @@ class FibaroSensor(FibaroEntity, SensorEntity): ) @override - def update(self) -> None: + def _async_update_data(self) -> None: """Update the state.""" - super().update() + super()._async_update_data() with suppress(TypeError): self._attr_native_value = self.fibaro_device.value.float_value() @@ -182,9 +182,9 @@ class FibaroAdditionalSensor(FibaroEntity, SensorEntity): self._attr_unique_id = f"{self._attr_unique_id}_{entity_description.key}" @override - def update(self) -> None: + def _async_update_data(self) -> None: """Update the state.""" - super().update() + super()._async_update_data() with suppress(KeyError, ValueError): self._attr_native_value = convert( self.fibaro_device.properties[self.entity_description.key], diff --git a/homeassistant/components/fibaro/switch.py b/homeassistant/components/fibaro/switch.py index 293a31339591..49d9805cb129 100644 --- a/homeassistant/components/fibaro/switch.py +++ b/homeassistant/components/fibaro/switch.py @@ -24,8 +24,7 @@ async def async_setup_entry( [ FibaroSwitch(device, controller) for device in controller.fibaro_devices[Platform.SWITCH] - ], - True, + ] ) @@ -42,16 +41,14 @@ class FibaroSwitch(FibaroEntity, SwitchEntity): def turn_on(self, **kwargs: Any) -> None: """Turn device on.""" self.call_turn_on() - self._attr_is_on = True @override def turn_off(self, **kwargs: Any) -> None: """Turn device off.""" self.call_turn_off() - self._attr_is_on = False @override - def update(self) -> None: + def _async_update_data(self) -> None: """Update device state.""" - super().update() + super()._async_update_data() self._attr_is_on = self.current_binary_state diff --git a/tests/components/fibaro/conftest.py b/tests/components/fibaro/conftest.py index f393db65360b..711dba4f5804 100644 --- a/tests/components/fibaro/conftest.py +++ b/tests/components/fibaro/conftest.py @@ -542,7 +542,7 @@ def mock_switch() -> Mock: } value_mock = Mock() value_mock.has_value = True - value_mock.int_value.return_value = True + value_mock.bool_value.return_value = True switch.value = value_mock return switch diff --git a/tests/components/fibaro/test_switch.py b/tests/components/fibaro/test_switch.py index 89a02e0a61d9..47ed0457c86a 100644 --- a/tests/components/fibaro/test_switch.py +++ b/tests/components/fibaro/test_switch.py @@ -36,6 +36,36 @@ async def test_switch_setup( assert entry.original_name == "Room 1 Test switch" +async def test_switch_state_update( + hass: HomeAssistant, + mock_fibaro_client: Mock, + mock_config_entry: MockConfigEntry, + mock_switch: Mock, + mock_room: Mock, +) -> None: + """Test that the switch state is updated when Fibaro sends a change.""" + + mock_fibaro_client.read_rooms.return_value = [mock_room] + mock_fibaro_client.read_devices.return_value = [mock_switch] + + with patch("homeassistant.components.fibaro.PLATFORMS", [Platform.SWITCH]): + await init_integration(hass, mock_config_entry) + + entity_id = "switch.test_switch_room_1_test_switch" + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "on" + + update_handler = mock_fibaro_client.register_update_handler.call_args.args[0] + mock_switch.value.bool_value.return_value = False + update_handler({"changes": [{"id": mock_switch.fibaro_id, "value": False}]}) + await hass.async_block_till_done() + + state = hass.states.get(entity_id) + assert state is not None + assert state.state == "off" + + async def test_switch_turn_on( hass: HomeAssistant, mock_fibaro_client: Mock,