From 5b22cfa9b3e903a200438df153e693337a0ca67c Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 21 Sep 2024 16:30:40 +0200 Subject: [PATCH] Use HassKey in todo (#126325) * Use HassKey in todo * One more --- homeassistant/components/todo/__init__.py | 20 +++++++++----------- homeassistant/components/todo/const.py | 11 +++++++++++ homeassistant/components/todo/intent.py | 9 +++++---- 3 files changed, 25 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/todo/__init__.py b/homeassistant/components/todo/__init__.py index d35d9d6bbea..533ae354dd2 100644 --- a/homeassistant/components/todo/__init__.py +++ b/homeassistant/components/todo/__init__.py @@ -1,5 +1,7 @@ """The todo integration.""" +from __future__ import annotations + from collections.abc import Callable, Iterable import dataclasses import datetime @@ -37,6 +39,7 @@ from .const import ( ATTR_RENAME, ATTR_STATUS, DOMAIN, + DOMAIN_DATA, TodoItemStatus, TodoListEntityFeature, TodoServices, @@ -111,7 +114,7 @@ def _validate_supported_features( async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up Todo entities.""" - component = hass.data[DOMAIN] = EntityComponent[TodoListEntity]( + component = hass.data[DOMAIN_DATA] = EntityComponent[TodoListEntity]( _LOGGER, DOMAIN, hass, SCAN_INTERVAL ) @@ -194,14 +197,12 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up a config entry.""" - component: EntityComponent[TodoListEntity] = hass.data[DOMAIN] - return await component.async_setup_entry(entry) + return await hass.data[DOMAIN_DATA].async_setup_entry(entry) async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" - component: EntityComponent[TodoListEntity] = hass.data[DOMAIN] - return await component.async_unload_entry(entry) + return await hass.data[DOMAIN_DATA].async_unload_entry(entry) @dataclasses.dataclass @@ -331,10 +332,9 @@ async def websocket_handle_subscribe_todo_items( hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any] ) -> None: """Subscribe to To-do list item updates.""" - component: EntityComponent[TodoListEntity] = hass.data[DOMAIN] entity_id: str = msg["entity_id"] - if not (entity := component.get_entity(entity_id)): + if not (entity := hass.data[DOMAIN_DATA].get_entity(entity_id)): connection.send_error( msg["id"], "invalid_entity_id", @@ -387,10 +387,9 @@ async def websocket_handle_todo_item_list( hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any] ) -> None: """Handle the list of To-do items in a To-do- list.""" - component: EntityComponent[TodoListEntity] = hass.data[DOMAIN] if ( not (entity_id := msg[CONF_ENTITY_ID]) - or not (entity := component.get_entity(entity_id)) + or not (entity := hass.data[DOMAIN_DATA].get_entity(entity_id)) or not isinstance(entity, TodoListEntity) ): connection.send_error(msg["id"], ERR_NOT_FOUND, "Entity not found") @@ -423,8 +422,7 @@ async def websocket_handle_todo_item_move( hass: HomeAssistant, connection: websocket_api.ActiveConnection, msg: dict[str, Any] ) -> None: """Handle move of a To-do item within a To-do list.""" - component: EntityComponent[TodoListEntity] = hass.data[DOMAIN] - if not (entity := component.get_entity(msg["entity_id"])): + if not (entity := hass.data[DOMAIN_DATA].get_entity(msg["entity_id"])): connection.send_error(msg["id"], ERR_NOT_FOUND, "Entity not found") return diff --git a/homeassistant/components/todo/const.py b/homeassistant/components/todo/const.py index ee7ef53715d..634075d7f32 100644 --- a/homeassistant/components/todo/const.py +++ b/homeassistant/components/todo/const.py @@ -1,8 +1,19 @@ """Constants for the To-do integration.""" +from __future__ import annotations + from enum import IntFlag, StrEnum +from typing import TYPE_CHECKING + +from homeassistant.util.hass_dict import HassKey + +if TYPE_CHECKING: + from homeassistant.helpers.entity_component import EntityComponent + + from . import TodoListEntity DOMAIN = "todo" +DOMAIN_DATA: HassKey[EntityComponent[TodoListEntity]] = HassKey(DOMAIN) ATTR_DUE = "due" ATTR_DUE_DATE = "due_date" diff --git a/homeassistant/components/todo/intent.py b/homeassistant/components/todo/intent.py index cd8ad7f02ab..6520e6c12b7 100644 --- a/homeassistant/components/todo/intent.py +++ b/homeassistant/components/todo/intent.py @@ -6,9 +6,9 @@ import voluptuous as vol from homeassistant.core import HomeAssistant from homeassistant.helpers import intent -from homeassistant.helpers.entity_component import EntityComponent -from . import DOMAIN, TodoItem, TodoItemStatus, TodoListEntity +from . import TodoItem, TodoItemStatus, TodoListEntity +from .const import DOMAIN, DOMAIN_DATA INTENT_LIST_ADD_ITEM = "HassListAddItem" @@ -37,7 +37,6 @@ class ListAddItemIntent(intent.IntentHandler): item = slots["item"]["value"] list_name = slots["name"]["value"] - component: EntityComponent[TodoListEntity] = hass.data[DOMAIN] target_list: TodoListEntity | None = None # Find matching list @@ -50,7 +49,9 @@ class ListAddItemIntent(intent.IntentHandler): result=match_result, constraints=match_constraints ) - target_list = component.get_entity(match_result.states[0].entity_id) + target_list = hass.data[DOMAIN_DATA].get_entity( + match_result.states[0].entity_id + ) if target_list is None: raise intent.IntentHandleError(f"No to-do list: {list_name}")