Skip to content

Commit 8bdae45

Browse files
committed
Replace xml attrib[] by get()
1 parent c005c19 commit 8bdae45

File tree

6 files changed

+31
-31
lines changed

6 files changed

+31
-31
lines changed

plugwise/common.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,14 @@ def _get_groups(self) -> dict[str, GwEntityData]:
200200

201201
for group in self._domain_objects.findall("./group"):
202202
members: list[str] = []
203-
group_id = group.attrib["id"]
203+
group_id = group.get("id")
204204
group_name = group.find("name").text
205205
group_type = group.find("type").text
206206
group_appliances = group.findall("appliances/appliance")
207207
for item in group_appliances:
208208
# Check if members are not orphaned - stretch
209-
if item.attrib["id"] in self.gw_entities:
210-
members.append(item.attrib["id"])
209+
if item.get("id") in self.gw_entities:
210+
members.append(item.get("id"))
211211

212212
if group_type in GROUP_TYPES and members:
213213
groups[group_id] = {
@@ -266,7 +266,7 @@ def _get_module_data(
266266
if key is not None and key not in link_tag:
267267
continue
268268

269-
link_id = appl_search.attrib["id"]
269+
link_id = appl_search.get("id")
270270
loc = f".//services/{link_tag}[@id='{link_id}']...."
271271
# Not possible to walrus for some reason...
272272
# xml_2: self._modules for legacy, self._domain_objects for actual

plugwise/helper.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _get_appliances(self) -> None:
117117
for appliance in self._domain_objects.findall("./appliance"):
118118
appl = Munch()
119119
appl.available = None
120-
appl.entity_id = appliance.attrib["id"]
120+
appl.entity_id = appliance.get("id")
121121
appl.location = None
122122
appl.name = appliance.find("name").text
123123
appl.model = None
@@ -139,7 +139,7 @@ def _get_appliances(self) -> None:
139139
continue
140140

141141
if (appl_loc := appliance.find("location")) is not None:
142-
appl.location = appl_loc.attrib["id"]
142+
appl.location = appl_loc.get("id")
143143
# Set location to the _home_loc_id when the appliance-location is not found,
144144
# except for thermostat-devices without a location, they are not active
145145
elif appl.pwclass not in THERMOSTAT_CLASSES:
@@ -206,7 +206,7 @@ def _get_locations(self) -> None:
206206
loc = Munch()
207207
locations = self._domain_objects.findall("./location")
208208
for location in locations:
209-
loc.loc_id = location.attrib["id"]
209+
loc.loc_id = location.get("id")
210210
loc.name = location.find("name").text
211211
loc._type = location.find("type").text
212212
self._loc_data[loc.loc_id] = {
@@ -269,7 +269,7 @@ def _appliance_info_finder(self, appl: Munch, appliance: etree.Element) -> Munch
269269

270270
def _appl_gateway_info(self, appl: Munch, appliance: etree.Element) -> Munch:
271271
"""Helper-function for _appliance_info_finder()."""
272-
self._gateway_id = appliance.attrib["id"]
272+
self._gateway_id = appliance.get("id")
273273
appl.firmware = str(self.smile.version)
274274
appl.hardware = self.smile.hw_version
275275
appl.mac = self.smile.mac_address
@@ -319,7 +319,7 @@ def _get_appliances_with_offset_functionality(self) -> list[str]:
319319
'.//actuator_functionalities/offset_functionality[type="temperature_offset"]/offset/../../..'
320320
)
321321
for item in offset_appls:
322-
therm_list.append(item.attrib["id"])
322+
therm_list.append(item.get("id"))
323323

324324
return therm_list
325325

@@ -502,7 +502,7 @@ def _get_plugwise_notifications(self) -> None:
502502
self._notifications = {}
503503
for notification in self._domain_objects.findall("./notification"):
504504
try:
505-
msg_id = notification.attrib["id"]
505+
msg_id = notification.get("id")
506506
msg_type = notification.find("type").text
507507
msg = notification.find("message").text
508508
self._notifications[msg_id] = {msg_type: msg}
@@ -903,7 +903,7 @@ def _presets(self, loc_id: str) -> dict[str, list[float]]:
903903
directives = self._domain_objects.find(f'rule[@id="{rule_id}"]/directives')
904904
for directive in directives:
905905
preset = directive.find("then").attrib
906-
presets[directive.attrib["preset"]] = [
906+
presets[directive.get("preset")] = [
907907
float(preset["heating_setpoint"]),
908908
float(preset["cooling_setpoint"]),
909909
]
@@ -920,13 +920,13 @@ def _rule_ids_by_name(self, name: str, loc_id: str) -> dict[str, dict[str, str]]
920920
for rule in self._domain_objects.findall(f'./rule[name="{name}"]'):
921921
active = rule.find("active").text
922922
if rule.find(locator) is not None:
923-
schedule_ids[rule.attrib["id"]] = {
923+
schedule_ids[rule.get("id")] = {
924924
"location": loc_id,
925925
"name": name,
926926
"active": active,
927927
}
928928
else:
929-
schedule_ids[rule.attrib["id"]] = {
929+
schedule_ids[rule.get("id")] = {
930930
"location": NONE,
931931
"name": name,
932932
"active": active,
@@ -947,13 +947,13 @@ def _rule_ids_by_tag(self, tag: str, loc_id: str) -> dict[str, dict[str, str]]:
947947
name = rule.find("name").text
948948
active = rule.find("active").text
949949
if rule.find(locator2) is not None:
950-
schedule_ids[rule.attrib["id"]] = {
950+
schedule_ids[rule.get("id")] = {
951951
"location": loc_id,
952952
"name": name,
953953
"active": active,
954954
}
955955
else:
956-
schedule_ids[rule.attrib["id"]] = {
956+
schedule_ids[rule.get("id")] = {
957957
"location": NONE,
958958
"name": name,
959959
"active": active,
@@ -1002,6 +1002,6 @@ def _thermostat_uri(self, loc_id: str) -> str:
10021002
Determine the location-set_temperature uri - from LOCATIONS.
10031003
"""
10041004
locator = f'./location[@id="{loc_id}"]/actuator_functionalities/thermostat_functionality'
1005-
thermostat_functionality_id = self._domain_objects.find(locator).attrib["id"]
1005+
thermostat_functionality_id = self._domain_objects.find(locator).get("id")
10061006

10071007
return f"{LOCATIONS};id={loc_id}/thermostat;id={thermostat_functionality_id}"

plugwise/legacy/helper.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _all_appliances(self) -> None:
107107
continue # pragma: no cover
108108

109109
appl.location = self._home_loc_id
110-
appl.entity_id = appliance.attrib["id"]
110+
appl.entity_id = appliance.get("id")
111111
appl.name = appliance.find("name").text
112112
# Extend device_class name when a Circle/Stealth is type heater_central -- Pw-Beta Issue #739
113113
if (
@@ -148,7 +148,7 @@ def _all_locations(self) -> None:
148148
return
149149

150150
for location in locations:
151-
loc.loc_id = location.attrib["id"]
151+
loc.loc_id = location.get("id")
152152
loc.name = location.find("name").text
153153
loc._type = location.find("type").text
154154
# Filter the valid single location for P1 legacy: services not empty
@@ -395,8 +395,8 @@ def _presets(self) -> dict[str, list[float]]:
395395
for directive in self._domain_objects.findall("rule/directives/when/then"):
396396
if directive is not None and directive.get("icon") is not None:
397397
# Ensure list of heating_setpoint, cooling_setpoint
398-
presets[directive.attrib["icon"]] = [
399-
float(directive.attrib["temperature"]),
398+
presets[directive.get("icon")] = [
399+
float(directive.get("temperature")),
400400
0,
401401
]
402402

@@ -411,7 +411,7 @@ def _schedules(self) -> tuple[list[str], str]:
411411
search = self._domain_objects
412412
if (result := search.find("./rule[name='Thermostat schedule']")) is not None:
413413
name = "Thermostat schedule"
414-
rule_id = result.attrib["id"]
414+
rule_id = result.get("id")
415415

416416
log_type = "schedule_state"
417417
locator = f"./appliance[type='thermostat']/logs/point_log[type='{log_type}']/period/measurement"
@@ -432,5 +432,5 @@ def _schedules(self) -> tuple[list[str], str]:
432432
def _thermostat_uri(self) -> str:
433433
"""Determine the location-set_temperature uri - from APPLIANCES."""
434434
locator = "./appliance[type='thermostat']"
435-
appliance_id = self._appliances.find(locator).attrib["id"]
435+
appliance_id = self._appliances.find(locator).get("id")
436436
return f"{APPLIANCES};id={appliance_id}/thermostat"

plugwise/legacy/smile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ async def set_preset(self, _: str, preset: str) -> None:
160160
raise PlugwiseError("Plugwise: invalid preset.")
161161

162162
locator = f'rule/directives/when/then[@icon="{preset}"].../.../...'
163-
rule_id = self._domain_objects.find(locator).attrib["id"]
163+
rule_id = self._domain_objects.find(locator).get("id")
164164
data = f"<rules><rule id='{rule_id}'><active>true</active></rule></rules>"
165165
await self.call_request(RULES, method="put", data=data)
166166

@@ -192,7 +192,7 @@ async def set_schedule_state(
192192
schedule_rule_id: str | None = None
193193
for rule in self._domain_objects.findall("rule"):
194194
if rule.find("name").text == name:
195-
schedule_rule_id = rule.attrib["id"]
195+
schedule_rule_id = rule.get("id")
196196
break
197197

198198
if schedule_rule_id is None:
@@ -205,7 +205,7 @@ async def set_schedule_state(
205205
new_state = "true"
206206

207207
locator = f'.//*[@id="{schedule_rule_id}"]/template'
208-
template_id = self._domain_objects.find(locator).attrib["id"]
208+
template_id = self._domain_objects.find(locator).get("id")
209209

210210
data = (
211211
"<rules>"

plugwise/smile.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ async def set_number(
178178
if th_func_list := self._domain_objects.findall(locator):
179179
for th_func in th_func_list:
180180
if th_func.find("type").text == key:
181-
thermostat_id = th_func.attrib["id"]
181+
thermostat_id = th_func.get("id")
182182

183183
if thermostat_id is None:
184184
raise PlugwiseError(f"Plugwise: cannot change setpoint, {key} not found.")
@@ -358,7 +358,7 @@ async def set_schedule_state(
358358
)
359359
if self.check_name(ANNA):
360360
locator = f'.//*[@id="{schedule_rule_id}"]/template'
361-
template_id = self._domain_objects.find(locator).attrib["id"]
361+
template_id = self._domain_objects.find(locator).get("id")
362362
template = f'<template id="{template_id}" />'
363363

364364
contexts = self.determine_contexts(loc_id, new_state, schedule_rule_id)
@@ -427,10 +427,10 @@ async def set_switch_state(
427427
# multiple types of e.g. toggle_functionality present
428428
if (sw_type := item.find("type")) is not None:
429429
if sw_type.text == switch.act_type:
430-
switch_id = item.attrib["id"]
430+
switch_id = item.get("id")
431431
break
432432
else: # actuators with a single item like relay_functionality
433-
switch_id = item.attrib["id"]
433+
switch_id = item.get("id")
434434

435435
uri = f"{APPLIANCES};id={appl_id}/{switch.device};id={switch_id}"
436436
if model == "relay":
@@ -456,7 +456,7 @@ async def _set_groupswitch_member_state(
456456
switched = 0
457457
for member in members:
458458
locator = f'appliance[@id="{member}"]/{switch.actuator}/{switch.func_type}'
459-
switch_id = self._domain_objects.find(locator).attrib["id"]
459+
switch_id = self._domain_objects.find(locator).get("id")
460460
uri = f"{APPLIANCES};id={member}/{switch.device};id={switch_id}"
461461
lock_blocked = self.gw_entities[member]["switches"].get("lock")
462462
# Assume Plugs under Plugwise control are not part of a group

plugwise/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def check_heater_central(xml: etree.Element) -> str:
8686
hc_list: list[dict[str, bool]] = []
8787
for heater_central in xml.findall(locator):
8888
hc_count += 1
89-
hc_id: str = heater_central.attrib["id"]
89+
hc_id: str = heater_central.get("id")
9090
has_actuators: bool = (
9191
heater_central.find("actuator_functionalities/") is not None
9292
)

0 commit comments

Comments
 (0)