Skip to content

Commit d56a072

Browse files
committed
Handle difficult heater_central module
1 parent 2daf9c0 commit d56a072

File tree

1 file changed

+64
-35
lines changed

1 file changed

+64
-35
lines changed

plugwise/__init__.py

Lines changed: 64 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from __future__ import annotations
77

88
import json
9-
from typing import cast
9+
from typing import Any, cast
1010

1111
from plugwise.constants import (
1212
DEFAULT_LEGACY_TIMEOUT,
@@ -44,6 +44,55 @@
4444
import xmltodict
4545

4646

47+
def collect_module_data(result: dict[str, Any] , count=1) -> dict[str, Any]:
48+
"""Collect the module data and link to a service id."""
49+
modules:dict[str, dict[str, str]] = {}
50+
for module in result["domain_objects"]["module"]:
51+
link_id: str | None = None
52+
if module["services"] is not None:
53+
for value in module["services"].values():
54+
if isinstance(value, list):
55+
for item in value:
56+
for value_2 in item.values():
57+
link_id = value_2
58+
break
59+
break
60+
else:
61+
link_id = value["id"]
62+
if count == 1:
63+
break
64+
else: # find the 2nd id
65+
link_id = value["id"]
66+
break
67+
68+
69+
if link_id is not None:
70+
modules[link_id] = {
71+
"firmware_version": module["firmware_version"],
72+
"hardware_version": module["hardware_version"],
73+
"vendor_model": module["vendor_model"],
74+
"vendor_name": module["vendor_name"],
75+
}
76+
77+
return modules
78+
79+
80+
def add_module_to_appliance(
81+
appliance: dict[str, Any],
82+
modules: dict[str, Any]
83+
) -> tuple[dict[str, Any], bool]:
84+
"""Add module data to appliance."""
85+
for module in modules:
86+
for log in appliance["logs"]["point_log"]:
87+
for _, item in log.items():
88+
if isinstance(item, dict) and "id" in item:
89+
if item["id"] == module:
90+
appliance["module"] = modules[module]
91+
module_set = True
92+
93+
return appliance, module_set
94+
95+
4796
class Smile(SmileComm):
4897
"""The main Plugwise Smile API class."""
4998

@@ -124,45 +173,25 @@ async def connect(self) -> Version:
124173
result_dict = dict(xmltodict.parse(result_str, attr_prefix=""))
125174
for key in ["ame_regulation", "template"]:
126175
result_dict["domain_objects"].pop(key, None)
127-
modules:dict[str, dict[str, str]] = {}
128-
for module in result_dict["domain_objects"]["module"]:
129-
link_id: str | None = None
130-
if module["services"] is not None:
131-
for value in module["services"].values():
132-
if isinstance(value, list):
133-
for item in value:
134-
for value_2 in item.values():
135-
link_id = value_2
136-
break
137-
break
138-
else:
139-
link_id = value["id"]
140-
break
141176

142-
if link_id is not None:
143-
modules[link_id] = {
144-
"firmware_version": module["firmware_version"],
145-
"hardware_version": module["hardware_version"],
146-
"vendor_model": module["vendor_model"],
147-
"vendor_name": module["vendor_name"],
148-
}
177+
modules = collect_module_data(result_dict)
149178

179+
150180
for appliance in result_dict["domain_objects"]["appliance"]:
151181
module_set = False
152-
for module in modules:
153-
for log in appliance["logs"]["point_log"]:
154-
for _, item in log.items():
155-
if isinstance(item, dict) and "id" in item:
156-
if item["id"] == module:
157-
appliance["module"] = modules[module]
158-
module_set = True
182+
appliance, module_set = add_module_to_appliance(appliance, modules)
159183
if not module_set:
160-
appliance["module"] = {
161-
"firmware_version": None,
162-
"hardware_version": None,
163-
"vendor_model": None,
164-
"vendor_name": None,
165-
}
184+
modules = collect_module_data(result, count=2) # repeat for 2nd id
185+
for appliance in result_dict["domain_objects"]["appliance"]:
186+
module_set = False
187+
appliance, module_set = add_module_to_appliance(appliance, modules)
188+
if not module_set:
189+
appliance["module"] = {
190+
"firmware_version": None,
191+
"hardware_version": None,
192+
"vendor_model": None,
193+
"vendor_name": None,
194+
}
166195

167196
result_dict["domain_objects"].pop("module")
168197
LOGGER.debug("HOI result_dict: %s", json.dumps(result_dict, indent=4))

0 commit comments

Comments
 (0)