Skip to content

Commit

Permalink
split features and move battery to base entity
Browse files Browse the repository at this point in the history
  • Loading branch information
mib1185 authored and hthiery committed Jan 15, 2024
1 parent 8b4f020 commit cebbc58
Show file tree
Hide file tree
Showing 19 changed files with 150 additions and 81 deletions.
5 changes: 5 additions & 0 deletions pyfritzhome/devicetypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from .fritzhomedevicealarm import FritzhomeDeviceAlarm
from .fritzhomedevicebutton import FritzhomeDeviceButton
from .fritzhomedevicehumidity import FritzhomeDeviceHumidity
from .fritzhomedevicelevel import FritzhomeDeviceLevel
from .fritzhomedevicepowermeter import FritzhomeDevicePowermeter
from .fritzhomedevicerepeater import FritzhomeDeviceRepeater
from .fritzhomedeviceswitch import FritzhomeDeviceSwitch
Expand All @@ -11,9 +13,12 @@
from .fritzhomedeviceblind import FritzhomeDeviceBlind
from .fritzhometemplate import FritzhomeTemplate


__all__ = (
"FritzhomeDeviceAlarm",
"FritzhomeDeviceButton",
"FritzhomeDeviceHumidity",
"FritzhomeDeviceLevel",
"FritzhomeDevicePowermeter",
"FritzhomeDeviceRepeater",
"FritzhomeDeviceSwitch",
Expand Down
1 change: 1 addition & 0 deletions pyfritzhome/devicetypes/fritzhomedevicealarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def has_alarm(self):
return self._has_feature(FritzhomeDeviceFeatures.ALARM)

def _update_alarm_from_node(self, node):
_LOGGER.debug("update alert device")
val = node.find("alert")
try:
self.alert_state = self.get_node_value_as_int_as_bool(val, "state")
Expand Down
15 changes: 15 additions & 0 deletions pyfritzhome/devicetypes/fritzhomedevicebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
class FritzhomeDeviceBase(FritzhomeEntityBase):
"""The Fritzhome Device class."""

battery_level = None
battery_low = None
identifier = None
is_group = None
fw_version = None
group_members = None
manufacturer = None
productname = None
present = None
tx_busy = None

def __repr__(self):
"""Return a string."""
Expand All @@ -37,6 +40,7 @@ def update(self):
self._fritz.update_devices()

def _update_from_node(self, node):
_LOGGER.debug("update base device")
super()._update_from_node(node)
self.ain = node.attrib["identifier"]
self.identifier = node.attrib["id"]
Expand All @@ -51,6 +55,17 @@ def _update_from_node(self, node):
if self.is_group:
self.group_members = str(groupinfo.findtext("members")).split(",")

try:
self.tx_busy = self.get_node_value_as_int_as_bool(node, "txbusy")
except Exception:
pass

try:
self.battery_low = self.get_node_value_as_int_as_bool(node, "batterylow")
self.battery_level = int(self.get_node_value_as_int(node, "battery"))
except Exception:
pass

# General
def get_present(self):
"""Check if the device is present."""
Expand Down
35 changes: 2 additions & 33 deletions pyfritzhome/devicetypes/fritzhomedeviceblind.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
class FritzhomeDeviceBlind(FritzhomeDeviceBase):
"""The Fritzhome Device class."""

tx_busy = None
endpositionsset = None
level = None
levelpercentage = None

def _update_from_node(self, node):
super()._update_from_node(node)
Expand All @@ -25,49 +22,21 @@ def _update_from_node(self, node):
if self.has_blind:
self._update_blind_from_node(node)

# Button
# Blind
@property
def has_blind(self):
"""Check if the device has blind function."""
return self._has_feature(FritzhomeDeviceFeatures.BLIND)

def _update_blind_from_node(self, node):
# print("update from blind")
try:
self.tx_busy = self.get_node_value_as_int_as_bool(node, "txbusy")
except Exception:
pass
_LOGGER.debug("update blind device")
blind_element = node.find("blind")
try:
self.endpositionsset = self.get_node_value_as_int_as_bool(
blind_element, "endpositionsset"
)
except Exception:
pass
levelcontrol_element = node.find("levelcontrol")
try:
self.level = self.get_node_value_as_int(levelcontrol_element, "level")
self.levelpercentage = self.get_node_value_as_int(
levelcontrol_element, "levelpercentage"
)
except Exception:
pass

def get_level(self):
"""Get the blind level."""
return self.level

def get_level_percentage(self):
"""Get the blind level in percentage."""
return self.levelpercentage

def set_level(self, level):
"""Set the blind level."""
self._fritz.set_level(self.ain, level)

def set_level_percentage(self, levelpercentage):
"""Set the blind level in percentage."""
self._fritz.set_level_percentage(self.ain, levelpercentage)

def set_blind_open(self):
"""Open the blind."""
Expand Down
8 changes: 1 addition & 7 deletions pyfritzhome/devicetypes/fritzhomedevicebutton.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,13 @@ def has_button(self):
return self._has_feature(FritzhomeDeviceFeatures.BUTTON)

def _update_button_from_node(self, node):
_LOGGER.debug("update button device")
self.buttons = {}

for element in node.findall("button"):
button = FritzhomeButton(element)
self.buttons[button.ain] = button

try:
self.tx_busy = self.get_node_value_as_int_as_bool(node, "txbusy")
self.battery_low = self.get_node_value_as_int_as_bool(node, "batterylow")
self.battery_level = int(self.get_node_value_as_int(node, "battery"))
except Exception:
pass

def get_button_by_ain(self, ain):
"""Return the button by AIN."""
return self.buttons[ain]
Expand Down
39 changes: 39 additions & 0 deletions pyfritzhome/devicetypes/fritzhomedevicehumidity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""The humidity device class."""
# -*- coding: utf-8 -*-

import logging

from .fritzhomedevicebase import FritzhomeDeviceBase
from .fritzhomedevicefeatures import FritzhomeDeviceFeatures

_LOGGER = logging.getLogger(__name__)


class FritzhomeDeviceHumidity(FritzhomeDeviceBase):
"""The Fritzhome Device class."""

rel_humidity = None

def _update_from_node(self, node):
super()._update_from_node(node)
if self.present is False:
return

if self.has_humidity_sensor:
self._update_humidity_from_node(node)

# Humidity
@property
def has_humidity_sensor(self):
"""Check if the device has humidity function."""
return self._has_feature(FritzhomeDeviceFeatures.HUMIDITY)

def _update_humidity_from_node(self, node):
_LOGGER.debug("update humidity device")
humidity_element = node.find("humidity")
try:
self.rel_humidity = self.get_node_value_as_int(
humidity_element, "rel_humidity"
)
except ValueError:
pass
57 changes: 57 additions & 0 deletions pyfritzhome/devicetypes/fritzhomedevicelevel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""The level device class."""
# -*- coding: utf-8 -*-

import logging

from .fritzhomedevicebase import FritzhomeDeviceBase
from .fritzhomedevicefeatures import FritzhomeDeviceFeatures

_LOGGER = logging.getLogger(__name__)


class FritzhomeDeviceLevel(FritzhomeDeviceBase):
"""The Fritzhome Device class."""

level = None
levelpercentage = None

def _update_from_node(self, node):
super()._update_from_node(node)
if self.present is False:
return

if self.has_level:
self._update_level_from_node(node)

# Level
@property
def has_level(self):
"""Check if the device has level function."""
return self._has_feature(FritzhomeDeviceFeatures.LEVEL)

def _update_level_from_node(self, node):
_LOGGER.debug("update level device")
levelcontrol_element = node.find("levelcontrol")
try:
self.level = self.get_node_value_as_int(levelcontrol_element, "level")
self.levelpercentage = self.get_node_value_as_int(
levelcontrol_element, "levelpercentage"
)
except Exception:
pass

def get_level(self):
"""Get the level."""
return self.level

def get_level_percentage(self):
"""Get the level in percentage."""
return self.levelpercentage

def set_level(self, level):
"""Set the level."""
self._fritz.set_level(self.ain, level)

def set_level_percentage(self, levelpercentage):
"""Set the level in percentage."""
self._fritz.set_level_percentage(self.ain, levelpercentage)
26 changes: 1 addition & 25 deletions pyfritzhome/devicetypes/fritzhomedevicelightbulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class FritzhomeDeviceLightBulb(FritzhomeDeviceBase):
"""The Fritzhome Device class."""

state = None
level = None
hue = None
saturation = None
unmapped_hue = None
Expand All @@ -36,33 +35,20 @@ def has_lightbulb(self):
"""Check if the device has LightBulb function."""
return self._has_feature(FritzhomeDeviceFeatures.LIGHTBULB)

@property
def has_level(self):
"""Check if the device has LightBulb function."""
return self._has_feature(FritzhomeDeviceFeatures.LEVEL)

@property
def has_color(self):
"""Check if the device has LightBulb function."""
return self._has_feature(FritzhomeDeviceFeatures.COLOR)

def _update_lightbulb_from_node(self, node):
_LOGGER.debug("update light bulb device")
state_element = node.find("simpleonoff")
try:
self.state = self.get_node_value_as_int_as_bool(state_element, "state")

except ValueError:
pass

if self.has_level:
level_element = node.find("levelcontrol")
try:
self.level = self.get_node_value_as_int(level_element, "level")

self.level_percentage = int(self.level / 2.55)
except ValueError:
pass

if self.has_color:
colorcontrol_element = node.find("colorcontrol")
try:
Expand Down Expand Up @@ -120,16 +106,6 @@ def set_state_toggle(self):
self.state = True
self._fritz.set_state_toggle(self.ain)

def set_level(self, level):
"""Set level."""
if self.has_level:
self._fritz.set_level(self.ain, level)

def set_level_percentage(self, level):
"""Set HSV color in percent."""
if self.has_level:
self._fritz.set_level_percentage(self.ain, level)

def get_colors(self):
"""Get the supported colors."""
if self.has_color:
Expand Down
1 change: 1 addition & 0 deletions pyfritzhome/devicetypes/fritzhomedevicepowermeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def has_powermeter(self):
return self._has_feature(FritzhomeDeviceFeatures.POWER_METER)

def _update_powermeter_from_node(self, node):
_LOGGER.debug("update powermeter device")
val = node.find("powermeter")
self.power = int(val.findtext("power"))
self.energy = int(val.findtext("energy"))
Expand Down
6 changes: 4 additions & 2 deletions pyfritzhome/devicetypes/fritzhomedeviceswitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ def has_switch(self):
if self._has_feature(FritzhomeDeviceFeatures.SWITCH):
# for AVM plugs like FRITZ!DECT 200 and FRITZ!DECT 210
return True
if (self._has_feature(FritzhomeDeviceFeatures.SWITCHABLE)
and not self._has_feature(FritzhomeDeviceFeatures.LIGHTBULB)):
if self._has_feature(
FritzhomeDeviceFeatures.SWITCHABLE
) and not self._has_feature(FritzhomeDeviceFeatures.LIGHTBULB):
# for HAN-FUN plugs
return True
return False

def _update_switch_from_node(self, node):
_LOGGER.debug("update switch device")
if self._has_feature(FritzhomeDeviceFeatures.SWITCH):
val = node.find("switch")
try:
Expand Down
11 changes: 1 addition & 10 deletions pyfritzhome/devicetypes/fritzhomedevicetemperature.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class FritzhomeDeviceTemperature(FritzhomeDeviceBase):

offset = None
temperature = None
rel_humidity = None

def _update_from_node(self, node):
super()._update_from_node(node)
Expand All @@ -31,6 +30,7 @@ def has_temperature_sensor(self):
return self._has_feature(FritzhomeDeviceFeatures.TEMPERATURE)

def _update_temperature_from_node(self, node):
_LOGGER.debug("update temperature device")
temperature_element = node.find("temperature")
try:
self.offset = (
Expand All @@ -45,12 +45,3 @@ def _update_temperature_from_node(self, node):
)
except ValueError:
pass

humidity_element = node.find("humidity")
if humidity_element is not None:
try:
self.rel_humidity = self.get_node_value_as_int(
humidity_element, "rel_humidity"
)
except ValueError:
pass
4 changes: 2 additions & 2 deletions pyfritzhome/devicetypes/fritzhomedevicethermostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class FritzhomeDeviceThermostat(FritzhomeDeviceBase):
device_lock = None
lock = None
error_code = None
battery_low = None
battery_level = None
window_open = None
window_open_endtime = None
summer_active = None
Expand All @@ -44,6 +42,7 @@ def has_thermostat(self):
return self._has_feature(FritzhomeDeviceFeatures.THERMOSTAT)

def _update_hkr_from_node(self, node):
_LOGGER.debug("update thermostat device")
hkr_element = node.find("hkr")

try:
Expand All @@ -62,6 +61,7 @@ def _update_hkr_from_node(self, node):
)
self.lock = self.get_node_value_as_int_as_bool(hkr_element, "lock")
self.error_code = self.get_node_value_as_int(hkr_element, "errorcode")
# keep battery values as fallback for Fritz!OS < 7.08
self.battery_low = self.get_node_value_as_int_as_bool(
hkr_element, "batterylow"
)
Expand Down
Loading

0 comments on commit cebbc58

Please sign in to comment.