From 29358277fea1e2268ccee30b739cd5bfecb8505d Mon Sep 17 00:00:00 2001 From: Xavier Moreno Date: Fri, 21 May 2021 18:49:06 +0200 Subject: [PATCH] fix(type_controller): call render_template before calling get_state related to #305 --- RELEASE_NOTES.md | 4 +++- apps/controllerx/cx_core/controller.py | 11 +++++++++++ apps/controllerx/cx_core/type_controller.py | 4 ++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index dba4abfb..e44067e2 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -10,7 +10,7 @@ PRERELEASE_NOTE ## :hammer: Fixes -- Fix Z2M mapping for [W2049](https://xaviml.github.io/controllerx/controllers/W2049) [ #292 ] +- ValueError when render template is present in the entity name [ #305 ] + \ No newline at end of file diff --git a/apps/controllerx/cx_core/controller.py b/apps/controllerx/cx_core/controller.py index 194df72a..6a5f7003 100644 --- a/apps/controllerx/cx_core/controller.py +++ b/apps/controllerx/cx_core/controller.py @@ -308,6 +308,17 @@ async def call_service(self, service: str, **attributes) -> Optional[Any]: self.log("\n".join(to_log), level="INFO", ascii_encode=False) return await Hass.call_service(self, service, **attributes) # type: ignore + async def get_state( + self, + entity_id: Optional[str] = None, + attribute: Optional[str] = None, + default: Any = None, + copy: bool = True, + **kwargs, + ) -> Optional[Any]: + rendered_entity_id = await self.render_value(entity_id) + return await super().get_state(rendered_entity_id, attribute, default, copy, **kwargs) # type: ignore + async def handle_action( self, action_key: str, extra: Optional[EventData] = None ) -> None: diff --git a/apps/controllerx/cx_core/type_controller.py b/apps/controllerx/cx_core/type_controller.py index 52d81cd0..27418633 100644 --- a/apps/controllerx/cx_core/type_controller.py +++ b/apps/controllerx/cx_core/type_controller.py @@ -56,7 +56,7 @@ async def check_domain(self, entity_name: str) -> None: if self.contains_templating(entity_name): return elif entity_name.startswith("group."): - entities = await self.get_state(entity_name, attribute="entity_id") # type: ignore + entities: List[str] = await self.get_state(entity_name, attribute="entity_id") # type: ignore same_domain = all( ( any(elem.startswith(domain + ".") for domain in self.domains) @@ -78,7 +78,7 @@ async def get_entity_state( self, entity: str, attribute: Optional[str] = None ) -> Any: if entity.startswith("group."): - entities = await self.get_state(entity, attribute="entity_id") # type: ignore + entities: List[str] = await self.get_state(entity, attribute="entity_id") # type: ignore if len(entities) == 0: raise ValueError( f"The group `{entity}` does not have any entities registered."