Fix push threading issues in Fibaro (#195249)

This commit is contained in:
rappenze
2026-11-29 09:19:28 +01:00
committed by GitHub
parent d6e48fe1ce
commit 5a00a4db65
11 changed files with 73 additions and 43 deletions
@@ -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()
+4 -3
View File
@@ -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
+5 -5
View File
@@ -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()
+13 -3
View File
@@ -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
+3 -6
View File
@@ -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
)
+3 -4
View File
@@ -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
+3 -6
View File
@@ -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
+5 -5
View File
@@ -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],
+3 -6
View File
@@ -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
+1 -1
View File
@@ -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
+30
View File
@@ -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,