Skip to content

Commit 3a6ccc8

Browse files
committed
Optimize code
1 parent aad81a7 commit 3a6ccc8

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

plugwise/smile.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -512,31 +512,26 @@ async def set_schedule_state(self, loc_id: str, name: str, state: str) -> bool:
512512

513513
schema_rule_id: str = next(iter(schema_rule))
514514

515+
template = (
516+
'<template tag="zone_preset_based_on_time_and_presence_with_override" />'
517+
)
515518
if self.smile_name != "Adam":
516519
locator = f'.//*[@id="{schema_rule_id}"]/template'
517520
template_id = self._domain_objects.find(locator).attrib["id"]
518521
template = f'<template id="{template_id}" />'
519-
if state == "off":
520-
contexts = "<contexts> </contexts>"
521-
if state == "on":
522-
contexts = f'<contexts><context><zone><location id="{loc_id}" /></zone></context></contexts>'
523-
else: # Adam
524-
template = '<template tag="zone_preset_based_on_time_and_presence_with_override" />'
525-
locator = f'.//*[@id="{schema_rule_id}"]/contexts'
526-
contexts = self._domain_objects.find(locator)
527-
locator = f'.//*[@id="{loc_id}"].../...'
528-
subject = contexts.find(locator)
529-
if subject is None:
530-
subject = f'<context><zone><location id="{loc_id}" /></zone></context>'
531-
subject = etree.fromstring(subject)
532-
533-
if state == "off":
534-
self._last_active[loc_id] = name
535-
contexts.remove(subject)
536-
if state == "on":
537-
contexts.append(subject)
538-
539-
contexts = etree.tostring(contexts, encoding="unicode").rstrip()
522+
523+
locator = f'.//*[@id="{schema_rule_id}"]/contexts'
524+
contexts = self._domain_objects.find(locator)
525+
subject = f'<context><zone><location id="{loc_id}" /></zone></context>'
526+
subject = etree.fromstring(subject)
527+
528+
if state == "off":
529+
self._last_active[loc_id] = name
530+
contexts.remove(subject)
531+
if state == "on":
532+
contexts.append(subject)
533+
534+
contexts = etree.tostring(contexts, encoding="unicode").rstrip()
540535

541536
uri = f"{RULES};id={schema_rule_id}"
542537
data = (

0 commit comments

Comments
 (0)