From e08301f362fde2e7daa781cd4c87f7ae48d76c94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20H=C3=B6rsken?= Date: Sun, 14 Apr 2024 23:11:42 +0200 Subject: [PATCH] Move Alexa entity id generation into abstract config class (#115593) This makes it possible to change the id schema in implementations. --- homeassistant/components/alexa/config.py | 5 +++++ homeassistant/components/alexa/entities.py | 7 +------ homeassistant/components/alexa/state_report.py | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/alexa/config.py b/homeassistant/components/alexa/config.py index fb589dde566..0801a32a607 100644 --- a/homeassistant/components/alexa/config.py +++ b/homeassistant/components/alexa/config.py @@ -13,6 +13,7 @@ from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback from homeassistant.helpers.storage import Store from .const import DOMAIN +from .entities import TRANSLATION_TABLE from .state_report import async_enable_proactive_mode STORE_AUTHORIZED = "authorized" @@ -101,6 +102,10 @@ class AbstractConfig(ABC): """If an entity should be exposed.""" return False + def generate_alexa_id(self, entity_id: str) -> str: + """Return the alexa ID for an entity ID.""" + return entity_id.replace(".", "#").translate(TRANSLATION_TABLE) + @callback def async_invalidate_access_token(self) -> None: """Invalidate access token.""" diff --git a/homeassistant/components/alexa/entities.py b/homeassistant/components/alexa/entities.py index 240f676b5f3..ca7b78f7ff5 100644 --- a/homeassistant/components/alexa/entities.py +++ b/homeassistant/components/alexa/entities.py @@ -259,11 +259,6 @@ class DisplayCategory: WEARABLE = "WEARABLE" -def generate_alexa_id(entity_id: str) -> str: - """Return the alexa ID for an entity ID.""" - return entity_id.replace(".", "#").translate(TRANSLATION_TABLE) - - class AlexaEntity: """An adaptation of an entity, expressed in Alexa's terms. @@ -298,7 +293,7 @@ class AlexaEntity: def alexa_id(self) -> str: """Return the Alexa API entity id.""" - return generate_alexa_id(self.entity.entity_id) + return self.config.generate_alexa_id(self.entity.entity_id) def display_categories(self) -> list[str] | None: """Return a list of display categories.""" diff --git a/homeassistant/components/alexa/state_report.py b/homeassistant/components/alexa/state_report.py index 24d750e7cb7..dc6c8ee3186 100644 --- a/homeassistant/components/alexa/state_report.py +++ b/homeassistant/components/alexa/state_report.py @@ -41,7 +41,7 @@ from .const import ( Cause, ) from .diagnostics import async_redact_auth_data -from .entities import ENTITY_ADAPTERS, AlexaEntity, generate_alexa_id +from .entities import ENTITY_ADAPTERS, AlexaEntity from .errors import AlexaInvalidEndpointError, NoTokenAvailable, RequireRelink if TYPE_CHECKING: @@ -492,7 +492,7 @@ async def async_send_delete_message( if domain not in ENTITY_ADAPTERS: continue - endpoints.append({"endpointId": generate_alexa_id(entity_id)}) + endpoints.append({"endpointId": config.generate_alexa_id(entity_id)}) payload: dict[str, Any] = { "endpoints": endpoints,