@@ -511,24 +511,37 @@ async def set_schedule_state(self, loc_id: str, name: str, state: str) -> bool:
511511 return False
512512
513513 schema_rule_id : str = next (iter (schema_rule ))
514- info = ""
515- if state == "on" :
516- info = f'<context><zone><location id="{ loc_id } " /></zone></context>'
517514
518- template = (
519- '<template tag="zone_preset_based_on_time_and_presence_with_override" />'
520- )
521515 if self .smile_name != "Adam" :
522516 locator = f'.//*[@id="{ schema_rule_id } "]/template'
523517 template_id = self ._domain_objects .find (locator ).attrib ["id" ]
524518 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+ contexts .remove (subject )
535+ if state == "on" :
536+ contexts .append (subject )
537+
538+ contexts = etree .tostring (contexts , encoding = "unicode" ).rstrip ()
525539
526540 uri = f"{ RULES } ;id={ schema_rule_id } "
527541 data = (
528542 f'<rules><rule id="{ schema_rule_id } "><name><![CDATA[{ name } ]]></name>'
529- f"{ template } <contexts> { info } </ contexts> </rule></rules>"
543+ f"{ template } { contexts } </rule></rules>"
530544 )
531-
532545 await self ._request (uri , method = "put" , data = data )
533546
534547 return True
0 commit comments