@@ -602,22 +602,47 @@ 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 (
606+ self , loc_id : str , name : str , state : str , sched_id : str
607+ ) -> etree :
608+ """Helper-function for set_schedule_state()."""
609+ locator = f'.//*[@id="{ sched_id } "]/contexts'
610+ contexts = self ._domain_objects .find (locator )
611+ locator = f'.//*[@id="{ loc_id } "].../...'
612+ if (subject := contexts .find (locator )) is None :
613+ subject = f'<context><zone><location id="{ loc_id } " /></zone></context>'
614+ subject = etree .fromstring (subject )
615+
616+ if state == "off" :
617+ self ._last_active [loc_id ] = name
618+ contexts .remove (subject )
619+ if state == "on" :
620+ contexts .append (subject )
621+
622+ return etree .tostring (contexts , encoding = "unicode" ).rstrip ()
623+
605624 async def set_schedule_state (
606- self , loc_id : str , name : str | None , new_state : str
625+ self ,
626+ loc_id : str ,
627+ new_state : str ,
628+ name : str | None = None ,
607629 ) -> None :
608630 """Activate/deactivate the Schedule, with the given name, on the relevant Thermostat.
609631
610632 Determined from - DOMAIN_OBJECTS.
611- In HA Core used to set the hvac_mode: in practice switch between schedule on - off.
633+ Used in HA Core to set the hvac_mode: in practice switch between schedule on - off.
612634 """
613635 # Input checking
614636 if new_state not in ["on" , "off" ]:
615637 raise PlugwiseError ("Plugwise: invalid schedule state." )
616638 if name is None :
617- raise PlugwiseError (
618- "Plugwise: cannot change schedule-state: no schedule name provided"
619- )
639+ for device in self .gw_devices .values ():
640+ if device ["location" ] == loc_id and device ["last_used" ]:
641+ name = device ["last_used" ]
642+ else :
643+ return
620644
645+ assert isinstance (name , str )
621646 if self ._smile_legacy :
622647 await self ._set_schedule_state_legacy (loc_id , name , new_state )
623648 return
@@ -641,21 +666,7 @@ async def set_schedule_state(
641666 template_id = self ._domain_objects .find (locator ).attrib ["id" ]
642667 template = f'<template id="{ template_id } " />'
643668
644- locator = f'.//*[@id="{ schedule_rule_id } "]/contexts'
645- contexts = self ._domain_objects .find (locator )
646- locator = f'.//*[@id="{ loc_id } "].../...'
647- if (subject := contexts .find (locator )) is None :
648- subject = f'<context><zone><location id="{ loc_id } " /></zone></context>'
649- subject = etree .fromstring (subject )
650-
651- if new_state == "off" :
652- self ._last_active [loc_id ] = name
653- contexts .remove (subject )
654- if new_state == "on" :
655- contexts .append (subject )
656-
657- contexts = etree .tostring (contexts , encoding = "unicode" ).rstrip ()
658-
669+ contexts = self .determine_contexts (loc_id , name , new_state , schedule_rule_id )
659670 uri = f"{ RULES } ;id={ schedule_rule_id } "
660671 data = (
661672 f'<rules><rule id="{ schedule_rule_id } "><name><![CDATA[{ name } ]]></name>'
0 commit comments