Skip to content

Commit 0f96c53

Browse files
committed
Improve xml readability
1 parent fdd8f1c commit 0f96c53

File tree

2 files changed

+96
-56
lines changed

2 files changed

+96
-56
lines changed

plugwise/legacy/smile.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,14 @@ async def set_preset(self, _: str, preset: str) -> None:
172172

173173
locator = f'rule/directives/when/then[@icon="{preset}"].../.../...'
174174
rule = self._domain_objects.find(locator)
175-
data = f'<rules><rule id="{rule.attrib["id"]}"><active>true</active></rule></rules>'
176-
177-
await self.call_request(RULES, method="put", data=data)
175+
data = f'''
176+
<rules>
177+
<rule id="{rule.attrib["id"]}">
178+
<active>true</active>
179+
</rule>
180+
</rules>
181+
'''
182+
await self.call_request(RULES, method="put", data=data.strip())
178183

179184
async def set_regulation_mode(self, mode: str) -> None:
180185
"""Set-function placeholder for legacy devices."""
@@ -219,14 +224,17 @@ async def set_schedule_state(
219224
for rule in self._domain_objects.findall(locator):
220225
template_id = rule.attrib["id"]
221226

227+
data = f'''
228+
<rules>
229+
<rule id="{schedule_rule_id}">
230+
<name><![CDATA[{name}]]></name>
231+
<template id="{template_id}" />
232+
<active>{new_state}</active>
233+
</rule>
234+
</rules>
235+
'''
222236
uri = f"{RULES};id={schedule_rule_id}"
223-
data = (
224-
"<rules><rule"
225-
f' id="{schedule_rule_id}"><name><![CDATA[{name}]]></name><template'
226-
f' id="{template_id}" /><active>{new_state}</active></rule></rules>'
227-
)
228-
229-
await self.call_request(uri, method="put", data=data)
237+
await self.call_request(uri, method="put", data=data.strip())
230238

231239
async def set_switch_state(
232240
self, appl_id: str, members: list[str] | None, model: str, state: str
@@ -262,8 +270,9 @@ async def set_switch_state(
262270
</{switch.func_type}>
263271
</{switch.actuator}>
264272
</appliance>
265-
</appliances>'''.strip()
266-
await self.call_request(APPLIANCES, method="post", data=data)
273+
</appliances>
274+
'''
275+
await self.call_request(APPLIANCES, method="post", data=data.strip())
267276
return
268277

269278
# Handle group of switches
@@ -308,13 +317,13 @@ async def set_temperature(self, _: str, items: dict[str, float]) -> None:
308317
) # pragma: no cover"
309318

310319
temperature = str(setpoint)
320+
data = f"""
321+
<thermostat_functionality>
322+
<setpoint>{temperature}</setpoint>
323+
</thermostat_functionality>
324+
"""
311325
uri = self._thermostat_uri()
312-
data = (
313-
"<thermostat_functionality><setpoint>"
314-
f"{temperature}</setpoint></thermostat_functionality>"
315-
)
316-
317-
await self.call_request(uri, method="put", data=data)
326+
await self.call_request(uri, method="put", data=data.strip())
318327

319328
async def call_request(self, uri: str, **kwargs: Any) -> None:
320329
"""ConnectionFailedError wrapper for calling request()."""

plugwise/smile.py

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,13 @@ async def set_number(
174174
if thermostat_id is None:
175175
raise PlugwiseError(f"Plugwise: cannot change setpoint, {key} not found.")
176176

177+
data = f"""
178+
<thermostat_functionality>
179+
<setpoint>{temp}</setpoint>
180+
</thermostat_functionality>
181+
"""
177182
uri = f"{APPLIANCES};id={self._heater_id}/thermostat;id={thermostat_id}"
178-
data = f"<thermostat_functionality><setpoint>{temp}</setpoint></thermostat_functionality>"
179-
await self.call_request(uri, method="put", data=data)
183+
await self.call_request(uri, method="put", data=data.strip())
180184

181185
async def set_offset(self, dev_id: str, offset: float) -> None:
182186
"""Set the Temperature offset for thermostats that support this feature."""
@@ -186,10 +190,13 @@ async def set_offset(self, dev_id: str, offset: float) -> None:
186190
)
187191

188192
value = str(offset)
193+
data = f"""
194+
<offset_functionality>
195+
<offset>{value}</offset>
196+
</offset_functionality>
197+
"""
189198
uri = f"{APPLIANCES};id={dev_id}/offset;type=temperature_offset"
190-
data = f"<offset_functionality><offset>{value}</offset></offset_functionality>"
191-
192-
await self.call_request(uri, method="put", data=data)
199+
await self.call_request(uri, method="put", data=data.strip())
193200

194201
async def set_preset(self, loc_id: str, preset: str) -> None:
195202
"""Set the given Preset on the relevant Thermostat - from LOCATIONS."""
@@ -201,14 +208,16 @@ async def set_preset(self, loc_id: str, preset: str) -> None:
201208
current_location = self._domain_objects.find(f'location[@id="{loc_id}"]')
202209
location_name = current_location.find("name").text
203210
location_type = current_location.find("type").text
204-
211+
data = f'''
212+
<locations>
213+
<location id="{loc_id}">
214+
<name>{location_name}</name>
215+
<type>{location_type}</type>
216+
<preset>{preset}</preset>
217+
</location>
218+
</locations>
219+
'''
205220
uri = f"{LOCATIONS};id={loc_id}"
206-
data = (
207-
"<locations><location"
208-
f' id="{loc_id}"><name>{location_name}</name><type>{location_type}'
209-
f"</type><preset>{preset}</preset></location></locations>"
210-
)
211-
212221
await self.call_request(uri, method="put", data=data)
213222

214223
async def set_select(
@@ -231,10 +240,13 @@ async def set_dhw_mode(self, mode: str) -> None:
231240
if mode not in self._dhw_allowed_modes:
232241
raise PlugwiseError("Plugwise: invalid dhw mode.")
233242

243+
data = f"""
244+
<domestic_hot_water_mode_control_functionality>
245+
<mode>{mode}</mode>
246+
</domestic_hot_water_mode_control_functionality>
247+
"""
234248
uri = f"{APPLIANCES};type=heater_central/domestic_hot_water_mode_control"
235-
data = f"<domestic_hot_water_mode_control_functionality><mode>{mode}</mode></domestic_hot_water_mode_control_functionality>"
236-
237-
await self.call_request(uri, method="put", data=data)
249+
await self.call_request(uri, method="put", data=data.strip())
238250

239251
async def set_gateway_mode(self, mode: str) -> None:
240252
"""Set the gateway mode."""
@@ -259,23 +271,32 @@ async def set_gateway_mode(self, mode: str) -> None:
259271
vacation_time = time_2 + "T23:00:00.000Z"
260272
valid = f"<valid_from>{vacation_time}</valid_from><valid_to>{end_time}</valid_to>"
261273

274+
data = f"""
275+
<gateway_mode_control_functionality>
276+
<mode>{mode}</mode>
277+
{valid}
278+
</gateway_mode_control_functionality>
279+
"""
262280
uri = f"{APPLIANCES};id={self._smile_props['gateway_id']}/gateway_mode_control"
263-
data = f"<gateway_mode_control_functionality><mode>{mode}</mode>{valid}</gateway_mode_control_functionality>"
264-
265-
await self.call_request(uri, method="put", data=data)
281+
await self.call_request(uri, method="put", data=data.strip())
266282

267283
async def set_regulation_mode(self, mode: str) -> None:
268284
"""Set the heating regulation mode."""
269285
if mode not in self._reg_allowed_modes:
270286
raise PlugwiseError("Plugwise: invalid regulation mode.")
271287

272-
uri = f"{APPLIANCES};type=gateway/regulation_mode_control"
273288
duration = ""
274289
if "bleeding" in mode:
275290
duration = "<duration>300</duration>"
276-
data = f"<regulation_mode_control_functionality>{duration}<mode>{mode}</mode></regulation_mode_control_functionality>"
277291

278-
await self.call_request(uri, method="put", data=data)
292+
data = f"""
293+
<regulation_mode_control_functionality>
294+
{duration}
295+
<mode>{mode}</mode>
296+
</regulation_mode_control_functionality>
297+
"""
298+
uri = f"{APPLIANCES};type=gateway/regulation_mode_control"
299+
await self.call_request(uri, method="put", data=data.strip())
279300

280301
async def set_schedule_state(
281302
self,
@@ -323,13 +344,17 @@ async def set_schedule_state(
323344
template = f'<template id="{template_id}" />'
324345

325346
contexts = self.determine_contexts(loc_id, name, new_state, schedule_rule_id)
347+
data = f'''
348+
<rules>
349+
<rule id="{schedule_rule_id}">
350+
<name><![CDATA[{name}]]></name>
351+
{template}
352+
{contexts}
353+
</rule>
354+
</rules>
355+
'''
326356
uri = f"{RULES};id={schedule_rule_id}"
327-
data = (
328-
f'<rules><rule id="{schedule_rule_id}"><name><![CDATA[{name}]]></name>'
329-
f"{template}{contexts}</rule></rules>"
330-
)
331-
332-
await self.call_request(uri, method="put", data=data)
357+
await self.call_request(uri, method="put", data=data.strip())
333358
self._schedule_old_states[loc_id][name] = new_state
334359

335360
def determine_contexts(
@@ -387,9 +412,12 @@ async def set_switch_state(
387412
switch_id = item.attrib["id"]
388413
break
389414

415+
data = f"""
416+
<{switch.func_type}>
417+
<{switch.func}>{state}</{switch.func}>
418+
</{switch.func_type}>
419+
"""
390420
uri = f"{APPLIANCES};id={appl_id}/{switch.device};id={switch_id}"
391-
data = f"<{switch.func_type}><{switch.func}>{state}</{switch.func}></{switch.func_type}>"
392-
393421
if model == "relay":
394422
locator = (
395423
f'appliance[@id="{appl_id}"]/{switch.actuator}/{switch.func_type}/lock'
@@ -411,9 +439,12 @@ async def _set_groupswitch_member_state(
411439
locator = f'appliance[@id="{member}"]/{switch.actuator}/{switch.func_type}'
412440
switch_id = self._domain_objects.find(locator).attrib["id"]
413441
uri = f"{APPLIANCES};id={member}/{switch.device};id={switch_id}"
414-
data = f"<{switch.func_type}><{switch.func}>{state}</{switch.func}></{switch.func_type}>"
415-
416-
await self.call_request(uri, method="put", data=data)
442+
data = f"""
443+
<{switch.func_type}>
444+
<{switch.func}>{state}</{switch.func}>
445+
</{switch.func_type}>
446+
"""
447+
await self.call_request(uri, method="put", data=data.strip())
417448

418449
async def set_temperature(self, loc_id: str, items: dict[str, float]) -> None:
419450
"""Set the given Temperature on the relevant Thermostat."""
@@ -448,13 +479,13 @@ async def set_temperature(self, loc_id: str, items: dict[str, float]) -> None:
448479
) # pragma: no cover"
449480

450481
temperature = str(setpoint)
482+
data = f"""
483+
<thermostat_functionality>
484+
<setpoint>{temperature}</setpoint>
485+
</thermostat_functionality>
486+
"""
451487
uri = self._thermostat_uri(loc_id)
452-
data = (
453-
"<thermostat_functionality><setpoint>"
454-
f"{temperature}</setpoint></thermostat_functionality>"
455-
)
456-
457-
await self.call_request(uri, method="put", data=data)
488+
await self.call_request(uri, method="put", data=data.strip())
458489

459490
async def call_request(self, uri: str, **kwargs: Any) -> None:
460491
"""ConnectionFailedError wrapper for calling request()."""

0 commit comments

Comments
 (0)