1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
|
"""devolo Home Control."""
from __future__ import annotations
import threading
from typing import Any
import requests
from requests.adapters import HTTPAdapter
from urllib3 import Retry
from zeroconf import Zeroconf
from . import __version__
from .backend import MESSAGE_TYPES, Mprm
from .devices import Gateway, Zwave
from .helper import (
camel_case_to_snake_case,
get_device_type_from_element_uid,
get_device_uid_from_element_uid,
get_device_uid_from_setting_uid,
get_home_id_from_device_uid,
)
from .mydevolo import Mydevolo
from .properties import (
BinarySensorProperty,
BinarySwitchProperty,
ConsumptionProperty,
HumidityBarProperty,
MultiLevelSensorProperty,
MultiLevelSwitchProperty,
RemoteControlProperty,
SettingsProperty,
)
from .publisher import Publisher, Updater
class HomeControl(Mprm):
"""
Representing object for your Home Control setup. This is more or less the glue between your devolo Home Control Central
Unit, your devices and their properties.
:param gateway_id: Gateway ID (aka serial number), typically found on the label of the device
:param mydevolo_instance: Mydevolo instance for talking to the devolo Cloud
:param zeroconf_instance: Zeroconf instance to be potentially reused
"""
def __init__(self, gateway_id: str, mydevolo_instance: Mydevolo, zeroconf_instance: Zeroconf | None = None) -> None:
"""Initialize communication with your Home Control setup."""
retry = Retry(total=5, backoff_factor=0.1, allowed_methods=("GET", "POST"))
adapter = HTTPAdapter(max_retries=retry)
self._mydevolo = mydevolo_instance
self._session = requests.Session()
self._session.headers.update({"User-Agent": f"devolo_home_control_api/{__version__}"})
self._session.mount("http://", adapter)
self._zeroconf = zeroconf_instance
self.gateway = Gateway(gateway_id, mydevolo_instance)
super().__init__()
self._grouping()
# Create the initial device dict
self.devices: dict[str, Zwave] = {}
self._inspect_devices(self.get_all_devices())
self.device_names = {
f"{device.settings_property['general_device_settings'].name}\\"
f"{device.settings_property['general_device_settings'].zone}": device.uid
for device in self.devices.values()
}
self.gateway.home_id = get_home_id_from_device_uid(next(iter(self.device_names.values())))
self.publisher = Publisher(self.devices.keys())
self.updater = Updater(devices=self.devices, gateway=self.gateway, publisher=self.publisher)
self.updater.on_device_change = self.device_change
threading.Thread(target=self.websocket_connect, name=f"{self.__class__.__name__}.websocket_connect").start()
self.wait_for_websocket_establishment()
@property
def binary_sensor_devices(self) -> list[Zwave]:
"""Get all binary sensor devices."""
return [uid for uid in self.devices.values() if hasattr(uid, "binary_sensor_property")]
@property
def binary_switch_devices(self) -> list[Zwave]:
"""Get all binary switch devices."""
return [uid for uid in self.devices.values() if hasattr(uid, "binary_switch_property")]
@property
def blinds_devices(self) -> list[Zwave]:
"""Get all blinds devices."""
blinds_devices = []
for device in self.multi_level_switch_devices:
blinds_devices.extend(
[
self.devices[device.uid]
for multi_level_switch_property in device.multi_level_switch_property
if multi_level_switch_property.startswith("devolo.Blinds")
]
)
return blinds_devices
@property
def multi_level_sensor_devices(self) -> list[Zwave]:
"""Get all multi level sensor devices."""
return [uid for uid in self.devices.values() if hasattr(uid, "multi_level_sensor_property")]
@property
def multi_level_switch_devices(self) -> list[Zwave]:
"""Get all multi level switch devices. This also includes blinds devices."""
return [uid for uid in self.devices.values() if hasattr(uid, "multi_level_switch_property")]
@property
def remote_control_devices(self) -> list[Zwave]:
"""Get all remote control devices."""
return [uid for uid in self.devices.values() if hasattr(uid, "remote_control_property")]
def device_change(self, device_uids: list[str]) -> tuple[str, str]:
"""
React on new devices or removed devices. As the Z-Wave controller can only be in inclusion or exclusion mode, we
assume, that you cannot add and remove devices at the same time. So if the number of devices increases, there is
a new one and if the number decreases, a device was removed.
:param device_uids: List of UIDs known by the backend
"""
if len(device_uids) > len(self.devices):
devices = [device for device in device_uids if device not in self.devices]
mode = "add"
self._inspect_devices([devices[0]])
self._logger.debug("Device %s added.", devices[0])
else:
devices = [device for device in self.devices if device not in device_uids]
mode = "del"
self.devices.pop(devices[0])
self._logger.debug("Device %s removed.", devices[0])
self.updater.devices = self.devices
return (devices[0], mode)
def on_update(self, message: dict[str, Any]) -> None:
"""
Initialize steps needed to update properties on a new message.
:param message: Message because of which we need to update properties
"""
self.updater.update(message)
def _binary_sensor(self, uid_info: dict[str, Any]) -> None:
"""Process BinarySensor properties."""
device_uid = get_device_uid_from_element_uid(uid_info["UID"])
if not hasattr(self.devices[device_uid], "binary_sensor_property"):
self.devices[device_uid].binary_sensor_property = {}
self._logger.debug("Adding binary sensor property to %s.", device_uid)
self.devices[device_uid].binary_sensor_property[uid_info["UID"]] = BinarySensorProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
state=bool(uid_info["properties"]["state"]),
sensor_type=uid_info["properties"]["sensorType"],
sub_type=uid_info["properties"]["subType"],
)
def _binary_switch(self, uid_info: dict[str, Any]) -> None:
"""Process BinarySwitch properties."""
device_uid = get_device_uid_from_element_uid(uid_info["UID"])
if not hasattr(self.devices[device_uid], "binary_switch_property"):
self.devices[device_uid].binary_switch_property = {}
self._logger.debug("Adding binary switch property to %s.", device_uid)
self.devices[device_uid].binary_switch_property[uid_info["UID"]] = BinarySwitchProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_binary_switch,
state=bool(uid_info["properties"]["state"]),
enabled=uid_info["properties"]["guiEnabled"],
)
def _general_device(self, uid_info: dict[str, Any]) -> None:
"""Process general device setting (gds) properties."""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
self._logger.debug("Adding general device settings to %s.", device_uid)
self.devices[device_uid].settings_property["general_device_settings"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
events_enabled=uid_info["properties"]["settings"]["eventsEnabled"],
name=uid_info["properties"]["settings"]["name"],
zone_id=uid_info["properties"]["settings"]["zoneID"],
icon=uid_info["properties"]["settings"]["icon"],
zones=self.gateway.zones,
)
def _grouping(self) -> None:
"""Get all zones (also called rooms)."""
self.gateway.zones = self.get_all_zones()
def _humidity_bar(self, uid_info: dict[str, Any]) -> None:
"""
Process HumidityBarZone and HumidityBarValue properties.
For whatever reason, the zone and the position within that zone are two different FIs. Nevertheless, it does not make
sense to separate them. So we fake an FI and a sensorType to combine them together into one object.
"""
device_uid = get_device_uid_from_element_uid(uid_info["UID"])
fake_element_uid = f"devolo.HumidityBar:{uid_info['UID'].split(':', 1)[1]}"
if not hasattr(self.devices[device_uid], "humidity_bar_property"):
self.devices[device_uid].humidity_bar_property = {}
if self.devices[device_uid].humidity_bar_property.get(fake_element_uid) is None:
self.devices[device_uid].humidity_bar_property[fake_element_uid] = HumidityBarProperty(
element_uid=fake_element_uid,
tz=self.gateway.timezone,
sensorType="humidityBar",
)
if uid_info["properties"]["sensorType"] == "humidityBarZone":
self._logger.debug("Adding humidity bar zone property to %s.", device_uid)
self.devices[device_uid].humidity_bar_property[fake_element_uid].zone = uid_info["properties"]["value"]
elif uid_info["properties"]["sensorType"] == "humidityBarPos":
self._logger.debug("Adding humidity bar position property to %s.", device_uid)
self.devices[device_uid].humidity_bar_property[fake_element_uid].value = uid_info["properties"]["value"]
def _inspect_devices(self, devices: list[str]) -> None:
"""Inspect device properties of given list of devices."""
devices_properties = self.get_data_from_uid_list(devices)
for device_properties in devices_properties:
properties = device_properties["properties"]
self.devices[device_properties["UID"]] = Zwave(mydevolo_instance=self._mydevolo, **properties)
self.devices[device_properties["UID"]].settings_property = {}
threading.Thread(
target=self.devices[device_properties["UID"]].get_zwave_info,
name=f"{self.__class__.__name__}.{self.devices[device_properties['UID']].uid}",
).start()
# List comprehension gets the list of uids from every device
nested_uids_lists = [
(uid["properties"].get("settingUIDs") + uid["properties"]["elementUIDs"]) for uid in devices_properties
]
# List comprehension gets all uids into one list to make one big call against the mPRM
uid_list = [uid for sublist in nested_uids_lists for uid in sublist]
device_properties_list = self.get_data_from_uid_list(uid_list)
for uid_info in device_properties_list:
message_type = MESSAGE_TYPES.get(get_device_type_from_element_uid(uid_info["UID"]), "_unknown")
getattr(self, message_type)(uid_info)
try:
uid = self.devices[get_device_uid_from_element_uid(uid_info["UID"])]
except KeyError:
uid = self.devices[get_device_uid_from_setting_uid(uid_info["UID"])]
uid.pending_operations = uid.pending_operations or bool(uid_info["properties"].get("pendingOperations"))
# Last activity messages sometimes arrive before a device was initialized and therefore need to be handled afterwards.
for uid_info in device_properties_list:
if uid_info["UID"].startswith("devolo.LastActivity"):
self._last_activity(uid_info)
def _automatic_calibration(self, uid_info: dict[str, Any]) -> None:
"""Process automatic calibration (acs) properties."""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
self._logger.debug("Adding automatic calibration setting to %s.", device_uid)
self.devices[device_uid].settings_property["automatic_calibration"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
calibration_status=bool(uid_info["properties"]["calibrationStatus"]),
)
def _binary_sync(self, uid_info: dict[str, Any]) -> None:
"""
Process binary sync setting (bss) properties. Currently only the direction of a shutter in known to be a binary sync
setting property, so it is named nicely.
"""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
self._logger.debug("Adding binary sync setting to %s.", device_uid)
self.devices[device_uid].settings_property["movement_direction"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
inverted=uid_info["properties"]["value"],
)
def _binary_async(self, uid_info: dict[str, Any]) -> None:
"""Process binary async setting (bas) properties."""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
self._logger.debug("Adding binary async settings to %s.", device_uid)
settings_property = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
value=uid_info["properties"]["value"],
)
# The siren needs to be handled differently, as otherwise their binary async setting will not be named nicely
if self.devices[device_uid].device_model_uid == "devolo.model.Siren":
self.devices[device_uid].settings_property["muted"] = settings_property
# As some devices have multiple binary async settings, we use the settings UID split after a '#' as key
else:
key = camel_case_to_snake_case(uid_info["UID"].split("#")[-1])
self.devices[device_uid].settings_property[key] = settings_property
def _last_activity(self, uid_info: dict[str, Any]) -> None:
"""
Process last activity properties. Those don't go into an own property but will be appended to a parent property.
This parent property is found by string replacement.
"""
device_uid = get_device_uid_from_element_uid(uid_info["UID"])
parent_element_uid = uid_info["UID"].replace("LastActivity", "BinarySensor")
try:
self.devices[device_uid].binary_sensor_property[parent_element_uid].last_activity = uid_info["properties"][
"lastActivityTime"
]
except AttributeError:
parent_element_uid = uid_info["UID"].replace("LastActivity", "SirenMultiLevelSwitch")
self.devices[device_uid].multi_level_switch_property[parent_element_uid].last_activity = uid_info["properties"][
"lastActivityTime"
]
def _led(self, uid_info: dict[str, Any]) -> None:
"""Process LED information setting (lis) and visual feedback setting (vfs) properties."""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
self._logger.debug("Adding led settings to %s.", device_uid)
try:
led_setting = uid_info["properties"]["led"]
except KeyError:
led_setting = uid_info["properties"]["feedback"]
self.devices[device_uid].settings_property["led"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
led_setting=led_setting,
)
def _meter(self, uid_info: dict[str, Any]) -> None:
"""Process meter properties."""
device_uid = get_device_uid_from_element_uid(uid_info["UID"])
if not hasattr(self.devices[device_uid], "consumption_property"):
self.devices[device_uid].consumption_property = {}
self._logger.debug("Adding consumption property to %s.", device_uid)
self.devices[device_uid].consumption_property[uid_info["UID"]] = ConsumptionProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
current=uid_info["properties"]["currentValue"],
total=uid_info["properties"]["totalValue"],
total_since=uid_info["properties"]["sinceTime"],
)
def _multilevel_async(self, uid_info: dict[str, Any]) -> None:
"""Process multilevel async setting (mas) properties."""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
try:
name = camel_case_to_snake_case(uid_info["properties"]["itemId"])
# The Metering Plug has an multilevel async setting without an ID
except TypeError:
if self.devices[device_uid].device_model_uid == "devolo.model.Wall:Plug:Switch:and:Meter":
name = "flash_mode"
else:
raise
self._logger.debug("Adding %s setting to %s.", name, device_uid)
self.devices[device_uid].settings_property[name] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
value=uid_info["properties"]["value"],
)
def _multilevel_sync(self, uid_info: dict[str, Any]) -> None:
"""Process multilevel sync setting (mss) properties."""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
# The siren needs to be handled differently, as otherwise their multilevel sync setting will not be named nicely.
if self.devices[device_uid].device_model_uid == "devolo.model.Siren":
self._logger.debug("Adding tone settings to %s.", device_uid)
self.devices[device_uid].settings_property["tone"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
tone=uid_info["properties"]["value"],
)
# The shutter needs to be handled differently, as otherwise their multilevel sync setting will not be named nicely.
elif self.devices[device_uid].device_model_uid in ("devolo.model.OldShutter", "devolo.model.Shutter"):
self._logger.debug("Adding shutter duration settings to %s.", device_uid)
self.devices[device_uid].settings_property["shutter_duration"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
shutter_duration=uid_info["properties"]["value"],
)
# Other devices are up to now always motion sensors.
else:
self._logger.debug("Adding motion sensitivity settings to %s.", device_uid)
self.devices[device_uid].settings_property["motion_sensitivity"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
motion_sensitivity=uid_info["properties"]["value"],
)
def _multi_level_sensor(self, uid_info: dict[str, Any]) -> None:
"""Process multi level sensor properties."""
device_uid = get_device_uid_from_element_uid(uid_info["UID"])
if not hasattr(self.devices[device_uid], "multi_level_sensor_property"):
self.devices[device_uid].multi_level_sensor_property = {}
self._logger.debug("Adding multi level sensor property %s to %s.", uid_info["UID"], device_uid)
self.devices[device_uid].multi_level_sensor_property[uid_info["UID"]] = MultiLevelSensorProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
value=uid_info["properties"]["value"],
unit=uid_info["properties"]["unit"],
sensor_type=uid_info["properties"]["sensorType"],
)
def _multi_level_switch(self, uid_info: dict[str, Any]) -> None:
"""Process multi level switch properties."""
device_uid = get_device_uid_from_element_uid(uid_info["UID"])
if not hasattr(self.devices[device_uid], "multi_level_switch_property"):
self.devices[device_uid].multi_level_switch_property = {}
self._logger.debug("Adding multi level switch property %s to %s.", uid_info["UID"], device_uid)
self.devices[device_uid].multi_level_switch_property[uid_info["UID"]] = MultiLevelSwitchProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_multi_level_switch,
value=uid_info["properties"]["value"],
switch_type=uid_info["properties"]["switchType"],
max=uid_info["properties"]["max"],
min=uid_info["properties"]["min"],
)
def _parameter(self, uid_info: dict[str, Any]) -> None:
"""Process custom parameter setting (cps) properties."""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
self._logger.debug("Adding parameter settings to %s.", device_uid)
self.devices[device_uid].settings_property["param_changed"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
param_changed=uid_info["properties"]["paramChanged"],
)
def _protection(self, uid_info: dict[str, Any]) -> None:
"""Process protection setting (ps) properties."""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
self._logger.debug("Adding protection settings to %s.", device_uid)
self.devices[device_uid].settings_property["protection"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
local_switching=uid_info["properties"]["localSwitch"],
remote_switching=uid_info["properties"]["remoteSwitch"],
)
def _remote_control(self, uid_info: dict[str, Any]) -> None:
"""Process remote control properties."""
device_uid = get_device_uid_from_element_uid(uid_info["UID"])
self._logger.debug("Adding remote control to %s.", device_uid)
if not hasattr(self.devices[device_uid], "remote_control_property"):
self.devices[device_uid].remote_control_property = {}
self.devices[device_uid].remote_control_property[uid_info["UID"]] = RemoteControlProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_remote_control,
key_count=uid_info["properties"]["keyCount"],
key_pressed=uid_info["properties"]["keyPressed"],
type=uid_info["properties"]["type"],
)
def _switch_type(self, uid_info: dict[str, Any]) -> None:
"""
Process switch type setting (sts) properties. Interestingly, a switch with two buttons reports a switchType of 1 and a
switch with four buttons reports a switchType of 2. This confusing behavior is corrected by doubling the value.
"""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
self._logger.debug("Adding switch type setting to %s.", device_uid)
self.devices[device_uid].settings_property["switch_type"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
value=uid_info["properties"]["switchType"] * 2,
)
def _temperature_report(self, uid_info: dict[str, Any]) -> None:
"""Process temperature report setting (trs) properties."""
device_uid = get_device_uid_from_setting_uid(uid_info["UID"])
self._logger.debug("Adding temperature report settings to %s.", device_uid)
self.devices[device_uid].settings_property["temperature_report"] = SettingsProperty(
element_uid=uid_info["UID"],
tz=self.gateway.timezone,
setter=self.set_setting,
temp_report=uid_info["properties"]["tempReport"],
target_temp_report=uid_info["properties"]["targetTempReport"],
)
def _unknown(self, uid_info: dict[str, Any]) -> None:
"""Ignore unknown properties."""
ignore = ("devolo.SirenBinarySensor", "devolo.SirenMultiLevelSensor", "ss", "mcs")
if not uid_info["UID"].startswith(ignore):
self._logger.debug("Found an unexpected element uid: %s", uid_info["UID"])
|