|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
8 | 8 | import json |
9 | | -from typing import cast |
| 9 | +from typing import Any, cast |
10 | 10 |
|
11 | 11 | from plugwise.constants import ( |
12 | 12 | DEFAULT_LEGACY_TIMEOUT, |
|
44 | 44 | import xmltodict |
45 | 45 |
|
46 | 46 |
|
| 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 | + |
47 | 96 | class Smile(SmileComm): |
48 | 97 | """The main Plugwise Smile API class.""" |
49 | 98 |
|
@@ -124,45 +173,25 @@ async def connect(self) -> Version: |
124 | 173 | result_dict = dict(xmltodict.parse(result_str, attr_prefix="")) |
125 | 174 | for key in ["ame_regulation", "template"]: |
126 | 175 | 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 |
141 | 176 |
|
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) |
149 | 178 |
|
| 179 | + |
150 | 180 | for appliance in result_dict["domain_objects"]["appliance"]: |
151 | 181 | 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) |
159 | 183 | 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 | + } |
166 | 195 |
|
167 | 196 | result_dict["domain_objects"].pop("module") |
168 | 197 | LOGGER.debug("HOI result_dict: %s", json.dumps(result_dict, indent=4)) |
|
0 commit comments