Skip to content

Commit a937a7b

Browse files
authored
Merge pull request #485 from plugwise/qual
Quality improvements (through sonar cloud)
2 parents 6c55c79 + 9a10254 commit a937a7b

File tree

5 files changed

+48
-41
lines changed

5 files changed

+48
-41
lines changed

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
## Ongoing
44

55
- Refresh adam_plus_anna_new userdata and adapt.
6-
- Bump actions and requirements to Python 3.12
7-
- Ruff as per #470 (defaulting black and isort to ruff)
8-
- Modularize/split testing, including separation of code and data
6+
- Bump actions and requirements to Python 3.12.
7+
- Ruff as per #470 (defaulting black and isort to ruff).
8+
- Modularize/split testing, including separation of code and data.
9+
- Improve quality as indicated by SonarCloud.
910

1011
## v0.35.4
1112

plugwise/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ async def _smile_detect_legacy(
400400
self, result: etree, dsmrmain: etree, model: str
401401
) -> str:
402402
"""Helper-function for _smile_detect()."""
403+
return_model = model
403404
# Stretch: find the MAC of the zigbee master_controller (= Stick)
404405
if network := result.find("./module/protocols/master_controller"):
405406
self.smile_zigbee_mac_address = network.find("mac_address").text
@@ -417,7 +418,7 @@ async def _smile_detect_legacy(
417418
):
418419
self._system = await self._request(SYSTEM)
419420
self.smile_fw_version = self._system.find("./gateway/firmware").text
420-
model = self._system.find("./gateway/product").text
421+
return_model = self._system.find("./gateway/product").text
421422
self.smile_hostname = self._system.find("./gateway/hostname").text
422423
# If wlan0 contains data it's active, so eth0 should be checked last
423424
for network in ("wlan0", "eth0"):
@@ -428,10 +429,9 @@ async def _smile_detect_legacy(
428429
elif dsmrmain is not None:
429430
self._status = await self._request(STATUS)
430431
self.smile_fw_version = self._status.find("./system/version").text
431-
model = self._status.find("./system/product").text
432+
return_model = self._status.find("./system/product").text
432433
self.smile_hostname = self._status.find("./network/hostname").text
433434
self.smile_mac_address = self._status.find("./network/mac_address").text
434-
435435
else: # pragma: no cover
436436
# No cornercase, just end of the line
437437
LOGGER.error(
@@ -441,7 +441,7 @@ async def _smile_detect_legacy(
441441
raise ResponseError
442442

443443
self._smile_legacy = True
444-
return model
444+
return return_model
445445

446446
async def _smile_detect(self, result: etree, dsmrmain: etree) -> None:
447447
"""Helper-function for connect().

plugwise/constants.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,19 +183,25 @@
183183
"outdoor_temperature",
184184
)
185185

186+
# Literals
187+
SMILE_P1 = "Smile P1"
188+
POWER = "power"
189+
STRETCH = "stretch"
190+
THERMOSTAT = "thermostat"
191+
186192
# Known types of Smiles and Stretches
187193
SMILE = namedtuple("SMILE", "smile_type smile_name")
188194
SMILES: Final[dict[str, SMILE]] = {
189-
"smile_v2": SMILE("power", "Smile P1"),
190-
"smile_v3": SMILE("power", "Smile P1"),
191-
"smile_v4": SMILE("power", "Smile P1"),
192-
"smile_open_therm_v2": SMILE("thermostat", ADAM),
193-
"smile_open_therm_v3": SMILE("thermostat", ADAM),
194-
"smile_thermo_v1": SMILE("thermostat", ANNA),
195-
"smile_thermo_v3": SMILE("thermostat", ANNA),
196-
"smile_thermo_v4": SMILE("thermostat", ANNA),
197-
"stretch_v2": SMILE("stretch", "Stretch"),
198-
"stretch_v3": SMILE("stretch", "Stretch"),
195+
"smile_v2": SMILE(POWER, SMILE_P1),
196+
"smile_v3": SMILE(POWER, SMILE_P1),
197+
"smile_v4": SMILE(POWER, SMILE_P1),
198+
"smile_open_therm_v2": SMILE(THERMOSTAT, ADAM),
199+
"smile_open_therm_v3": SMILE(THERMOSTAT, ADAM),
200+
"smile_thermo_v1": SMILE(THERMOSTAT, ANNA),
201+
"smile_thermo_v3": SMILE(THERMOSTAT, ANNA),
202+
"smile_thermo_v4": SMILE(THERMOSTAT, ANNA),
203+
"stretch_v2": SMILE(STRETCH, "Stretch"),
204+
"stretch_v3": SMILE(STRETCH, "Stretch"),
199205
}
200206
REQUIRE_APPLIANCES: Final[list[str]] = [
201207
"smile_thermo_v1",

plugwise/helper.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,22 @@ async def _request(
179179
"""Get/put/delete data from a give URL."""
180180
resp: ClientResponse
181181
url = f"{self._endpoint}{command}"
182+
use_headers = headers
182183

183184
try:
184185
if method == "delete":
185186
resp = await self._websession.delete(url, auth=self._auth)
186187
if method == "get":
187188
# Work-around for Stretchv2, should not hurt the other smiles
188-
headers = {"Accept-Encoding": "gzip"}
189-
resp = await self._websession.get(url, headers=headers, auth=self._auth)
189+
use_headers = {"Accept-Encoding": "gzip"}
190+
resp = await self._websession.get(
191+
url, headers=use_headers, auth=self._auth
192+
)
190193
if method == "put":
191-
headers = {"Content-type": "text/xml"}
194+
use_headers = {"Content-type": "text/xml"}
192195
resp = await self._websession.put(
193196
url,
194-
headers=headers,
197+
headers=use_headers,
195198
data=data,
196199
auth=self._auth,
197200
)
@@ -314,8 +317,6 @@ def _all_locations(self) -> None:
314317

315318
self._loc_data[loc.loc_id] = {"name": loc.name}
316319

317-
return
318-
319320
def _get_module_data(
320321
self, appliance: etree, locator: str, mod_type: str
321322
) -> ModelData:
@@ -805,15 +806,16 @@ def _appliance_measurements(
805806
updated_date_locator = (
806807
f'.//logs/point_log[type="{measurement}"]/updated_date'
807808
)
808-
if measurement in OBSOLETE_MEASUREMENTS:
809-
if (
810-
updated_date_key := appliance.find(updated_date_locator)
811-
) is not None:
812-
updated_date = updated_date_key.text.split("T")[0]
813-
date_1 = dt.datetime.strptime(updated_date, "%Y-%m-%d")
814-
date_2 = dt.datetime.now()
815-
if int((date_2 - date_1).days) > 7:
816-
continue
809+
if (
810+
measurement in OBSOLETE_MEASUREMENTS
811+
and (updated_date_key := appliance.find(updated_date_locator))
812+
is not None
813+
):
814+
updated_date = updated_date_key.text.split("T")[0]
815+
date_1 = dt.datetime.strptime(updated_date, "%Y-%m-%d")
816+
date_2 = dt.datetime.now()
817+
if int((date_2 - date_1).days) > 7:
818+
continue
817819

818820
if new_name := getattr(attrs, ATTR_NAME, None):
819821
measurement = new_name
@@ -931,7 +933,7 @@ def _get_actuator_functionalities(
931933
locator = (
932934
f'.//actuator_functionalities/{functionality}[type="{item}"]/{key}'
933935
)
934-
if (function := xml.find(locator)) is not None:
936+
if (pw_function := xml.find(locator)) is not None:
935937
if key == "offset":
936938
# Add limits and resolution for temperature_offset,
937939
# not provided by Plugwise in the XML data
@@ -943,7 +945,7 @@ def _get_actuator_functionalities(
943945
key = "setpoint"
944946

945947
act_key = cast(ActuatorDataType, key)
946-
temp_dict[act_key] = format_measure(function.text, TEMP_CELSIUS)
948+
temp_dict[act_key] = format_measure(pw_function.text, TEMP_CELSIUS)
947949
self._count += 1
948950

949951
if temp_dict:
@@ -1267,7 +1269,7 @@ def power_data_energy_diff(
12671269

12681270
return direct_data
12691271

1270-
def _power_data_peak_value(self, direct_data: DeviceData, loc: Munch) -> Munch:
1272+
def _power_data_peak_value(self, loc: Munch) -> Munch:
12711273
"""Helper-function for _power_data_from_location() and _power_data_from_modules()."""
12721274
loc.found = True
12731275
# If locator not found look for P1 gas_consumed or phase data (without tariff)
@@ -1337,12 +1339,11 @@ def _power_data_from_location(self, loc_id: str) -> DeviceData:
13371339
for loc.measurement, loc.attrs in P1_MEASUREMENTS.items():
13381340
for loc.log_type in log_list:
13391341
for loc.peak_select in peak_list:
1340-
# meter_string = ".//{}[type='{}']/"
13411342
loc.locator = (
13421343
f'./{loc.log_type}[type="{loc.measurement}"]/period/'
13431344
f'measurement[@{t_string}="{loc.peak_select}"]'
13441345
)
1345-
loc = self._power_data_peak_value(direct_data, loc)
1346+
loc = self._power_data_peak_value(loc)
13461347
if not loc.found:
13471348
continue
13481349

@@ -1377,7 +1378,7 @@ def _power_data_from_modules(self) -> DeviceData:
13771378
f"./{loc.meas_list[0]}_{loc.log_type}/measurement"
13781379
f'[@directionality="{loc.meas_list[1]}"][@{t_string}="{loc.peak_select}"]'
13791380
)
1380-
loc = self._power_data_peak_value(direct_data, loc)
1381+
loc = self._power_data_peak_value(loc)
13811382
if not loc.found:
13821383
continue
13831384

plugwise/util.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ def format_measure(measure: str, unit: str) -> float | int:
2727
result = float(measure)
2828
except ValueError:
2929
float_measure = float(measure)
30-
if unit == PERCENTAGE:
31-
if 0 < float_measure <= 1:
32-
return int(float_measure * 100)
30+
if unit == PERCENTAGE and 0 < float_measure <= 1:
31+
return int(float_measure * 100)
3332

3433
if unit == ENERGY_KILO_WATT_HOUR:
3534
float_measure = float_measure / 1000

0 commit comments

Comments
 (0)