From 960138846d8604e6cd56b8941c116bef3ad727bc Mon Sep 17 00:00:00 2001 From: mib1185 Date: Sat, 13 Jan 2024 11:10:07 +0000 Subject: [PATCH] add device_and_unit_id property --- .../devicetypes/fritzhomeentitybase.py | 11 ++++++++++ tests/test_fritzhomedevicebase.py | 22 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/pyfritzhome/devicetypes/fritzhomeentitybase.py b/pyfritzhome/devicetypes/fritzhomeentitybase.py index 274b5d1..bcd2cf8 100644 --- a/pyfritzhome/devicetypes/fritzhomeentitybase.py +++ b/pyfritzhome/devicetypes/fritzhomeentitybase.py @@ -43,6 +43,17 @@ def _update_from_node(self, node): self.name = node.findtext("name").strip() + @property + def device_and_unit_id(self): + """Get the device and possible unit id.""" + if self.ain.startswith("tmp") or self.ain.startswith("grp"): + return (self.ain, None) + elif self.ain.startswith("Z") and len(self.ain) == 19: + return (self.ain[0:17], self.ain[17:]) + elif "-" in self.ain: + return tuple(self.ain.split("-")) + return (self.ain, None) + # XML Helpers def get_node_value(self, elem, node): diff --git a/tests/test_fritzhomedevicebase.py b/tests/test_fritzhomedevicebase.py index 0e3241c..493a581 100644 --- a/tests/test_fritzhomedevicebase.py +++ b/tests/test_fritzhomedevicebase.py @@ -4,6 +4,7 @@ from unittest.mock import MagicMock from pyfritzhome import Fritzhome +from pyfritzhome.devicetypes.fritzhomeentitybase import FritzhomeEntityBase from .helper import Helper @@ -89,3 +90,24 @@ def test_get_device_present(self): "http://10.0.0.1/webservices/homeautoswitch.lua", {"ain": "08761 0000434", "switchcmd": "getswitchpresent", "sid": None}, ) + + def test_device_and_unit_id(self): + device = FritzhomeEntityBase() + + device.ain = "11630 0114733" + assert device.device_and_unit_id == ("11630 0114733", None) + + device.ain = "11630 0114733-1" + assert device.device_and_unit_id == ("11630 0114733", "1") + + device.ain = "ZA4C1380C30E07AB1" + assert device.device_and_unit_id == ("ZA4C1380C30E07AB1", None) + + device.ain = "ZA4C1380C30E07AB101" + assert device.device_and_unit_id == ("ZA4C1380C30E07AB1", "01") + + device.ain = "grp303E4F-3F7D9BE07" + assert device.device_and_unit_id == ("grp303E4F-3F7D9BE07", None) + + device.ain = "tmp816271-3F6EB615E" + assert device.device_and_unit_id == ("tmp816271-3F6EB615E", None)