Skip to content

Commit 52f28a5

Browse files
authored
Merge pull request #765 from plugwise/reduce-complexity
Reduce complexity
2 parents e2e7524 + d1ee9d5 commit 52f28a5

File tree

7 files changed

+39
-44
lines changed

7 files changed

+39
-44
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Changelog
22

3-
## Ongoing
3+
## v1.7.7
44

5-
- Implement code quality improvements as suggested by SonarCloud
5+
- Implement code quality improvements as suggested by SonarCloud via [#762](https://github.com/plugwise/python-plugwise/pull/762), [#763](https://github.com/plugwise/python-plugwise/pull/763), [#764](https://github.com/plugwise/python-plugwise/pull/764), and [#765](https://github.com/plugwise/python-plugwise/pull/765)
66

77
## v1.7.6
88

plugwise/common.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from plugwise.constants import (
1111
ANNA,
1212
NONE,
13+
PRIORITY_DEVICE_CLASSES,
1314
SPECIAL_PLUG_TYPES,
1415
SWITCH_GROUP_TYPES,
1516
ApplianceType,
@@ -152,6 +153,16 @@ def _create_gw_entities(self, appl: Munch) -> None:
152153
self.gw_entities[appl.entity_id][appl_key] = value
153154
self._count += 1
154155

156+
def _reorder_devices(self) -> None:
157+
"""Place the gateway and optional heater_central devices as 1st and 2nd."""
158+
reordered = {}
159+
for dev_class in PRIORITY_DEVICE_CLASSES:
160+
for entity_id, entity in dict(self.gw_entities).items():
161+
if entity["dev_class"] == dev_class:
162+
reordered[entity_id] = self.gw_entities.pop(entity_id)
163+
break
164+
self.gw_entities = {**reordered, **self.gw_entities}
165+
155166
def _entity_switching_group(self, entity: GwEntityData, data: GwEntityData) -> None:
156167
"""Helper-function for _get_device_zone_data().
157168

plugwise/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
MODULE_LOCATOR: Final = "./logs/point_log/*[@id]"
8787
NONE: Final = "None"
8888
OFF: Final = "off"
89-
PRIORITY_DEVICE_CLASSES = ("heater_central", "gateway")
89+
PRIORITY_DEVICE_CLASSES = ("gateway", "heater_central")
9090

9191
# XML data paths
9292
APPLIANCES: Final = "/core/appliances"

plugwise/helper.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
NONE,
2929
OFF,
3030
P1_MEASUREMENTS,
31-
PRIORITY_DEVICE_CLASSES,
3231
TEMP_CELSIUS,
3332
THERMOSTAT_CLASSES,
3433
TOGGLES,
@@ -160,7 +159,7 @@ def _all_appliances(self) -> None:
160159
self._get_p1_smartmeter_info()
161160

162161
# Sort the gw_entities
163-
self._sort_gw_entities()
162+
self._reorder_devices()
164163

165164
def _get_p1_smartmeter_info(self) -> None:
166165
"""For P1 collect the connected SmartMeter info from the Home/building location.
@@ -193,18 +192,6 @@ def _get_p1_smartmeter_info(self) -> None:
193192

194193
self._create_gw_entities(appl)
195194

196-
def _sort_gw_entities(self) -> None:
197-
"""Place the gateway and optional heater_central entities as 1st and 2nd."""
198-
for dev_class in PRIORITY_DEVICE_CLASSES:
199-
for entity_id, entity in dict(self.gw_entities).items():
200-
if entity["dev_class"] == dev_class:
201-
priority_entity = entity
202-
self.gw_entities.pop(entity_id)
203-
other_entities = self.gw_entities
204-
priority_entities = {entity_id: priority_entity}
205-
self.gw_entities = {**priority_entities, **other_entities}
206-
break
207-
208195
def _all_locations(self) -> None:
209196
"""Collect all locations."""
210197
loc = Munch()

plugwise/legacy/helper.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
NONE,
2424
OFF,
2525
P1_LEGACY_MEASUREMENTS,
26-
PRIORITY_DEVICE_CLASSES,
2726
TEMP_CELSIUS,
2827
THERMOSTAT_CLASSES,
2928
UOM,
@@ -136,17 +135,7 @@ def _all_appliances(self) -> None:
136135
continue # pragma: no cover
137136

138137
self._create_gw_entities(appl)
139-
140-
# Place the gateway and optional heater_central devices as 1st and 2nd
141-
for dev_class in PRIORITY_DEVICE_CLASSES:
142-
for entity_id, entity in dict(self.gw_entities).items():
143-
if entity["dev_class"] == dev_class:
144-
tmp_entity = entity
145-
self.gw_entities.pop(entity_id)
146-
cleared_dict = self.gw_entities
147-
add_to_front = {entity_id: tmp_entity}
148-
self.gw_entities = {**add_to_front, **cleared_dict}
149-
break
138+
self._reorder_devices()
150139

151140
def _all_locations(self) -> None:
152141
"""Collect all locations."""

plugwise/smile.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@
3737
from munch import Munch
3838

3939

40+
def model_to_switch_items(model: str, state: str, switch: Munch) -> tuple[str, Munch]:
41+
"""Translate state and switch attributes based on model name.
42+
43+
Helper function for set_switch_state().
44+
"""
45+
match model:
46+
case "dhw_cm_switch":
47+
switch.device = "toggle"
48+
switch.func_type = "toggle_functionality"
49+
switch.act_type = "domestic_hot_water_comfort_mode"
50+
case "cooling_ena_switch":
51+
switch.device = "toggle"
52+
switch.func_type = "toggle_functionality"
53+
switch.act_type = "cooling_enabled"
54+
case "lock":
55+
switch.func = "lock"
56+
state = "true" if state == STATE_ON else "false"
57+
58+
return state, switch
59+
60+
4061
class SmileAPI(SmileData):
4162
"""The Plugwise SmileAPI helper class for actual Plugwise devices."""
4263

@@ -381,20 +402,7 @@ async def set_switch_state(
381402
switch.device = "relay"
382403
switch.func_type = "relay_functionality"
383404
switch.func = "state"
384-
if model == "dhw_cm_switch":
385-
switch.device = "toggle"
386-
switch.func_type = "toggle_functionality"
387-
switch.act_type = "domestic_hot_water_comfort_mode"
388-
389-
if model == "cooling_ena_switch":
390-
switch.device = "toggle"
391-
switch.func_type = "toggle_functionality"
392-
switch.act_type = "cooling_enabled"
393-
394-
if model == "lock":
395-
switch.func = "lock"
396-
state = "true" if state == STATE_ON else "false"
397-
405+
state, switch = model_to_switch_items(model, state, switch)
398406
data = (
399407
f"<{switch.func_type}>"
400408
f"<{switch.func}>{state}</{switch.func}>"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "plugwise"
7-
version = "1.7.6"
7+
version = "1.7.7"
88
license = "MIT"
99
description = "Plugwise Smile (Adam/Anna/P1) and Stretch module for Python 3."
1010
readme = "README.md"

0 commit comments

Comments
 (0)