diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b101fa22..858855752 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,8 @@ # Changelog -## Ongoing +## v1.7.7 -- Implement code quality improvements as suggested by SonarCloud +- 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) ## v1.7.6 diff --git a/plugwise/common.py b/plugwise/common.py index 933a2d6c2..9534a2263 100644 --- a/plugwise/common.py +++ b/plugwise/common.py @@ -10,6 +10,7 @@ from plugwise.constants import ( ANNA, NONE, + PRIORITY_DEVICE_CLASSES, SPECIAL_PLUG_TYPES, SWITCH_GROUP_TYPES, ApplianceType, @@ -152,6 +153,16 @@ def _create_gw_entities(self, appl: Munch) -> None: self.gw_entities[appl.entity_id][appl_key] = value self._count += 1 + def _reorder_devices(self) -> None: + """Place the gateway and optional heater_central devices as 1st and 2nd.""" + reordered = {} + for dev_class in PRIORITY_DEVICE_CLASSES: + for entity_id, entity in dict(self.gw_entities).items(): + if entity["dev_class"] == dev_class: + reordered[entity_id] = self.gw_entities.pop(entity_id) + break + self.gw_entities = {**reordered, **self.gw_entities} + def _entity_switching_group(self, entity: GwEntityData, data: GwEntityData) -> None: """Helper-function for _get_device_zone_data(). diff --git a/plugwise/constants.py b/plugwise/constants.py index 4e1becd56..4710229e8 100644 --- a/plugwise/constants.py +++ b/plugwise/constants.py @@ -86,7 +86,7 @@ MODULE_LOCATOR: Final = "./logs/point_log/*[@id]" NONE: Final = "None" OFF: Final = "off" -PRIORITY_DEVICE_CLASSES = ("heater_central", "gateway") +PRIORITY_DEVICE_CLASSES = ("gateway", "heater_central") # XML data paths APPLIANCES: Final = "/core/appliances" diff --git a/plugwise/helper.py b/plugwise/helper.py index 1b66af34b..16ef1f0a3 100644 --- a/plugwise/helper.py +++ b/plugwise/helper.py @@ -28,7 +28,6 @@ NONE, OFF, P1_MEASUREMENTS, - PRIORITY_DEVICE_CLASSES, TEMP_CELSIUS, THERMOSTAT_CLASSES, TOGGLES, @@ -160,7 +159,7 @@ def _all_appliances(self) -> None: self._get_p1_smartmeter_info() # Sort the gw_entities - self._sort_gw_entities() + self._reorder_devices() def _get_p1_smartmeter_info(self) -> None: """For P1 collect the connected SmartMeter info from the Home/building location. @@ -193,18 +192,6 @@ def _get_p1_smartmeter_info(self) -> None: self._create_gw_entities(appl) - def _sort_gw_entities(self) -> None: - """Place the gateway and optional heater_central entities as 1st and 2nd.""" - for dev_class in PRIORITY_DEVICE_CLASSES: - for entity_id, entity in dict(self.gw_entities).items(): - if entity["dev_class"] == dev_class: - priority_entity = entity - self.gw_entities.pop(entity_id) - other_entities = self.gw_entities - priority_entities = {entity_id: priority_entity} - self.gw_entities = {**priority_entities, **other_entities} - break - def _all_locations(self) -> None: """Collect all locations.""" loc = Munch() diff --git a/plugwise/legacy/helper.py b/plugwise/legacy/helper.py index d975ac617..d5e6918ab 100644 --- a/plugwise/legacy/helper.py +++ b/plugwise/legacy/helper.py @@ -23,7 +23,6 @@ NONE, OFF, P1_LEGACY_MEASUREMENTS, - PRIORITY_DEVICE_CLASSES, TEMP_CELSIUS, THERMOSTAT_CLASSES, UOM, @@ -136,17 +135,7 @@ def _all_appliances(self) -> None: continue # pragma: no cover self._create_gw_entities(appl) - - # Place the gateway and optional heater_central devices as 1st and 2nd - for dev_class in PRIORITY_DEVICE_CLASSES: - for entity_id, entity in dict(self.gw_entities).items(): - if entity["dev_class"] == dev_class: - tmp_entity = entity - self.gw_entities.pop(entity_id) - cleared_dict = self.gw_entities - add_to_front = {entity_id: tmp_entity} - self.gw_entities = {**add_to_front, **cleared_dict} - break + self._reorder_devices() def _all_locations(self) -> None: """Collect all locations.""" diff --git a/plugwise/smile.py b/plugwise/smile.py index 8cdb1546e..970181ca1 100644 --- a/plugwise/smile.py +++ b/plugwise/smile.py @@ -37,6 +37,27 @@ from munch import Munch +def model_to_switch_items(model: str, state: str, switch: Munch) -> tuple[str, Munch]: + """Translate state and switch attributes based on model name. + + Helper function for set_switch_state(). + """ + match model: + case "dhw_cm_switch": + switch.device = "toggle" + switch.func_type = "toggle_functionality" + switch.act_type = "domestic_hot_water_comfort_mode" + case "cooling_ena_switch": + switch.device = "toggle" + switch.func_type = "toggle_functionality" + switch.act_type = "cooling_enabled" + case "lock": + switch.func = "lock" + state = "true" if state == STATE_ON else "false" + + return state, switch + + class SmileAPI(SmileData): """The Plugwise SmileAPI helper class for actual Plugwise devices.""" @@ -381,20 +402,7 @@ async def set_switch_state( switch.device = "relay" switch.func_type = "relay_functionality" switch.func = "state" - if model == "dhw_cm_switch": - switch.device = "toggle" - switch.func_type = "toggle_functionality" - switch.act_type = "domestic_hot_water_comfort_mode" - - if model == "cooling_ena_switch": - switch.device = "toggle" - switch.func_type = "toggle_functionality" - switch.act_type = "cooling_enabled" - - if model == "lock": - switch.func = "lock" - state = "true" if state == STATE_ON else "false" - + state, switch = model_to_switch_items(model, state, switch) data = ( f"<{switch.func_type}>" f"<{switch.func}>{state}" diff --git a/pyproject.toml b/pyproject.toml index ef4299677..4fc91a160 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "plugwise" -version = "1.7.6" +version = "1.7.7" license = "MIT" description = "Plugwise Smile (Adam/Anna/P1) and Stretch module for Python 3." readme = "README.md"