@@ -783,22 +783,17 @@ def _match_and_rank_thermostats(self) -> None:
783783
784784 Match thermostat-appliances with locations, rank them for locations with multiple thermostats.
785785 """
786- # Build location index
787- entities_by_location : dict [str , list [tuple [str , GwEntityData ]]] = {}
788- for entity_id , entity in self .gw_entities .items ():
789- if "location" in entity :
790- loc = entity ["location" ]
791- entities_by_location .setdefault (loc , []).append ((entity_id , entity ))
792-
793- # Rank thermostats per location
794786 for location_id , location in self ._loc_data .items ():
795- for entity_id , entity in entities_by_location .get (location_id , []):
796- self ._rank_thermostat (entity_id , entity , location , THERMO_MATCHING )
787+ for entity_id , entity in self .gw_entities .items ():
788+ self ._rank_thermostat (
789+ entity_id , entity , location_id , location , THERMO_MATCHING
790+ )
797791
798792 def _rank_thermostat (
799793 self ,
800794 entity_id : str ,
801795 entity : GwEntityData ,
796+ location_id : str ,
802797 location : ThermoLoc ,
803798 thermo_matching : dict [str , int ],
804799 ) -> None :
@@ -807,15 +802,17 @@ def _rank_thermostat(
807802 Rank the thermostat based on entity-thermostat-type: primary or secondary.
808803 There can be several primary and secondary thermostats per location.
809804 """
810- if (appl_class := entity ["dev_class" ]) not in thermo_matching :
805+ if not (
806+ "location" in entity
807+ and location_id == entity ["location" ]
808+ and (appl_class := entity ["dev_class" ]) in thermo_matching
809+ ):
811810 return None
812811
813812 # Pre-elect new primary
814813 if thermo_matching [appl_class ] == location ["primary_prio" ]:
815814 location ["primary" ].append (entity_id )
816- elif (thermo_rank := thermo_matching [appl_class ]) > location [
817- "primary_prio"
818- ]:
815+ elif (thermo_rank := thermo_matching [appl_class ]) > location ["primary_prio" ]:
819816 location ["primary_prio" ] = thermo_rank
820817 # Demote former primary
821818 if tl_primary := location ["primary" ]:
0 commit comments