Skip to content

Commit 1a6d924

Browse files
authored
Merge pull request #709 from plugwise/redo-xml
Remove f-strings with .strip(), not working
2 parents b74e505 + 22ec44f commit 1a6d924

File tree

2 files changed

+100
-110
lines changed

2 files changed

+100
-110
lines changed

plugwise/legacy/smile.py

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,9 @@ async def set_preset(self, _: str, preset: str) -> None:
171171
raise PlugwiseError("Plugwise: invalid preset.")
172172

173173
locator = f'rule/directives/when/then[@icon="{preset}"].../.../...'
174-
rule = self._domain_objects.find(locator)
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())
174+
rule_id = self._domain_objects.find(locator).attrib["id"]
175+
data = f"<rules><rule id='{rule_id}'><active>true</active></rule></rules>"
176+
await self.call_request(RULES, method="put", data=data)
183177

184178
async def set_regulation_mode(self, mode: str) -> None:
185179
"""Set-function placeholder for legacy devices."""
@@ -224,17 +218,17 @@ async def set_schedule_state(
224218
for rule in self._domain_objects.findall(locator):
225219
template_id = rule.attrib["id"]
226220

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-
'''
221+
data = (
222+
"<rules>"
223+
f"<rule id='{schedule_rule_id}'>"
224+
f"<name><![CDATA[{name}]]></name>"
225+
f"<template id='{template_id}' />"
226+
f"<active>{new_state}</active>"
227+
"</rule>"
228+
"</rules>"
229+
)
236230
uri = f"{RULES};id={schedule_rule_id}"
237-
await self.call_request(uri, method="put", data=data.strip())
231+
await self.call_request(uri, method="put", data=data)
238232

239233
async def set_switch_state(
240234
self, appl_id: str, members: list[str] | None, model: str, state: str
@@ -258,21 +252,21 @@ async def set_switch_state(
258252
appliance = self._appliances.find(f'appliance[@id="{appl_id}"]')
259253
appl_name = appliance.find("name").text
260254
appl_type = appliance.find("type").text
261-
data = f'''
262-
<appliances>
263-
<appliance id="{appl_id}">
264-
<name><![CDATA[{appl_name}]]></name>
265-
<description><![CDATA[]]></description>
266-
<type><![CDATA[{appl_type}]]></type>
267-
<{switch.actuator}>
268-
<{switch.func_type}>
269-
<lock>{state}</lock>
270-
</{switch.func_type}>
271-
</{switch.actuator}>
272-
</appliance>
273-
</appliances>
274-
'''
275-
await self.call_request(APPLIANCES, method="post", data=data.strip())
255+
data = (
256+
"<appliances>"
257+
f"<appliance id='{appl_id}'>"
258+
f"<name><![CDATA[{appl_name}]]></name>"
259+
f"<description><![CDATA[]]></description>"
260+
f"<type><![CDATA[{appl_type}]]></type>"
261+
f"<{switch.actuator}>"
262+
f"<{switch.func_type}>"
263+
f"<lock>{state}</lock>"
264+
f"</{switch.func_type}>"
265+
f"</{switch.actuator}>"
266+
"</appliance>"
267+
"</appliances>"
268+
)
269+
await self.call_request(APPLIANCES, method="post", data=data)
276270
return
277271

278272
# Handle group of switches
@@ -317,13 +311,13 @@ async def set_temperature(self, _: str, items: dict[str, float]) -> None:
317311
) # pragma: no cover"
318312

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

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

plugwise/smile.py

Lines changed: 66 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +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-
"""
177+
data = (
178+
"<thermostat_functionality>"
179+
f"<setpoint>{temp}</setpoint>"
180+
"</thermostat_functionality>"
181+
)
182182
uri = f"{APPLIANCES};id={self._heater_id}/thermostat;id={thermostat_id}"
183-
await self.call_request(uri, method="put", data=data.strip())
183+
await self.call_request(uri, method="put", data=data)
184184

185185
async def set_offset(self, dev_id: str, offset: float) -> None:
186186
"""Set the Temperature offset for thermostats that support this feature."""
@@ -190,13 +190,9 @@ async def set_offset(self, dev_id: str, offset: float) -> None:
190190
)
191191

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

201197
async def set_preset(self, loc_id: str, preset: str) -> None:
202198
"""Set the given Preset on the relevant Thermostat - from LOCATIONS."""
@@ -208,17 +204,17 @@ async def set_preset(self, loc_id: str, preset: str) -> None:
208204
current_location = self._domain_objects.find(f'location[@id="{loc_id}"]')
209205
location_name = current_location.find("name").text
210206
location_type = current_location.find("type").text
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-
'''
207+
data = (
208+
"<locations>"
209+
f"<location id='{loc_id}''>"
210+
f"<name>{location_name}</name>"
211+
f"<type>{location_type}</type>"
212+
f"<preset>{preset}</preset>"
213+
"</location>"
214+
"</locations>"
215+
)
220216
uri = f"{LOCATIONS};id={loc_id}"
221-
await self.call_request(uri, method="put", data=data.strip())
217+
await self.call_request(uri, method="put", data=data)
222218

223219
async def set_select(
224220
self, key: str, loc_id: str, option: str, state: str | None
@@ -240,13 +236,13 @@ async def set_dhw_mode(self, mode: str) -> None:
240236
if mode not in self._dhw_allowed_modes:
241237
raise PlugwiseError("Plugwise: invalid dhw mode.")
242238

243-
data = f"""
244-
<domestic_hot_water_mode_control_functionality>
245-
<mode>{mode}</mode>
246-
</domestic_hot_water_mode_control_functionality>
247-
"""
239+
data = (
240+
"<domestic_hot_water_mode_control_functionality>"
241+
f"<mode>{mode}</mode>"
242+
"</domestic_hot_water_mode_control_functionality>"
243+
)
248244
uri = f"{APPLIANCES};type=heater_central/domestic_hot_water_mode_control"
249-
await self.call_request(uri, method="put", data=data.strip())
245+
await self.call_request(uri, method="put", data=data)
250246

251247
async def set_gateway_mode(self, mode: str) -> None:
252248
"""Set the gateway mode."""
@@ -271,14 +267,14 @@ async def set_gateway_mode(self, mode: str) -> None:
271267
vacation_time = time_2 + "T23:00:00.000Z"
272268
valid = f"<valid_from>{vacation_time}</valid_from><valid_to>{end_time}</valid_to>"
273269

274-
data = f"""
275-
<gateway_mode_control_functionality>
276-
<mode>{mode}</mode>
277-
{valid}
278-
</gateway_mode_control_functionality>
279-
"""
270+
data = (
271+
"<gateway_mode_control_functionality>"
272+
f"<mode>{mode}</mode>"
273+
f"{valid}"
274+
"</gateway_mode_control_functionality>"
275+
)
280276
uri = f"{APPLIANCES};id={self._smile_props['gateway_id']}/gateway_mode_control"
281-
await self.call_request(uri, method="put", data=data.strip())
277+
await self.call_request(uri, method="put", data=data)
282278

283279
async def set_regulation_mode(self, mode: str) -> None:
284280
"""Set the heating regulation mode."""
@@ -289,14 +285,14 @@ async def set_regulation_mode(self, mode: str) -> None:
289285
if "bleeding" in mode:
290286
duration = "<duration>300</duration>"
291287

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

301297
async def set_schedule_state(
302298
self,
@@ -344,17 +340,17 @@ async def set_schedule_state(
344340
template = f'<template id="{template_id}" />'
345341

346342
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-
'''
343+
data = (
344+
"<rules>"
345+
f"<rule id='{schedule_rule_id}'>"
346+
f"<name><![CDATA[{name}]]></name>"
347+
f"{template}"
348+
f"{contexts}"
349+
"</rule>"
350+
"</rules>"
351+
)
356352
uri = f"{RULES};id={schedule_rule_id}"
357-
await self.call_request(uri, method="put", data=data.strip())
353+
await self.call_request(uri, method="put", data=data)
358354
self._schedule_old_states[loc_id][name] = new_state
359355

360356
def determine_contexts(
@@ -412,11 +408,11 @@ async def set_switch_state(
412408
switch_id = item.attrib["id"]
413409
break
414410

415-
data = f"""
416-
<{switch.func_type}>
417-
<{switch.func}>{state}</{switch.func}>
418-
</{switch.func_type}>
419-
"""
411+
data = (
412+
f"<{switch.func_type}>"
413+
f"<{switch.func}>{state}</{switch.func}>"
414+
f"</{switch.func_type}>"
415+
)
420416
uri = f"{APPLIANCES};id={appl_id}/{switch.device};id={switch_id}"
421417
if model == "relay":
422418
locator = (
@@ -426,7 +422,7 @@ async def set_switch_state(
426422
if self._domain_objects.find(locator).text == "true":
427423
raise PlugwiseError("Plugwise: the locked Relay was not switched.")
428424

429-
await self.call_request(uri, method="put", data=data.strip())
425+
await self.call_request(uri, method="put", data=data)
430426

431427
async def _set_groupswitch_member_state(
432428
self, members: list[str], state: str, switch: Munch
@@ -439,12 +435,12 @@ async def _set_groupswitch_member_state(
439435
locator = f'appliance[@id="{member}"]/{switch.actuator}/{switch.func_type}'
440436
switch_id = self._domain_objects.find(locator).attrib["id"]
441437
uri = f"{APPLIANCES};id={member}/{switch.device};id={switch_id}"
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())
438+
data = (
439+
f"<{switch.func_type}>"
440+
f"<{switch.func}>{state}</{switch.func}>"
441+
f"</{switch.func_type}>"
442+
)
443+
await self.call_request(uri, method="put", data=data)
448444

449445
async def set_temperature(self, loc_id: str, items: dict[str, float]) -> None:
450446
"""Set the given Temperature on the relevant Thermostat."""
@@ -479,13 +475,13 @@ async def set_temperature(self, loc_id: str, items: dict[str, float]) -> None:
479475
) # pragma: no cover"
480476

481477
temperature = str(setpoint)
482-
data = f"""
483-
<thermostat_functionality>
484-
<setpoint>{temperature}</setpoint>
485-
</thermostat_functionality>
486-
"""
478+
data = (
479+
"<thermostat_functionality>"
480+
f"<setpoint>{temperature}</setpoint>"
481+
"</thermostat_functionality>"
482+
)
487483
uri = self._thermostat_uri(loc_id)
488-
await self.call_request(uri, method="put", data=data.strip())
484+
await self.call_request(uri, method="put", data=data)
489485

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

0 commit comments

Comments
 (0)