Skip to content

Commit f6eb523

Browse files
authored
Merge pull request #763 from plugwise/sc-fixes-2
Solve more SonarQube issues
2 parents bd253bf + d70f243 commit f6eb523

File tree

3 files changed

+55
-118
lines changed

3 files changed

+55
-118
lines changed

plugwise/data.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def _detect_low_batteries(self) -> list[str]:
8686
mac_pattern = re.compile(r"(?:[0-9A-F]{2}){8}")
8787
matches = ["Battery", "below"]
8888
if self._notifications:
89-
for msg_id, notification in list(self._notifications.items()):
89+
for msg_id, notification in self._notifications.copy().items():
9090
mac_address: str | None = None
9191
message: str | None = notification.get("message")
9292
warning: str | None = notification.get("warning")
@@ -232,12 +232,12 @@ def _get_adam_data(self, entity: GwEntityData, data: GwEntityData) -> None:
232232
if self._on_off_device and isinstance(self._heating_valves(), int):
233233
data["binary_sensors"]["heating_state"] = self._heating_valves() != 0
234234
# Add cooling_enabled binary_sensor
235-
if "binary_sensors" in data:
236-
if (
237-
"cooling_enabled" not in data["binary_sensors"]
238-
and self._cooling_present
239-
):
240-
data["binary_sensors"]["cooling_enabled"] = self._cooling_enabled
235+
if (
236+
"binary_sensors" in data
237+
and "cooling_enabled" not in data["binary_sensors"]
238+
and self._cooling_present
239+
):
240+
data["binary_sensors"]["cooling_enabled"] = self._cooling_enabled
241241

242242
# Show the allowed regulation_modes and gateway_modes
243243
if entity["dev_class"] == "gateway":

plugwise/helper.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ def _scan_thermostats(self) -> None:
723723
for entity_id, entity in self.gw_entities.items():
724724
self._rank_thermostat(thermo_matching, loc_id, entity_id, entity)
725725

726-
for loc_id, loc_data in list(self._thermo_locs.items()):
726+
for loc_id, loc_data in self._thermo_locs.items():
727727
if loc_data["primary_prio"] != 0:
728728
self._zones[loc_id] = {
729729
"dev_class": "climate",

tests/test_init.py

Lines changed: 47 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ async def load_testdata(
8989
content = await testdata_file.read()
9090
return json.loads(content)
9191

92-
async def setup_app(
92+
def setup_app(
9393
self,
9494
broken=False,
95-
timeout=False,
95+
timeout_happened=False,
9696
raise_timeout=False,
9797
fail_auth=False,
9898
stretch=False,
@@ -108,7 +108,7 @@ async def setup_app(
108108

109109
if broken:
110110
app.router.add_get(CORE_DOMAIN_OBJECTS, self.smile_broken)
111-
elif timeout:
111+
elif timeout_happened:
112112
app.router.add_get(CORE_DOMAIN_OBJECTS, self.smile_timeout)
113113
else:
114114
app.router.add_get(CORE_DOMAIN_OBJECTS, self.smile_domain_objects)
@@ -132,10 +132,10 @@ async def setup_app(
132132

133133
return app
134134

135-
async def setup_legacy_app(
135+
def setup_legacy_app(
136136
self,
137137
broken=False,
138-
timeout=False,
138+
timeout_happened=False,
139139
raise_timeout=False,
140140
fail_auth=False,
141141
stretch=False,
@@ -151,7 +151,7 @@ async def setup_legacy_app(
151151

152152
if broken:
153153
app.router.add_get(CORE_LOCATIONS, self.smile_broken)
154-
elif timeout:
154+
elif timeout_happened:
155155
app.router.add_get(CORE_LOCATIONS, self.smile_timeout)
156156
else:
157157
app.router.add_get(CORE_LOCATIONS, self.smile_locations)
@@ -264,24 +264,27 @@ async def smile_fail_auth(cls, request):
264264
raise aiohttp.web.HTTPUnauthorized()
265265

266266
@staticmethod
267-
def connect_status(broken, timeout, fail_auth):
267+
def connect_status(broken, timeout_happened, fail_auth):
268268
"""Determine assumed status from settings."""
269269
assumed_status = 200
270270
if broken:
271271
assumed_status = 500
272-
if timeout:
272+
if timeout_happened:
273273
assumed_status = 504
274274
if fail_auth:
275275
assumed_status = 401
276276
return assumed_status
277277

278278
async def connect(
279279
self,
280+
function,
280281
broken=False,
281-
timeout=False,
282+
timeout_happened=False,
282283
raise_timeout=False,
284+
real_timeout_value=10,
283285
fail_auth=False,
284286
stretch=False,
287+
url_part=CORE_DOMAIN_OBJECTS,
285288
):
286289
"""Connect to a smile environment and perform basic asserts."""
287290
port = aiohttp.test_utils.unused_port()
@@ -290,7 +293,7 @@ async def connect(
290293
)
291294

292295
# Happy flow
293-
app = await self.setup_app(broken, timeout, raise_timeout, fail_auth, stretch)
296+
app = function(broken, timeout_happened, raise_timeout, fail_auth, stretch)
294297

295298
server = aiohttp.test_utils.TestServer(
296299
app, port=port, scheme="http", host="127.0.0.1"
@@ -300,20 +303,20 @@ async def connect(
300303
client = aiohttp.test_utils.TestClient(server)
301304
websession = client.session
302305

303-
url = f"{server.scheme}://{server.host}:{server.port}{CORE_DOMAIN_OBJECTS}"
306+
url = f"{server.scheme}://{server.host}:{server.port}{url_part}"
304307

305308
# Try/exceptpass to accommodate for Timeout of aoihttp
306309
try:
307310
resp = await websession.get(url)
308-
assumed_status = self.connect_status(broken, timeout, fail_auth)
311+
assumed_status = self.connect_status(broken, timeout_happened, fail_auth)
309312
assert resp.status == assumed_status
310313
timeoutpass_result = False
311314
assert timeoutpass_result
312315
except Exception: # pylint: disable=broad-except
313316
timeoutpass_result = True
314317
assert timeoutpass_result
315318

316-
if not broken and not timeout and not fail_auth:
319+
if not broken and not timeout_happened and not fail_auth:
317320
text = await resp.text()
318321
assert "xml" in text
319322

@@ -340,101 +343,15 @@ async def connect(
340343
websession=websession,
341344
)
342345

343-
if not timeout:
346+
if not timeout_happened:
344347
assert smile._timeout == 30
345348

346349
# Connect to the smile
347350
smile_version = None
348351
try:
349352
smile_version = await smile.connect()
350353
assert smile_version is not None
351-
assert smile._timeout == 10
352-
return server, smile, client
353-
except (
354-
pw_exceptions.ConnectionFailedError,
355-
pw_exceptions.InvalidXMLError,
356-
pw_exceptions.InvalidAuthentication,
357-
) as exception:
358-
assert smile_version is None
359-
await self.disconnect(server, client)
360-
raise exception
361-
362-
async def connect_legacy(
363-
self,
364-
broken=False,
365-
timeout=False,
366-
raise_timeout=False,
367-
fail_auth=False,
368-
stretch=False,
369-
):
370-
"""Connect to a smile environment and perform basic asserts."""
371-
port = aiohttp.test_utils.unused_port()
372-
test_password = "".join(
373-
secrets.choice(string.ascii_lowercase) for _ in range(8)
374-
)
375-
376-
# Happy flow
377-
app = await self.setup_legacy_app(
378-
broken, timeout, raise_timeout, fail_auth, stretch
379-
)
380-
381-
server = aiohttp.test_utils.TestServer(
382-
app, port=port, scheme="http", host="127.0.0.1"
383-
)
384-
await server.start_server()
385-
386-
client = aiohttp.test_utils.TestClient(server)
387-
websession = client.session
388-
389-
url = f"{server.scheme}://{server.host}:{server.port}{CORE_LOCATIONS}"
390-
391-
# Try/exceptpass to accommodate for Timeout of aoihttp
392-
try:
393-
resp = await websession.get(url)
394-
assumed_status = self.connect_status(broken, timeout, fail_auth)
395-
assert resp.status == assumed_status
396-
timeoutpass_result = False
397-
assert timeoutpass_result
398-
except Exception: # pylint: disable=broad-except
399-
timeoutpass_result = True
400-
assert timeoutpass_result
401-
402-
if not broken and not timeout and not fail_auth:
403-
text = await resp.text()
404-
assert "xml" in text
405-
406-
# Test lack of websession
407-
try:
408-
smile = pw_smile.Smile(
409-
host=server.host,
410-
username=pw_constants.DEFAULT_USERNAME,
411-
password=test_password,
412-
port=server.port,
413-
websession=None,
414-
)
415-
lack_of_websession = False
416-
assert lack_of_websession
417-
except Exception: # pylint: disable=broad-except
418-
lack_of_websession = True
419-
assert lack_of_websession
420-
421-
smile = pw_smile.Smile(
422-
host=server.host,
423-
username=pw_constants.DEFAULT_USERNAME,
424-
password=test_password,
425-
port=server.port,
426-
websession=websession,
427-
)
428-
429-
if not timeout:
430-
assert smile._timeout == 30
431-
432-
# Connect to the smile
433-
smile_version = None
434-
try:
435-
smile_version = await smile.connect()
436-
assert smile_version is not None
437-
assert smile._timeout == 30
354+
assert smile._timeout == real_timeout_value
438355
return server, smile, client
439356
except (
440357
pw_exceptions.ConnectionFailedError,
@@ -453,7 +370,7 @@ async def connect_wrapper(
453370
if fail_auth:
454371
try:
455372
_LOGGER.warning("Connecting to device with invalid credentials:")
456-
await self.connect(fail_auth=fail_auth)
373+
await self.connect(self.setup_app, fail_auth=fail_auth)
457374
_LOGGER.error(" - invalid credentials not handled") # pragma: no cover
458375
raise self.ConnectError # pragma: no cover
459376
except pw_exceptions.InvalidAuthentication as exc:
@@ -462,53 +379,73 @@ async def connect_wrapper(
462379

463380
if raise_timeout:
464381
_LOGGER.warning("Connecting to device exceeding timeout in handling:")
465-
return await self.connect(raise_timeout=True)
382+
return await self.connect(self.setup_app, raise_timeout=True)
466383

467384
try:
468385
_LOGGER.warning("Connecting to device exceeding timeout in response:")
469-
await self.connect(timeout=True)
386+
await self.connect(self.setup_app, timeout_happened=True)
470387
_LOGGER.error(" - timeout not handled") # pragma: no cover
471388
raise self.ConnectError # pragma: no cover
472389
except pw_exceptions.ConnectionFailedError:
473390
_LOGGER.info(" + successfully passed timeout handling.")
474391

475392
try:
476393
_LOGGER.warning("Connecting to device with missing data:")
477-
await self.connect(broken=True)
394+
await self.connect(self.setup_app, broken=True)
478395
_LOGGER.error(" - broken information not handled") # pragma: no cover
479396
raise self.ConnectError # pragma: no cover
480397
except pw_exceptions.InvalidXMLError:
481398
_LOGGER.info(" + successfully passed XML issue handling.")
482399

483400
_LOGGER.info("Connecting to functioning device:")
484-
return await self.connect(stretch=stretch)
401+
return await self.connect(self.setup_app, stretch=stretch)
485402

486403
async def connect_legacy_wrapper(
487404
self, raise_timeout=False, fail_auth=False, stretch=False
488405
):
489406
"""Wrap connect to try negative testing before positive testing."""
490407
if raise_timeout:
491408
_LOGGER.warning("Connecting to device exceeding timeout in handling:")
492-
return await self.connect_legacy(raise_timeout=True)
409+
return await self.connect(
410+
self.setup_legacy_app,
411+
raise_timeout=True,
412+
real_timeout_value=30,
413+
url_part=CORE_LOCATIONS,
414+
)
493415

494416
try:
495417
_LOGGER.warning("Connecting to device exceeding timeout in response:")
496-
await self.connect_legacy(timeout=True)
418+
await self.connect(
419+
self.setup_legacy_app,
420+
real_timeout_value=30,
421+
timeout_happened=True,
422+
url_part=CORE_LOCATIONS,
423+
)
497424
_LOGGER.error(" - timeout not handled") # pragma: no cover
498425
raise self.ConnectError # pragma: no cover
499426
except pw_exceptions.ConnectionFailedError:
500427
_LOGGER.info(" + successfully passed timeout handling.")
501428

502429
try:
503430
_LOGGER.warning("Connecting to device with missing data:")
504-
await self.connect_legacy(broken=True)
431+
await self.connect(
432+
self.setup_legacy_app,
433+
broken=True,
434+
real_timeout_value=30,
435+
url_part=CORE_LOCATIONS,
436+
)
505437
_LOGGER.error(" - broken information not handled") # pragma: no cover
506438
raise self.ConnectError # pragma: no cover
507439
except pw_exceptions.InvalidXMLError:
508440
_LOGGER.info(" + successfully passed XML issue handling.")
509441

510442
_LOGGER.info("Connecting to functioning device:")
511-
return await self.connect_legacy(stretch=stretch)
443+
return await self.connect(
444+
self.setup_legacy_app,
445+
real_timeout_value=30,
446+
stretch=stretch,
447+
url_part=CORE_LOCATIONS,
448+
)
512449

513450
# Generic disconnect
514451
@classmethod

0 commit comments

Comments
 (0)