From 3c580cc57286dc339fffbca9ca37879fdb733399 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 8 Oct 2026 09:46:59 +0200 Subject: [PATCH] Move person YAML filtering to helpers module (#184690) Co-authored-by: Claude Opus 5.5 Co-authored-by: Erwin Douna --- homeassistant/components/person/__init__.py | 42 ++---------------- homeassistant/components/person/const.py | 2 + homeassistant/components/person/helpers.py | 47 +++++++++++++++++++++ 3 files changed, 52 insertions(+), 39 deletions(-) create mode 100644 homeassistant/components/person/helpers.py diff --git a/homeassistant/components/person/__init__.py b/homeassistant/components/person/__init__.py index 2e9ecfb860f5..5c45993c245d 100644 --- a/homeassistant/components/person/__init__.py +++ b/homeassistant/components/person/__init__.py @@ -9,7 +9,7 @@ from typing import Any, Self, override import probatio from homeassistant.auth import EVENT_USER_REMOVED -from homeassistant.components import persistent_notification, websocket_api +from homeassistant.components import websocket_api from homeassistant.components.device_tracker import ( DOMAIN as DEVICE_TRACKER_DOMAIN, DeviceTrackerEntityCapabilityAttribute, @@ -61,7 +61,8 @@ from homeassistant.helpers.restore_state import RestoreEntity from homeassistant.helpers.storage import Store from homeassistant.helpers.typing import ConfigType, VolDictType -from .const import DATA_PERSON, DOMAIN, PersonEntityStateAttribute +from .const import CONF_USER_ID, DATA_PERSON, DOMAIN, PersonEntityStateAttribute +from .helpers import filter_yaml_data _LOGGER = logging.getLogger(__name__) @@ -70,7 +71,6 @@ ATTR_USER_ID = "user_id" ATTR_DEVICE_TRACKERS = "device_trackers" CONF_DEVICE_TRACKERS = "device_trackers" -CONF_USER_ID = "user_id" CONF_PICTURE = "picture" STORAGE_KEY = DOMAIN @@ -429,42 +429,6 @@ def _validate_own_picture(hass: HomeAssistant, picture: str | None) -> None: ) -async def filter_yaml_data(hass: HomeAssistant, persons: list[dict]) -> list[dict]: - """Validate YAML data that we can't validate via schema.""" - filtered = [] - person_invalid_user = [] - - for person_conf in persons: - user_id = person_conf.get(CONF_USER_ID) - - if user_id is not None and await hass.auth.async_get_user(user_id) is None: - _LOGGER.error( - "Invalid user_id detected for person %s", - person_conf[CONF_ID], - ) - person_invalid_user.append( - f"- Person {person_conf[CONF_NAME]} (id: {person_conf[CONF_ID]}) points" - f" at invalid user {user_id}" - ) - continue - - filtered.append(person_conf) - - if person_invalid_user: - persistent_notification.async_create( - hass, - f""" -The following persons point at invalid users: - -{"- ".join(person_invalid_user)} - """, - "Invalid Person Configuration", - DOMAIN, - ) - - return filtered - - @dataclass(slots=True) class PersonData: """Runtime data for the person integration.""" diff --git a/homeassistant/components/person/const.py b/homeassistant/components/person/const.py index fdc7c593222e..6beb5a1dac64 100644 --- a/homeassistant/components/person/const.py +++ b/homeassistant/components/person/const.py @@ -10,6 +10,8 @@ if TYPE_CHECKING: DOMAIN = "person" +CONF_USER_ID = "user_id" + DATA_PERSON: HassKey[PersonData] = HassKey(DOMAIN) diff --git a/homeassistant/components/person/helpers.py b/homeassistant/components/person/helpers.py new file mode 100644 index 000000000000..117ce9b45a3e --- /dev/null +++ b/homeassistant/components/person/helpers.py @@ -0,0 +1,47 @@ +"""Helpers for the person integration.""" + +import logging + +from homeassistant.components import persistent_notification +from homeassistant.const import CONF_ID, CONF_NAME +from homeassistant.core import HomeAssistant + +from .const import CONF_USER_ID, DOMAIN + +_LOGGER = logging.getLogger(__name__) + + +async def filter_yaml_data(hass: HomeAssistant, persons: list[dict]) -> list[dict]: + """Validate YAML data that we can't validate via schema.""" + filtered = [] + person_invalid_user = [] + + for person_conf in persons: + user_id = person_conf.get(CONF_USER_ID) + + if user_id is not None and await hass.auth.async_get_user(user_id) is None: + _LOGGER.error( + "Invalid user_id detected for person %s", + person_conf[CONF_ID], + ) + person_invalid_user.append( + f"- Person {person_conf[CONF_NAME]} (id: {person_conf[CONF_ID]}) points" + f" at invalid user {user_id}" + ) + continue + + filtered.append(person_conf) + + if person_invalid_user: + persistent_notification.async_create( + hass, + f""" +The following persons point at invalid users: + +{"- ".join(person_invalid_user)} + """, + "Invalid Person Configuration", + DOMAIN, + ) + + return filtered