Skip to content

Commit 90ed6f6

Browse files
committed
Move error-message in raise, clean up
1 parent 1d3708e commit 90ed6f6

File tree

1 file changed

+34
-59
lines changed

1 file changed

+34
-59
lines changed

plugwise/smile.py

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
from .exceptions import (
3535
ConnectionFailedError,
3636
InvalidSetupError,
37-
InvalidXMLError,
37+
PlugwiseException,
3838
UnsupportedDeviceError,
3939
)
4040
from .helper import SmileComm, SmileHelper, update_helper
@@ -275,21 +275,18 @@ async def connect(self) -> bool:
275275
dsmrmain = result.find("./module/protocols/dsmrmain")
276276
if "Plugwise" not in names:
277277
if dsmrmain is None: # pragma: no cover
278-
LOGGER.error(
279-
"Connected but expected text not returned, \
280-
we got %s. Please create an issue on \
281-
http://github.com/plugwise/python-plugwise",
282-
result,
278+
raise ConnectionFailedError(
279+
f"Connected but expected text not returned, \
280+
we got {result}. Please create an issue on \
281+
http://github.com/plugwise/python-plugwise"
283282
)
284-
raise ConnectionFailedError
285283

286284
# Check if Anna is connected to an Adam
287285
if "159.2" in models:
288-
LOGGER.error(
286+
raise InvalidSetupError(
289287
"Your Anna is connected to an Adam, make \
290-
sure to only add the Adam as integration.",
288+
sure to only add the Adam as integration."
291289
)
292-
raise InvalidSetupError
293290

294291
# Determine smile specifics
295292
await self._smile_detect(result, dsmrmain)
@@ -320,38 +317,31 @@ async def _smile_detect_legacy(self, result: etree, dsmrmain: etree) -> str:
320317
if result.find('./appliance[type="thermostat"]') is None:
321318
# It's a P1 legacy:
322319
if dsmrmain is not None:
323-
try:
324-
status = await self._request(STATUS)
325-
self.smile_fw_version = status.find("./system/version").text
326-
model = status.find("./system/product").text
327-
self.smile_hostname = status.find("./network/hostname").text
328-
self.smile_mac_address = status.find("./network/mac_address").text
329-
except InvalidXMLError: # pragma: no cover
330-
# Corner case check
331-
raise ConnectionFailedError
320+
status = await self._request(STATUS)
321+
self.smile_fw_version = status.find("./system/version").text
322+
model = status.find("./system/product").text
323+
self.smile_hostname = status.find("./network/hostname").text
324+
self.smile_mac_address = status.find("./network/mac_address").text
332325

333326
# Or a legacy Stretch:
334327
elif network is not None:
335-
try:
336-
system = await self._request(SYSTEM)
337-
self.smile_fw_version = system.find("./gateway/firmware").text
338-
model = system.find("./gateway/product").text
339-
self.smile_hostname = system.find("./gateway/hostname").text
340-
# If wlan0 contains data it's active, so eth0 should be checked last
341-
for network in ["wlan0", "eth0"]:
342-
locator = f"./{network}/mac"
343-
if (net_locator := system.find(locator)) is not None:
344-
self.smile_mac_address = net_locator.text
345-
except InvalidXMLError: # pragma: no cover
346-
# Corner case check
347-
raise ConnectionFailedError
328+
system = await self._request(SYSTEM)
329+
self.smile_fw_version = system.find("./gateway/firmware").text
330+
model = system.find("./gateway/product").text
331+
self.smile_hostname = system.find("./gateway/hostname").text
332+
# If wlan0 contains data it's active, so eth0 should be checked last
333+
for network in ["wlan0", "eth0"]:
334+
locator = f"./{network}/mac"
335+
if (net_locator := system.find(locator)) is not None:
336+
self.smile_mac_address = net_locator.text
337+
348338
else: # pragma: no cover
349339
# No cornercase, just end of the line
350-
LOGGER.error(
340+
raise ConnectionFailedError(
351341
"Connected but no gateway device information found, please create \
352342
an issue on http://github.com/plugwise/python-plugwise"
353343
)
354-
raise ConnectionFailedError
344+
355345
return model
356346

357347
async def _smile_detect(self, result: etree, dsmrmain: etree) -> None:
@@ -370,23 +360,20 @@ async def _smile_detect(self, result: etree, dsmrmain: etree) -> None:
370360

371361
if model is None or self.smile_fw_version is None: # pragma: no cover
372362
# Corner case check
373-
LOGGER.error(
363+
raise UnsupportedDeviceError(
374364
"Unable to find model or version information, please create \
375365
an issue on http://github.com/plugwise/python-plugwise"
376366
)
377-
raise UnsupportedDeviceError
378367

379368
ver = semver.VersionInfo.parse(self.smile_fw_version)
380369
target_smile = f"{model}_v{ver.major}"
381370
LOGGER.debug("Plugwise identified as %s", target_smile)
382371
if target_smile not in SMILES:
383-
LOGGER.error(
384-
'Your version Smile identified as "%s" seems\
372+
raise UnsupportedDeviceError(
373+
"Your version Smile identified as {target_smile} seems\
385374
unsupported by our plugin, please create an issue\
386-
on http://github.com/plugwise/python-plugwise',
387-
target_smile,
375+
on http://github.com/plugwise/python-plugwise"
388376
)
389-
raise UnsupportedDeviceError
390377

391378
self.smile_name = SMILES[target_smile]["friendly_name"]
392379
self.smile_type = SMILES[target_smile]["type"]
@@ -480,7 +467,7 @@ async def _set_schedule_state_legacy(self, name: str, status: str) -> bool:
480467
schedule_rule_id = rule.attrib["id"]
481468

482469
if schedule_rule_id is None:
483-
return False
470+
raise PlugwiseException("No schedule available.")
484471

485472
state = "false"
486473
if status == "on":
@@ -497,7 +484,6 @@ async def _set_schedule_state_legacy(self, name: str, status: str) -> bool:
497484
)
498485

499486
await self._request(uri, method="put", data=data)
500-
return True
501487

502488
async def set_schedule_state(self, loc_id: str, name: str, state: str) -> bool:
503489
"""Set the Schedule, with the given name, on the relevant Thermostat.
@@ -508,7 +494,7 @@ async def set_schedule_state(self, loc_id: str, name: str, state: str) -> bool:
508494

509495
schedule_rule = self._rule_ids_by_name(name, loc_id)
510496
if not schedule_rule or schedule_rule is None:
511-
return False
497+
raise PlugwiseException("No schedule available.")
512498

513499
schedule_rule_id: str = next(iter(schedule_rule))
514500

@@ -543,19 +529,16 @@ async def set_schedule_state(self, loc_id: str, name: str, state: str) -> bool:
543529
)
544530
await self._request(uri, method="put", data=data)
545531

546-
return True
547-
548532
async def _set_preset_legacy(self, preset: str) -> bool:
549533
"""Set the given Preset on the relevant Thermostat - from DOMAIN_OBJECTS."""
550534
locator = f'rule/directives/when/then[@icon="{preset}"].../.../...'
551535
if (rule := self._domain_objects.find(locator)) is None:
552-
return False
536+
raise PlugwiseException("No presets available.")
553537

554538
uri = RULES
555539
data = f'<rules><rule id="{rule.attrib["id"]}"><active>true</active></rule></rules>'
556540

557541
await self._request(uri, method="put", data=data)
558-
return True
559542

560543
async def set_preset(self, loc_id: str, preset: str) -> bool:
561544
"""Set the given Preset on the relevant Thermostat - from LOCATIONS."""
@@ -567,7 +550,7 @@ async def set_preset(self, loc_id: str, preset: str) -> bool:
567550
location_type = current_location.find("type").text
568551

569552
if preset not in self._presets(loc_id):
570-
return False
553+
raise PlugwiseException("Preset not available.")
571554

572555
uri = f"{LOCATIONS};id={loc_id}"
573556
data = (
@@ -577,7 +560,6 @@ async def set_preset(self, loc_id: str, preset: str) -> bool:
577560
)
578561

579562
await self._request(uri, method="put", data=data)
580-
return True
581563

582564
async def set_temperature(self, loc_id: str, temperature: str) -> bool:
583565
"""Set the given Temperature on the relevant Thermostat."""
@@ -588,7 +570,6 @@ async def set_temperature(self, loc_id: str, temperature: str) -> bool:
588570
)
589571

590572
await self._request(uri, method="put", data=data)
591-
return True
592573

593574
async def set_max_boiler_temperature(self, temperature: str) -> bool:
594575
"""Set the max. Boiler Temperature on the Central heating boiler."""
@@ -601,7 +582,6 @@ async def set_max_boiler_temperature(self, temperature: str) -> bool:
601582
data = f"<thermostat_functionality><setpoint>{temperature}</setpoint></thermostat_functionality>"
602583

603584
await self._request(uri, method="put", data=data)
604-
return True
605585

606586
async def _set_groupswitch_member_state(
607587
self, members: list[str], state: str, switch: Munch
@@ -619,8 +599,6 @@ async def _set_groupswitch_member_state(
619599

620600
await self._request(uri, method="put", data=data)
621601

622-
return True
623-
624602
async def set_switch_state(
625603
self, appl_id: str, members: list[str] | None, model: str, state: str
626604
) -> bool:
@@ -659,15 +637,14 @@ async def set_switch_state(
659637
lock_state: str = self._appliances.find(locator).text
660638
# Don't bother switching a relay when the corresponding lock-state is true
661639
if lock_state == "true":
662-
return False
640+
return
663641

664642
await self._request(uri, method="put", data=data)
665-
return True
666643

667644
async def set_regulation_mode(self, mode: str) -> bool:
668645
"""Set the heating regulation mode."""
669646
if mode not in self._allowed_modes:
670-
return False
647+
raise PlugwiseException("Invalid regulation mode.")
671648

672649
uri = f"{APPLIANCES};type=gateway/regulation_mode_control"
673650
duration = ""
@@ -676,11 +653,9 @@ async def set_regulation_mode(self, mode: str) -> bool:
676653
data = f"<regulation_mode_control_functionality>{duration}<mode>{mode}</mode></regulation_mode_control_functionality>"
677654

678655
await self._request(uri, method="put", data=data)
679-
return True
680656

681657
async def delete_notification(self) -> bool:
682658
"""Delete the active Plugwise Notification."""
683659
uri = NOTIFICATIONS
684660

685661
await self._request(uri, method="delete")
686-
return True

0 commit comments

Comments
 (0)