@@ -53,23 +53,30 @@ def get_tracking_entities(self) -> list[str]:
5353 def _create_matcher (self , matcher_config : dict ) -> SubProfileMatcher :
5454 """Create a matcher from json config. Can be extended for more matchers in the future."""
5555 matcher_type : SubProfileMatcherType = matcher_config ["type" ]
56-
57- matcher_classes : dict [SubProfileMatcherType , type [SubProfileMatcher ]] = {
58- SubProfileMatcherType .ATTRIBUTE : AttributeMatcher ,
59- SubProfileMatcherType .ENTITY_STATE : EntityStateMatcher ,
60- SubProfileMatcherType .ENTITY_ID : EntityIdMatcher ,
61- SubProfileMatcherType .ENTITY_REGISTRY : EntityRegistryMatcher ,
62- SubProfileMatcherType .INTEGRATION : IntegrationMatcher ,
63- SubProfileMatcherType .MODEL_ID : ModelIdMatcher ,
64- }
65- if matcher_type not in matcher_classes :
66- raise PowercalcSetupError (f"Unknown sub profile matcher type: { matcher_type } " )
67-
68- return matcher_classes [matcher_type ].from_config (
69- matcher_config ,
70- hass = self ._hass ,
71- source_entity = self ._source_entity ,
72- )
56+ match matcher_type :
57+ case SubProfileMatcherType .ATTRIBUTE :
58+ return AttributeMatcher (matcher_config ["attribute" ], matcher_config ["map" ])
59+ case SubProfileMatcherType .ENTITY_STATE :
60+ return EntityStateMatcher (
61+ self ._hass ,
62+ self ._source_entity ,
63+ matcher_config ["entity_id" ],
64+ matcher_config ["map" ],
65+ )
66+ case SubProfileMatcherType .ENTITY_ID :
67+ return EntityIdMatcher (matcher_config ["pattern" ], matcher_config ["profile" ])
68+ case SubProfileMatcherType .ENTITY_REGISTRY :
69+ return EntityRegistryMatcher (
70+ matcher_config ["property" ],
71+ matcher_config ["value" ],
72+ matcher_config ["profile" ],
73+ )
74+ case SubProfileMatcherType .INTEGRATION :
75+ return IntegrationMatcher (matcher_config ["integration" ], matcher_config ["profile" ])
76+ case SubProfileMatcherType .MODEL_ID :
77+ return ModelIdMatcher (matcher_config ["model_id" ], matcher_config ["profile" ])
78+ case _:
79+ raise PowercalcSetupError (f"Unknown sub profile matcher type: { matcher_type } " )
7380
7481
7582class SubProfileSelectConfig (NamedTuple ):
@@ -78,21 +85,12 @@ class SubProfileSelectConfig(NamedTuple):
7885
7986
8087class SubProfileMatcher (Protocol ):
81- @classmethod
82- def from_config (
83- cls ,
84- config : dict ,
85- * ,
86- hass : HomeAssistant | None = None ,
87- source_entity : SourceEntity | None = None ,
88- ) -> SubProfileMatcher :
89- """Create a matcher from a config dict."""
90-
9188 def match (self , entity_state : State , source_entity : SourceEntity ) -> str | None :
9289 """Returns a sub profile."""
9390
9491 def get_tracking_entities (self ) -> list [str ]:
9592 """Get extra entities to track for state changes."""
93+ return []
9694
9795
9896class EntityStateMatcher (SubProfileMatcher ):
@@ -119,17 +117,6 @@ def match(self, entity_state: State, source_entity: SourceEntity) -> str | None:
119117
120118 return self ._mapping .get (state .state )
121119
122- @classmethod
123- def from_config (
124- cls ,
125- config : dict ,
126- * ,
127- hass : HomeAssistant | None = None ,
128- source_entity : SourceEntity | None = None ,
129- ) -> EntityStateMatcher :
130- assert hass is not None
131- return cls (hass , source_entity , config ["entity_id" ], config ["map" ])
132-
133120 def get_tracking_entities (self ) -> list [str ]:
134121 return [self ._entity_id ]
135122
@@ -146,19 +133,6 @@ def match(self, entity_state: State, source_entity: SourceEntity) -> str | None:
146133
147134 return self ._mapping .get (val )
148135
149- @classmethod
150- def from_config (
151- cls ,
152- config : dict ,
153- * ,
154- hass : HomeAssistant | None = None ,
155- source_entity : SourceEntity | None = None ,
156- ) -> AttributeMatcher :
157- return cls (config ["attribute" ], config ["map" ])
158-
159- def get_tracking_entities (self ) -> list [str ]:
160- return []
161-
162136
163137class EntityIdMatcher (SubProfileMatcher ):
164138 def __init__ (self , pattern : str , profile : str ) -> None :
@@ -171,19 +145,6 @@ def match(self, entity_state: State, source_entity: SourceEntity) -> str | None:
171145
172146 return None
173147
174- @classmethod
175- def from_config (
176- cls ,
177- config : dict ,
178- * ,
179- hass : HomeAssistant | None = None ,
180- source_entity : SourceEntity | None = None ,
181- ) -> EntityIdMatcher :
182- return cls (config ["pattern" ], config ["profile" ])
183-
184- def get_tracking_entities (self ) -> list [str ]:
185- return []
186-
187148
188149class IntegrationMatcher (SubProfileMatcher ):
189150 def __init__ (self , integration : str , profile : str ) -> None :
@@ -200,19 +161,6 @@ def match(self, entity_state: State, source_entity: SourceEntity) -> str | None:
200161
201162 return None
202163
203- @classmethod
204- def from_config (
205- cls ,
206- config : dict ,
207- * ,
208- hass : HomeAssistant | None = None ,
209- source_entity : SourceEntity | None = None ,
210- ) -> IntegrationMatcher :
211- return cls (config ["integration" ], config ["profile" ])
212-
213- def get_tracking_entities (self ) -> list [str ]:
214- return []
215-
216164
217165class EntityRegistryMatcher (SubProfileMatcher ):
218166 def __init__ (self , property_name : str , value : object , profile : str ) -> None :
@@ -234,19 +182,6 @@ def match(self, entity_state: State, source_entity: SourceEntity) -> str | None:
234182
235183 return None
236184
237- @classmethod
238- def from_config (
239- cls ,
240- config : dict ,
241- * ,
242- hass : HomeAssistant | None = None ,
243- source_entity : SourceEntity | None = None ,
244- ) -> EntityRegistryMatcher :
245- return cls (config ["property" ], config ["value" ], config ["profile" ])
246-
247- def get_tracking_entities (self ) -> list [str ]:
248- return []
249-
250185 def _matches_registry_value (self , registry_value : object ) -> bool :
251186 if registry_value == self ._value :
252187 return True
@@ -271,16 +206,3 @@ def match(self, entity_state: State, source_entity: SourceEntity) -> str | None:
271206 return self ._profile
272207
273208 return None
274-
275- @classmethod
276- def from_config (
277- cls ,
278- config : dict ,
279- * ,
280- hass : HomeAssistant | None = None ,
281- source_entity : SourceEntity | None = None ,
282- ) -> ModelIdMatcher :
283- return cls (config ["model_id" ], config ["profile" ])
284-
285- def get_tracking_entities (self ) -> list [str ]:
286- return []
0 commit comments