Skip to content

Commit 8471f0f

Browse files
committed
Break-out determine_contexts() function to reduce complexity
1 parent b419ca7 commit 8471f0f

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

plugwise/__init__.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,24 @@ async def _set_schedule_state_legacy(
602602
await self._request(uri, method="put", data=data)
603603
self._schedule_old_states[loc_id][name] = new_state
604604

605+
def determine_contexts(self, loc_id: str, schedule_rule_id: str) -> etree
606+
"""Helper function for set_schedule_state()."""
607+
locator = f'.//*[@id="{schedule_rule_id}"]/contexts'
608+
contexts = self._domain_objects.find(locator)
609+
locator = f'.//*[@id="{loc_id}"].../...'
610+
if (subject := contexts.find(locator)) is None:
611+
subject = f'<context><zone><location id="{loc_id}" /></zone></context>'
612+
subject = etree.fromstring(subject)
613+
614+
if new_state == "off":
615+
self._last_active[loc_id] = name
616+
contexts.remove(subject)
617+
if new_state == "on":
618+
contexts.append(subject)
619+
620+
contexts = etree.tostring(contexts, encoding="unicode").rstrip()
621+
return contexts
622+
605623
async def set_schedule_state(
606624
self,
607625
loc_id: str,
@@ -647,21 +665,7 @@ async def set_schedule_state(
647665
template_id = self._domain_objects.find(locator).attrib["id"]
648666
template = f'<template id="{template_id}" />'
649667

650-
locator = f'.//*[@id="{schedule_rule_id}"]/contexts'
651-
contexts = self._domain_objects.find(locator)
652-
locator = f'.//*[@id="{loc_id}"].../...'
653-
if (subject := contexts.find(locator)) is None:
654-
subject = f'<context><zone><location id="{loc_id}" /></zone></context>'
655-
subject = etree.fromstring(subject)
656-
657-
if new_state == "off":
658-
self._last_active[loc_id] = name
659-
contexts.remove(subject)
660-
if new_state == "on":
661-
contexts.append(subject)
662-
663-
contexts = etree.tostring(contexts, encoding="unicode").rstrip()
664-
668+
self.determine_contexts(loc_id, schedule_rule_id)
665669
uri = f"{RULES};id={schedule_rule_id}"
666670
data = (
667671
f'<rules><rule id="{schedule_rule_id}"><name><![CDATA[{name}]]></name>'

0 commit comments

Comments
 (0)