From 3f5f8fcdc821177cf8b805bc339ae714dbf832ab Mon Sep 17 00:00:00 2001 From: Adam Kingsley Date: Wed, 16 Oct 2024 13:06:45 +0200 Subject: [PATCH 1/2] Refactor function to device mapping class --- src/abbfreeathome/freeathome.py | 59 +++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/src/abbfreeathome/freeathome.py b/src/abbfreeathome/freeathome.py index 6749899..32e24f7 100644 --- a/src/abbfreeathome/freeathome.py +++ b/src/abbfreeathome/freeathome.py @@ -17,11 +17,16 @@ class FreeAtHome: _devices: dict = {} def __init__( - self, api: FreeAtHomeApi, interfaces: list[Interface] | None = None + self, + api: FreeAtHomeApi, + interfaces: list[Interface] | None = None, + device_classes: list[Base] | None = None, ) -> None: """Initialize the FreeAtHome class.""" self.api: FreeAtHomeApi = api + self._interfaces: list[Interface] = interfaces + self._device_classes: list[Base] = device_classes async def get_config(self, refresh: bool = False) -> dict: """Get the Free@Home Configuration.""" @@ -46,7 +51,7 @@ async def get_devices_by_function(self, function: Function) -> list[dict]: and int(_channel.get("functionID"), 16) == function.value ): _channel_name = _channel.get("displayName") - if _channel_name == "Ⓐ" or _channel_name is None: + if _channel_name in ["Ⓐ", "ⓑ"] or _channel_name is None: _channel_name = _device.get("displayName") _devices.append( @@ -114,22 +119,10 @@ def get_device_by_class(self, device_class: Base) -> list[Base]: async def load_devices(self): """Load all of the devices into the devices object.""" - - # SwitchActuator - await self._load_devices_by_function( - Function.FID_SWITCH_ACTUATOR, SwitchActuator - ) - - # Trigger - await self._load_devices_by_function(Function.FID_TRIGGER, Trigger) - - # MovementDetector - await self._load_devices_by_function( - Function.FID_MOVEMENT_DETECTOR, MovementDetector - ) - - # SwitchSensor - await self._load_devices_by_function(Function.FID_SWITCH_SENSOR, SwitchSensor) + for _mapping in self._get_function_to_device_mapping(): + await self._load_devices_by_function( + _mapping.get("function"), _mapping.get("device_class") + ) async def _load_devices_by_function(self, function: Function, device_class: Base): _devices = await self.get_devices_by_function(function) @@ -166,3 +159,33 @@ async def update_device(self, data: dict): _device.update_device(_datapoint_key, _datapoint_value) except KeyError: continue + + def _get_function_to_device_mapping(self) -> list[dict[str, Function | Base]]: + _function_to_device_mapping = [ + { + "function": Function.FID_SWITCH_ACTUATOR, + "device_class": SwitchActuator, + }, + { + "function": Function.FID_SWITCH_SENSOR, + "device_class": SwitchSensor, + }, + { + "function": Function.FID_TRIGGER, + "device_class": Trigger, + }, + { + "function": Function.FID_MOVEMENT_DETECTOR, + "device_class": MovementDetector, + }, + ] + + return ( + _function_to_device_mapping + if not self._device_classes + else [ + _mapping + for _mapping in _function_to_device_mapping + if _mapping.get("device_class") in self._device_classes + ] + ) From 382989e1f6b0873b1f933bc0feffeb8c3bff0d0c Mon Sep 17 00:00:00 2001 From: Adam Kingsley Date: Wed, 16 Oct 2024 13:13:48 +0200 Subject: [PATCH 2/2] Fix return type of switch sensor state --- src/abbfreeathome/devices/switch_sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/abbfreeathome/devices/switch_sensor.py b/src/abbfreeathome/devices/switch_sensor.py index 665482a..45e0e81 100644 --- a/src/abbfreeathome/devices/switch_sensor.py +++ b/src/abbfreeathome/devices/switch_sensor.py @@ -43,7 +43,7 @@ def __init__( self._refresh_state_from_outputs() @property - def state(self) -> float: + def state(self) -> bool: """Get the switch state.""" return self._state