Skip to content

Commit e85dbcd

Browse files
committed
Revert "Smile.py: import _find and _findall and implement"
This reverts commit c3ae55b.
1 parent 58efd74 commit e85dbcd

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

plugwise/smile.py

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
SmileSwitches,
4141
)
4242
from .exceptions import PlugwiseError, ResponseError, UnsupportedDeviceError
43-
from .helper import SmileComm, SmileHelper, _find, _findall, update_helper
43+
from .helper import SmileComm, SmileHelper, update_helper
4444

4545

4646
class SmileData(SmileHelper):
@@ -124,11 +124,11 @@ def get_all_devices(self) -> None:
124124
# Start by determining the system capabilities:
125125
# Find the connected heating/cooling device (heater_central), e.g. heat-pump or gas-fired heater
126126
if self.smile_type == "thermostat":
127-
onoff_boiler: etree = _find(
128-
self._domain_objects, "./module/protocols/onoff_boiler"
127+
onoff_boiler: etree = self._domain_objects.find(
128+
"./module/protocols/onoff_boiler"
129129
)
130-
open_therm_boiler: etree = _find(
131-
self._domain_objects, "./module/protocols/open_therm_boiler"
130+
open_therm_boiler: etree = self._domain_objects.find(
131+
"./module/protocols/open_therm_boiler"
132132
)
133133
self._on_off_device = onoff_boiler is not None
134134
self._opentherm_device = open_therm_boiler is not None
@@ -138,10 +138,10 @@ def get_all_devices(self) -> None:
138138
locator_2 = "./gateway/features/elga_support"
139139
search = self._domain_objects
140140
self._cooling_present = False
141-
if _find(search, locator_1) is not None:
141+
if search.find(locator_1) is not None:
142142
self._cooling_present = True
143143
# Alternative method for the Anna with Elga
144-
elif _find(search, locator_2) is not None:
144+
elif search.find(locator_2) is not None:
145145
self._cooling_present = True
146146
self._elga = True
147147

@@ -343,15 +343,15 @@ async def connect(self) -> bool:
343343
"""Connect to Plugwise device and determine its name, type and version."""
344344
result = await self._request(DOMAIN_OBJECTS)
345345
# Work-around for Stretch fv 2.7.18
346-
if not (vendor_names := _findall(result, "./module/vendor_name")):
346+
if not (vendor_names := result.findall("./module/vendor_name")):
347347
result = await self._request(MODULES)
348-
vendor_names = _findall(result, "./module/vendor_name")
348+
vendor_names = result.findall("./module/vendor_name")
349349

350350
names: list[str] = []
351351
for name in vendor_names:
352352
names.append(name.text)
353353

354-
dsmrmain = _find(result, "./module/protocols/dsmrmain")
354+
dsmrmain = result.find("./module/protocols/dsmrmain")
355355
if "Plugwise" not in names and dsmrmain is None: # pragma: no cover
356356
LOGGER.error(
357357
"Connected but expected text not returned, we got %s. Please create \
@@ -371,42 +371,40 @@ async def connect(self) -> bool:
371371
async def _smile_detect_legacy(self, result: etree, dsmrmain: etree) -> str:
372372
"""Helper-function for _smile_detect()."""
373373
# Stretch: find the MAC of the zigbee master_controller (= Stick)
374-
if network := _find(result, "./module/protocols/master_controller"):
375-
self.smile_zigbee_mac_address = _find(network, "mac_address").text
374+
if network := result.find("./module/protocols/master_controller"):
375+
self.smile_zigbee_mac_address = network.find("mac_address").text
376376
# Find the active MAC in case there is an orphaned Stick
377-
if zb_networks := _findall(result, "./network"):
377+
if zb_networks := result.findall("./network"):
378378
for zb_network in zb_networks:
379-
if _find(zb_network, "./nodes/network_router"):
380-
network = _find(zb_network, "./master_controller")
381-
self.smile_zigbee_mac_address = _find(network, "mac_address").text
379+
if zb_network.find("./nodes/network_router"):
380+
network = zb_network.find("./master_controller")
381+
self.smile_zigbee_mac_address = network.find("mac_address").text
382382

383383
# Assume legacy
384384
self._smile_legacy = True
385385
# Try if it is a legacy Anna, assuming appliance thermostat,
386386
# fake insert version assuming Anna, couldn't find another way to identify as legacy Anna
387387
self.smile_fw_version = "1.8.0"
388388
model = "smile_thermo"
389-
if _find(result, './appliance[type="thermostat"]') is None:
389+
if result.find('./appliance[type="thermostat"]') is None:
390390
# It's a P1 legacy:
391391
if dsmrmain is not None:
392392
self._status = await self._request(STATUS)
393-
self.smile_fw_version = _find(self._status, "./system/version").text
394-
model = _find(self._status, "./system/product").text
395-
self.smile_hostname = _find(self._status, "./network/hostname").text
396-
self.smile_mac_address = _find(
397-
self._status, "./network/mac_address"
398-
).text
393+
self.smile_fw_version = self._status.find("./system/version").text
394+
model = self._status.find("./system/product").text
395+
self.smile_hostname = self._status.find("./network/hostname").text
396+
self.smile_mac_address = self._status.find("./network/mac_address").text
399397

400398
# Or a legacy Stretch:
401399
elif network is not None:
402400
self._system = await self._request(SYSTEM)
403-
self.smile_fw_version = _find(self._system, "./gateway/firmware").text
404-
model = _find(self._system, "./gateway/product").text
405-
self.smile_hostname = _find(self._system, "./gateway/hostname").text
401+
self.smile_fw_version = self._system.find("./gateway/firmware").text
402+
model = self._system.find("./gateway/product").text
403+
self.smile_hostname = self._system.find("./gateway/hostname").text
406404
# If wlan0 contains data it's active, so eth0 should be checked last
407405
for network in ("wlan0", "eth0"):
408406
locator = f"./{network}/mac"
409-
if (net_locator := _find(self._system, locator)) is not None:
407+
if (net_locator := self._system.find(locator)) is not None:
410408
self.smile_mac_address = net_locator.text
411409

412410
else: # pragma: no cover
@@ -424,12 +422,12 @@ async def _smile_detect(self, result: etree, dsmrmain: etree) -> None:
424422
Detect which type of Smile is connected.
425423
"""
426424
model: str | None = None
427-
if (gateway := _find(result, "./gateway")) is not None:
428-
model = _find(gateway, "vendor_model").text
429-
self.smile_fw_version = _find(gateway, "firmware_version").text
430-
self.smile_hw_version = _find(gateway, "hardware_version").text
431-
self.smile_hostname = _find(gateway, "hostname").text
432-
self.smile_mac_address = _find(gateway, "mac_address").text
425+
if (gateway := result.find("./gateway")) is not None:
426+
model = gateway.find("vendor_model").text
427+
self.smile_fw_version = gateway.find("firmware_version").text
428+
self.smile_hw_version = gateway.find("hardware_version").text
429+
self.smile_hostname = gateway.find("hostname").text
430+
self.smile_mac_address = gateway.find("mac_address").text
433431
else:
434432
model = await self._smile_detect_legacy(result, dsmrmain)
435433

@@ -484,11 +482,11 @@ async def _update_domain_objects(self) -> None:
484482

485483
# If Plugwise notifications present:
486484
self._notifications = {}
487-
for notification in _findall(self._domain_objects, "./notification"):
485+
for notification in self._domain_objects.findall("./notification"):
488486
try:
489487
msg_id = notification.attrib["id"]
490-
msg_type = _find(notification, "type").text
491-
msg = _find(notification, "message").text
488+
msg_type = notification.find("type").text
489+
msg = notification.find("message").text
492490
self._notifications.update({msg_id: {msg_type: msg}})
493491
LOGGER.debug("Plugwise notifications: %s", self._notifications)
494492
except AttributeError: # pragma: no cover
@@ -544,8 +542,8 @@ async def _set_schedule_state_legacy(
544542
) -> None:
545543
"""Helper-function for set_schedule_state()."""
546544
schedule_rule_id: str | None = None
547-
for rule in _findall(self._domain_objects, "rule"):
548-
if _find(rule, "name").text == name:
545+
for rule in self._domain_objects.findall("rule"):
546+
if rule.find("name").text == name:
549547
schedule_rule_id = rule.attrib["id"]
550548

551549
if schedule_rule_id is None:
@@ -559,7 +557,7 @@ async def _set_schedule_state_legacy(
559557
return
560558

561559
locator = f'.//*[@id="{schedule_rule_id}"]/template'
562-
for rule in _findall(self._domain_objects, locator):
560+
for rule in self._domain_objects.findall(locator):
563561
template_id = rule.attrib["id"]
564562

565563
uri = f"{RULES};id={schedule_rule_id}"
@@ -607,13 +605,13 @@ async def set_schedule_state(
607605
)
608606
if self.smile_name != "Adam":
609607
locator = f'.//*[@id="{schedule_rule_id}"]/template'
610-
template_id = _find(self._domain_objects, locator).attrib["id"]
608+
template_id = self._domain_objects.find(locator).attrib["id"]
611609
template = f'<template id="{template_id}" />'
612610

613611
locator = f'.//*[@id="{schedule_rule_id}"]/contexts'
614-
contexts = _find(self._domain_objects, locator)
612+
contexts = self._domain_objects.find(locator)
615613
locator = f'.//*[@id="{loc_id}"].../...'
616-
if (subject := _find(contexts, locator)) is None:
614+
if (subject := contexts.find(locator)) is None:
617615
subject = f'<context><zone><location id="{loc_id}" /></zone></context>'
618616
subject = etree.fromstring(subject)
619617

@@ -636,7 +634,7 @@ async def set_schedule_state(
636634
async def _set_preset_legacy(self, preset: str) -> None:
637635
"""Set the given Preset on the relevant Thermostat - from DOMAIN_OBJECTS."""
638636
locator = f'rule/directives/when/then[@icon="{preset}"].../.../...'
639-
rule = _find(self._domain_objects, locator)
637+
rule = self._domain_objects.find(locator)
640638
data = f'<rules><rule id="{rule.attrib["id"]}"><active>true</active></rule></rules>'
641639

642640
await self._request(RULES, method="put", data=data)
@@ -652,9 +650,9 @@ async def set_preset(self, loc_id: str, preset: str) -> None:
652650
await self._set_preset_legacy(preset)
653651
return
654652

655-
current_location = _find(self._locations, f'location[@id="{loc_id}"]')
656-
location_name = _find(current_location, "name").text
657-
location_type = _find(current_location, "type").text
653+
current_location = self._locations.find(f'location[@id="{loc_id}"]')
654+
location_name = current_location.find("name").text
655+
location_type = current_location.find("type").text
658656

659657
uri = f"{LOCATIONS};id={loc_id}"
660658
data = (
@@ -696,9 +694,9 @@ async def set_number_setpoint(self, key: str, temperature: float) -> None:
696694
temp = str(temperature)
697695
thermostat_id: str | None = None
698696
locator = f'appliance[@id="{self._heater_id}"]/actuator_functionalities/thermostat_functionality'
699-
if th_func_list := _findall(self._appliances, locator):
697+
if th_func_list := self._appliances.findall(locator):
700698
for th_func in th_func_list:
701-
if _find(th_func, "type").text == key:
699+
if th_func.find("type").text == key:
702700
thermostat_id = th_func.attrib["id"]
703701

704702
if thermostat_id is None:
@@ -716,7 +714,7 @@ async def _set_groupswitch_member_state(
716714
"""
717715
for member in members:
718716
locator = f'appliance[@id="{member}"]/{switch.actuator}/{switch.func_type}'
719-
switch_id = _find(self._appliances, locator).attrib["id"]
717+
switch_id = self._appliances.find(locator).attrib["id"]
720718
uri = f"{APPLIANCES};id={member}/{switch.device};id={switch_id}"
721719
if self._stretch_v2:
722720
uri = f"{APPLIANCES};id={member}/{switch.device}"
@@ -755,9 +753,9 @@ async def set_switch_state(
755753
return await self._set_groupswitch_member_state(members, state, switch)
756754

757755
locator = f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}'
758-
found: list[etree] = _findall(self._appliances, locator)
756+
found: list[etree] = self._appliances.findall(locator)
759757
for item in found:
760-
if (sw_type := _find(item, "type")) is not None:
758+
if (sw_type := item.find("type")) is not None:
761759
if sw_type.text == switch.act_type:
762760
switch_id = item.attrib["id"]
763761
else:
@@ -774,7 +772,7 @@ async def set_switch_state(
774772
f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}/lock'
775773
)
776774
# Don't bother switching a relay when the corresponding lock-state is true
777-
if _find(self._appliances, locator).text == "true":
775+
if self._appliances.find(locator).text == "true":
778776
raise PlugwiseError("Plugwise: the locked Relay was not switched.")
779777

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

0 commit comments

Comments
 (0)