Skip to content

Commit a179a5e

Browse files
committed
Improve
1 parent 0cba75d commit a179a5e

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
lines changed

plugwise/__init__.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
def collect_module_data(result: dict[str, Any] , count=1) -> dict[str, Any]:
4848
"""Collect the module data and link to a service id."""
49-
modules:dict[str, dict[str, str]] = {}
49+
modules:dict[str, dict[str, str | None]] = {}
5050
for module in result["domain_objects"]["module"]:
5151
link_id: str | None = None
5252
if module["services"] is not None:
@@ -61,11 +61,10 @@ def collect_module_data(result: dict[str, Any] , count=1) -> dict[str, Any]:
6161
link_id = value["id"]
6262
if count == 1:
6363
break
64-
else: # find the 2nd id
64+
else: # find the 2nd id when count=2
6565
link_id = value["id"]
6666
break
6767

68-
6968
if link_id is not None:
7069
modules[link_id] = {
7170
"firmware_version": module["firmware_version"],
@@ -78,19 +77,20 @@ def collect_module_data(result: dict[str, Any] , count=1) -> dict[str, Any]:
7877

7978

8079
def add_module_to_appliance(
81-
appliance: dict[str, Any],
82-
modules: dict[str, Any]
80+
appliance: list[dict[str, Any]],
81+
modules: list[dict[str, Any]]
8382
) -> tuple[dict[str, Any], bool]:
8483
"""Add module data to appliance."""
84+
module_set = False
8585
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:
86+
for log in appliance["logs"]["point_log"]: # list-type
87+
for value in log.values():
88+
if isinstance(value, dict) and "id" in value:
89+
if value["id"] == module:
9090
appliance["module"] = modules[module]
9191
module_set = True
9292

93-
return appliance, module_set
93+
return (appliance, module_set)
9494

9595

9696
class Smile(SmileComm):
@@ -175,20 +175,16 @@ async def connect(self) -> Version:
175175
result_dict["domain_objects"].pop(key, None)
176176

177177
modules = collect_module_data(result_dict)
178-
179-
180-
for appliance in result_dict["domain_objects"]["appliance"]:
181-
module_set = False
182-
appliance, module_set = add_module_to_appliance(appliance, modules)
178+
for appliance in result_dict["domain_objects"]["appliance"]: # list-type
179+
(appliance, module_set) = add_module_to_appliance(appliance, modules)
183180
# Set gateway firmwware_version
184181
if appliance["type"] == "gateway":
185182
appliance["module"]["firmware_version"] = result_dict["domain_objects"]["gateway"]["firmware_version"]
186183
# TODO set zigbee mac(s)
187184
if not module_set:
188185
modules = collect_module_data(result, count=2) # repeat trying with 2nd id
189186
for appliance in result_dict["domain_objects"]["appliance"]:
190-
module_set = False
191-
appliance, module_set = add_module_to_appliance(appliance, modules)
187+
(appliance, module_set) = add_module_to_appliance(appliance, modules)
192188
if not module_set:
193189
appliance["module"] = {
194190
"firmware_version": None,
@@ -197,7 +193,7 @@ async def connect(self) -> Version:
197193
"vendor_name": None,
198194
}
199195

200-
result_dict["domain_objects"].pop("module")
196+
result_dict["domain_objects"].pop("module", None)
201197
LOGGER.debug("HOI result_dict: %s", json.dumps(result_dict, indent=4))
202198

203199
# Work-around for Stretch fw 2.7.18

0 commit comments

Comments
 (0)