Skip to content

Commit

Permalink
add device_and_unit_id property
Browse files Browse the repository at this point in the history
  • Loading branch information
mib1185 committed Jan 13, 2024
1 parent 53afd61 commit 8e745fc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pyfritzhome/devicetypes/fritzhomeentitybase.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ 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)

if self.ain.startswith("Z"):
if len(self.ain) == 19:
return (self.ain[0:17], self.ain[17:])
return (self.ain, None)

if "-" in self.ain:
return tuple(self.ain.split("-"))
return (self.ain, None)

# XML Helpers

def get_node_value(self, elem, node):
Expand Down
22 changes: 22 additions & 0 deletions tests/test_fritzhomedevicebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest.mock import MagicMock

from pyfritzhome import Fritzhome
from pyfritzhome.devicetypes.fritzhomeentitybase import FritzhomeEntityBase

from .helper import Helper

Expand Down Expand Up @@ -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)

0 comments on commit 8e745fc

Please sign in to comment.