@@ -751,6 +751,7 @@ def _presets(self, loc_id: str) -> dict[str, list[float]]:
751751 return self ._presets_legacy ()
752752
753753 if not (rule_ids := self ._rule_ids_by_tag (tag_1 , loc_id )):
754+ rule_ids = None
754755 if not (rule_ids := self ._rule_ids_by_name (tag_2 , loc_id )):
755756 return presets # pragma: no cover
756757
@@ -767,35 +768,38 @@ def _presets(self, loc_id: str) -> dict[str, list[float]]:
767768
768769 return presets
769770
770- def _rule_ids_by_name (self , name : str , loc_id : str ) -> dict [str , str ]:
771+ def _rule_ids_by_name (self , name : str , loc_id : str ) -> dict [str , dict [ str , str ] ]:
771772 """Helper-function for _presets().
772773
773774 Obtain the rule_id from the given name and and provide the location_id, when present.
774775 """
775- schedule_ids : dict [str , str ] = {}
776+ schedule_ids : dict [str , dict [ str , str ] ] = {}
776777 locator = f'./contexts/context/zone/location[@id="{ loc_id } "]'
777778 for rule in self ._domain_objects .findall (f'./rule[name="{ name } "]' ):
779+ active = rule .find ("active" ).text
778780 if rule .find (locator ) is not None :
779- schedule_ids [rule .attrib ["id" ]] = loc_id
781+ schedule_ids [rule .attrib ["id" ]] = { "location" : loc_id , "name" : name , "active" : active }
780782 else :
781- schedule_ids [rule .attrib ["id" ]] = NONE
783+ schedule_ids [rule .attrib ["id" ]] = { "location" : NONE , "name" : name , "active" : active }
782784
783785 return schedule_ids
784786
785- def _rule_ids_by_tag (self , tag : str , loc_id : str ) -> dict [str , str ]:
787+ def _rule_ids_by_tag (self , tag : str , loc_id : str ) -> dict [str , dict [ str , str ] ]:
786788 """Helper-function for _presets(), _schedules() and _last_active_schedule().
787789
788790 Obtain the rule_id from the given template_tag and provide the location_id, when present.
789791 """
790- schedule_ids : dict [str , str ] = {}
792+ schedule_ids : dict [str , dict [ str , str ] ] = {}
791793 locator1 = f'./template[@tag="{ tag } "]'
792794 locator2 = f'./contexts/context/zone/location[@id="{ loc_id } "]'
793795 for rule in self ._domain_objects .findall ("./rule" ):
794796 if rule .find (locator1 ) is not None :
797+ name = rule .find ("name" ).text
798+ active = rule .find ("active" ).text
795799 if rule .find (locator2 ) is not None :
796- schedule_ids [rule .attrib ["id" ]] = loc_id
800+ schedule_ids [rule .attrib ["id" ]] = { "location" : loc_id , "name" : name , "active" : active }
797801 else :
798- schedule_ids [rule .attrib ["id" ]] = NONE
802+ schedule_ids [rule .attrib ["id" ]] = { "location" : NONE , "name" : name , "active" : active }
799803
800804 return schedule_ids
801805
@@ -1478,7 +1482,7 @@ def _schedules(self, location: str) -> tuple[list[str], str]:
14781482 NEW: when a location_id is present then the schedule is active. Valid for both Adam and non-legacy Anna.
14791483 """
14801484 available : list [str ] = [NONE ]
1481- rule_ids : dict [str , str ] = {}
1485+ rule_ids : dict [str , dict [ str , str ] ] = {}
14821486 selected = NONE
14831487
14841488 # Legacy Anna schedule, only one schedule allowed
@@ -1495,15 +1499,16 @@ def _schedules(self, location: str) -> tuple[list[str], str]:
14951499 return available , selected
14961500
14971501 schedules : list [str ] = []
1498- for rule_id , loc_id in rule_ids .items ():
1499- name = self ._domain_objects .find (f'./rule[@id="{ rule_id } "]/name' ).text
1502+ for rule_id , data in rule_ids .items ():
1503+ active = data ["active" ] == "true"
1504+ name = data ["name" ]
15001505 locator = f'./rule[@id="{ rule_id } "]/directives'
15011506 # Show an empty schedule as no schedule found
15021507 if self ._domain_objects .find (locator ) is None :
15031508 continue # pragma: no cover
15041509
15051510 available .append (name )
1506- if location == loc_id :
1511+ if location == data [ "location" ] and active :
15071512 selected = name
15081513 self ._last_active [location ] = selected
15091514 schedules .append (name )
0 commit comments