Skip to content

Commit 4b39b64

Browse files
committed
Corrections after review
1 parent c9459fb commit 4b39b64

File tree

1 file changed

+57
-68
lines changed

1 file changed

+57
-68
lines changed

plugwise/devices.py

Lines changed: 57 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
from __future__ import annotations
44

55
from dataclasses import dataclass
6-
from munch import Munch
76
from typing import Any
87

9-
from .constants import ZONE_THERMOSTATS
8+
from plugwise.constants import ZONE_THERMOSTATS
109

1110

1211
def process_key(data: dict[str, Any], key: str) -> Any | None:
@@ -79,13 +78,13 @@ def update_from_dict(self, data: dict[str, Any]) -> None:
7978

8079
super().update_from_dict(data)
8180
self.binary_sensors.update_from_dict(data)
82-
self.gateway_modes = process_key(data, "gateway_mode")
83-
self.hardware = process_key(data, "gateway_mode")
84-
self.model_id = process_key(data, "gateway_mode")
85-
self.regulation_modes = process_key(data, "gateway_mode")
86-
self.select_gateway_mode = process_key(data, "gateway_mode")
81+
self.gateway_modes = process_key(data, "gateway_modes")
82+
self.hardware = process_key(data, "hardware")
83+
self.model_id = process_key(data, "model_id")
84+
self.regulation_modes = process_key(data, "regulation_modes")
85+
self.select_gateway_mode = process_key(data, "select_gateway_mode")
8786
self.sensors.update_from_dict(data)
88-
self.zigbee_mac_address = process_key(data, "gateway_mode")
87+
self.zigbee_mac_address = process_key(data, "zigbee_mac_address")
8988

9089

9190
@dataclass(kw_only=True)
@@ -109,7 +108,7 @@ class Weather:
109108
outdoor_temperature: float | None = None
110109

111110
def update_from_dict(self, data: dict[str, Any]) -> None:
112-
"""Update this GatewayBinarySensors object with data from a dictionary."""
111+
"""Update this Weather object with data from a dictionary."""
113112

114113
self.outdoor_temperature = process_dict(data, "sensors", "outdoor_temperature")
115114

@@ -118,12 +117,14 @@ def update_from_dict(self, data: dict[str, Any]) -> None:
118117
class SmartEnergyMeter(DeviceBase):
119118
"""DSMR Energy Meter data class."""
120119

120+
available: bool | None
121121
sensors: SmartEnergySensors
122-
available: bool | None = None
123122

124123
def __init__(self) -> None:
125124
"""Init SmartEnergyMeter class and inherited functions."""
126125
super().__init__()
126+
self.available = None
127+
self.sensors = SmartEnergySensors()
127128

128129
def update_from_dict(self, data: dict[str, Any]) -> None:
129130
"""Update this SmartEnergyMeter object with data from a dictionary."""
@@ -365,12 +366,12 @@ class ThermostatSensors:
365366
valve_position: float | None = None # Tom/Floor
366367

367368
def update_from_dict(self, data: dict[str, Any]) -> None:
368-
"""Update this ZoneSensors object with data from a dictionary."""
369+
"""Update this ThermostatSensors object with data from a dictionary."""
369370

370371
self.battery = process_dict(data, "sensors", "battery")
371372
self.humidity = process_dict(data, "sensors", "humidity")
372373
self.illuminance = process_dict(data, "sensors", "illuminance")
373-
self.setpoint = process_dict(data, "sensors", "battsetpointery")
374+
self.setpoint = process_dict(data, "sensors", "setpoint")
374375
self.setpoint_high = process_dict(data, "sensors", "setpoint_high")
375376
self.setpoint_low = process_dict(data, "sensors", "setpoint_low")
376377
self.temperature = process_dict(data, "sensors", "temperature")
@@ -387,11 +388,9 @@ class WirelessThermostatBinarySensors:
387388
low_battery: bool | None = None
388389

389390
def update_from_dict(self, data: dict[str, Any]) -> None:
390-
"""Update this ZoneSensors object with data from a dictionary."""
391+
"""Update this WirelessThermostatBinarySensors object with data from a dictionary."""
391392

392-
self.electricity_consumed = process_dict(
393-
data, "sensors", "electricity_consumed"
394-
)
393+
self.low_battery = process_dict(data, "binary_sensors", "low_battery")
395394

396395

397396
@dataclass(kw_only=True)
@@ -407,11 +406,12 @@ class SetpointDict:
407406
upper_bound: float | None = None
408407

409408
def update_from_dict(self, data: dict[str, Any]) -> None:
410-
"""Update this ZoneSensors object with data from a dictionary."""
409+
"""Update this SetpointDict object with data from a dictionary."""
411410

412-
self.electricity_consumed = process_dict(
413-
data, "sensors", "electricity_consumed"
414-
)
411+
self.lower_bound = process_key(data, "lower_bound")
412+
self.resolution = process_key(data, "resolution")
413+
self.setpoint = process_key(data, "setpoint")
414+
self.upper_bound = process_key(data, "upper_bound")
415415

416416

417417
@dataclass(kw_only=True)
@@ -484,7 +484,7 @@ class ClimateDeviceBinarySensors:
484484
secondary_boiler_state: bool | None = None
485485

486486
def update_from_dict(self, data: dict[str, Any]) -> None:
487-
"""Update this ZoneSensors object with data from a dictionary."""
487+
"""Update this ClimateDeviceBinarySensors object with data from a dictionary."""
488488

489489
self.compressor_state = process_dict(data, "binary_sensors", "compressor_state")
490490
self.cooling_enabled = process_dict(data, "binary_sensors", "cooling_enabled")
@@ -511,7 +511,7 @@ class ClimateDeviceSensors:
511511
water_pressure: float | None = None
512512

513513
def update_from_dict(self, data: dict[str, Any]) -> None:
514-
"""Update this ZoneSensors object with data from a dictionary."""
514+
"""Update this ClimateDeviceSensors object with data from a dictionary."""
515515

516516
self.dhw_temperature = process_dict(data, "sensors", "dhw_temperature")
517517
self.domestic_hot_water_setpoint = process_dict(
@@ -537,7 +537,7 @@ class ClimateDeviceSwitches:
537537
dhw_cm_switch: bool | None = None
538538

539539
def update_from_dict(self, data: dict[str, Any]) -> None:
540-
"""Update this ZoneSensors object with data from a dictionary."""
540+
"""Update this ClimateDeviceSwitches object with data from a dictionary."""
541541

542542
self.cooling_ena_switch = process_dict(data, "switches", "cooling_ena_switch")
543543
self.dhw_cm_switch = process_dict(data, "switches", "dhw_cm_switch")
@@ -558,7 +558,7 @@ def __init__(self) -> None:
558558
"""Init Plug class and inherited functions."""
559559
super().__init__()
560560
self.sensors = PlugSensors()
561-
self. switches = PlugSwitches()
561+
self.switches = PlugSwitches()
562562

563563
def update_from_dict(self, data: dict[str, Any]) -> None:
564564
"""Update this Plug object with data from a dictionary."""
@@ -582,7 +582,7 @@ class PlugSensors:
582582
electricity_produced_interval: float | None = None
583583

584584
def update_from_dict(self, data: dict[str, Any]) -> None:
585-
"""Update this ZoneSensors object with data from a dictionary."""
585+
"""Update this PlugSensors object with data from a dictionary."""
586586

587587
self.electricity_consumed = process_dict(
588588
data, "sensors", "electricity_consumed"
@@ -606,7 +606,7 @@ class PlugSwitches:
606606
relay: bool | None = None
607607

608608
def update_from_dict(self, data: dict[str, Any]) -> None:
609-
"""Update this ZoneSensors object with data from a dictionary."""
609+
"""Update this PlugSwitches object with data from a dictionary."""
610610

611611
self.lock = process_dict(data, "switches", "lock")
612612
self.relay = process_dict(data, "switches", "relay")
@@ -652,80 +652,69 @@ class PlugwiseData:
652652
"""
653653

654654
gateway: Gateway = Gateway()
655-
climate_device: ClimateDevice | None = None
656-
zones: list[Zone] | None = None
657-
thermostats: list[Thermostat] | None = None
658-
plugs: list[Plug] | None = None
659-
p1_dsmr: SmartEnergyMeter | None = None
655+
climate_device: ClimateDevice | None
656+
zones: list[Zone] | None
657+
thermostats: list[Thermostat] | None
658+
plugs: list[Plug] | None
659+
p1_dsmr: SmartEnergyMeter | None
660660

661661
def __init__(self, data: Any) -> None:
662662
"""Initialize PlugwiseData class."""
663663
self.climate_device = None
664-
self.gateway = None
665664
self.p1_dsmr = None
666665
self.plugs = None
667666
self.thermostats = None
668667
self.zones = None
669668

670-
for device_id, device in data.items():
671-
self.gateway = Gateway()
669+
for _, device in data.items():
672670
if device["dev_class"] == "gateway":
673671
self.gateway.update_from_dict(device)
674-
675-
self.climate_device = ClimateDevice()
676672
if device["dev_class"] == "heater_central":
673+
if self.climate_device is None:
674+
self.climate_device = ClimateDevice()
677675
self.climate_device.update_from_dict(device)
678-
679-
self.zones = list[Zone()]
680676
if device["dev_class"] == "climate":
681-
for zone in self.zones:
682-
zone.update_from_dict(device)
683-
self.zones.append(zone)
684-
685-
self.thermostats = list[Thermostat()]
677+
if self.zones is None:
678+
self.zones = []
679+
zone = Zone()
680+
zone.update_from_dict(device)
681+
self.zones.append(zone)
686682
if device["dev_class"] in ZONE_THERMOSTATS:
687-
for thermostat in self.thermostats:
688-
thermostat.update_from_dict(device)
689-
self.thermostats.append(thermostat)
690-
691-
self.plugs = list[Plug()]
683+
if self.thermostats is None:
684+
self.thermostats = []
685+
thermostat = Thermostat()
686+
thermostat.update_from_dict(device)
687+
self.thermostats.append(thermostat)
692688
if device["dev_class"].endswith("_plug"):
693-
for plug in self.plugs:
694-
plug.update_from_dict(device)
695-
self.plugs.append(plug)
696-
697-
self.p1_dsmr = SmartEnergyMeter()
689+
if self.plugs is None:
690+
self.plugs = []
691+
plug = Plug()
692+
plug.update_from_dict(device)
693+
self.plugs.append(plug)
698694
if device["dev_class"] == "smartmeter":
699-
self.p1_dsmr.update_from_dict(device)
695+
if self.p1_dsmr is None:
696+
self.p1_dsmr = SmartEnergyMeter()
697+
self.p1_dsmr.update_from_dict(device)
700698

701699
def update_from_dict(self, data: dict[str, Any]) -> None:
702700
"""Update the status object with data received from the Plugwise API."""
703701

704702
for device_id, device in data.items():
705703
if device["dev_class"] == "gateway":
706-
self.gateway = Gateway()
707704
self.gateway.update_from_dict(device)
708-
if device["dev_class"] == "heater_central":
709-
self.climate_device = ClimateDevice()
705+
if self.climate_device and device["dev_class"] == "heater_central":
710706
self.climate_device.update_from_dict(device)
711-
if device["dev_class"] == "climate":
712-
self.zones = list[Zone()]
707+
if self.zones and device["dev_class"] == "climate":
713708
for zone in self.zones:
714709
if zone.location == device_id:
715710
zone.update_from_dict(device)
716-
self.zones.append(zone)
717-
if device["dev_class"] in ZONE_THERMOSTATS:
718-
self.thermostats = list[Thermostat()]
711+
if self.thermostats and device["dev_class"] in ZONE_THERMOSTATS:
719712
for thermostat in self.thermostats:
720713
if thermostat.location == device["location"]:
721714
thermostat.update_from_dict(device)
722-
self.thermostats.append(thermostat)
723-
if device["dev_class"].endswith("_plug"):
724-
self.plugs = list[Plug()]
715+
if self.plugs and device["dev_class"].endswith("_plug"):
725716
for plug in self.plugs:
726717
if plug.location == device["location"]:
727718
plug.update_from_dict(device)
728-
self.plugs.append(plug)
729-
if device["dev_class"] == "smartmeter":
730-
self.p1_dsmr = SmartEnergyMeter()
719+
if self.p1_dsmr and device["dev_class"] == "smartmeter":
731720
self.p1_dsmr.update_from_dict(device)

0 commit comments

Comments
 (0)