Use HassKey in todo (#126325)

* Use HassKey in todo

* One more
This commit is contained in:
epenet 2024-09-21 16:30:40 +02:00 committed by GitHub
parent aa736b2de6
commit 5b22cfa9b3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 15 deletions

View File

@ -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

View File

@ -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"

View File

@ -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}")