From a3b03caead353924fb46e4f4e3524e7682709c71 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 07:55:20 +0200 Subject: [PATCH 001/106] Deduce integration from module in `loader.async_get_issue_tracker` (#148017) --- homeassistant/loader.py | 7 +++++++ tests/test_loader.py | 8 ++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/homeassistant/loader.py b/homeassistant/loader.py index ae3709e383b..a66a09d7407 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -1789,6 +1789,13 @@ def async_get_issue_tracker( # If we know nothing about the integration, suggest opening an issue on HA core return issue_tracker + if module and not integration_domain: + # If we only have a module, we can try to get the integration domain from it + if module.startswith("custom_components."): + integration_domain = module.split(".")[1] + elif module.startswith("homeassistant.components."): + integration_domain = module.split(".")[2] + if not integration: integration = async_get_issue_integration(hass, integration_domain) diff --git a/tests/test_loader.py b/tests/test_loader.py index 2d5ad76aa8a..c67b520c7dc 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -1143,10 +1143,10 @@ CUSTOM_ISSUE_TRACKER = "https://blablabla.com" ("hue", "homeassistant.components.hue.sensor", CORE_ISSUE_TRACKER_HUE), ("hue", None, CORE_ISSUE_TRACKER_HUE), ("bla_built_in", None, CORE_ISSUE_TRACKER_BUILT_IN), - # Integration domain is not currently deduced from module - (None, "homeassistant.components.hue.sensor", CORE_ISSUE_TRACKER), + (None, "homeassistant.components.hue.sensor", CORE_ISSUE_TRACKER_HUE), ("hue", "homeassistant.components.mqtt.sensor", CORE_ISSUE_TRACKER_HUE), # Loaded custom integration with known issue tracker + (None, "custom_components.bla_custom.sensor", CUSTOM_ISSUE_TRACKER), ("bla_custom", "custom_components.bla_custom.sensor", CUSTOM_ISSUE_TRACKER), ("bla_custom", None, CUSTOM_ISSUE_TRACKER), # Loaded custom integration without known issue tracker @@ -1155,6 +1155,7 @@ CUSTOM_ISSUE_TRACKER = "https://blablabla.com" ("bla_custom_no_tracker", None, None), ("hue", "custom_components.bla.sensor", None), # Unloaded custom integration with known issue tracker + (None, "custom_components.bla_custom_not_loaded.sensor", CUSTOM_ISSUE_TRACKER), ("bla_custom_not_loaded", None, CUSTOM_ISSUE_TRACKER), # Unloaded custom integration without known issue tracker ("bla_custom_not_loaded_no_tracker", None, None), @@ -1218,8 +1219,7 @@ async def test_async_get_issue_tracker( ("hue", "homeassistant.components.hue.sensor", CORE_ISSUE_TRACKER_HUE), ("hue", None, CORE_ISSUE_TRACKER_HUE), ("bla_built_in", None, CORE_ISSUE_TRACKER_BUILT_IN), - # Integration domain is not currently deduced from module - (None, "homeassistant.components.hue.sensor", CORE_ISSUE_TRACKER), + (None, "homeassistant.components.hue.sensor", CORE_ISSUE_TRACKER_HUE), ("hue", "homeassistant.components.mqtt.sensor", CORE_ISSUE_TRACKER_HUE), # Custom integration with known issue tracker - can't find it without hass ("bla_custom", "custom_components.bla_custom.sensor", None), From 04cc451c765703d8714993b296a06fb182a78dd8 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 4 Jul 2025 08:36:34 +0200 Subject: [PATCH 002/106] Add AI Task platform to Google Gen AI (#146766) --- .../__init__.py | 25 +++- .../ai_task.py | 57 ++++++++ .../config_flow.py | 26 +++- .../const.py | 6 + .../strings.json | 28 ++++ .../conftest.py | 9 ++ .../snapshots/test_diagnostics.ambr | 8 + .../snapshots/test_init.ambr | 31 ++++ .../test_ai_task.py | 62 ++++++++ .../test_config_flow.py | 58 +++++++- .../test_init.py | 138 ++++++++++++++++-- 11 files changed, 423 insertions(+), 25 deletions(-) create mode 100644 homeassistant/components/google_generative_ai_conversation/ai_task.py create mode 100644 tests/components/google_generative_ai_conversation/test_ai_task.py diff --git a/homeassistant/components/google_generative_ai_conversation/__init__.py b/homeassistant/components/google_generative_ai_conversation/__init__.py index 346d5322b02..99e475a376b 100644 --- a/homeassistant/components/google_generative_ai_conversation/__init__.py +++ b/homeassistant/components/google_generative_ai_conversation/__init__.py @@ -3,6 +3,7 @@ from __future__ import annotations import asyncio +from functools import partial import mimetypes from pathlib import Path from types import MappingProxyType @@ -37,11 +38,13 @@ from homeassistant.helpers.typing import ConfigType from .const import ( CONF_PROMPT, + DEFAULT_AI_TASK_NAME, DEFAULT_TITLE, DEFAULT_TTS_NAME, DOMAIN, FILE_POLLING_INTERVAL_SECONDS, LOGGER, + RECOMMENDED_AI_TASK_OPTIONS, RECOMMENDED_CHAT_MODEL, RECOMMENDED_TTS_OPTIONS, TIMEOUT_MILLIS, @@ -53,6 +56,7 @@ CONF_FILENAMES = "filenames" CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) PLATFORMS = ( + Platform.AI_TASK, Platform.CONVERSATION, Platform.TTS, ) @@ -187,11 +191,9 @@ async def async_setup_entry( """Set up Google Generative AI Conversation from a config entry.""" try: - - def _init_client() -> Client: - return Client(api_key=entry.data[CONF_API_KEY]) - - client = await hass.async_add_executor_job(_init_client) + client = await hass.async_add_executor_job( + partial(Client, api_key=entry.data[CONF_API_KEY]) + ) await client.aio.models.get( model=RECOMMENDED_CHAT_MODEL, config={"http_options": {"timeout": TIMEOUT_MILLIS}}, @@ -350,6 +352,19 @@ async def async_migrate_entry( hass.config_entries.async_update_entry(entry, minor_version=2) + if entry.version == 2 and entry.minor_version == 2: + # Add AI Task subentry with default options + hass.config_entries.async_add_subentry( + entry, + ConfigSubentry( + data=MappingProxyType(RECOMMENDED_AI_TASK_OPTIONS), + subentry_type="ai_task_data", + title=DEFAULT_AI_TASK_NAME, + unique_id=None, + ), + ) + hass.config_entries.async_update_entry(entry, minor_version=3) + LOGGER.debug( "Migration to version %s:%s successful", entry.version, entry.minor_version ) diff --git a/homeassistant/components/google_generative_ai_conversation/ai_task.py b/homeassistant/components/google_generative_ai_conversation/ai_task.py new file mode 100644 index 00000000000..ab34af71ebe --- /dev/null +++ b/homeassistant/components/google_generative_ai_conversation/ai_task.py @@ -0,0 +1,57 @@ +"""AI Task integration for Google Generative AI Conversation.""" + +from __future__ import annotations + +from homeassistant.components import ai_task, conversation +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from .const import LOGGER +from .entity import ERROR_GETTING_RESPONSE, GoogleGenerativeAILLMBaseEntity + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up AI Task entities.""" + for subentry in config_entry.subentries.values(): + if subentry.subentry_type != "ai_task_data": + continue + + async_add_entities( + [GoogleGenerativeAITaskEntity(config_entry, subentry)], + config_subentry_id=subentry.subentry_id, + ) + + +class GoogleGenerativeAITaskEntity( + ai_task.AITaskEntity, + GoogleGenerativeAILLMBaseEntity, +): + """Google Generative AI AI Task entity.""" + + _attr_supported_features = ai_task.AITaskEntityFeature.GENERATE_DATA + + async def _async_generate_data( + self, + task: ai_task.GenDataTask, + chat_log: conversation.ChatLog, + ) -> ai_task.GenDataTaskResult: + """Handle a generate data task.""" + await self._async_handle_chat_log(chat_log) + + if not isinstance(chat_log.content[-1], conversation.AssistantContent): + LOGGER.error( + "Last content in chat log is not an AssistantContent: %s. This could be due to the model not returning a valid response", + chat_log.content[-1], + ) + raise HomeAssistantError(ERROR_GETTING_RESPONSE) + + return ai_task.GenDataTaskResult( + conversation_id=chat_log.conversation_id, + data=chat_log.content[-1].content or "", + ) diff --git a/homeassistant/components/google_generative_ai_conversation/config_flow.py b/homeassistant/components/google_generative_ai_conversation/config_flow.py index ade326cf71b..a68ca09e76d 100644 --- a/homeassistant/components/google_generative_ai_conversation/config_flow.py +++ b/homeassistant/components/google_generative_ai_conversation/config_flow.py @@ -3,6 +3,7 @@ from __future__ import annotations from collections.abc import Mapping +from functools import partial import logging from typing import Any, cast @@ -46,10 +47,12 @@ from .const import ( CONF_TOP_K, CONF_TOP_P, CONF_USE_GOOGLE_SEARCH_TOOL, + DEFAULT_AI_TASK_NAME, DEFAULT_CONVERSATION_NAME, DEFAULT_TITLE, DEFAULT_TTS_NAME, DOMAIN, + RECOMMENDED_AI_TASK_OPTIONS, RECOMMENDED_CHAT_MODEL, RECOMMENDED_CONVERSATION_OPTIONS, RECOMMENDED_HARM_BLOCK_THRESHOLD, @@ -72,12 +75,14 @@ STEP_API_DATA_SCHEMA = vol.Schema( ) -async def validate_input(data: dict[str, Any]) -> None: +async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> None: """Validate the user input allows us to connect. Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user. """ - client = genai.Client(api_key=data[CONF_API_KEY]) + client = await hass.async_add_executor_job( + partial(genai.Client, api_key=data[CONF_API_KEY]) + ) await client.aio.models.list( config={ "http_options": { @@ -92,7 +97,7 @@ class GoogleGenerativeAIConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for Google Generative AI Conversation.""" VERSION = 2 - MINOR_VERSION = 2 + MINOR_VERSION = 3 async def async_step_api( self, user_input: dict[str, Any] | None = None @@ -102,7 +107,7 @@ class GoogleGenerativeAIConfigFlow(ConfigFlow, domain=DOMAIN): if user_input is not None: self._async_abort_entries_match(user_input) try: - await validate_input(user_input) + await validate_input(self.hass, user_input) except (APIError, Timeout) as err: if isinstance(err, ClientError) and "API_KEY_INVALID" in str(err): errors["base"] = "invalid_auth" @@ -133,6 +138,12 @@ class GoogleGenerativeAIConfigFlow(ConfigFlow, domain=DOMAIN): "title": DEFAULT_TTS_NAME, "unique_id": None, }, + { + "subentry_type": "ai_task_data", + "data": RECOMMENDED_AI_TASK_OPTIONS, + "title": DEFAULT_AI_TASK_NAME, + "unique_id": None, + }, ], ) return self.async_show_form( @@ -181,6 +192,7 @@ class GoogleGenerativeAIConfigFlow(ConfigFlow, domain=DOMAIN): return { "conversation": LLMSubentryFlowHandler, "tts": LLMSubentryFlowHandler, + "ai_task_data": LLMSubentryFlowHandler, } @@ -214,6 +226,8 @@ class LLMSubentryFlowHandler(ConfigSubentryFlow): options: dict[str, Any] if self._subentry_type == "tts": options = RECOMMENDED_TTS_OPTIONS.copy() + elif self._subentry_type == "ai_task_data": + options = RECOMMENDED_AI_TASK_OPTIONS.copy() else: options = RECOMMENDED_CONVERSATION_OPTIONS.copy() else: @@ -288,6 +302,8 @@ async def google_generative_ai_config_option_schema( default_name = options[CONF_NAME] elif subentry_type == "tts": default_name = DEFAULT_TTS_NAME + elif subentry_type == "ai_task_data": + default_name = DEFAULT_AI_TASK_NAME else: default_name = DEFAULT_CONVERSATION_NAME schema: dict[vol.Required | vol.Optional, Any] = { @@ -315,6 +331,7 @@ async def google_generative_ai_config_option_schema( ), } ) + schema.update( { vol.Required( @@ -443,4 +460,5 @@ async def google_generative_ai_config_option_schema( ): bool, } ) + return schema diff --git a/homeassistant/components/google_generative_ai_conversation/const.py b/homeassistant/components/google_generative_ai_conversation/const.py index 72665cd3437..e7c5ba6bd22 100644 --- a/homeassistant/components/google_generative_ai_conversation/const.py +++ b/homeassistant/components/google_generative_ai_conversation/const.py @@ -12,6 +12,7 @@ CONF_PROMPT = "prompt" DEFAULT_CONVERSATION_NAME = "Google AI Conversation" DEFAULT_TTS_NAME = "Google AI TTS" +DEFAULT_AI_TASK_NAME = "Google AI Task" CONF_RECOMMENDED = "recommended" CONF_CHAT_MODEL = "chat_model" @@ -35,6 +36,7 @@ RECOMMENDED_USE_GOOGLE_SEARCH_TOOL = False TIMEOUT_MILLIS = 10000 FILE_POLLING_INTERVAL_SECONDS = 0.05 + RECOMMENDED_CONVERSATION_OPTIONS = { CONF_PROMPT: llm.DEFAULT_INSTRUCTIONS_PROMPT, CONF_LLM_HASS_API: [llm.LLM_API_ASSIST], @@ -44,3 +46,7 @@ RECOMMENDED_CONVERSATION_OPTIONS = { RECOMMENDED_TTS_OPTIONS = { CONF_RECOMMENDED: True, } + +RECOMMENDED_AI_TASK_OPTIONS = { + CONF_RECOMMENDED: True, +} diff --git a/homeassistant/components/google_generative_ai_conversation/strings.json b/homeassistant/components/google_generative_ai_conversation/strings.json index eef595ad05d..774f41f0279 100644 --- a/homeassistant/components/google_generative_ai_conversation/strings.json +++ b/homeassistant/components/google_generative_ai_conversation/strings.json @@ -88,6 +88,34 @@ "entry_not_loaded": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::abort::entry_not_loaded%]", "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" } + }, + "ai_task_data": { + "initiate_flow": { + "user": "Add Generate data with AI service", + "reconfigure": "Reconfigure Generate data with AI service" + }, + "entry_type": "Generate data with AI service", + "step": { + "set_options": { + "data": { + "name": "[%key:common::config_flow::data::name%]", + "recommended": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::step::set_options::data::recommended%]", + "chat_model": "[%key:common::generic::model%]", + "temperature": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::step::set_options::data::temperature%]", + "top_p": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::step::set_options::data::top_p%]", + "top_k": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::step::set_options::data::top_k%]", + "max_tokens": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::step::set_options::data::max_tokens%]", + "harassment_block_threshold": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::step::set_options::data::harassment_block_threshold%]", + "hate_block_threshold": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::step::set_options::data::hate_block_threshold%]", + "sexual_block_threshold": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::step::set_options::data::sexual_block_threshold%]", + "dangerous_block_threshold": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::step::set_options::data::dangerous_block_threshold%]" + } + } + }, + "abort": { + "entry_not_loaded": "[%key:component::google_generative_ai_conversation::config_subentries::conversation::abort::entry_not_loaded%]", + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" + } } }, "services": { diff --git a/tests/components/google_generative_ai_conversation/conftest.py b/tests/components/google_generative_ai_conversation/conftest.py index 331afc723ae..244ac518fbd 100644 --- a/tests/components/google_generative_ai_conversation/conftest.py +++ b/tests/components/google_generative_ai_conversation/conftest.py @@ -7,6 +7,7 @@ import pytest from homeassistant.components.google_generative_ai_conversation.const import ( CONF_USE_GOOGLE_SEARCH_TOOL, + DEFAULT_AI_TASK_NAME, DEFAULT_CONVERSATION_NAME, DEFAULT_TTS_NAME, ) @@ -29,6 +30,7 @@ def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry: "api_key": "bla", }, version=2, + minor_version=3, subentries_data=[ { "data": {}, @@ -44,6 +46,13 @@ def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry: "subentry_id": "ulid-tts", "unique_id": None, }, + { + "data": {}, + "subentry_type": "ai_task_data", + "title": DEFAULT_AI_TASK_NAME, + "subentry_id": "ulid-ai-task", + "unique_id": None, + }, ], ) entry.runtime_data = Mock() diff --git a/tests/components/google_generative_ai_conversation/snapshots/test_diagnostics.ambr b/tests/components/google_generative_ai_conversation/snapshots/test_diagnostics.ambr index 48091d83a00..bf44b1cbc04 100644 --- a/tests/components/google_generative_ai_conversation/snapshots/test_diagnostics.ambr +++ b/tests/components/google_generative_ai_conversation/snapshots/test_diagnostics.ambr @@ -7,6 +7,14 @@ 'options': dict({ }), 'subentries': dict({ + 'ulid-ai-task': dict({ + 'data': dict({ + }), + 'subentry_id': 'ulid-ai-task', + 'subentry_type': 'ai_task_data', + 'title': 'Google AI Task', + 'unique_id': None, + }), 'ulid-conversation': dict({ 'data': dict({ 'chat_model': 'models/gemini-2.5-flash', diff --git a/tests/components/google_generative_ai_conversation/snapshots/test_init.ambr b/tests/components/google_generative_ai_conversation/snapshots/test_init.ambr index 5722713bc56..a2603328959 100644 --- a/tests/components/google_generative_ai_conversation/snapshots/test_init.ambr +++ b/tests/components/google_generative_ai_conversation/snapshots/test_init.ambr @@ -32,6 +32,37 @@ 'sw_version': None, 'via_device_id': None, }), + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'config_entries_subentries': , + 'configuration_url': None, + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': , + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'google_generative_ai_conversation', + 'ulid-ai-task', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'Google', + 'model': 'gemini-2.5-flash', + 'model_id': None, + 'name': 'Google AI Task', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'suggested_area': None, + 'sw_version': None, + 'via_device_id': None, + }), DeviceRegistryEntrySnapshot({ 'area_id': None, 'config_entries': , diff --git a/tests/components/google_generative_ai_conversation/test_ai_task.py b/tests/components/google_generative_ai_conversation/test_ai_task.py new file mode 100644 index 00000000000..72b62b64615 --- /dev/null +++ b/tests/components/google_generative_ai_conversation/test_ai_task.py @@ -0,0 +1,62 @@ +"""Test AI Task platform of Google Generative AI Conversation integration.""" + +from unittest.mock import AsyncMock + +from google.genai.types import GenerateContentResponse +import pytest + +from homeassistant.components import ai_task +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from tests.common import MockConfigEntry +from tests.components.conversation import ( + MockChatLog, + mock_chat_log, # noqa: F401 +) + + +@pytest.mark.usefixtures("mock_init_component") +async def test_run_task( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_chat_log: MockChatLog, # noqa: F811 + mock_send_message_stream: AsyncMock, + entity_registry: er.EntityRegistry, +) -> None: + """Test empty response.""" + entity_id = "ai_task.google_ai_task" + + # Ensure it's linked to the subentry + entity_entry = entity_registry.async_get(entity_id) + ai_task_entry = next( + iter( + entry + for entry in mock_config_entry.subentries.values() + if entry.subentry_type == "ai_task_data" + ) + ) + assert entity_entry.config_entry_id == mock_config_entry.entry_id + assert entity_entry.config_subentry_id == ai_task_entry.subentry_id + + mock_send_message_stream.return_value = [ + [ + GenerateContentResponse( + candidates=[ + { + "content": { + "parts": [{"text": "Hi there!"}], + "role": "model", + }, + } + ], + ), + ], + ] + result = await ai_task.async_generate_data( + hass, + task_name="Test Task", + entity_id=entity_id, + instructions="Test prompt", + ) + assert result.data == "Hi there!" diff --git a/tests/components/google_generative_ai_conversation/test_config_flow.py b/tests/components/google_generative_ai_conversation/test_config_flow.py index a3fa487e1d3..bf3e2aedb45 100644 --- a/tests/components/google_generative_ai_conversation/test_config_flow.py +++ b/tests/components/google_generative_ai_conversation/test_config_flow.py @@ -19,9 +19,11 @@ from homeassistant.components.google_generative_ai_conversation.const import ( CONF_TOP_K, CONF_TOP_P, CONF_USE_GOOGLE_SEARCH_TOOL, + DEFAULT_AI_TASK_NAME, DEFAULT_CONVERSATION_NAME, DEFAULT_TTS_NAME, DOMAIN, + RECOMMENDED_AI_TASK_OPTIONS, RECOMMENDED_CHAT_MODEL, RECOMMENDED_CONVERSATION_OPTIONS, RECOMMENDED_HARM_BLOCK_THRESHOLD, @@ -121,6 +123,12 @@ async def test_form(hass: HomeAssistant) -> None: "title": DEFAULT_TTS_NAME, "unique_id": None, }, + { + "subentry_type": "ai_task_data", + "data": RECOMMENDED_AI_TASK_OPTIONS, + "title": DEFAULT_AI_TASK_NAME, + "unique_id": None, + }, ] assert len(mock_setup_entry.mock_calls) == 1 @@ -222,7 +230,7 @@ async def test_creating_tts_subentry( assert result2["title"] == "Mock TTS" assert result2["data"] == RECOMMENDED_TTS_OPTIONS - assert len(mock_config_entry.subentries) == 3 + assert len(mock_config_entry.subentries) == 4 new_subentry_id = list(set(mock_config_entry.subentries) - old_subentries)[0] new_subentry = mock_config_entry.subentries[new_subentry_id] @@ -232,13 +240,59 @@ async def test_creating_tts_subentry( assert new_subentry.title == "Mock TTS" +async def test_creating_ai_task_subentry( + hass: HomeAssistant, + mock_init_component: None, + mock_config_entry: MockConfigEntry, +) -> None: + """Test creating an AI task subentry.""" + with patch( + "google.genai.models.AsyncModels.list", + return_value=get_models_pager(), + ): + result = await hass.config_entries.subentries.async_init( + (mock_config_entry.entry_id, "ai_task_data"), + context={"source": config_entries.SOURCE_USER}, + ) + + assert result["type"] is FlowResultType.FORM, result + assert result["step_id"] == "set_options" + assert not result["errors"] + + old_subentries = set(mock_config_entry.subentries) + + with patch( + "google.genai.models.AsyncModels.list", + return_value=get_models_pager(), + ): + result2 = await hass.config_entries.subentries.async_configure( + result["flow_id"], + {CONF_NAME: "Mock AI Task", **RECOMMENDED_AI_TASK_OPTIONS}, + ) + await hass.async_block_till_done() + + assert result2["type"] is FlowResultType.CREATE_ENTRY + assert result2["title"] == "Mock AI Task" + assert result2["data"] == RECOMMENDED_AI_TASK_OPTIONS + + assert len(mock_config_entry.subentries) == 4 + + new_subentry_id = list(set(mock_config_entry.subentries) - old_subentries)[0] + new_subentry = mock_config_entry.subentries[new_subentry_id] + + assert new_subentry.subentry_type == "ai_task_data" + assert new_subentry.data == RECOMMENDED_AI_TASK_OPTIONS + assert new_subentry.title == "Mock AI Task" + + async def test_creating_conversation_subentry_not_loaded( hass: HomeAssistant, mock_init_component: None, mock_config_entry: MockConfigEntry, ) -> None: - """Test creating a conversation subentry.""" + """Test that subentry fails to init if entry not loaded.""" await hass.config_entries.async_unload(mock_config_entry.entry_id) + with patch( "google.genai.models.AsyncModels.list", return_value=get_models_pager(), diff --git a/tests/components/google_generative_ai_conversation/test_init.py b/tests/components/google_generative_ai_conversation/test_init.py index 9702aae4c9e..c0a610f6a0a 100644 --- a/tests/components/google_generative_ai_conversation/test_init.py +++ b/tests/components/google_generative_ai_conversation/test_init.py @@ -8,9 +8,13 @@ from requests.exceptions import Timeout from syrupy.assertion import SnapshotAssertion from homeassistant.components.google_generative_ai_conversation.const import ( + DEFAULT_AI_TASK_NAME, + DEFAULT_CONVERSATION_NAME, DEFAULT_TITLE, DEFAULT_TTS_NAME, DOMAIN, + RECOMMENDED_AI_TASK_OPTIONS, + RECOMMENDED_CONVERSATION_OPTIONS, RECOMMENDED_TTS_OPTIONS, ) from homeassistant.config_entries import ConfigEntryState, ConfigSubentryData @@ -397,7 +401,7 @@ async def test_load_entry_with_unloaded_entries( assert [tuple(mock_call) for mock_call in mock_generate.mock_calls] == snapshot -async def test_migration_from_v1_to_v2( +async def test_migration_from_v1( hass: HomeAssistant, device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, @@ -473,10 +477,10 @@ async def test_migration_from_v1_to_v2( assert len(entries) == 1 entry = entries[0] assert entry.version == 2 - assert entry.minor_version == 2 + assert entry.minor_version == 3 assert not entry.options assert entry.title == DEFAULT_TITLE - assert len(entry.subentries) == 3 + assert len(entry.subentries) == 4 conversation_subentries = [ subentry for subentry in entry.subentries.values() @@ -495,6 +499,14 @@ async def test_migration_from_v1_to_v2( assert len(tts_subentries) == 1 assert tts_subentries[0].data == RECOMMENDED_TTS_OPTIONS assert tts_subentries[0].title == DEFAULT_TTS_NAME + ai_task_subentries = [ + subentry + for subentry in entry.subentries.values() + if subentry.subentry_type == "ai_task_data" + ] + assert len(ai_task_subentries) == 1 + assert ai_task_subentries[0].data == RECOMMENDED_AI_TASK_OPTIONS + assert ai_task_subentries[0].title == DEFAULT_AI_TASK_NAME subentry = conversation_subentries[0] @@ -542,7 +554,7 @@ async def test_migration_from_v1_to_v2( } -async def test_migration_from_v1_to_v2_with_multiple_keys( +async def test_migration_from_v1_with_multiple_keys( hass: HomeAssistant, device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, @@ -619,10 +631,10 @@ async def test_migration_from_v1_to_v2_with_multiple_keys( for entry in entries: assert entry.version == 2 - assert entry.minor_version == 2 + assert entry.minor_version == 3 assert not entry.options assert entry.title == DEFAULT_TITLE - assert len(entry.subentries) == 2 + assert len(entry.subentries) == 3 subentry = list(entry.subentries.values())[0] assert subentry.subentry_type == "conversation" assert subentry.data == options @@ -631,6 +643,10 @@ async def test_migration_from_v1_to_v2_with_multiple_keys( assert subentry.subentry_type == "tts" assert subentry.data == RECOMMENDED_TTS_OPTIONS assert subentry.title == DEFAULT_TTS_NAME + subentry = list(entry.subentries.values())[2] + assert subentry.subentry_type == "ai_task_data" + assert subentry.data == RECOMMENDED_AI_TASK_OPTIONS + assert subentry.title == DEFAULT_AI_TASK_NAME dev = device_registry.async_get_device( identifiers={(DOMAIN, list(entry.subentries.values())[0].subentry_id)} @@ -642,7 +658,7 @@ async def test_migration_from_v1_to_v2_with_multiple_keys( } -async def test_migration_from_v1_to_v2_with_same_keys( +async def test_migration_from_v1_with_same_keys( hass: HomeAssistant, device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, @@ -718,10 +734,10 @@ async def test_migration_from_v1_to_v2_with_same_keys( assert len(entries) == 1 entry = entries[0] assert entry.version == 2 - assert entry.minor_version == 2 + assert entry.minor_version == 3 assert not entry.options assert entry.title == DEFAULT_TITLE - assert len(entry.subentries) == 3 + assert len(entry.subentries) == 4 conversation_subentries = [ subentry for subentry in entry.subentries.values() @@ -740,6 +756,14 @@ async def test_migration_from_v1_to_v2_with_same_keys( assert len(tts_subentries) == 1 assert tts_subentries[0].data == RECOMMENDED_TTS_OPTIONS assert tts_subentries[0].title == DEFAULT_TTS_NAME + ai_task_subentries = [ + subentry + for subentry in entry.subentries.values() + if subentry.subentry_type == "ai_task_data" + ] + assert len(ai_task_subentries) == 1 + assert ai_task_subentries[0].data == RECOMMENDED_AI_TASK_OPTIONS + assert ai_task_subentries[0].title == DEFAULT_AI_TASK_NAME subentry = conversation_subentries[0] @@ -829,7 +853,7 @@ async def test_migration_from_v1_to_v2_with_same_keys( ), ], ) -async def test_migration_from_v2_1_to_v2_2( +async def test_migration_from_v2_1( hass: HomeAssistant, device_registry: dr.DeviceRegistry, entity_registry: er.EntityRegistry, @@ -837,12 +861,13 @@ async def test_migration_from_v2_1_to_v2_2( extra_subentries: list[ConfigSubentryData], expected_device_subentries: dict[str, set[str | None]], ) -> None: - """Test migration from version 2.1 to version 2.2. + """Test migration from version 2.1. This tests we clean up the broken migration in Home Assistant Core - 2025.7.0b0-2025.7.0b1: + 2025.7.0b0-2025.7.0b1 and add AI Task subentry: - Fix device registry (Fixed in Home Assistant Core 2025.7.0b2) - Add TTS subentry (Added in Home Assistant Core 2025.7.0b1) + - Add AI Task subentry (Added in version 2.3) """ # Create a v2.1 config entry with 2 subentries, devices and entities options = { @@ -930,10 +955,10 @@ async def test_migration_from_v2_1_to_v2_2( assert len(entries) == 1 entry = entries[0] assert entry.version == 2 - assert entry.minor_version == 2 + assert entry.minor_version == 3 assert not entry.options assert entry.title == DEFAULT_TITLE - assert len(entry.subentries) == 3 + assert len(entry.subentries) == 4 conversation_subentries = [ subentry for subentry in entry.subentries.values() @@ -952,6 +977,14 @@ async def test_migration_from_v2_1_to_v2_2( assert len(tts_subentries) == 1 assert tts_subentries[0].data == RECOMMENDED_TTS_OPTIONS assert tts_subentries[0].title == DEFAULT_TTS_NAME + ai_task_subentries = [ + subentry + for subentry in entry.subentries.values() + if subentry.subentry_type == "ai_task_data" + ] + assert len(ai_task_subentries) == 1 + assert ai_task_subentries[0].data == RECOMMENDED_AI_TASK_OPTIONS + assert ai_task_subentries[0].title == DEFAULT_AI_TASK_NAME subentry = conversation_subentries[0] @@ -1011,3 +1044,80 @@ async def test_devices( device_registry, mock_config_entry.entry_id ) assert devices == snapshot + + +async def test_migrate_entry_from_v2_2(hass: HomeAssistant) -> None: + """Test migration from version 2.2.""" + # Create a v2.2 config entry with conversation and TTS subentries + mock_config_entry = MockConfigEntry( + domain=DOMAIN, + data={CONF_API_KEY: "test-api-key"}, + version=2, + minor_version=2, + subentries_data=[ + { + "data": RECOMMENDED_CONVERSATION_OPTIONS, + "subentry_type": "conversation", + "title": DEFAULT_CONVERSATION_NAME, + "unique_id": None, + }, + { + "data": RECOMMENDED_TTS_OPTIONS, + "subentry_type": "tts", + "title": DEFAULT_TTS_NAME, + "unique_id": None, + }, + ], + ) + mock_config_entry.add_to_hass(hass) + + # Verify initial state + assert mock_config_entry.version == 2 + assert mock_config_entry.minor_version == 2 + assert len(mock_config_entry.subentries) == 2 + + # Run setup to trigger migration + with patch( + "homeassistant.components.google_generative_ai_conversation.async_setup_entry", + return_value=True, + ): + result = await hass.config_entries.async_setup(mock_config_entry.entry_id) + assert result is True + await hass.async_block_till_done() + + # Verify migration completed + entries = hass.config_entries.async_entries(DOMAIN) + assert len(entries) == 1 + entry = entries[0] + + # Check version and subversion were updated + assert entry.version == 2 + assert entry.minor_version == 3 + + # Check we now have conversation, tts and ai_task_data subentries + assert len(entry.subentries) == 3 + + subentries = { + subentry.subentry_type: subentry for subentry in entry.subentries.values() + } + assert "conversation" in subentries + assert "tts" in subentries + assert "ai_task_data" in subentries + + # Find and verify the ai_task_data subentry + ai_task_subentry = subentries["ai_task_data"] + assert ai_task_subentry is not None + assert ai_task_subentry.title == DEFAULT_AI_TASK_NAME + assert ai_task_subentry.data == RECOMMENDED_AI_TASK_OPTIONS + + # Verify conversation subentry is still there and unchanged + conversation_subentry = subentries["conversation"] + assert conversation_subentry is not None + assert conversation_subentry.title == DEFAULT_CONVERSATION_NAME + assert conversation_subentry.data == RECOMMENDED_CONVERSATION_OPTIONS + + # Verify TTS subentry is still there and unchanged + tts_subentry = subentries["tts"] + assert tts_subentry is not None + assert tts_subentry.title == DEFAULT_TTS_NAME + assert tts_subentry.data == RECOMMENDED_TTS_OPTIONS From 8641a2141c0e0343e6aa580a6294f659d738f311 Mon Sep 17 00:00:00 2001 From: tronikos Date: Fri, 4 Jul 2025 01:10:21 -0700 Subject: [PATCH 003/106] Fix has-entity-name and entity-translations in Opower (#148098) --- homeassistant/components/opower/sensor.py | 43 ++++++++-------- homeassistant/components/opower/strings.json | 52 ++++++++++++++++++++ tests/components/opower/test_sensor.py | 28 ++++++++--- 3 files changed, 94 insertions(+), 29 deletions(-) diff --git a/homeassistant/components/opower/sensor.py b/homeassistant/components/opower/sensor.py index 46aa9e9b318..9fc4d7e536a 100644 --- a/homeassistant/components/opower/sensor.py +++ b/homeassistant/components/opower/sensor.py @@ -24,6 +24,8 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN from .coordinator import OpowerConfigEntry, OpowerCoordinator +PARALLEL_UPDATES = 0 + @dataclass(frozen=True, kw_only=True) class OpowerEntityDescription(SensorEntityDescription): @@ -38,7 +40,7 @@ class OpowerEntityDescription(SensorEntityDescription): ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( OpowerEntityDescription( key="elec_usage_to_date", - name="Current bill electric usage to date", + translation_key="elec_usage_to_date", device_class=SensorDeviceClass.ENERGY, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, # Not TOTAL_INCREASING because it can decrease for accounts with solar @@ -48,7 +50,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="elec_forecasted_usage", - name="Current bill electric forecasted usage", + translation_key="elec_forecasted_usage", device_class=SensorDeviceClass.ENERGY, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, state_class=SensorStateClass.TOTAL, @@ -57,7 +59,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="elec_typical_usage", - name="Typical monthly electric usage", + translation_key="elec_typical_usage", device_class=SensorDeviceClass.ENERGY, native_unit_of_measurement=UnitOfEnergy.KILO_WATT_HOUR, state_class=SensorStateClass.TOTAL, @@ -66,7 +68,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="elec_cost_to_date", - name="Current bill electric cost to date", + translation_key="elec_cost_to_date", device_class=SensorDeviceClass.MONETARY, native_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, @@ -75,7 +77,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="elec_forecasted_cost", - name="Current bill electric forecasted cost", + translation_key="elec_forecasted_cost", device_class=SensorDeviceClass.MONETARY, native_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, @@ -84,7 +86,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="elec_typical_cost", - name="Typical monthly electric cost", + translation_key="elec_typical_cost", device_class=SensorDeviceClass.MONETARY, native_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, @@ -93,7 +95,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="elec_start_date", - name="Current bill electric start date", + translation_key="elec_start_date", device_class=SensorDeviceClass.DATE, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, @@ -101,7 +103,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="elec_end_date", - name="Current bill electric end date", + translation_key="elec_end_date", device_class=SensorDeviceClass.DATE, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, @@ -111,7 +113,7 @@ ELEC_SENSORS: tuple[OpowerEntityDescription, ...] = ( GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( OpowerEntityDescription( key="gas_usage_to_date", - name="Current bill gas usage to date", + translation_key="gas_usage_to_date", device_class=SensorDeviceClass.GAS, native_unit_of_measurement=UnitOfVolume.CENTUM_CUBIC_FEET, state_class=SensorStateClass.TOTAL, @@ -120,7 +122,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="gas_forecasted_usage", - name="Current bill gas forecasted usage", + translation_key="gas_forecasted_usage", device_class=SensorDeviceClass.GAS, native_unit_of_measurement=UnitOfVolume.CENTUM_CUBIC_FEET, state_class=SensorStateClass.TOTAL, @@ -129,7 +131,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="gas_typical_usage", - name="Typical monthly gas usage", + translation_key="gas_typical_usage", device_class=SensorDeviceClass.GAS, native_unit_of_measurement=UnitOfVolume.CENTUM_CUBIC_FEET, state_class=SensorStateClass.TOTAL, @@ -138,7 +140,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="gas_cost_to_date", - name="Current bill gas cost to date", + translation_key="gas_cost_to_date", device_class=SensorDeviceClass.MONETARY, native_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, @@ -147,7 +149,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="gas_forecasted_cost", - name="Current bill gas forecasted cost", + translation_key="gas_forecasted_cost", device_class=SensorDeviceClass.MONETARY, native_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, @@ -156,7 +158,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="gas_typical_cost", - name="Typical monthly gas cost", + translation_key="gas_typical_cost", device_class=SensorDeviceClass.MONETARY, native_unit_of_measurement="USD", state_class=SensorStateClass.TOTAL, @@ -165,7 +167,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="gas_start_date", - name="Current bill gas start date", + translation_key="gas_start_date", device_class=SensorDeviceClass.DATE, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, @@ -173,7 +175,7 @@ GAS_SENSORS: tuple[OpowerEntityDescription, ...] = ( ), OpowerEntityDescription( key="gas_end_date", - name="Current bill gas end date", + translation_key="gas_end_date", device_class=SensorDeviceClass.DATE, entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, @@ -229,6 +231,7 @@ async def async_setup_entry( class OpowerSensor(CoordinatorEntity[OpowerCoordinator], SensorEntity): """Representation of an Opower sensor.""" + _attr_has_entity_name = True entity_description: OpowerEntityDescription def __init__( @@ -249,8 +252,6 @@ class OpowerSensor(CoordinatorEntity[OpowerCoordinator], SensorEntity): @property def native_value(self) -> StateType | date: """Return the state.""" - if self.coordinator.data is not None: - return self.entity_description.value_fn( - self.coordinator.data[self.utility_account_id] - ) - return None + return self.entity_description.value_fn( + self.coordinator.data[self.utility_account_id] + ) diff --git a/homeassistant/components/opower/strings.json b/homeassistant/components/opower/strings.json index 3af968cf789..cd22bd8d7a1 100644 --- a/homeassistant/components/opower/strings.json +++ b/homeassistant/components/opower/strings.json @@ -37,5 +37,57 @@ "title": "Return to grid statistics for account: {utility_account_id}", "description": "We found negative values in your existing consumption statistics, likely because you have solar. We split those in separate return statistics for a better experience in the Energy dashboard.\n\nPlease visit the [Energy configuration page]({energy_settings}) to add the following statistics in the **Return to grid** section:\n\n{target_ids}\n\nOnce you have added them, ignore this issue." } + }, + "entity": { + "sensor": { + "elec_usage_to_date": { + "name": "Current bill electric usage to date" + }, + "elec_forecasted_usage": { + "name": "Current bill electric forecasted usage" + }, + "elec_typical_usage": { + "name": "Typical monthly electric usage" + }, + "elec_cost_to_date": { + "name": "Current bill electric cost to date" + }, + "elec_forecasted_cost": { + "name": "Current bill electric forecasted cost" + }, + "elec_typical_cost": { + "name": "Typical monthly electric cost" + }, + "elec_start_date": { + "name": "Current bill electric start date" + }, + "elec_end_date": { + "name": "Current bill electric end date" + }, + "gas_usage_to_date": { + "name": "Current bill gas usage to date" + }, + "gas_forecasted_usage": { + "name": "Current bill gas forecasted usage" + }, + "gas_typical_usage": { + "name": "Typical monthly gas usage" + }, + "gas_cost_to_date": { + "name": "Current bill gas cost to date" + }, + "gas_forecasted_cost": { + "name": "Current bill gas forecasted cost" + }, + "gas_typical_cost": { + "name": "Typical monthly gas cost" + }, + "gas_start_date": { + "name": "Current bill gas start date" + }, + "gas_end_date": { + "name": "Current bill gas end date" + } + } } } diff --git a/tests/components/opower/test_sensor.py b/tests/components/opower/test_sensor.py index 91ffb271b2b..883bf86f883 100644 --- a/tests/components/opower/test_sensor.py +++ b/tests/components/opower/test_sensor.py @@ -25,36 +25,48 @@ async def test_sensors( entity_registry = er.async_get(hass) # Check electric sensors - entry = entity_registry.async_get("sensor.current_bill_electric_usage_to_date") + entry = entity_registry.async_get( + "sensor.elec_account_111111_current_bill_electric_usage_to_date" + ) assert entry assert entry.unique_id == "pge_111111_elec_usage_to_date" - state = hass.states.get("sensor.current_bill_electric_usage_to_date") + state = hass.states.get( + "sensor.elec_account_111111_current_bill_electric_usage_to_date" + ) assert state assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfEnergy.KILO_WATT_HOUR assert state.state == "100" - entry = entity_registry.async_get("sensor.current_bill_electric_cost_to_date") + entry = entity_registry.async_get( + "sensor.elec_account_111111_current_bill_electric_cost_to_date" + ) assert entry assert entry.unique_id == "pge_111111_elec_cost_to_date" - state = hass.states.get("sensor.current_bill_electric_cost_to_date") + state = hass.states.get( + "sensor.elec_account_111111_current_bill_electric_cost_to_date" + ) assert state assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "USD" assert state.state == "20.0" # Check gas sensors - entry = entity_registry.async_get("sensor.current_bill_gas_usage_to_date") + entry = entity_registry.async_get( + "sensor.gas_account_222222_current_bill_gas_usage_to_date" + ) assert entry assert entry.unique_id == "pge_222222_gas_usage_to_date" - state = hass.states.get("sensor.current_bill_gas_usage_to_date") + state = hass.states.get("sensor.gas_account_222222_current_bill_gas_usage_to_date") assert state assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == UnitOfVolume.CUBIC_METERS # Convert 50 CCF to m³ assert float(state.state) == pytest.approx(50 * 2.83168, abs=1e-3) - entry = entity_registry.async_get("sensor.current_bill_gas_cost_to_date") + entry = entity_registry.async_get( + "sensor.gas_account_222222_current_bill_gas_cost_to_date" + ) assert entry assert entry.unique_id == "pge_222222_gas_cost_to_date" - state = hass.states.get("sensor.current_bill_gas_cost_to_date") + state = hass.states.get("sensor.gas_account_222222_current_bill_gas_cost_to_date") assert state assert state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) == "USD" assert state.state == "15.0" From 1fc624c7a7cef1e6745bb98eeb3355be6dae17ad Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Fri, 4 Jul 2025 04:05:16 -0700 Subject: [PATCH 004/106] Update LLM selector serializer to support ObjectSelector fields and arrays (#148094) --- homeassistant/helpers/llm.py | 18 +++++++++++- tests/helpers/test_llm.py | 53 ++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/homeassistant/helpers/llm.py b/homeassistant/helpers/llm.py index 5d9e4c3bdef..bf89e693870 100644 --- a/homeassistant/helpers/llm.py +++ b/homeassistant/helpers/llm.py @@ -777,7 +777,23 @@ def _selector_serializer(schema: Any) -> Any: # noqa: C901 return result if isinstance(schema, selector.ObjectSelector): - return {"type": "object", "additionalProperties": True} + result = {"type": "object"} + if fields := schema.config.get("fields"): + result["properties"] = { + field: convert( + selector.selector(field_schema["selector"]), + custom_serializer=_selector_serializer, + ) + for field, field_schema in fields.items() + } + else: + result["additionalProperties"] = True + if schema.config.get("multiple"): + result = { + "type": "array", + "items": result, + } + return result if isinstance(schema, selector.SelectSelector): options = [ diff --git a/tests/helpers/test_llm.py b/tests/helpers/test_llm.py index b6894505534..b978559130c 100644 --- a/tests/helpers/test_llm.py +++ b/tests/helpers/test_llm.py @@ -1139,6 +1139,59 @@ async def test_selector_serializer( "type": "object", "additionalProperties": True, } + assert selector_serializer( + selector.ObjectSelector( + { + "fields": { + "name": { + "required": True, + "selector": {"text": {}}, + }, + "percentage": { + "selector": {"number": {"min": 30, "max": 100}}, + }, + }, + "multiple": False, + "label_field": "name", + }, + ) + ) == { + "type": "object", + "properties": { + "name": {"type": "string"}, + "percentage": {"type": "number", "minimum": 30, "maximum": 100}, + }, + } + assert selector_serializer( + selector.ObjectSelector( + { + "fields": { + "name": { + "required": True, + "selector": {"text": {}}, + }, + "percentage": { + "selector": {"number": {"min": 30, "max": 100}}, + }, + }, + "multiple": True, + "label_field": "name", + }, + ) + ) == { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": {"type": "string"}, + "percentage": { + "type": "number", + "minimum": 30, + "maximum": 100, + }, + }, + }, + } assert selector_serializer( selector.SelectSelector( { From 4be2e84ce65de68883f16770818cf2e354cd6cc7 Mon Sep 17 00:00:00 2001 From: Robin Thoni Date: Fri, 4 Jul 2025 14:36:25 +0200 Subject: [PATCH 005/106] Add backward compatibility with older versions of Traccar server (#146639) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Joakim Sørensen --- .../components/traccar_server/coordinator.py | 6 +++--- .../components/traccar_server/helpers.py | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/traccar_server/coordinator.py b/homeassistant/components/traccar_server/coordinator.py index 2c878856cc2..3a0bfe47e5f 100644 --- a/homeassistant/components/traccar_server/coordinator.py +++ b/homeassistant/components/traccar_server/coordinator.py @@ -31,7 +31,7 @@ from .const import ( EVENTS, LOGGER, ) -from .helpers import get_device, get_first_geofence +from .helpers import get_device, get_first_geofence, get_geofence_ids class TraccarServerCoordinatorDataDevice(TypedDict): @@ -131,7 +131,7 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat "device": device, "geofence": get_first_geofence( geofences, - position["geofenceIds"] or [], + get_geofence_ids(device, position), ), "position": position, "attributes": attr, @@ -187,7 +187,7 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat self.data[device_id]["attributes"] = attr self.data[device_id]["geofence"] = get_first_geofence( self._geofences, - position["geofenceIds"] or [], + get_geofence_ids(self.data[device_id]["device"], position), ) update_devices.add(device_id) diff --git a/homeassistant/components/traccar_server/helpers.py b/homeassistant/components/traccar_server/helpers.py index 971f51376b8..9a22f2784bc 100644 --- a/homeassistant/components/traccar_server/helpers.py +++ b/homeassistant/components/traccar_server/helpers.py @@ -2,7 +2,7 @@ from __future__ import annotations -from pytraccar import DeviceModel, GeofenceModel +from pytraccar import DeviceModel, GeofenceModel, PositionModel def get_device(device_id: int, devices: list[DeviceModel]) -> DeviceModel | None: @@ -22,3 +22,17 @@ def get_first_geofence( (geofence for geofence in geofences if geofence["id"] in target), None, ) + + +def get_geofence_ids( + device: DeviceModel, + position: PositionModel, +) -> list[int]: + """Compatibility helper to return a list of geofence IDs.""" + # For Traccar >=5.8 https://github.com/traccar/traccar/commit/30bafaed42e74863c5ca68a33c87f39d1e2de93d + if "geofenceIds" in position: + return position["geofenceIds"] or [] + # For Traccar <5.8 + if "geofenceIds" in device: + return device["geofenceIds"] or [] + return [] From 99d63c49bbe23e2acb2fc80b28aa8f1f8499608c Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 14:47:01 +0200 Subject: [PATCH 006/106] Add comment about error assigning in frame.report_usage (#148105) --- homeassistant/helpers/frame.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/homeassistant/helpers/frame.py b/homeassistant/helpers/frame.py index d7a647e02eb..8f0741b5166 100644 --- a/homeassistant/helpers/frame.py +++ b/homeassistant/helpers/frame.py @@ -193,6 +193,11 @@ def report_usage( exclude_integrations=exclude_integrations ) except MissingIntegrationFrame as err: + # We need to be careful with assigning the error here as it affects the + # cleanup of objects referenced from the stack trace as seen in + # https://github.com/home-assistant/core/pull/148021#discussion_r2182379834 + # When core_behavior is ReportBehavior.ERROR, we will re-raise the error, + # so we can safely assign it to integration_frame_err. if core_behavior is ReportBehavior.ERROR: integration_frame_err = err _report_usage_partial = functools.partial( From b3d9908cd978df3943bfce4563f9660f78187f5a Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Fri, 4 Jul 2025 06:03:34 -0700 Subject: [PATCH 007/106] Add AI task structured output (#148083) Co-authored-by: Paulus Schoutsen Co-authored-by: Claude Co-authored-by: Paulus Schoutsen --- homeassistant/components/ai_task/__init__.py | 32 +++- homeassistant/components/ai_task/const.py | 2 + .../components/ai_task/services.yaml | 6 + homeassistant/components/ai_task/strings.json | 4 + homeassistant/components/ai_task/task.py | 7 + homeassistant/helpers/service.py | 2 + tests/components/ai_task/conftest.py | 12 +- tests/components/ai_task/test_entity.py | 39 +++++ tests/components/ai_task/test_init.py | 163 +++++++++++++++++- 9 files changed, 262 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/ai_task/__init__.py b/homeassistant/components/ai_task/__init__.py index 692e5d410ae..95c080cc472 100644 --- a/homeassistant/components/ai_task/__init__.py +++ b/homeassistant/components/ai_task/__init__.py @@ -1,11 +1,12 @@ """Integration to offer AI tasks to Home Assistant.""" import logging +from typing import Any import voluptuous as vol from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.const import ATTR_ENTITY_ID, CONF_DESCRIPTION, CONF_SELECTOR from homeassistant.core import ( HassJobType, HomeAssistant, @@ -14,12 +15,14 @@ from homeassistant.core import ( SupportsResponse, callback, ) -from homeassistant.helpers import config_validation as cv, storage +from homeassistant.helpers import config_validation as cv, selector, storage from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.typing import UNDEFINED, ConfigType, UndefinedType from .const import ( ATTR_INSTRUCTIONS, + ATTR_REQUIRED, + ATTR_STRUCTURE, ATTR_TASK_NAME, DATA_COMPONENT, DATA_PREFERENCES, @@ -47,6 +50,27 @@ _LOGGER = logging.getLogger(__name__) CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) +STRUCTURE_FIELD_SCHEMA = vol.Schema( + { + vol.Optional(CONF_DESCRIPTION): str, + vol.Optional(ATTR_REQUIRED): bool, + vol.Required(CONF_SELECTOR): selector.validate_selector, + } +) + + +def _validate_structure_fields(value: dict[str, Any]) -> vol.Schema: + """Validate the structure fields as a voluptuous Schema.""" + if not isinstance(value, dict): + raise vol.Invalid("Structure must be a dictionary") + fields = {} + for k, v in value.items(): + field_class = vol.Required if v.get(ATTR_REQUIRED, False) else vol.Optional + fields[field_class(k, description=v.get(CONF_DESCRIPTION))] = selector.selector( + v[CONF_SELECTOR] + ) + return vol.Schema(fields, extra=vol.PREVENT_EXTRA) + async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Register the process service.""" @@ -64,6 +88,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: vol.Required(ATTR_TASK_NAME): cv.string, vol.Optional(ATTR_ENTITY_ID): cv.entity_id, vol.Required(ATTR_INSTRUCTIONS): cv.string, + vol.Optional(ATTR_STRUCTURE): vol.All( + vol.Schema({str: STRUCTURE_FIELD_SCHEMA}), + _validate_structure_fields, + ), } ), supports_response=SupportsResponse.ONLY, diff --git a/homeassistant/components/ai_task/const.py b/homeassistant/components/ai_task/const.py index 8b612e90560..fa8702ed69e 100644 --- a/homeassistant/components/ai_task/const.py +++ b/homeassistant/components/ai_task/const.py @@ -21,6 +21,8 @@ SERVICE_GENERATE_DATA = "generate_data" ATTR_INSTRUCTIONS: Final = "instructions" ATTR_TASK_NAME: Final = "task_name" +ATTR_STRUCTURE: Final = "structure" +ATTR_REQUIRED: Final = "required" DEFAULT_SYSTEM_PROMPT = ( "You are a Home Assistant expert and help users with their tasks." diff --git a/homeassistant/components/ai_task/services.yaml b/homeassistant/components/ai_task/services.yaml index a531ca599b1..d55b0e60fac 100644 --- a/homeassistant/components/ai_task/services.yaml +++ b/homeassistant/components/ai_task/services.yaml @@ -17,3 +17,9 @@ generate_data: domain: ai_task supported_features: - ai_task.AITaskEntityFeature.GENERATE_DATA + structure: + advanced: true + required: false + example: '{ "name": { "selector": { "text": }, "description": "Name of the user", "required": "True" } } }, "age": { "selector": { "number": }, "description": "Age of the user" } }' + selector: + object: diff --git a/homeassistant/components/ai_task/strings.json b/homeassistant/components/ai_task/strings.json index 877174de681..92106c3baca 100644 --- a/homeassistant/components/ai_task/strings.json +++ b/homeassistant/components/ai_task/strings.json @@ -15,6 +15,10 @@ "entity_id": { "name": "Entity ID", "description": "Entity ID to run the task on. If not provided, the preferred entity will be used." + }, + "structure": { + "name": "Structured output", + "description": "When set, the AI Task will output fields with this in structure. The structure is a dictionary where the keys are the field names and the values contain a 'description', a 'selector', and an optional 'required' field." } } } diff --git a/homeassistant/components/ai_task/task.py b/homeassistant/components/ai_task/task.py index 2e546897602..b6defbfad31 100644 --- a/homeassistant/components/ai_task/task.py +++ b/homeassistant/components/ai_task/task.py @@ -5,6 +5,8 @@ from __future__ import annotations from dataclasses import dataclass from typing import Any +import voluptuous as vol + from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError @@ -17,6 +19,7 @@ async def async_generate_data( task_name: str, entity_id: str | None = None, instructions: str, + structure: vol.Schema | None = None, ) -> GenDataTaskResult: """Run a task in the AI Task integration.""" if entity_id is None: @@ -38,6 +41,7 @@ async def async_generate_data( GenDataTask( name=task_name, instructions=instructions, + structure=structure, ) ) @@ -52,6 +56,9 @@ class GenDataTask: instructions: str """Instructions on what needs to be done.""" + structure: vol.Schema | None = None + """Optional structure for the data to be generated.""" + def __str__(self) -> str: """Return task as a string.""" return f"" diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 51d9c97ceeb..c7d4a26c86e 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -86,6 +86,7 @@ ALL_SERVICE_DESCRIPTIONS_CACHE: HassKey[ def _base_components() -> dict[str, ModuleType]: """Return a cached lookup of base components.""" from homeassistant.components import ( # noqa: PLC0415 + ai_task, alarm_control_panel, assist_satellite, calendar, @@ -107,6 +108,7 @@ def _base_components() -> dict[str, ModuleType]: ) return { + "ai_task": ai_task, "alarm_control_panel": alarm_control_panel, "assist_satellite": assist_satellite, "calendar": calendar, diff --git a/tests/components/ai_task/conftest.py b/tests/components/ai_task/conftest.py index 7efbd1ffcdb..e80e70ddaed 100644 --- a/tests/components/ai_task/conftest.py +++ b/tests/components/ai_task/conftest.py @@ -1,5 +1,7 @@ """Test helpers for AI Task integration.""" +import json + import pytest from homeassistant.components.ai_task import ( @@ -45,12 +47,18 @@ class MockAITaskEntity(AITaskEntity): ) -> GenDataTaskResult: """Mock handling of generate data task.""" self.mock_generate_data_tasks.append(task) + if task.structure is not None: + data = {"name": "Tracy Chen", "age": 30} + data_chat_log = json.dumps(data) + else: + data = "Mock result" + data_chat_log = data chat_log.async_add_assistant_content_without_tools( - AssistantContent(self.entity_id, "Mock result") + AssistantContent(self.entity_id, data_chat_log) ) return GenDataTaskResult( conversation_id=chat_log.conversation_id, - data="Mock result", + data=data, ) diff --git a/tests/components/ai_task/test_entity.py b/tests/components/ai_task/test_entity.py index 3ed1c393588..08f1bb42836 100644 --- a/tests/components/ai_task/test_entity.py +++ b/tests/components/ai_task/test_entity.py @@ -1,10 +1,12 @@ """Tests for the AI Task entity model.""" from freezegun import freeze_time +import voluptuous as vol from homeassistant.components.ai_task import async_generate_data from homeassistant.const import STATE_UNKNOWN from homeassistant.core import HomeAssistant +from homeassistant.helpers import selector from .conftest import TEST_ENTITY_ID, MockAITaskEntity @@ -37,3 +39,40 @@ async def test_state_generate_data( assert mock_ai_task_entity.mock_generate_data_tasks task = mock_ai_task_entity.mock_generate_data_tasks[0] assert task.instructions == "Test prompt" + + +async def test_generate_structured_data( + hass: HomeAssistant, + init_components: None, + mock_config_entry: MockConfigEntry, + mock_ai_task_entity: MockAITaskEntity, +) -> None: + """Test the entity can generate structured data.""" + result = await async_generate_data( + hass, + task_name="Test task", + entity_id=TEST_ENTITY_ID, + instructions="Please generate a profile for a new user", + structure=vol.Schema( + { + vol.Required("name"): selector.TextSelector(), + vol.Optional("age"): selector.NumberSelector( + config=selector.NumberSelectorConfig( + min=0, + max=120, + ) + ), + } + ), + ) + # Arbitrary data returned by the mock entity (not determined by above schema in test) + assert result.data == { + "name": "Tracy Chen", + "age": 30, + } + + assert mock_ai_task_entity.mock_generate_data_tasks + task = mock_ai_task_entity.mock_generate_data_tasks[0] + assert task.instructions == "Please generate a profile for a new user" + assert task.structure + assert isinstance(task.structure, vol.Schema) diff --git a/tests/components/ai_task/test_init.py b/tests/components/ai_task/test_init.py index fdfaaccd0a4..d32b09adec5 100644 --- a/tests/components/ai_task/test_init.py +++ b/tests/components/ai_task/test_init.py @@ -1,13 +1,17 @@ """Test initialization of the AI Task component.""" +from typing import Any + from freezegun.api import FrozenDateTimeFactory import pytest +import voluptuous as vol from homeassistant.components.ai_task import AITaskPreferences from homeassistant.components.ai_task.const import DATA_PREFERENCES from homeassistant.core import HomeAssistant +from homeassistant.helpers import selector -from .conftest import TEST_ENTITY_ID +from .conftest import TEST_ENTITY_ID, MockAITaskEntity from tests.common import flush_store @@ -82,3 +86,160 @@ async def test_generate_data_service( ) assert result["data"] == "Mock result" + + +async def test_generate_data_service_structure_fields( + hass: HomeAssistant, + init_components: None, + mock_ai_task_entity: MockAITaskEntity, +) -> None: + """Test the entity can generate structured data with a top level object schema.""" + result = await hass.services.async_call( + "ai_task", + "generate_data", + { + "task_name": "Profile Generation", + "instructions": "Please generate a profile for a new user", + "entity_id": TEST_ENTITY_ID, + "structure": { + "name": { + "description": "First and last name of the user such as Alice Smith", + "required": True, + "selector": {"text": {}}, + }, + "age": { + "description": "Age of the user", + "selector": { + "number": { + "min": 0, + "max": 120, + } + }, + }, + }, + }, + blocking=True, + return_response=True, + ) + # Arbitrary data returned by the mock entity (not determined by above schema in test) + assert result["data"] == { + "name": "Tracy Chen", + "age": 30, + } + + assert mock_ai_task_entity.mock_generate_data_tasks + task = mock_ai_task_entity.mock_generate_data_tasks[0] + assert task.instructions == "Please generate a profile for a new user" + assert task.structure + assert isinstance(task.structure, vol.Schema) + schema = list(task.structure.schema.items()) + assert len(schema) == 2 + + name_key, name_value = schema[0] + assert name_key == "name" + assert isinstance(name_key, vol.Required) + assert name_key.description == "First and last name of the user such as Alice Smith" + assert isinstance(name_value, selector.TextSelector) + + age_key, age_value = schema[1] + assert age_key == "age" + assert isinstance(age_key, vol.Optional) + assert age_key.description == "Age of the user" + assert isinstance(age_value, selector.NumberSelector) + assert age_value.config["min"] == 0 + assert age_value.config["max"] == 120 + + +@pytest.mark.parametrize( + ("structure", "expected_exception", "expected_error"), + [ + ( + { + "name": { + "description": "First and last name of the user such as Alice Smith", + "selector": {"invalid-selector": {}}, + }, + }, + vol.Invalid, + r"Unknown selector type invalid-selector.*", + ), + ( + { + "name": { + "description": "First and last name of the user such as Alice Smith", + "selector": { + "text": { + "extra-config": False, + } + }, + }, + }, + vol.Invalid, + r"extra keys not allowed.*", + ), + ( + { + "name": { + "description": "First and last name of the user such as Alice Smith", + }, + }, + vol.Invalid, + r"required key not provided.*selector.*", + ), + (12345, vol.Invalid, r"xpected a dictionary.*"), + ("name", vol.Invalid, r"xpected a dictionary.*"), + (["name"], vol.Invalid, r"xpected a dictionary.*"), + ( + { + "name": { + "description": "First and last name of the user such as Alice Smith", + "selector": {"text": {}}, + "extra-fields": "Some extra fields", + }, + }, + vol.Invalid, + r"extra keys not allowed .*", + ), + ( + { + "name": { + "description": "First and last name of the user such as Alice Smith", + "selector": "invalid-schema", + }, + }, + vol.Invalid, + r"xpected a dictionary for dictionary.", + ), + ], + ids=( + "invalid-selector", + "invalid-selector-config", + "missing-selector", + "structure-is-int-not-object", + "structure-is-str-not-object", + "structure-is-list-not-object", + "extra-fields", + "invalid-selector-schema", + ), +) +async def test_generate_data_service_invalid_structure( + hass: HomeAssistant, + init_components: None, + structure: Any, + expected_exception: Exception, + expected_error: str, +) -> None: + """Test the entity can generate structured data.""" + with pytest.raises(expected_exception, match=expected_error): + await hass.services.async_call( + "ai_task", + "generate_data", + { + "task_name": "Profile Generation", + "instructions": "Please generate a profile for a new user", + "entity_id": TEST_ENTITY_ID, + "structure": structure, + }, + blocking=True, + return_response=True, + ) From e47bdc06a0dc84e95e9bbc4af1928bd1a29036ef Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 4 Jul 2025 16:00:37 +0200 Subject: [PATCH 008/106] Set docstyle convention to google in ruff (#148142) --- homeassistant/components/habitica/util.py | 19 ++--- homeassistant/helpers/event.py | 97 +++++++++++------------ pyproject.toml | 1 + tests/conftest.py | 2 +- 4 files changed, 54 insertions(+), 65 deletions(-) diff --git a/homeassistant/components/habitica/util.py b/homeassistant/components/habitica/util.py index 9ef0b8cbadd..35e1577ae21 100644 --- a/homeassistant/components/habitica/util.py +++ b/homeassistant/components/habitica/util.py @@ -95,21 +95,16 @@ def get_recurrence_rule(recurrence: rrule) -> str: 'DTSTART:YYYYMMDDTHHMMSS\nRRULE:FREQ=YEARLY;INTERVAL=2' - Parameters - ---------- - recurrence : rrule - An RRULE object. + Args: + recurrence: An RRULE object. - Returns - ------- - str + Returns: The recurrence rule portion of the RRULE string, starting with 'FREQ='. - Example - ------- - >>> rule = get_recurrence_rule(task) - >>> print(rule) - 'FREQ=YEARLY;INTERVAL=2' + Example: + >>> rule = get_recurrence_rule(task) + >>> print(rule) + 'FREQ=YEARLY;INTERVAL=2' """ return str(recurrence).split("RRULE:")[1] diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index baf1f144a3f..3b959337b6d 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -866,19 +866,17 @@ def async_track_state_change_filtered( ) -> _TrackStateChangeFiltered: """Track state changes with a TrackStates filter that can be updated. - Parameters - ---------- - hass - Home assistant object. - track_states - A TrackStates data class. - action - Callable to call with results. + Args: + hass: + Home assistant object. + track_states: + A TrackStates data class. + action: + Callable to call with results. - Returns - ------- - Object used to update the listeners (async_update_listeners) with a new - TrackStates or cancel the tracking (async_remove). + Returns: + Object used to update the listeners (async_update_listeners) with a new + TrackStates or cancel the tracking (async_remove). """ tracker = _TrackStateChangeFiltered(hass, track_states, action) @@ -907,29 +905,26 @@ def async_track_template( exception, the listener will still be registered but will only fire if the template result becomes true without an exception. - Action arguments - ---------------- - entity_id - ID of the entity that triggered the state change. - old_state - The old state of the entity that changed. - new_state - New state of the entity that changed. + Action args: + entity_id: + ID of the entity that triggered the state change. + old_state: + The old state of the entity that changed. + new_state: + New state of the entity that changed. - Parameters - ---------- - hass - Home assistant object. - template - The template to calculate. - action - Callable to call with results. See above for arguments. - variables - Variables to pass to the template. + Args: + hass: + Home assistant object. + template: + The template to calculate. + action: + Callable to call with results. See above for arguments. + variables: + Variables to pass to the template. - Returns - ------- - Callable to unregister the listener. + Returns: + Callable to unregister the listener. """ job = HassJob(action, f"track template {template}") @@ -1361,26 +1356,24 @@ def async_track_template_result( Once the template returns to a non-error condition the result is sent to the action as usual. - Parameters - ---------- - hass - Home assistant object. - track_templates - An iterable of TrackTemplate. - action - Callable to call with results. - strict - When set to True, raise on undefined variables. - log_fn - If not None, template error messages will logging by calling log_fn - instead of the normal logging facility. - has_super_template - When set to True, the first template will block rendering of other - templates if it doesn't render as True. + Args: + hass: + Home assistant object. + track_templates: + An iterable of TrackTemplate. + action: + Callable to call with results. + strict: + When set to True, raise on undefined variables. + log_fn: + If not None, template error messages will logging by calling log_fn + instead of the normal logging facility. + has_super_template: + When set to True, the first template will block rendering of other + templates if it doesn't render as True. - Returns - ------- - Info object used to unregister the listener, and refresh the template. + Returns: + Info object used to unregister the listener, and refresh the template. """ tracker = TrackTemplateResultInfo(hass, track_templates, action, has_super_template) diff --git a/pyproject.toml b/pyproject.toml index 399d35ffb41..25f4d6d4a1a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -913,4 +913,5 @@ split-on-trailing-comma = false max-complexity = 25 [tool.ruff.lint.pydocstyle] +convention = "google" property-decorators = ["propcache.api.cached_property"] diff --git a/tests/conftest.py b/tests/conftest.py index ef31eee4004..9fdf010eb64 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1724,7 +1724,7 @@ async def async_test_recorder( wait_recorder: bool = True, wait_recorder_setup: bool = True, ) -> AsyncGenerator[recorder.Recorder]: - """Setup and return recorder instance.""" # noqa: D401 + """Setup and return recorder instance.""" await _async_init_recorder_component( hass, config, From 510fd09163a82c3bad97d8da559d24257c767eef Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 16:03:42 +0200 Subject: [PATCH 009/106] Allow core integrations to describe their conditions (#147529) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Abílio Costa --- homeassistant/bootstrap.py | 2 + .../components/websocket_api/commands.py | 53 ++++ homeassistant/helpers/condition.py | 222 +++++++++++++- homeassistant/loader.py | 6 + script/hassfest/__main__.py | 2 + script/hassfest/conditions.py | 225 ++++++++++++++ script/hassfest/icons.py | 11 + script/hassfest/translations.py | 16 + tests/common.py | 2 + .../components/websocket_api/test_commands.py | 86 ++++++ tests/helpers/test_condition.py | 288 +++++++++++++++++- 11 files changed, 907 insertions(+), 6 deletions(-) create mode 100644 script/hassfest/conditions.py diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 0b86bdb7087..397f765174d 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -76,6 +76,7 @@ from .exceptions import HomeAssistantError from .helpers import ( area_registry, category_registry, + condition, config_validation as cv, device_registry, entity, @@ -452,6 +453,7 @@ async def async_load_base_functionality(hass: core.HomeAssistant) -> None: create_eager_task(restore_state.async_load(hass)), create_eager_task(hass.config_entries.async_initialize()), create_eager_task(async_get_system_info(hass)), + create_eager_task(condition.async_setup(hass)), create_eager_task(trigger.async_setup(hass)), ) diff --git a/homeassistant/components/websocket_api/commands.py b/homeassistant/components/websocket_api/commands.py index 701a9a659b1..b63e5e14820 100644 --- a/homeassistant/components/websocket_api/commands.py +++ b/homeassistant/components/websocket_api/commands.py @@ -35,6 +35,10 @@ from homeassistant.exceptions import ( Unauthorized, ) from homeassistant.helpers import config_validation as cv, entity, template +from homeassistant.helpers.condition import ( + async_get_all_descriptions as async_get_all_condition_descriptions, + async_subscribe_platform_events as async_subscribe_condition_platform_events, +) from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entityfilter import ( INCLUDE_EXCLUDE_BASE_FILTER_SCHEMA, @@ -76,6 +80,7 @@ from . import const, decorators, messages from .connection import ActiveConnection from .messages import construct_event_message, construct_result_message +ALL_CONDITION_DESCRIPTIONS_JSON_CACHE = "websocket_api_all_condition_descriptions_json" ALL_SERVICE_DESCRIPTIONS_JSON_CACHE = "websocket_api_all_service_descriptions_json" ALL_TRIGGER_DESCRIPTIONS_JSON_CACHE = "websocket_api_all_trigger_descriptions_json" @@ -101,6 +106,7 @@ def async_register_commands( async_reg(hass, handle_ping) async_reg(hass, handle_render_template) async_reg(hass, handle_subscribe_bootstrap_integrations) + async_reg(hass, handle_subscribe_condition_platforms) async_reg(hass, handle_subscribe_events) async_reg(hass, handle_subscribe_trigger) async_reg(hass, handle_subscribe_trigger_platforms) @@ -501,6 +507,53 @@ def _send_handle_entities_init_response( ) +async def _async_get_all_condition_descriptions_json(hass: HomeAssistant) -> bytes: + """Return JSON of descriptions (i.e. user documentation) for all condition.""" + descriptions = await async_get_all_condition_descriptions(hass) + if ALL_CONDITION_DESCRIPTIONS_JSON_CACHE in hass.data: + cached_descriptions, cached_json_payload = hass.data[ + ALL_CONDITION_DESCRIPTIONS_JSON_CACHE + ] + # If the descriptions are the same, return the cached JSON payload + if cached_descriptions is descriptions: + return cast(bytes, cached_json_payload) + json_payload = json_bytes( + { + condition: description + for condition, description in descriptions.items() + if description is not None + } + ) + hass.data[ALL_CONDITION_DESCRIPTIONS_JSON_CACHE] = (descriptions, json_payload) + return json_payload + + +@decorators.websocket_command({vol.Required("type"): "condition_platforms/subscribe"}) +@decorators.async_response +async def handle_subscribe_condition_platforms( + hass: HomeAssistant, connection: ActiveConnection, msg: dict[str, Any] +) -> None: + """Handle subscribe conditions command.""" + + async def on_new_conditions(new_conditions: set[str]) -> None: + """Forward new conditions to websocket.""" + descriptions = await async_get_all_condition_descriptions(hass) + new_condition_descriptions = {} + for condition in new_conditions: + if (description := descriptions[condition]) is not None: + new_condition_descriptions[condition] = description + if not new_condition_descriptions: + return + connection.send_event(msg["id"], new_condition_descriptions) + + connection.subscriptions[msg["id"]] = async_subscribe_condition_platform_events( + hass, on_new_conditions + ) + connection.send_result(msg["id"]) + conditions_json = await _async_get_all_condition_descriptions_json(hass) + connection.send_message(construct_event_message(msg["id"], conditions_json)) + + async def _async_get_all_service_descriptions_json(hass: HomeAssistant) -> bytes: """Return JSON of descriptions (i.e. user documentation) for all service calls.""" descriptions = await async_get_all_service_descriptions(hass) diff --git a/homeassistant/helpers/condition.py b/homeassistant/helpers/condition.py index 86b8a1002f1..5a9ffb6d91b 100644 --- a/homeassistant/helpers/condition.py +++ b/homeassistant/helpers/condition.py @@ -5,19 +5,17 @@ from __future__ import annotations import abc import asyncio from collections import deque -from collections.abc import Callable, Container, Generator +from collections.abc import Callable, Container, Coroutine, Generator, Iterable from contextlib import contextmanager from datetime import datetime, time as dt_time, timedelta import functools as ft import logging import re import sys -from typing import Any, Protocol, cast +from typing import TYPE_CHECKING, Any, Protocol, cast import voluptuous as vol -from homeassistant.components import zone as zone_cmp -from homeassistant.components.sensor import SensorDeviceClass from homeassistant.const import ( ATTR_DEVICE_CLASS, ATTR_GPS_ACCURACY, @@ -54,11 +52,20 @@ from homeassistant.exceptions import ( HomeAssistantError, TemplateError, ) -from homeassistant.loader import IntegrationNotFound, async_get_integration +from homeassistant.loader import ( + Integration, + IntegrationNotFound, + async_get_integration, + async_get_integrations, +) from homeassistant.util import dt as dt_util from homeassistant.util.async_ import run_callback_threadsafe +from homeassistant.util.hass_dict import HassKey +from homeassistant.util.yaml import load_yaml_dict +from homeassistant.util.yaml.loader import JSON_TYPE from . import config_validation as cv, entity_registry as er +from .integration_platform import async_process_integration_platforms from .template import Template, render_complex from .trace import ( TraceElement, @@ -76,6 +83,8 @@ ASYNC_FROM_CONFIG_FORMAT = "async_{}_from_config" FROM_CONFIG_FORMAT = "{}_from_config" VALIDATE_CONFIG_FORMAT = "{}_validate_config" +_LOGGER = logging.getLogger(__name__) + _PLATFORM_ALIASES: dict[str | None, str | None] = { "and": None, "device": "device_automation", @@ -94,6 +103,99 @@ INPUT_ENTITY_ID = re.compile( ) +CONDITION_DESCRIPTION_CACHE: HassKey[dict[str, dict[str, Any] | None]] = HassKey( + "condition_description_cache" +) +CONDITION_PLATFORM_SUBSCRIPTIONS: HassKey[ + list[Callable[[set[str]], Coroutine[Any, Any, None]]] +] = HassKey("condition_platform_subscriptions") +CONDITIONS: HassKey[dict[str, str]] = HassKey("conditions") + + +# Basic schemas to sanity check the condition descriptions, +# full validation is done by hassfest.conditions +_FIELD_SCHEMA = vol.Schema( + {}, + extra=vol.ALLOW_EXTRA, +) + +_CONDITION_SCHEMA = vol.Schema( + { + vol.Optional("fields"): vol.Schema({str: _FIELD_SCHEMA}), + }, + extra=vol.ALLOW_EXTRA, +) + + +def starts_with_dot(key: str) -> str: + """Check if key starts with dot.""" + if not key.startswith("."): + raise vol.Invalid("Key does not start with .") + return key + + +_CONDITIONS_SCHEMA = vol.Schema( + { + vol.Remove(vol.All(str, starts_with_dot)): object, + cv.slug: vol.Any(None, _CONDITION_SCHEMA), + } +) + + +async def async_setup(hass: HomeAssistant) -> None: + """Set up the condition helper.""" + hass.data[CONDITION_DESCRIPTION_CACHE] = {} + hass.data[CONDITION_PLATFORM_SUBSCRIPTIONS] = [] + hass.data[CONDITIONS] = {} + await async_process_integration_platforms( + hass, "condition", _register_condition_platform, wait_for_platforms=True + ) + + +@callback +def async_subscribe_platform_events( + hass: HomeAssistant, + on_event: Callable[[set[str]], Coroutine[Any, Any, None]], +) -> Callable[[], None]: + """Subscribe to condition platform events.""" + condition_platform_event_subscriptions = hass.data[CONDITION_PLATFORM_SUBSCRIPTIONS] + + def remove_subscription() -> None: + condition_platform_event_subscriptions.remove(on_event) + + condition_platform_event_subscriptions.append(on_event) + return remove_subscription + + +async def _register_condition_platform( + hass: HomeAssistant, integration_domain: str, platform: ConditionProtocol +) -> None: + """Register a condition platform.""" + + new_conditions: set[str] = set() + + if hasattr(platform, "async_get_conditions"): + for condition_key in await platform.async_get_conditions(hass): + hass.data[CONDITIONS][condition_key] = integration_domain + new_conditions.add(condition_key) + else: + _LOGGER.debug( + "Integration %s does not provide condition support, skipping", + integration_domain, + ) + return + + # We don't use gather here because gather adds additional overhead + # when wrapping each coroutine in a task, and we expect our listeners + # to call condition.async_get_all_descriptions which will only yield + # the first time it's called, after that it returns cached data. + for listener in hass.data[CONDITION_PLATFORM_SUBSCRIPTIONS]: + try: + await listener(new_conditions) + except Exception: + _LOGGER.exception("Error while notifying condition platform listener") + + class Condition(abc.ABC): """Condition class.""" @@ -717,6 +819,8 @@ def time( for the opposite. "(23:59 <= now < 00:01)" would be the same as "not (00:01 <= now < 23:59)". """ + from homeassistant.components.sensor import SensorDeviceClass # noqa: PLC0415 + now = dt_util.now() now_time = now.time() @@ -824,6 +928,8 @@ def zone( Async friendly. """ + from homeassistant.components import zone as zone_cmp # noqa: PLC0415 + if zone_ent is None: raise ConditionErrorMessage("zone", "no zone specified") @@ -1080,3 +1186,109 @@ def async_extract_devices(config: ConfigType | Template) -> set[str]: referenced.add(device_id) return referenced + + +def _load_conditions_file(hass: HomeAssistant, integration: Integration) -> JSON_TYPE: + """Load conditions file for an integration.""" + try: + return cast( + JSON_TYPE, + _CONDITIONS_SCHEMA( + load_yaml_dict(str(integration.file_path / "conditions.yaml")) + ), + ) + except FileNotFoundError: + _LOGGER.warning( + "Unable to find conditions.yaml for the %s integration", integration.domain + ) + return {} + except (HomeAssistantError, vol.Invalid) as ex: + _LOGGER.warning( + "Unable to parse conditions.yaml for the %s integration: %s", + integration.domain, + ex, + ) + return {} + + +def _load_conditions_files( + hass: HomeAssistant, integrations: Iterable[Integration] +) -> dict[str, JSON_TYPE]: + """Load condition files for multiple integrations.""" + return { + integration.domain: _load_conditions_file(hass, integration) + for integration in integrations + } + + +async def async_get_all_descriptions( + hass: HomeAssistant, +) -> dict[str, dict[str, Any] | None]: + """Return descriptions (i.e. user documentation) for all conditions.""" + descriptions_cache = hass.data[CONDITION_DESCRIPTION_CACHE] + + conditions = hass.data[CONDITIONS] + # See if there are new conditions not seen before. + # Any condition that we saw before already has an entry in description_cache. + all_conditions = set(conditions) + previous_all_conditions = set(descriptions_cache) + # If the conditions are the same, we can return the cache + if previous_all_conditions == all_conditions: + return descriptions_cache + + # Files we loaded for missing descriptions + new_conditions_descriptions: dict[str, JSON_TYPE] = {} + # We try to avoid making a copy in the event the cache is good, + # but now we must make a copy in case new conditions get added + # while we are loading the missing ones so we do not + # add the new ones to the cache without their descriptions + conditions = conditions.copy() + + if missing_conditions := all_conditions.difference(descriptions_cache): + domains_with_missing_conditions = { + conditions[missing_condition] for missing_condition in missing_conditions + } + ints_or_excs = await async_get_integrations( + hass, domains_with_missing_conditions + ) + integrations: list[Integration] = [] + for domain, int_or_exc in ints_or_excs.items(): + if type(int_or_exc) is Integration and int_or_exc.has_conditions: + integrations.append(int_or_exc) + continue + if TYPE_CHECKING: + assert isinstance(int_or_exc, Exception) + _LOGGER.debug( + "Failed to load conditions.yaml for integration: %s", + domain, + exc_info=int_or_exc, + ) + + if integrations: + new_conditions_descriptions = await hass.async_add_executor_job( + _load_conditions_files, hass, integrations + ) + + # Make a copy of the old cache and add missing descriptions to it + new_descriptions_cache = descriptions_cache.copy() + for missing_condition in missing_conditions: + domain = conditions[missing_condition] + + if ( + yaml_description := new_conditions_descriptions.get(domain, {}).get( # type: ignore[union-attr] + missing_condition + ) + ) is None: + _LOGGER.debug( + "No condition descriptions found for condition %s, skipping", + missing_condition, + ) + new_descriptions_cache[missing_condition] = None + continue + + description = {"fields": yaml_description.get("fields", {})} + + new_descriptions_cache[missing_condition] = description + + hass.data[CONDITION_DESCRIPTION_CACHE] = new_descriptions_cache + return new_descriptions_cache diff --git a/homeassistant/loader.py b/homeassistant/loader.py index a66a09d7407..1e338be0a0f 100644 --- a/homeassistant/loader.py +++ b/homeassistant/loader.py @@ -67,6 +67,7 @@ _LOGGER = logging.getLogger(__name__) # BASE_PRELOAD_PLATFORMS = [ "backup", + "condition", "config", "config_flow", "diagnostics", @@ -857,6 +858,11 @@ class Integration: # True. return self.manifest.get("import_executor", True) + @cached_property + def has_conditions(self) -> bool: + """Return if the integration has conditions.""" + return "conditions.yaml" in self._top_level_files + @cached_property def has_services(self) -> bool: """Return if the integration has services.""" diff --git a/script/hassfest/__main__.py b/script/hassfest/__main__.py index 05c0d455af6..dfa99c6bc75 100644 --- a/script/hassfest/__main__.py +++ b/script/hassfest/__main__.py @@ -12,6 +12,7 @@ from . import ( application_credentials, bluetooth, codeowners, + conditions, config_flow, config_schema, dependencies, @@ -38,6 +39,7 @@ INTEGRATION_PLUGINS = [ application_credentials, bluetooth, codeowners, + conditions, config_schema, dependencies, dhcp, diff --git a/script/hassfest/conditions.py b/script/hassfest/conditions.py new file mode 100644 index 00000000000..7eb9a2c3fc0 --- /dev/null +++ b/script/hassfest/conditions.py @@ -0,0 +1,225 @@ +"""Validate conditions.""" + +from __future__ import annotations + +import contextlib +import json +import pathlib +import re +from typing import Any + +import voluptuous as vol +from voluptuous.humanize import humanize_error + +from homeassistant.const import CONF_SELECTOR +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import condition, config_validation as cv, selector +from homeassistant.util.yaml import load_yaml_dict + +from .model import Config, Integration + + +def exists(value: Any) -> Any: + """Check if value exists.""" + if value is None: + raise vol.Invalid("Value cannot be None") + return value + + +FIELD_SCHEMA = vol.Schema( + { + vol.Optional("example"): exists, + vol.Optional("default"): exists, + vol.Optional("required"): bool, + vol.Optional(CONF_SELECTOR): selector.validate_selector, + } +) + +CONDITION_SCHEMA = vol.Any( + vol.Schema( + { + vol.Optional("fields"): vol.Schema({str: FIELD_SCHEMA}), + } + ), + None, +) + +CONDITIONS_SCHEMA = vol.Schema( + { + vol.Remove(vol.All(str, condition.starts_with_dot)): object, + cv.slug: CONDITION_SCHEMA, + } +) + +NON_MIGRATED_INTEGRATIONS = { + "device_automation", + "sun", +} + + +def grep_dir(path: pathlib.Path, glob_pattern: str, search_pattern: str) -> bool: + """Recursively go through a dir and it's children and find the regex.""" + pattern = re.compile(search_pattern) + + for fil in path.glob(glob_pattern): + if not fil.is_file(): + continue + + if pattern.search(fil.read_text()): + return True + + return False + + +def validate_conditions(config: Config, integration: Integration) -> None: # noqa: C901 + """Validate conditions.""" + try: + data = load_yaml_dict(str(integration.path / "conditions.yaml")) + except FileNotFoundError: + # Find if integration uses conditions + has_conditions = grep_dir( + integration.path, + "**/condition.py", + r"async_get_conditions", + ) + + if has_conditions and integration.domain not in NON_MIGRATED_INTEGRATIONS: + integration.add_error( + "conditions", "Registers conditions but has no conditions.yaml" + ) + return + except HomeAssistantError: + integration.add_error("conditions", "Invalid conditions.yaml") + return + + try: + conditions = CONDITIONS_SCHEMA(data) + except vol.Invalid as err: + integration.add_error( + "conditions", f"Invalid conditions.yaml: {humanize_error(data, err)}" + ) + return + + icons_file = integration.path / "icons.json" + icons = {} + if icons_file.is_file(): + with contextlib.suppress(ValueError): + icons = json.loads(icons_file.read_text()) + condition_icons = icons.get("conditions", {}) + + # Try loading translation strings + if integration.core: + strings_file = integration.path / "strings.json" + else: + # For custom integrations, use the en.json file + strings_file = integration.path / "translations/en.json" + + strings = {} + if strings_file.is_file(): + with contextlib.suppress(ValueError): + strings = json.loads(strings_file.read_text()) + + error_msg_suffix = "in the translations file" + if not integration.core: + error_msg_suffix = f"and is not {error_msg_suffix}" + + # For each condition in the integration: + # 1. Check if the condition description is set, if not, + # check if it's in the strings file else add an error. + # 2. Check if the condition has an icon set in icons.json. + # raise an error if not., + for condition_name, condition_schema in conditions.items(): + if integration.core and condition_name not in condition_icons: + # This is enforced for Core integrations only + integration.add_error( + "conditions", + f"Condition {condition_name} has no icon in icons.json.", + ) + if condition_schema is None: + continue + if "name" not in condition_schema and integration.core: + try: + strings["conditions"][condition_name]["name"] + except KeyError: + integration.add_error( + "conditions", + f"Condition {condition_name} has no name {error_msg_suffix}", + ) + + if "description" not in condition_schema and integration.core: + try: + strings["conditions"][condition_name]["description"] + except KeyError: + integration.add_error( + "conditions", + f"Condition {condition_name} has no description {error_msg_suffix}", + ) + + # The same check is done for the description in each of the fields of the + # condition schema. + for field_name, field_schema in condition_schema.get("fields", {}).items(): + if "fields" in field_schema: + # This is a section + continue + if "name" not in field_schema and integration.core: + try: + strings["conditions"][condition_name]["fields"][field_name]["name"] + except KeyError: + integration.add_error( + "conditions", + ( + f"Condition {condition_name} has a field {field_name} with no " + f"name {error_msg_suffix}" + ), + ) + + if "description" not in field_schema and integration.core: + try: + strings["conditions"][condition_name]["fields"][field_name][ + "description" + ] + except KeyError: + integration.add_error( + "conditions", + ( + f"Condition {condition_name} has a field {field_name} with no " + f"description {error_msg_suffix}" + ), + ) + + if "selector" in field_schema: + with contextlib.suppress(KeyError): + translation_key = field_schema["selector"]["select"][ + "translation_key" + ] + try: + strings["selector"][translation_key] + except KeyError: + integration.add_error( + "conditions", + f"Condition {condition_name} has a field {field_name} with a selector with a translation key {translation_key} that is not in the translations file", + ) + + # The same check is done for the description in each of the sections of the + # condition schema. + for section_name, section_schema in condition_schema.get("fields", {}).items(): + if "fields" not in section_schema: + # This is not a section + continue + if "name" not in section_schema and integration.core: + try: + strings["conditions"][condition_name]["sections"][section_name][ + "name" + ] + except KeyError: + integration.add_error( + "conditions", + f"Condition {condition_name} has a section {section_name} with no name {error_msg_suffix}", + ) + + +def validate(integrations: dict[str, Integration], config: Config) -> None: + """Handle dependencies for integrations.""" + # check conditions.yaml is valid + for integration in integrations.values(): + validate_conditions(config, integration) diff --git a/script/hassfest/icons.py b/script/hassfest/icons.py index 6abe338e45b..79ad7eec5ff 100644 --- a/script/hassfest/icons.py +++ b/script/hassfest/icons.py @@ -120,6 +120,16 @@ CUSTOM_INTEGRATION_SERVICE_ICONS_SCHEMA = cv.schema_with_slug_keys( ) +CONDITION_ICONS_SCHEMA = cv.schema_with_slug_keys( + vol.Schema( + { + vol.Optional("condition"): icon_value_validator, + } + ), + slug_validator=translation_key_validator, +) + + TRIGGER_ICONS_SCHEMA = cv.schema_with_slug_keys( vol.Schema( { @@ -166,6 +176,7 @@ def icon_schema( schema = vol.Schema( { + vol.Optional("conditions"): CONDITION_ICONS_SCHEMA, vol.Optional("config"): DATA_ENTRY_ICONS_SCHEMA, vol.Optional("issues"): vol.Schema( {str: {"fix_flow": DATA_ENTRY_ICONS_SCHEMA}} diff --git a/script/hassfest/translations.py b/script/hassfest/translations.py index 93fd212b981..4e0cf349aec 100644 --- a/script/hassfest/translations.py +++ b/script/hassfest/translations.py @@ -416,6 +416,22 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema: }, slug_validator=translation_key_validator, ), + vol.Optional("conditions"): cv.schema_with_slug_keys( + { + vol.Required("name"): translation_value_validator, + vol.Required("description"): translation_value_validator, + vol.Required("description_configured"): translation_value_validator, + vol.Optional("fields"): cv.schema_with_slug_keys( + { + vol.Required("name"): str, + vol.Required("description"): translation_value_validator, + vol.Optional("example"): translation_value_validator, + }, + slug_validator=translation_key_validator, + ), + }, + slug_validator=translation_key_validator, + ), vol.Optional("triggers"): cv.schema_with_slug_keys( { vol.Required("name"): translation_value_validator, diff --git a/tests/common.py b/tests/common.py index ff64dcb33a7..7652a020117 100644 --- a/tests/common.py +++ b/tests/common.py @@ -75,6 +75,7 @@ from homeassistant.core import ( from homeassistant.helpers import ( area_registry as ar, category_registry as cr, + condition, device_registry as dr, entity, entity_platform, @@ -296,6 +297,7 @@ async def async_test_home_assistant( # Load the registries entity.async_setup(hass) loader.async_setup(hass) + await condition.async_setup(hass) await trigger.async_setup(hass) # setup translation cache instead of calling translation.async_setup(hass) diff --git a/tests/components/websocket_api/test_commands.py b/tests/components/websocket_api/test_commands.py index bfb8c917f71..b513a04a40b 100644 --- a/tests/components/websocket_api/test_commands.py +++ b/tests/components/websocket_api/test_commands.py @@ -19,6 +19,7 @@ from homeassistant.components.websocket_api.auth import ( TYPE_AUTH_REQUIRED, ) from homeassistant.components.websocket_api.commands import ( + ALL_CONDITION_DESCRIPTIONS_JSON_CACHE, ALL_SERVICE_DESCRIPTIONS_JSON_CACHE, ALL_TRIGGER_DESCRIPTIONS_JSON_CACHE, ) @@ -710,6 +711,91 @@ async def test_get_services( assert hass.data[ALL_SERVICE_DESCRIPTIONS_JSON_CACHE] is old_cache +@patch("annotatedyaml.loader.load_yaml") +@patch.object(Integration, "has_conditions", return_value=True) +async def test_subscribe_conditions( + mock_has_conditions: Mock, + mock_load_yaml: Mock, + hass: HomeAssistant, + websocket_client: MockHAClientWebSocket, +) -> None: + """Test condition_platforms/subscribe command.""" + sun_condition_descriptions = """ + sun: {} + """ + device_automation_condition_descriptions = """ + device: {} + """ + + def _load_yaml(fname, secrets=None): + if fname.endswith("device_automation/conditions.yaml"): + condition_descriptions = device_automation_condition_descriptions + elif fname.endswith("sun/conditions.yaml"): + condition_descriptions = sun_condition_descriptions + else: + raise FileNotFoundError + with io.StringIO(condition_descriptions) as file: + return parse_yaml(file) + + mock_load_yaml.side_effect = _load_yaml + + assert await async_setup_component(hass, "sun", {}) + assert await async_setup_component(hass, "system_health", {}) + await hass.async_block_till_done() + + assert ALL_CONDITION_DESCRIPTIONS_JSON_CACHE not in hass.data + + await websocket_client.send_json_auto_id({"type": "condition_platforms/subscribe"}) + + # Test start subscription with initial event + msg = await websocket_client.receive_json() + assert msg == {"id": 1, "result": None, "success": True, "type": "result"} + msg = await websocket_client.receive_json() + assert msg == {"event": {"sun": {"fields": {}}}, "id": 1, "type": "event"} + + old_cache = hass.data[ALL_CONDITION_DESCRIPTIONS_JSON_CACHE] + + # Test we receive an event when a new platform is loaded, if it has descriptions + assert await async_setup_component(hass, "calendar", {}) + assert await async_setup_component(hass, "device_automation", {}) + await hass.async_block_till_done() + msg = await websocket_client.receive_json() + assert msg == { + "event": {"device": {"fields": {}}}, + "id": 1, + "type": "event", + } + + # Initiate a second subscription to check the cache is updated because of the new + # condition + await websocket_client.send_json_auto_id({"type": "condition_platforms/subscribe"}) + msg = await websocket_client.receive_json() + assert msg == {"id": 2, "result": None, "success": True, "type": "result"} + msg = await websocket_client.receive_json() + assert msg == { + "event": {"device": {"fields": {}}, "sun": {"fields": {}}}, + "id": 2, + "type": "event", + } + + assert hass.data[ALL_CONDITION_DESCRIPTIONS_JSON_CACHE] is not old_cache + + # Initiate a third subscription to check the cache is not updated because no new + # condition was added + old_cache = hass.data[ALL_CONDITION_DESCRIPTIONS_JSON_CACHE] + await websocket_client.send_json_auto_id({"type": "condition_platforms/subscribe"}) + msg = await websocket_client.receive_json() + assert msg == {"id": 3, "result": None, "success": True, "type": "result"} + msg = await websocket_client.receive_json() + assert msg == { + "event": {"device": {"fields": {}}, "sun": {"fields": {}}}, + "id": 3, + "type": "event", + } + + assert hass.data[ALL_CONDITION_DESCRIPTIONS_JSON_CACHE] is old_cache + + @patch("annotatedyaml.loader.load_yaml") @patch.object(Integration, "has_triggers", return_value=True) async def test_subscribe_triggers( diff --git a/tests/helpers/test_condition.py b/tests/helpers/test_condition.py index 246afcb3022..1c10048fee9 100644 --- a/tests/helpers/test_condition.py +++ b/tests/helpers/test_condition.py @@ -1,14 +1,21 @@ """Test the condition helper.""" from datetime import timedelta +import io from typing import Any from unittest.mock import AsyncMock, Mock, patch from freezegun import freeze_time import pytest +from pytest_unordered import unordered import voluptuous as vol +from homeassistant.components.device_automation import ( + DOMAIN as DOMAIN_DEVICE_AUTOMATION, +) from homeassistant.components.sensor import SensorDeviceClass +from homeassistant.components.sun import DOMAIN as DOMAIN_SUN +from homeassistant.components.system_health import DOMAIN as DOMAIN_SYSTEM_HEALTH from homeassistant.const import ( ATTR_DEVICE_CLASS, CONF_CONDITION, @@ -27,10 +34,12 @@ from homeassistant.helpers import ( ) from homeassistant.helpers.template import Template from homeassistant.helpers.typing import ConfigType +from homeassistant.loader import Integration, async_get_integration from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util +from homeassistant.util.yaml.loader import parse_yaml -from tests.common import MockModule, mock_integration, mock_platform +from tests.common import MockModule, MockPlatform, mock_integration, mock_platform def assert_element(trace_element, expected_element, path): @@ -2517,3 +2526,280 @@ async def test_or_condition_with_disabled_condition(hass: HomeAssistant) -> None "conditions/1/entity_id/0": [{"result": {"result": True, "state": 100.0}}], } ) + + +@pytest.mark.parametrize( + "sun_condition_descriptions", + [ + """ + sun: + fields: + after: + example: sunrise + selector: + select: + options: + - sunrise + - sunset + after_offset: + selector: + time: null + before: + example: sunrise + selector: + select: + options: + - sunrise + - sunset + before_offset: + selector: + time: null + """, + """ + .sunrise_sunset_selector: &sunrise_sunset_selector + example: sunrise + selector: + select: + options: + - sunrise + - sunset + .offset_selector: &offset_selector + selector: + time: null + sun: + fields: + after: *sunrise_sunset_selector + after_offset: *offset_selector + before: *sunrise_sunset_selector + before_offset: *offset_selector + """, + ], +) +async def test_async_get_all_descriptions( + hass: HomeAssistant, sun_condition_descriptions: str +) -> None: + """Test async_get_all_descriptions.""" + device_automation_condition_descriptions = """ + device: {} + """ + + assert await async_setup_component(hass, DOMAIN_SUN, {}) + assert await async_setup_component(hass, DOMAIN_SYSTEM_HEALTH, {}) + await hass.async_block_till_done() + + def _load_yaml(fname, secrets=None): + if fname.endswith("device_automation/conditions.yaml"): + condition_descriptions = device_automation_condition_descriptions + elif fname.endswith("sun/conditions.yaml"): + condition_descriptions = sun_condition_descriptions + with io.StringIO(condition_descriptions) as file: + return parse_yaml(file) + + with ( + patch( + "homeassistant.helpers.condition._load_conditions_files", + side_effect=condition._load_conditions_files, + ) as proxy_load_conditions_files, + patch( + "annotatedyaml.loader.load_yaml", + side_effect=_load_yaml, + ), + patch.object(Integration, "has_conditions", return_value=True), + ): + descriptions = await condition.async_get_all_descriptions(hass) + + # Test we only load conditions.yaml for integrations with conditions, + # system_health has no conditions + assert proxy_load_conditions_files.mock_calls[0][1][1] == unordered( + [ + await async_get_integration(hass, DOMAIN_SUN), + ] + ) + + # system_health does not have conditions and should not be in descriptions + assert descriptions == { + DOMAIN_SUN: { + "fields": { + "after": { + "example": "sunrise", + "selector": {"select": {"options": ["sunrise", "sunset"]}}, + }, + "after_offset": {"selector": {"time": None}}, + "before": { + "example": "sunrise", + "selector": {"select": {"options": ["sunrise", "sunset"]}}, + }, + "before_offset": {"selector": {"time": None}}, + } + } + } + + # Verify the cache returns the same object + assert await condition.async_get_all_descriptions(hass) is descriptions + + # Load the device_automation integration and check a new cache object is created + assert await async_setup_component(hass, DOMAIN_DEVICE_AUTOMATION, {}) + await hass.async_block_till_done() + + with ( + patch( + "annotatedyaml.loader.load_yaml", + side_effect=_load_yaml, + ), + patch.object(Integration, "has_conditions", return_value=True), + ): + new_descriptions = await condition.async_get_all_descriptions(hass) + assert new_descriptions is not descriptions + assert new_descriptions == { + "device": { + "fields": {}, + }, + DOMAIN_SUN: { + "fields": { + "after": { + "example": "sunrise", + "selector": {"select": {"options": ["sunrise", "sunset"]}}, + }, + "after_offset": {"selector": {"time": None}}, + "before": { + "example": "sunrise", + "selector": {"select": {"options": ["sunrise", "sunset"]}}, + }, + "before_offset": {"selector": {"time": None}}, + } + }, + } + + # Verify the cache returns the same object + assert await condition.async_get_all_descriptions(hass) is new_descriptions + + +@pytest.mark.parametrize( + ("yaml_error", "expected_message"), + [ + ( + FileNotFoundError("Blah"), + "Unable to find conditions.yaml for the sun integration", + ), + ( + HomeAssistantError("Test error"), + "Unable to parse conditions.yaml for the sun integration: Test error", + ), + ], +) +async def test_async_get_all_descriptions_with_yaml_error( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, + yaml_error: Exception, + expected_message: str, +) -> None: + """Test async_get_all_descriptions.""" + assert await async_setup_component(hass, DOMAIN_SUN, {}) + await hass.async_block_till_done() + + def _load_yaml_dict(fname, secrets=None): + raise yaml_error + + with ( + patch( + "homeassistant.helpers.condition.load_yaml_dict", + side_effect=_load_yaml_dict, + ), + patch.object(Integration, "has_conditions", return_value=True), + ): + descriptions = await condition.async_get_all_descriptions(hass) + + assert descriptions == {DOMAIN_SUN: None} + + assert expected_message in caplog.text + + +async def test_async_get_all_descriptions_with_bad_description( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test async_get_all_descriptions.""" + sun_service_descriptions = """ + sun: + fields: not_a_dict + """ + + assert await async_setup_component(hass, DOMAIN_SUN, {}) + await hass.async_block_till_done() + + def _load_yaml(fname, secrets=None): + with io.StringIO(sun_service_descriptions) as file: + return parse_yaml(file) + + with ( + patch( + "annotatedyaml.loader.load_yaml", + side_effect=_load_yaml, + ), + patch.object(Integration, "has_conditions", return_value=True), + ): + descriptions = await condition.async_get_all_descriptions(hass) + + assert descriptions == {DOMAIN_SUN: None} + + assert ( + "Unable to parse conditions.yaml for the sun integration: " + "expected a dictionary for dictionary value @ data['sun']['fields']" + ) in caplog.text + + +async def test_invalid_condition_platform( + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test invalid condition platform.""" + mock_integration(hass, MockModule("test", async_setup=AsyncMock(return_value=True))) + mock_platform(hass, "test.condition", MockPlatform()) + + await async_setup_component(hass, "test", {}) + + assert ( + "Integration test does not provide condition support, skipping" in caplog.text + ) + + +@patch("annotatedyaml.loader.load_yaml") +@patch.object(Integration, "has_conditions", return_value=True) +async def test_subscribe_conditions( + mock_has_conditions: Mock, + mock_load_yaml: Mock, + hass: HomeAssistant, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test condition.async_subscribe_platform_events.""" + sun_condition_descriptions = """ + sun: {} + """ + + def _load_yaml(fname, secrets=None): + if fname.endswith("sun/conditions.yaml"): + condition_descriptions = sun_condition_descriptions + else: + raise FileNotFoundError + with io.StringIO(condition_descriptions) as file: + return parse_yaml(file) + + mock_load_yaml.side_effect = _load_yaml + + async def broken_subscriber(_): + """Simulate a broken subscriber.""" + raise Exception("Boom!") # noqa: TRY002 + + condition_events = [] + + async def good_subscriber(new_conditions: set[str]): + """Simulate a working subscriber.""" + condition_events.append(new_conditions) + + condition.async_subscribe_platform_events(hass, broken_subscriber) + condition.async_subscribe_platform_events(hass, good_subscriber) + + assert await async_setup_component(hass, "sun", {}) + + assert condition_events == [{"sun"}] + assert "Error while notifying condition platform listener" in caplog.text From 40fcc3b75bc74875d5e4d303ead44800ccee3f62 Mon Sep 17 00:00:00 2001 From: Harry Heymann Date: Fri, 4 Jul 2025 10:13:40 -0400 Subject: [PATCH 010/106] Rename Matter device conversion methods (#148090) --- .../components/matter/binary_sensor.py | 40 ++++++++-------- homeassistant/components/matter/entity.py | 4 +- homeassistant/components/matter/number.py | 42 ++++++++--------- homeassistant/components/matter/select.py | 34 +++++++------- homeassistant/components/matter/sensor.py | 46 +++++++++---------- homeassistant/components/matter/switch.py | 12 ++--- 6 files changed, 89 insertions(+), 89 deletions(-) diff --git a/homeassistant/components/matter/binary_sensor.py b/homeassistant/components/matter/binary_sensor.py index 95efe46309c..09321bd33b2 100644 --- a/homeassistant/components/matter/binary_sensor.py +++ b/homeassistant/components/matter/binary_sensor.py @@ -54,7 +54,7 @@ class MatterBinarySensor(MatterEntity, BinarySensorEntity): value = self.get_matter_attribute_value(self._entity_info.primary_attribute) if value in (None, NullValue): value = None - elif value_convert := self.entity_description.measurement_to_ha: + elif value_convert := self.entity_description.device_to_ha: value = value_convert(value) if TYPE_CHECKING: value = cast(bool | None, value) @@ -70,7 +70,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="HueMotionSensor", device_class=BinarySensorDeviceClass.MOTION, - measurement_to_ha=lambda x: (x & 1 == 1) if x is not None else None, + device_to_ha=lambda x: (x & 1 == 1) if x is not None else None, ), entity_class=MatterBinarySensor, required_attributes=(clusters.OccupancySensing.Attributes.Occupancy,), @@ -83,7 +83,7 @@ DISCOVERY_SCHEMAS = [ key="OccupancySensor", device_class=BinarySensorDeviceClass.OCCUPANCY, # The first bit = if occupied - measurement_to_ha=lambda x: (x & 1 == 1) if x is not None else None, + device_to_ha=lambda x: (x & 1 == 1) if x is not None else None, ), entity_class=MatterBinarySensor, required_attributes=(clusters.OccupancySensing.Attributes.Occupancy,), @@ -94,7 +94,7 @@ DISCOVERY_SCHEMAS = [ key="BatteryChargeLevel", device_class=BinarySensorDeviceClass.BATTERY, entity_category=EntityCategory.DIAGNOSTIC, - measurement_to_ha=lambda x: x + device_to_ha=lambda x: x != clusters.PowerSource.Enums.BatChargeLevelEnum.kOk, ), entity_class=MatterBinarySensor, @@ -109,7 +109,7 @@ DISCOVERY_SCHEMAS = [ key="ContactSensor", device_class=BinarySensorDeviceClass.DOOR, # value is inverted on matter to what we expect - measurement_to_ha=lambda x: not x, + device_to_ha=lambda x: not x, ), entity_class=MatterBinarySensor, required_attributes=(clusters.BooleanState.Attributes.StateValue,), @@ -153,7 +153,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="LockDoorStateSensor", device_class=BinarySensorDeviceClass.DOOR, - measurement_to_ha={ + device_to_ha={ clusters.DoorLock.Enums.DoorStateEnum.kDoorOpen: True, clusters.DoorLock.Enums.DoorStateEnum.kDoorJammed: True, clusters.DoorLock.Enums.DoorStateEnum.kDoorForcedOpen: True, @@ -168,7 +168,7 @@ DISCOVERY_SCHEMAS = [ platform=Platform.BINARY_SENSOR, entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmDeviceMutedSensor", - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.SmokeCoAlarm.Enums.MuteStateEnum.kMuted ), translation_key="muted", @@ -181,7 +181,7 @@ DISCOVERY_SCHEMAS = [ platform=Platform.BINARY_SENSOR, entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmEndfOfServiceSensor", - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.SmokeCoAlarm.Enums.EndOfServiceEnum.kExpired ), translation_key="end_of_service", @@ -195,7 +195,7 @@ DISCOVERY_SCHEMAS = [ platform=Platform.BINARY_SENSOR, entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmBatteryAlertSensor", - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal ), translation_key="battery_alert", @@ -232,7 +232,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmSmokeStateSensor", device_class=BinarySensorDeviceClass.SMOKE, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal ), ), @@ -244,7 +244,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmInterconnectSmokeAlarmSensor", device_class=BinarySensorDeviceClass.SMOKE, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal ), translation_key="interconnected_smoke_alarm", @@ -257,7 +257,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmInterconnectCOAlarmSensor", device_class=BinarySensorDeviceClass.CO, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal ), translation_key="interconnected_co_alarm", @@ -271,7 +271,7 @@ DISCOVERY_SCHEMAS = [ key="EnergyEvseChargingStatusSensor", translation_key="evse_charging_status", device_class=BinarySensorDeviceClass.BATTERY_CHARGING, - measurement_to_ha={ + device_to_ha={ clusters.EnergyEvse.Enums.StateEnum.kNotPluggedIn: False, clusters.EnergyEvse.Enums.StateEnum.kPluggedInNoDemand: False, clusters.EnergyEvse.Enums.StateEnum.kPluggedInDemand: False, @@ -291,7 +291,7 @@ DISCOVERY_SCHEMAS = [ key="EnergyEvsePlugStateSensor", translation_key="evse_plug_state", device_class=BinarySensorDeviceClass.PLUG, - measurement_to_ha={ + device_to_ha={ clusters.EnergyEvse.Enums.StateEnum.kNotPluggedIn: False, clusters.EnergyEvse.Enums.StateEnum.kPluggedInNoDemand: True, clusters.EnergyEvse.Enums.StateEnum.kPluggedInDemand: True, @@ -311,7 +311,7 @@ DISCOVERY_SCHEMAS = [ key="EnergyEvseSupplyStateSensor", translation_key="evse_supply_charging_state", device_class=BinarySensorDeviceClass.RUNNING, - measurement_to_ha={ + device_to_ha={ clusters.EnergyEvse.Enums.SupplyStateEnum.kDisabled: False, clusters.EnergyEvse.Enums.SupplyStateEnum.kChargingEnabled: True, clusters.EnergyEvse.Enums.SupplyStateEnum.kDischargingEnabled: False, @@ -327,7 +327,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="WaterHeaterManagementBoostStateSensor", translation_key="boost_state", - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.WaterHeaterManagement.Enums.BoostStateEnum.kActive ), ), @@ -342,7 +342,7 @@ DISCOVERY_SCHEMAS = [ device_class=BinarySensorDeviceClass.PROBLEM, entity_category=EntityCategory.DIAGNOSTIC, # DeviceFault or SupplyFault bit enabled - measurement_to_ha={ + device_to_ha={ clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kDeviceFault: True, clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSupplyFault: True, clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSpeedLow: False, @@ -366,7 +366,7 @@ DISCOVERY_SCHEMAS = [ key="PumpStatusRunning", translation_key="pump_running", device_class=BinarySensorDeviceClass.RUNNING, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kRunning ), @@ -384,7 +384,7 @@ DISCOVERY_SCHEMAS = [ translation_key="dishwasher_alarm_inflow", device_class=BinarySensorDeviceClass.PROBLEM, entity_category=EntityCategory.DIAGNOSTIC, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kInflowError ), ), @@ -399,7 +399,7 @@ DISCOVERY_SCHEMAS = [ translation_key="dishwasher_alarm_door", device_class=BinarySensorDeviceClass.PROBLEM, entity_category=EntityCategory.DIAGNOSTIC, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kDoorError ), ), diff --git a/homeassistant/components/matter/entity.py b/homeassistant/components/matter/entity.py index fded57d34f5..028feab9c88 100644 --- a/homeassistant/components/matter/entity.py +++ b/homeassistant/components/matter/entity.py @@ -59,8 +59,8 @@ class MatterEntityDescription(EntityDescription): """Describe the Matter entity.""" # convert the value from the primary attribute to the value used by HA - measurement_to_ha: Callable[[Any], Any] | None = None - ha_to_native_value: Callable[[Any], Any] | None = None + device_to_ha: Callable[[Any], Any] | None = None + ha_to_device: Callable[[Any], Any] | None = None command_timeout: int | None = None diff --git a/homeassistant/components/matter/number.py b/homeassistant/components/matter/number.py index 7d138ba5018..c948f39834a 100644 --- a/homeassistant/components/matter/number.py +++ b/homeassistant/components/matter/number.py @@ -55,7 +55,7 @@ class MatterRangeNumberEntityDescription( ): """Describe Matter Number Input entities with min and max values.""" - ha_to_native_value: Callable[[Any], Any] + ha_to_device: Callable[[Any], Any] # attribute descriptors to get the min and max value min_attribute: type[ClusterAttributeDescriptor] @@ -74,7 +74,7 @@ class MatterNumber(MatterEntity, NumberEntity): async def async_set_native_value(self, value: float) -> None: """Update the current value.""" sendvalue = int(value) - if value_convert := self.entity_description.ha_to_native_value: + if value_convert := self.entity_description.ha_to_device: sendvalue = value_convert(value) await self.write_attribute( value=sendvalue, @@ -84,7 +84,7 @@ class MatterNumber(MatterEntity, NumberEntity): def _update_from_device(self) -> None: """Update from device.""" value = self.get_matter_attribute_value(self._entity_info.primary_attribute) - if value_convert := self.entity_description.measurement_to_ha: + if value_convert := self.entity_description.device_to_ha: value = value_convert(value) self._attr_native_value = value @@ -96,7 +96,7 @@ class MatterRangeNumber(MatterEntity, NumberEntity): async def async_set_native_value(self, value: float) -> None: """Update the current value.""" - send_value = self.entity_description.ha_to_native_value(value) + send_value = self.entity_description.ha_to_device(value) # custom command defined to set the new value await self.send_device_command( self.entity_description.command(send_value), @@ -106,7 +106,7 @@ class MatterRangeNumber(MatterEntity, NumberEntity): def _update_from_device(self) -> None: """Update from device.""" value = self.get_matter_attribute_value(self._entity_info.primary_attribute) - if value_convert := self.entity_description.measurement_to_ha: + if value_convert := self.entity_description.device_to_ha: value = value_convert(value) self._attr_native_value = value self._attr_native_min_value = ( @@ -133,7 +133,7 @@ class MatterLevelControlNumber(MatterEntity, NumberEntity): async def async_set_native_value(self, value: float) -> None: """Set level value.""" send_value = int(value) - if value_convert := self.entity_description.ha_to_native_value: + if value_convert := self.entity_description.ha_to_device: send_value = value_convert(value) await self.send_device_command( clusters.LevelControl.Commands.MoveToLevel( @@ -145,7 +145,7 @@ class MatterLevelControlNumber(MatterEntity, NumberEntity): def _update_from_device(self) -> None: """Update from device.""" value = self.get_matter_attribute_value(self._entity_info.primary_attribute) - if value_convert := self.entity_description.measurement_to_ha: + if value_convert := self.entity_description.device_to_ha: value = value_convert(value) self._attr_native_value = value @@ -162,8 +162,8 @@ DISCOVERY_SCHEMAS = [ native_min_value=0, mode=NumberMode.BOX, # use 255 to indicate that the value should revert to the default - measurement_to_ha=lambda x: 255 if x is None else x, - ha_to_native_value=lambda x: None if x == 255 else int(x), + device_to_ha=lambda x: 255 if x is None else x, + ha_to_device=lambda x: None if x == 255 else int(x), native_step=1, native_unit_of_measurement=None, ), @@ -180,8 +180,8 @@ DISCOVERY_SCHEMAS = [ translation_key="on_transition_time", native_max_value=65534, native_min_value=0, - measurement_to_ha=lambda x: None if x is None else x / 10, - ha_to_native_value=lambda x: round(x * 10), + device_to_ha=lambda x: None if x is None else x / 10, + ha_to_device=lambda x: round(x * 10), native_step=0.1, native_unit_of_measurement=UnitOfTime.SECONDS, mode=NumberMode.BOX, @@ -199,8 +199,8 @@ DISCOVERY_SCHEMAS = [ translation_key="off_transition_time", native_max_value=65534, native_min_value=0, - measurement_to_ha=lambda x: None if x is None else x / 10, - ha_to_native_value=lambda x: round(x * 10), + device_to_ha=lambda x: None if x is None else x / 10, + ha_to_device=lambda x: round(x * 10), native_step=0.1, native_unit_of_measurement=UnitOfTime.SECONDS, mode=NumberMode.BOX, @@ -218,8 +218,8 @@ DISCOVERY_SCHEMAS = [ translation_key="on_off_transition_time", native_max_value=65534, native_min_value=0, - measurement_to_ha=lambda x: None if x is None else x / 10, - ha_to_native_value=lambda x: round(x * 10), + device_to_ha=lambda x: None if x is None else x / 10, + ha_to_device=lambda x: round(x * 10), native_step=0.1, native_unit_of_measurement=UnitOfTime.SECONDS, mode=NumberMode.BOX, @@ -256,8 +256,8 @@ DISCOVERY_SCHEMAS = [ native_min_value=-50, native_step=0.5, native_unit_of_measurement=UnitOfTemperature.CELSIUS, - measurement_to_ha=lambda x: None if x is None else x / 10, - ha_to_native_value=lambda x: round(x * 10), + device_to_ha=lambda x: None if x is None else x / 10, + ha_to_device=lambda x: round(x * 10), mode=NumberMode.BOX, ), entity_class=MatterNumber, @@ -275,10 +275,10 @@ DISCOVERY_SCHEMAS = [ native_max_value=100, native_min_value=0.5, native_step=0.5, - measurement_to_ha=( + device_to_ha=( lambda x: None if x is None else x / 2 # Matter range (1-200) ), - ha_to_native_value=lambda x: round(x * 2), # HA range 0.5–100.0% + ha_to_device=lambda x: round(x * 2), # HA range 0.5–100.0% mode=NumberMode.SLIDER, ), entity_class=MatterLevelControlNumber, @@ -326,8 +326,8 @@ DISCOVERY_SCHEMAS = [ targetTemperature=value ), native_unit_of_measurement=UnitOfTemperature.CELSIUS, - measurement_to_ha=lambda x: None if x is None else x / 100, - ha_to_native_value=lambda x: round(x * 100), + device_to_ha=lambda x: None if x is None else x / 100, + ha_to_device=lambda x: round(x * 100), min_attribute=clusters.TemperatureControl.Attributes.MinTemperature, max_attribute=clusters.TemperatureControl.Attributes.MaxTemperature, mode=NumberMode.SLIDER, diff --git a/homeassistant/components/matter/select.py b/homeassistant/components/matter/select.py index ac1bc2d1f8f..d700b39258c 100644 --- a/homeassistant/components/matter/select.py +++ b/homeassistant/components/matter/select.py @@ -71,8 +71,8 @@ class MatterSelectEntityDescription(SelectEntityDescription, MatterEntityDescrip class MatterMapSelectEntityDescription(MatterSelectEntityDescription): """Describe Matter select entities for MatterMapSelectEntityDescription.""" - measurement_to_ha: Callable[[int], str | None] - ha_to_native_value: Callable[[str], int | None] + device_to_ha: Callable[[int], str | None] + ha_to_device: Callable[[str], int | None] # list attribute: the attribute descriptor to get the list of values (= list of integers) list_attribute: type[ClusterAttributeDescriptor] @@ -97,7 +97,7 @@ class MatterAttributeSelectEntity(MatterEntity, SelectEntity): async def async_select_option(self, option: str) -> None: """Change the selected mode.""" - value_convert = self.entity_description.ha_to_native_value + value_convert = self.entity_description.ha_to_device if TYPE_CHECKING: assert value_convert is not None await self.write_attribute( @@ -109,7 +109,7 @@ class MatterAttributeSelectEntity(MatterEntity, SelectEntity): """Update from device.""" value: Nullable | int | None value = self.get_matter_attribute_value(self._entity_info.primary_attribute) - value_convert = self.entity_description.measurement_to_ha + value_convert = self.entity_description.device_to_ha if TYPE_CHECKING: assert value_convert is not None self._attr_current_option = value_convert(value) @@ -132,7 +132,7 @@ class MatterMapSelectEntity(MatterAttributeSelectEntity): self._attr_options = [ mapped_value for value in available_values - if (mapped_value := self.entity_description.measurement_to_ha(value)) + if (mapped_value := self.entity_description.device_to_ha(value)) ] # use base implementation from MatterAttributeSelectEntity to set the current option super()._update_from_device() @@ -333,13 +333,13 @@ DISCOVERY_SCHEMAS = [ entity_category=EntityCategory.CONFIG, translation_key="startup_on_off", options=["on", "off", "toggle", "previous"], - measurement_to_ha={ + device_to_ha={ 0: "off", 1: "on", 2: "toggle", None: "previous", }.get, - ha_to_native_value={ + ha_to_device={ "off": 0, "on": 1, "toggle": 2, @@ -358,12 +358,12 @@ DISCOVERY_SCHEMAS = [ entity_category=EntityCategory.CONFIG, translation_key="sensitivity_level", options=["high", "standard", "low"], - measurement_to_ha={ + device_to_ha={ 0: "high", 1: "standard", 2: "low", }.get, - ha_to_native_value={ + ha_to_device={ "high": 0, "standard": 1, "low": 2, @@ -379,11 +379,11 @@ DISCOVERY_SCHEMAS = [ entity_category=EntityCategory.CONFIG, translation_key="temperature_display_mode", options=["Celsius", "Fahrenheit"], - measurement_to_ha={ + device_to_ha={ 0: "Celsius", 1: "Fahrenheit", }.get, - ha_to_native_value={ + ha_to_device={ "Celsius": 0, "Fahrenheit": 1, }.get, @@ -432,8 +432,8 @@ DISCOVERY_SCHEMAS = [ key="MatterLaundryWasherNumberOfRinses", translation_key="laundry_washer_number_of_rinses", list_attribute=clusters.LaundryWasherControls.Attributes.SupportedRinses, - measurement_to_ha=NUMBER_OF_RINSES_STATE_MAP.get, - ha_to_native_value=NUMBER_OF_RINSES_STATE_MAP_REVERSE.get, + device_to_ha=NUMBER_OF_RINSES_STATE_MAP.get, + ha_to_device=NUMBER_OF_RINSES_STATE_MAP_REVERSE.get, ), entity_class=MatterMapSelectEntity, required_attributes=( @@ -450,13 +450,13 @@ DISCOVERY_SCHEMAS = [ entity_category=EntityCategory.CONFIG, translation_key="door_lock_sound_volume", options=["silent", "low", "medium", "high"], - measurement_to_ha={ + device_to_ha={ 0: "silent", 1: "low", 3: "medium", 2: "high", }.get, - ha_to_native_value={ + ha_to_device={ "silent": 0, "low": 1, "medium": 3, @@ -472,8 +472,8 @@ DISCOVERY_SCHEMAS = [ key="PumpConfigurationAndControlOperationMode", translation_key="pump_operation_mode", options=list(PUMP_OPERATION_MODE_MAP.values()), - measurement_to_ha=PUMP_OPERATION_MODE_MAP.get, - ha_to_native_value=PUMP_OPERATION_MODE_MAP_REVERSE.get, + device_to_ha=PUMP_OPERATION_MODE_MAP.get, + ha_to_device=PUMP_OPERATION_MODE_MAP_REVERSE.get, ), entity_class=MatterAttributeSelectEntity, required_attributes=( diff --git a/homeassistant/components/matter/sensor.py b/homeassistant/components/matter/sensor.py index f744ec8885a..62c70f777e7 100644 --- a/homeassistant/components/matter/sensor.py +++ b/homeassistant/components/matter/sensor.py @@ -194,7 +194,7 @@ class MatterSensor(MatterEntity, SensorEntity): value = self.get_matter_attribute_value(self._entity_info.primary_attribute) if value in (None, NullValue): value = None - elif value_convert := self.entity_description.measurement_to_ha: + elif value_convert := self.entity_description.device_to_ha: value = value_convert(value) self._attr_native_value = value @@ -296,7 +296,7 @@ DISCOVERY_SCHEMAS = [ key="TemperatureSensor", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, - measurement_to_ha=lambda x: x / 100, + device_to_ha=lambda x: x / 100, state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -308,7 +308,7 @@ DISCOVERY_SCHEMAS = [ key="PressureSensor", native_unit_of_measurement=UnitOfPressure.KPA, device_class=SensorDeviceClass.PRESSURE, - measurement_to_ha=lambda x: x / 10, + device_to_ha=lambda x: x / 10, state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -320,7 +320,7 @@ DISCOVERY_SCHEMAS = [ key="FlowSensor", native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR, translation_key="flow", - measurement_to_ha=lambda x: x / 10, + device_to_ha=lambda x: x / 10, state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -332,7 +332,7 @@ DISCOVERY_SCHEMAS = [ key="HumiditySensor", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.HUMIDITY, - measurement_to_ha=lambda x: x / 100, + device_to_ha=lambda x: x / 100, state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -346,7 +346,7 @@ DISCOVERY_SCHEMAS = [ key="LightSensor", native_unit_of_measurement=LIGHT_LUX, device_class=SensorDeviceClass.ILLUMINANCE, - measurement_to_ha=lambda x: round(pow(10, ((x - 1) / 10000)), 1), + device_to_ha=lambda x: round(pow(10, ((x - 1) / 10000)), 1), state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -360,7 +360,7 @@ DISCOVERY_SCHEMAS = [ device_class=SensorDeviceClass.BATTERY, entity_category=EntityCategory.DIAGNOSTIC, # value has double precision - measurement_to_ha=lambda x: int(x / 2), + device_to_ha=lambda x: int(x / 2), state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -402,7 +402,7 @@ DISCOVERY_SCHEMAS = [ device_class=SensorDeviceClass.ENUM, entity_category=EntityCategory.DIAGNOSTIC, options=[state for state in CHARGE_STATE_MAP.values() if state is not None], - measurement_to_ha=CHARGE_STATE_MAP.get, + device_to_ha=CHARGE_STATE_MAP.get, ), entity_class=MatterSensor, required_attributes=(clusters.PowerSource.Attributes.BatChargeState,), @@ -589,7 +589,7 @@ DISCOVERY_SCHEMAS = [ state_class=None, # convert to set first to remove the duplicate unknown value options=[x for x in AIR_QUALITY_MAP.values() if x is not None], - measurement_to_ha=lambda x: AIR_QUALITY_MAP[x], + device_to_ha=lambda x: AIR_QUALITY_MAP[x], ), entity_class=MatterSensor, required_attributes=(clusters.AirQuality.Attributes.AirQuality,), @@ -668,7 +668,7 @@ DISCOVERY_SCHEMAS = [ native_unit_of_measurement=UnitOfPower.WATT, suggested_display_precision=2, state_class=SensorStateClass.MEASUREMENT, - measurement_to_ha=lambda x: x / 1000, + device_to_ha=lambda x: x / 1000, ), entity_class=MatterSensor, required_attributes=( @@ -685,7 +685,7 @@ DISCOVERY_SCHEMAS = [ native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, suggested_display_precision=3, state_class=SensorStateClass.TOTAL_INCREASING, - measurement_to_ha=lambda x: x / 1000, + device_to_ha=lambda x: x / 1000, ), entity_class=MatterSensor, required_attributes=( @@ -702,7 +702,7 @@ DISCOVERY_SCHEMAS = [ native_unit_of_measurement=UnitOfPower.WATT, suggested_display_precision=2, state_class=SensorStateClass.MEASUREMENT, - measurement_to_ha=lambda x: x / 10, + device_to_ha=lambda x: x / 10, ), entity_class=MatterSensor, required_attributes=(NeoCluster.Attributes.Watt,), @@ -731,7 +731,7 @@ DISCOVERY_SCHEMAS = [ native_unit_of_measurement=UnitOfElectricPotential.VOLT, suggested_display_precision=0, state_class=SensorStateClass.MEASUREMENT, - measurement_to_ha=lambda x: x / 10, + device_to_ha=lambda x: x / 10, ), entity_class=MatterSensor, required_attributes=(NeoCluster.Attributes.Voltage,), @@ -823,7 +823,7 @@ DISCOVERY_SCHEMAS = [ suggested_display_precision=3, state_class=SensorStateClass.TOTAL_INCREASING, # id 0 of the EnergyMeasurementStruct is the cumulative energy (in mWh) - measurement_to_ha=lambda x: x.energy, + device_to_ha=lambda x: x.energy, ), entity_class=MatterSensor, required_attributes=( @@ -842,7 +842,7 @@ DISCOVERY_SCHEMAS = [ suggested_display_precision=3, state_class=SensorStateClass.TOTAL_INCREASING, # id 0 of the EnergyMeasurementStruct is the cumulative energy (in mWh) - measurement_to_ha=lambda x: x.energy, + device_to_ha=lambda x: x.energy, ), entity_class=MatterSensor, required_attributes=( @@ -910,7 +910,7 @@ DISCOVERY_SCHEMAS = [ translation_key="contamination_state", device_class=SensorDeviceClass.ENUM, options=list(CONTAMINATION_STATE_MAP.values()), - measurement_to_ha=CONTAMINATION_STATE_MAP.get, + device_to_ha=CONTAMINATION_STATE_MAP.get, ), entity_class=MatterSensor, required_attributes=(clusters.SmokeCoAlarm.Attributes.ContaminationState,), @@ -922,7 +922,7 @@ DISCOVERY_SCHEMAS = [ translation_key="expiry_date", device_class=SensorDeviceClass.TIMESTAMP, # raw value is epoch seconds - measurement_to_ha=datetime.fromtimestamp, + device_to_ha=datetime.fromtimestamp, ), entity_class=MatterSensor, required_attributes=(clusters.SmokeCoAlarm.Attributes.ExpiryDate,), @@ -993,7 +993,7 @@ DISCOVERY_SCHEMAS = [ key="ThermostatLocalTemperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, - measurement_to_ha=lambda x: x / 100, + device_to_ha=lambda x: x / 100, state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -1044,7 +1044,7 @@ DISCOVERY_SCHEMAS = [ entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, translation_key="window_covering_target_position", - measurement_to_ha=lambda x: round((10000 - x) / 100), + device_to_ha=lambda x: round((10000 - x) / 100), native_unit_of_measurement=PERCENTAGE, ), entity_class=MatterSensor, @@ -1060,7 +1060,7 @@ DISCOVERY_SCHEMAS = [ device_class=SensorDeviceClass.ENUM, entity_category=EntityCategory.DIAGNOSTIC, options=list(EVSE_FAULT_STATE_MAP.values()), - measurement_to_ha=EVSE_FAULT_STATE_MAP.get, + device_to_ha=EVSE_FAULT_STATE_MAP.get, ), entity_class=MatterSensor, required_attributes=(clusters.EnergyEvse.Attributes.FaultState,), @@ -1173,7 +1173,7 @@ DISCOVERY_SCHEMAS = [ device_class=SensorDeviceClass.ENUM, entity_category=EntityCategory.DIAGNOSTIC, options=list(ESA_STATE_MAP.values()), - measurement_to_ha=ESA_STATE_MAP.get, + device_to_ha=ESA_STATE_MAP.get, ), entity_class=MatterSensor, required_attributes=(clusters.DeviceEnergyManagement.Attributes.ESAState,), @@ -1186,7 +1186,7 @@ DISCOVERY_SCHEMAS = [ device_class=SensorDeviceClass.ENUM, entity_category=EntityCategory.DIAGNOSTIC, options=list(DEM_OPT_OUT_STATE_MAP.values()), - measurement_to_ha=DEM_OPT_OUT_STATE_MAP.get, + device_to_ha=DEM_OPT_OUT_STATE_MAP.get, ), entity_class=MatterSensor, required_attributes=(clusters.DeviceEnergyManagement.Attributes.OptOutState,), @@ -1200,7 +1200,7 @@ DISCOVERY_SCHEMAS = [ options=[ mode for mode in PUMP_CONTROL_MODE_MAP.values() if mode is not None ], - measurement_to_ha=PUMP_CONTROL_MODE_MAP.get, + device_to_ha=PUMP_CONTROL_MODE_MAP.get, ), entity_class=MatterSensor, required_attributes=( diff --git a/homeassistant/components/matter/switch.py b/homeassistant/components/matter/switch.py index 870a9098492..df8581c5c4f 100644 --- a/homeassistant/components/matter/switch.py +++ b/homeassistant/components/matter/switch.py @@ -95,7 +95,7 @@ class MatterGenericCommandSwitch(MatterSwitch): def _update_from_device(self) -> None: """Update from device.""" value = self.get_matter_attribute_value(self._entity_info.primary_attribute) - if value_convert := self.entity_description.measurement_to_ha: + if value_convert := self.entity_description.device_to_ha: value = value_convert(value) self._attr_is_on = value @@ -141,7 +141,7 @@ class MatterNumericSwitch(MatterSwitch): async def _async_set_native_value(self, value: bool) -> None: """Update the current value.""" - if value_convert := self.entity_description.ha_to_native_value: + if value_convert := self.entity_description.ha_to_device: send_value = value_convert(value) await self.write_attribute( value=send_value, @@ -159,7 +159,7 @@ class MatterNumericSwitch(MatterSwitch): def _update_from_device(self) -> None: """Update from device.""" value = self.get_matter_attribute_value(self._entity_info.primary_attribute) - if value_convert := self.entity_description.measurement_to_ha: + if value_convert := self.entity_description.device_to_ha: value = value_convert(value) self._attr_is_on = value @@ -248,11 +248,11 @@ DISCOVERY_SCHEMAS = [ key="EveTrvChildLock", entity_category=EntityCategory.CONFIG, translation_key="child_lock", - measurement_to_ha={ + device_to_ha={ 0: False, 1: True, }.get, - ha_to_native_value={ + ha_to_device={ False: 0, True: 1, }.get, @@ -275,7 +275,7 @@ DISCOVERY_SCHEMAS = [ ), off_command=clusters.EnergyEvse.Commands.Disable, command_timeout=3000, - measurement_to_ha=EVSE_SUPPLY_STATE_MAP.get, + device_to_ha=EVSE_SUPPLY_STATE_MAP.get, ), entity_class=MatterGenericCommandSwitch, required_attributes=( From 40ec51c0a3fb108d6c5b5e258be2ff71a04a0cd4 Mon Sep 17 00:00:00 2001 From: tronikos Date: Fri, 4 Jul 2025 07:17:10 -0700 Subject: [PATCH 011/106] Add redirect URL in Google Assistant SDK setup (#148076) --- .../application_credentials.py | 18 ++++++---- .../google_assistant_sdk/strings.json | 2 +- .../test_application_credentials.py | 36 +++++++++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 tests/components/google_assistant_sdk/test_application_credentials.py diff --git a/homeassistant/components/google_assistant_sdk/application_credentials.py b/homeassistant/components/google_assistant_sdk/application_credentials.py index 8fa99157479..8f5b00edc7c 100644 --- a/homeassistant/components/google_assistant_sdk/application_credentials.py +++ b/homeassistant/components/google_assistant_sdk/application_credentials.py @@ -2,6 +2,10 @@ from homeassistant.components.application_credentials import AuthorizationServer from homeassistant.core import HomeAssistant +from homeassistant.helpers.config_entry_oauth2_flow import ( + AUTH_CALLBACK_PATH, + MY_AUTH_CALLBACK_PATH, +) async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationServer: @@ -14,12 +18,14 @@ async def async_get_authorization_server(hass: HomeAssistant) -> AuthorizationSe async def async_get_description_placeholders(hass: HomeAssistant) -> dict[str, str]: """Return description placeholders for the credentials dialog.""" + if "my" in hass.config.components: + redirect_url = MY_AUTH_CALLBACK_PATH + else: + ha_host = hass.config.external_url or "https://YOUR_DOMAIN:PORT" + redirect_url = f"{ha_host}{AUTH_CALLBACK_PATH}" return { - "oauth_consent_url": ( - "https://console.cloud.google.com/apis/credentials/consent" - ), - "more_info_url": ( - "https://www.home-assistant.io/integrations/google_assistant_sdk/" - ), + "oauth_consent_url": "https://console.cloud.google.com/apis/credentials/consent", + "more_info_url": "https://www.home-assistant.io/integrations/google_assistant_sdk/", "oauth_creds_url": "https://console.cloud.google.com/apis/credentials", + "redirect_url": redirect_url, } diff --git a/homeassistant/components/google_assistant_sdk/strings.json b/homeassistant/components/google_assistant_sdk/strings.json index 2622333e15f..2ebd04db4b6 100644 --- a/homeassistant/components/google_assistant_sdk/strings.json +++ b/homeassistant/components/google_assistant_sdk/strings.json @@ -46,7 +46,7 @@ } }, "application_credentials": { - "description": "Follow the [instructions]({more_info_url}) for [OAuth consent screen]({oauth_consent_url}) to give Home Assistant access to your Google Assistant SDK. You also need to create Application Credentials linked to your account:\n1. Go to [Credentials]({oauth_creds_url}) and select **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type." + "description": "Follow the [instructions]({more_info_url}) for [OAuth consent screen]({oauth_consent_url}) to give Home Assistant access to your Google Assistant SDK. You also need to create Application Credentials linked to your account:\n1. Go to [Credentials]({oauth_creds_url}) and select **Create Credentials**.\n1. From the drop-down list select **OAuth client ID**.\n1. Select **Web application** for the Application Type.\n1. Add `{redirect_url}` under *Authorized redirect URI*." }, "services": { "send_text_command": { diff --git a/tests/components/google_assistant_sdk/test_application_credentials.py b/tests/components/google_assistant_sdk/test_application_credentials.py new file mode 100644 index 00000000000..e7811677c53 --- /dev/null +++ b/tests/components/google_assistant_sdk/test_application_credentials.py @@ -0,0 +1,36 @@ +"""Test the Google Assistant SDK application_credentials.""" + +import pytest + +from homeassistant import setup +from homeassistant.components.google_assistant_sdk.application_credentials import ( + async_get_description_placeholders, +) +from homeassistant.core import HomeAssistant + + +@pytest.mark.parametrize( + ("additional_components", "external_url", "expected_redirect_uri"), + [ + ([], "https://example.com", "https://example.com/auth/external/callback"), + ([], None, "https://YOUR_DOMAIN:PORT/auth/external/callback"), + (["my"], "https://example.com", "https://my.home-assistant.io/redirect/oauth"), + ], +) +async def test_description_placeholders( + hass: HomeAssistant, + additional_components: list[str], + external_url: str | None, + expected_redirect_uri: str, +) -> None: + """Test description placeholders.""" + for component in additional_components: + assert await setup.async_setup_component(hass, component, {}) + hass.config.external_url = external_url + placeholders = await async_get_description_placeholders(hass) + assert placeholders == { + "oauth_consent_url": "https://console.cloud.google.com/apis/credentials/consent", + "more_info_url": "https://www.home-assistant.io/integrations/google_assistant_sdk/", + "oauth_creds_url": "https://console.cloud.google.com/apis/credentials", + "redirect_url": expected_redirect_uri, + } From e98fe7dc9c409229b9071930ce6952c27a8fa3f3 Mon Sep 17 00:00:00 2001 From: tronikos Date: Fri, 4 Jul 2025 07:17:41 -0700 Subject: [PATCH 012/106] Add data_description to Opower forms (#148099) --- homeassistant/components/opower/strings.json | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/homeassistant/components/opower/strings.json b/homeassistant/components/opower/strings.json index cd22bd8d7a1..8d8cecff905 100644 --- a/homeassistant/components/opower/strings.json +++ b/homeassistant/components/opower/strings.json @@ -6,12 +6,24 @@ "utility": "Utility name", "username": "[%key:common::config_flow::data::username%]", "password": "[%key:common::config_flow::data::password%]" + }, + "data_description": { + "utility": "The name of your utility provider", + "username": "The username for your utility account", + "password": "The password for your utility account" } }, "mfa": { "description": "The TOTP secret below is not one of the 6-digit time-based numeric codes. It is a string of around 16 characters containing the shared secret that enables your authenticator app to generate the correct time-based code at the appropriate time. See the documentation.", "data": { + "username": "[%key:common::config_flow::data::username%]", + "password": "[%key:common::config_flow::data::password%]", "totp_secret": "TOTP secret" + }, + "data_description": { + "username": "[%key:component::opower::config::step::user::data_description::username%]", + "password": "[%key:component::opower::config::step::user::data_description::password%]", + "totp_secret": "The TOTP secret for your utility account, used for multi-factor authentication (MFA)." } }, "reauth_confirm": { @@ -20,6 +32,11 @@ "username": "[%key:common::config_flow::data::username%]", "password": "[%key:common::config_flow::data::password%]", "totp_secret": "[%key:component::opower::config::step::mfa::data::totp_secret%]" + }, + "data_description": { + "username": "[%key:component::opower::config::step::user::data_description::username%]", + "password": "[%key:component::opower::config::step::user::data_description::password%]", + "totp_secret": "The TOTP secret for your utility account, used for multi-factor authentication (MFA)." } } }, From 1cb9767bb8bde5ced38bfec4875af252f40f397b Mon Sep 17 00:00:00 2001 From: tronikos Date: Fri, 4 Jul 2025 07:19:04 -0700 Subject: [PATCH 013/106] Enable strict typing for Opower (#148096) --- .strict-typing | 1 + mypy.ini | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/.strict-typing b/.strict-typing index a76ba3885bc..77e853262a1 100644 --- a/.strict-typing +++ b/.strict-typing @@ -381,6 +381,7 @@ homeassistant.components.openai_conversation.* homeassistant.components.openexchangerates.* homeassistant.components.opensky.* homeassistant.components.openuv.* +homeassistant.components.opower.* homeassistant.components.oralb.* homeassistant.components.otbr.* homeassistant.components.overkiz.* diff --git a/mypy.ini b/mypy.ini index a6b673be03b..48432118fa8 100644 --- a/mypy.ini +++ b/mypy.ini @@ -3566,6 +3566,16 @@ disallow_untyped_defs = true warn_return_any = true warn_unreachable = true +[mypy-homeassistant.components.opower.*] +check_untyped_defs = true +disallow_incomplete_defs = true +disallow_subclassing_any = true +disallow_untyped_calls = true +disallow_untyped_decorators = true +disallow_untyped_defs = true +warn_return_any = true +warn_unreachable = true + [mypy-homeassistant.components.oralb.*] check_untyped_defs = true disallow_incomplete_defs = true From 83ae5f52da9f5e6be33000072e259d76fba655a7 Mon Sep 17 00:00:00 2001 From: David Knowles Date: Fri, 4 Jul 2025 10:20:24 -0400 Subject: [PATCH 014/106] Bump pydrawise to 2025.7.0 (#148088) --- homeassistant/components/hydrawise/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/hydrawise/manifest.json b/homeassistant/components/hydrawise/manifest.json index 03b9dc68a79..a599ffa888e 100644 --- a/homeassistant/components/hydrawise/manifest.json +++ b/homeassistant/components/hydrawise/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/hydrawise", "iot_class": "cloud_polling", "loggers": ["pydrawise"], - "requirements": ["pydrawise==2025.6.0"] + "requirements": ["pydrawise==2025.7.0"] } diff --git a/requirements_all.txt b/requirements_all.txt index 4b622fe9c35..491ded4102e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1929,7 +1929,7 @@ pydiscovergy==3.0.2 pydoods==1.0.2 # homeassistant.components.hydrawise -pydrawise==2025.6.0 +pydrawise==2025.7.0 # homeassistant.components.android_ip_webcam pydroid-ipcam==3.0.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 4fbebe1bf9e..19f2df682a6 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1610,7 +1610,7 @@ pydexcom==0.2.3 pydiscovergy==3.0.2 # homeassistant.components.hydrawise -pydrawise==2025.6.0 +pydrawise==2025.7.0 # homeassistant.components.android_ip_webcam pydroid-ipcam==3.0.0 From cde17fc0ca3d95a81f7c9675d98eda7aba565e66 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 4 Jul 2025 16:21:11 +0200 Subject: [PATCH 015/106] add extra tests for media source URI parsing (#148114) --- .../components/media_source/models.py | 10 +++ tests/components/media_source/test_const.py | 80 +++++++++++++++++++ tests/components/media_source/test_models.py | 16 ++++ 3 files changed, 106 insertions(+) create mode 100644 tests/components/media_source/test_const.py diff --git a/homeassistant/components/media_source/models.py b/homeassistant/components/media_source/models.py index 5e64dc867f2..8588c5bcacc 100644 --- a/homeassistant/components/media_source/models.py +++ b/homeassistant/components/media_source/models.py @@ -45,6 +45,16 @@ class MediaSourceItem: identifier: str target_media_player: str | None + @property + def media_source_id(self) -> str: + """Return the media source ID.""" + uri = URI_SCHEME + if self.domain: + uri += self.domain + if self.identifier: + uri += f"/{self.identifier}" + return uri + async def async_browse(self) -> BrowseMediaSource: """Browse this item.""" if self.domain is None: diff --git a/tests/components/media_source/test_const.py b/tests/components/media_source/test_const.py new file mode 100644 index 00000000000..115c98a2c09 --- /dev/null +++ b/tests/components/media_source/test_const.py @@ -0,0 +1,80 @@ +"""Test constants for the media source component.""" + +import pytest + +from homeassistant.components.media_source.const import URI_SCHEME_REGEX + + +@pytest.mark.parametrize( + ("uri", "expected_domain", "expected_identifier"), + [ + ("media-source://", None, None), + ("media-source://local_media", "local_media", None), + ( + "media-source://local_media/some/path/file.mp3", + "local_media", + "some/path/file.mp3", + ), + ("media-source://a/b", "a", "b"), + ( + "media-source://domain/file with spaces.mp4", + "domain", + "file with spaces.mp4", + ), + ( + "media-source://domain/file-with-dashes.mp3", + "domain", + "file-with-dashes.mp3", + ), + ("media-source://domain/file.with.dots.mp3", "domain", "file.with.dots.mp3"), + ( + "media-source://domain/special!@#$%^&*()chars", + "domain", + "special!@#$%^&*()chars", + ), + ], +) +def test_valid_uri_patterns( + uri: str, expected_domain: str | None, expected_identifier: str | None +) -> None: + """Test various valid URI patterns.""" + match = URI_SCHEME_REGEX.match(uri) + assert match is not None + assert match.group("domain") == expected_domain + assert match.group("identifier") == expected_identifier + + +@pytest.mark.parametrize( + "uri", + [ + "media-source:", # missing // + "media-source:/", # missing second / + "media-source:///", # extra / + "media-source://domain/", # trailing slash after domain + "invalid-scheme://domain", # wrong scheme + "media-source//domain", # missing : + "MEDIA-SOURCE://domain", # uppercase scheme + "media_source://domain", # underscore in scheme + "", # empty string + "media-source", # scheme only + "media-source://domain extra", # extra content + "prefix media-source://domain", # prefix content + "media-source://domain suffix", # suffix content + # Invalid domain names + "media-source://_test", # starts with underscore + "media-source://test_", # ends with underscore + "media-source://_test_", # starts and ends with underscore + "media-source://_", # single underscore + "media-source://test-123", # contains hyphen + "media-source://test.123", # contains dot + "media-source://test 123", # contains space + "media-source://TEST", # uppercase letters + "media-source://Test", # mixed case + # Identifier cannot start with slash + "media-source://domain//invalid", # identifier starts with slash + ], +) +def test_invalid_uris(uri: str) -> None: + """Test invalid URI formats.""" + match = URI_SCHEME_REGEX.match(uri) + assert match is None, f"URI '{uri}' should be invalid" diff --git a/tests/components/media_source/test_models.py b/tests/components/media_source/test_models.py index 12685e28d69..1ed03a83961 100644 --- a/tests/components/media_source/test_models.py +++ b/tests/components/media_source/test_models.py @@ -2,6 +2,7 @@ from homeassistant.components.media_player import MediaClass, MediaType from homeassistant.components.media_source import const, models +from homeassistant.core import HomeAssistant async def test_browse_media_as_dict() -> None: @@ -68,3 +69,18 @@ async def test_media_source_default_name() -> None: """Test MediaSource uses domain as default name.""" source = models.MediaSource(const.DOMAIN) assert source.name == const.DOMAIN + + +async def test_media_source_item_media_source_id(hass: HomeAssistant) -> None: + """Test MediaSourceItem media_source_id property.""" + # Test with domain and identifier + item = models.MediaSourceItem(hass, "test_domain", "test/identifier", None) + assert item.media_source_id == "media-source://test_domain/test/identifier" + + # Test with domain only + item = models.MediaSourceItem(hass, "test_domain", "", None) + assert item.media_source_id == "media-source://test_domain" + + # Test with no domain (root) + item = models.MediaSourceItem(hass, None, "", None) + assert item.media_source_id == "media-source://" From 8ce30d9559ee3042acac2d23739e5d34dc74ce84 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 16:21:48 +0200 Subject: [PATCH 016/106] Add tests of legacy entity without platform writing state (#148109) --- tests/helpers/test_entity.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/helpers/test_entity.py b/tests/helpers/test_entity.py index 24205870779..30b25e9725d 100644 --- a/tests/helpers/test_entity.py +++ b/tests/helpers/test_entity.py @@ -2713,6 +2713,41 @@ async def test_platform_state( assert hass.states.get("test.test") is None +async def test_platform_state_no_platform(hass: HomeAssistant) -> None: + """Test platform state for entities which are not added by an entity platform.""" + + class MockEntity(entity.Entity): + entity_id = "test.test" + + def async_set_state(self, state: str) -> None: + self._attr_state = state + self.async_write_ha_state() + + ent = MockEntity() + ent.hass = hass + assert hass.states.get("test.test") is None + + # The attempt to write when in state NOT_ADDED should be allowed + assert ent._platform_state == entity.EntityPlatformState.NOT_ADDED + ent.async_set_state("not_added") + assert hass.states.get("test.test").state == "not_added" + + # The attempt to write when in state ADDING should be allowed + ent._platform_state = entity.EntityPlatformState.ADDING + ent.async_set_state("adding") + assert hass.states.get("test.test").state == "adding" + + # The attempt to write when in state ADDED should be allowed + ent._platform_state = entity.EntityPlatformState.ADDED + ent.async_set_state("added") + assert hass.states.get("test.test").state == "added" + + # The attempt to write when in state REMOVED should be ignored + ent._platform_state = entity.EntityPlatformState.REMOVED + ent.async_set_state("removed") + assert hass.states.get("test.test").state == "added" + + async def test_platform_state_fail_to_add( hass: HomeAssistant, entity_registry: er.EntityRegistry ) -> None: From 783102f2f6bd1c54c17bab28beda8650a8ceaa1a Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 4 Jul 2025 16:22:38 +0200 Subject: [PATCH 017/106] [ci] Fix typing issue with aiohttp and aiosignal (#148141) --- .github/workflows/ci.yaml | 2 +- homeassistant/components/http/ban.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f727d258d1e..ce7cf1ac124 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -37,7 +37,7 @@ on: type: boolean env: - CACHE_VERSION: 3 + CACHE_VERSION: 4 UV_CACHE_VERSION: 1 MYPY_CACHE_VERSION: 1 HA_SHORT_VERSION: "2025.8" diff --git a/homeassistant/components/http/ban.py b/homeassistant/components/http/ban.py index 71f3d54bef6..7e55191639b 100644 --- a/homeassistant/components/http/ban.py +++ b/homeassistant/components/http/ban.py @@ -64,7 +64,7 @@ def setup_bans(hass: HomeAssistant, app: Application, login_threshold: int) -> N """Initialize bans when app starts up.""" await app[KEY_BAN_MANAGER].async_load() - app.on_startup.append(ban_startup) + app.on_startup.append(ban_startup) # type: ignore[arg-type] @middleware From 3f752e13ff8aad45b0af0845a5080fb5f95ccf07 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 16:23:18 +0200 Subject: [PATCH 018/106] Replace MediaPlayerState.STANDBY with MediaPlayerState.OFF in roku (#148137) --- homeassistant/components/roku/media_player.py | 8 ++++---- tests/components/roku/test_media_player.py | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/roku/media_player.py b/homeassistant/components/roku/media_player.py index d0e1e3a53c0..7f815c4e458 100644 --- a/homeassistant/components/roku/media_player.py +++ b/homeassistant/components/roku/media_player.py @@ -142,7 +142,7 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity): def state(self) -> MediaPlayerState | None: """Return the state of the device.""" if self.coordinator.data.state.standby: - return MediaPlayerState.STANDBY + return MediaPlayerState.OFF if self.coordinator.data.app is None: return None @@ -308,21 +308,21 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity): @roku_exception_handler() async def async_media_pause(self) -> None: """Send pause command.""" - if self.state not in {MediaPlayerState.STANDBY, MediaPlayerState.PAUSED}: + if self.state not in {MediaPlayerState.OFF, MediaPlayerState.PAUSED}: await self.coordinator.roku.remote("play") await self.coordinator.async_request_refresh() @roku_exception_handler() async def async_media_play(self) -> None: """Send play command.""" - if self.state not in {MediaPlayerState.STANDBY, MediaPlayerState.PLAYING}: + if self.state not in {MediaPlayerState.OFF, MediaPlayerState.PLAYING}: await self.coordinator.roku.remote("play") await self.coordinator.async_request_refresh() @roku_exception_handler() async def async_media_play_pause(self) -> None: """Send play/pause command.""" - if self.state != MediaPlayerState.STANDBY: + if self.state != MediaPlayerState.OFF: await self.coordinator.roku.remote("play") await self.coordinator.async_request_refresh() diff --git a/tests/components/roku/test_media_player.py b/tests/components/roku/test_media_player.py index 5f8a41d16ac..7586e85b715 100644 --- a/tests/components/roku/test_media_player.py +++ b/tests/components/roku/test_media_player.py @@ -52,10 +52,10 @@ from homeassistant.const import ( SERVICE_VOLUME_MUTE, SERVICE_VOLUME_UP, STATE_IDLE, + STATE_OFF, STATE_ON, STATE_PAUSED, STATE_PLAYING, - STATE_STANDBY, STATE_UNAVAILABLE, ) from homeassistant.core import HomeAssistant @@ -112,7 +112,7 @@ async def test_idle_setup( """Test setup with idle device.""" state = hass.states.get(MAIN_ENTITY_ID) assert state - assert state.state == STATE_STANDBY + assert state.state == STATE_OFF @pytest.mark.parametrize("mock_device", ["roku/rokutv-7820x.json"], indirect=True) From b7f830523e6b64cbefe51129a69ba18e42251d0c Mon Sep 17 00:00:00 2001 From: Bram Kragten Date: Fri, 4 Jul 2025 16:25:28 +0200 Subject: [PATCH 019/106] Update frontend to 20250702.1 (#148131) --- homeassistant/components/frontend/manifest.json | 2 +- homeassistant/package_constraints.txt | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/frontend/manifest.json b/homeassistant/components/frontend/manifest.json index bfd868a5334..748d8f0c6f0 100644 --- a/homeassistant/components/frontend/manifest.json +++ b/homeassistant/components/frontend/manifest.json @@ -20,5 +20,5 @@ "documentation": "https://www.home-assistant.io/integrations/frontend", "integration_type": "system", "quality_scale": "internal", - "requirements": ["home-assistant-frontend==20250702.0"] + "requirements": ["home-assistant-frontend==20250702.1"] } diff --git a/homeassistant/package_constraints.txt b/homeassistant/package_constraints.txt index 2b891e1678d..9d985fae6c5 100644 --- a/homeassistant/package_constraints.txt +++ b/homeassistant/package_constraints.txt @@ -38,7 +38,7 @@ habluetooth==3.49.0 hass-nabucasa==0.105.0 hassil==2.2.3 home-assistant-bluetooth==1.13.1 -home-assistant-frontend==20250702.0 +home-assistant-frontend==20250702.1 home-assistant-intents==2025.6.23 httpx==0.28.1 ifaddr==0.2.0 diff --git a/requirements_all.txt b/requirements_all.txt index 491ded4102e..205d5f36ace 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1168,7 +1168,7 @@ hole==0.8.0 holidays==0.75 # homeassistant.components.frontend -home-assistant-frontend==20250702.0 +home-assistant-frontend==20250702.1 # homeassistant.components.conversation home-assistant-intents==2025.6.23 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 19f2df682a6..bc1bc2ee792 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1017,7 +1017,7 @@ hole==0.8.0 holidays==0.75 # homeassistant.components.frontend -home-assistant-frontend==20250702.0 +home-assistant-frontend==20250702.1 # homeassistant.components.conversation home-assistant-intents==2025.6.23 From fd86a43b287fb252ac269e4a0618490a9e8a4989 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 16:25:59 +0200 Subject: [PATCH 020/106] Replace MediaPlayerState.STANDBY with MediaPlayerState.OFF in ps4 (#148136) --- homeassistant/components/ps4/media_player.py | 4 ++-- tests/components/ps4/test_media_player.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/ps4/media_player.py b/homeassistant/components/ps4/media_player.py index aaec7cdf105..ea866aa3942 100644 --- a/homeassistant/components/ps4/media_player.py +++ b/homeassistant/components/ps4/media_player.py @@ -191,7 +191,7 @@ class PS4Device(MediaPlayerEntity): ) elif self.state != MediaPlayerState.IDLE: self.idle() - elif self.state != MediaPlayerState.STANDBY: + elif self.state != MediaPlayerState.OFF: self.state_standby() elif self._retry > DEFAULT_RETRIES: @@ -223,7 +223,7 @@ class PS4Device(MediaPlayerEntity): def state_standby(self) -> None: """Set states for state standby.""" self.reset_title() - self._attr_state = MediaPlayerState.STANDBY + self._attr_state = MediaPlayerState.OFF def state_unknown(self) -> None: """Set states for state unknown.""" diff --git a/tests/components/ps4/test_media_player.py b/tests/components/ps4/test_media_player.py index 737cc3c9f1b..af1f09d7d73 100644 --- a/tests/components/ps4/test_media_player.py +++ b/tests/components/ps4/test_media_player.py @@ -33,8 +33,8 @@ from homeassistant.const import ( CONF_REGION, CONF_TOKEN, STATE_IDLE, + STATE_OFF, STATE_PLAYING, - STATE_STANDBY, STATE_UNKNOWN, ) from homeassistant.core import HomeAssistant @@ -188,7 +188,7 @@ async def test_state_standby_is_set(hass: HomeAssistant) -> None: await mock_ddp_response(hass, MOCK_STATUS_STANDBY) - assert hass.states.get(mock_entity_id).state == STATE_STANDBY + assert hass.states.get(mock_entity_id).state == STATE_OFF async def test_state_playing_is_set(hass: HomeAssistant) -> None: @@ -308,7 +308,7 @@ async def test_device_info_is_set_from_status_correctly( mock_d_entries = device_registry.devices mock_entry = device_registry.async_get_device(identifiers={(DOMAIN, MOCK_HOST_ID)}) - assert mock_state == STATE_STANDBY + assert mock_state == STATE_OFF assert len(mock_d_entries) == 1 assert mock_entry.name == MOCK_HOST_NAME From 811f085556376be42cc571dea5278278a506014b Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 16:27:01 +0200 Subject: [PATCH 021/106] Replace MediaPlayerState.STANDBY with MediaPlayerState.IDLE in androidtv (#148130) --- homeassistant/components/androidtv/media_player.py | 2 +- tests/components/androidtv/test_media_player.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/androidtv/media_player.py b/homeassistant/components/androidtv/media_player.py index c9e62908cac..6a60d84e39e 100644 --- a/homeassistant/components/androidtv/media_player.py +++ b/homeassistant/components/androidtv/media_player.py @@ -56,7 +56,7 @@ SERVICE_UPLOAD = "upload" ANDROIDTV_STATES = { "off": MediaPlayerState.OFF, "idle": MediaPlayerState.IDLE, - "standby": MediaPlayerState.STANDBY, + "standby": MediaPlayerState.IDLE, "playing": MediaPlayerState.PLAYING, "paused": MediaPlayerState.PAUSED, } diff --git a/tests/components/androidtv/test_media_player.py b/tests/components/androidtv/test_media_player.py index 5a8d88dd9f6..efc05772a9a 100644 --- a/tests/components/androidtv/test_media_player.py +++ b/tests/components/androidtv/test_media_player.py @@ -54,9 +54,9 @@ from homeassistant.const import ( EVENT_HOMEASSISTANT_STOP, SERVICE_TURN_OFF, SERVICE_TURN_ON, + STATE_IDLE, STATE_OFF, STATE_PLAYING, - STATE_STANDBY, STATE_UNAVAILABLE, ) from homeassistant.core import HomeAssistant @@ -163,7 +163,7 @@ async def test_reconnect( state = hass.states.get(entity_id) assert state is not None - assert state.state == STATE_STANDBY + assert state.state == STATE_IDLE assert MSG_RECONNECT[patch_key] in caplog.record_tuples[2] @@ -672,7 +672,7 @@ async def test_update_lock_not_acquired(hass: HomeAssistant) -> None: await async_update_entity(hass, entity_id) state = hass.states.get(entity_id) assert state is not None - assert state.state == STATE_STANDBY + assert state.state == STATE_IDLE async def test_download(hass: HomeAssistant) -> None: From dc203755060ab7709ad4a78cd8c859fd419e2ea5 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 16:27:33 +0200 Subject: [PATCH 022/106] Replace MediaPlayerState.STANDBY with MediaPlayerState.OFF in snapcast (#148138) --- homeassistant/components/snapcast/media_player.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/snapcast/media_player.py b/homeassistant/components/snapcast/media_player.py index 5f011ca41ee..7d9cf74b2cc 100644 --- a/homeassistant/components/snapcast/media_player.py +++ b/homeassistant/components/snapcast/media_player.py @@ -343,7 +343,7 @@ class SnapcastClientDevice(SnapcastBaseDevice): if self.is_volume_muted or self._current_group.muted: return MediaPlayerState.IDLE return STREAM_STATUS.get(self._current_group.stream_status) - return MediaPlayerState.STANDBY + return MediaPlayerState.OFF @property def extra_state_attributes(self) -> Mapping[str, Any]: From 631523dfafba1d07b3e7870cdd246f3a401c2e83 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 16:27:54 +0200 Subject: [PATCH 023/106] Replace MediaPlayerState.STANDBY with MediaPlayerState.OFF in lookin (#148134) --- homeassistant/components/lookin/media_player.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/lookin/media_player.py b/homeassistant/components/lookin/media_player.py index f395c2b3885..16b69971370 100644 --- a/homeassistant/components/lookin/media_player.py +++ b/homeassistant/components/lookin/media_player.py @@ -136,7 +136,7 @@ class LookinMedia(LookinPowerPushRemoteEntity, MediaPlayerEntity): async def async_turn_off(self) -> None: """Turn the media player off.""" await self._async_send_command(self._power_off_command) - self._attr_state = MediaPlayerState.STANDBY + self._attr_state = MediaPlayerState.OFF self.async_write_ha_state() async def async_turn_on(self) -> None: @@ -159,7 +159,5 @@ class LookinMedia(LookinPowerPushRemoteEntity, MediaPlayerEntity): state = status[0] mute = status[2] - self._attr_state = ( - MediaPlayerState.ON if state == "1" else MediaPlayerState.STANDBY - ) + self._attr_state = MediaPlayerState.ON if state == "1" else MediaPlayerState.OFF self._attr_is_volume_muted = mute == "0" From a046530eaf06480b4cd5b5400ef49f3aa80bacba Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 16:30:03 +0200 Subject: [PATCH 024/106] Replace MediaPlayerState.STANDBY with MediaPlayerState.IDLE in mediaroom (#148135) --- homeassistant/components/mediaroom/media_player.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/mediaroom/media_player.py b/homeassistant/components/mediaroom/media_player.py index bccbe9f66ac..c7f7ee12ae8 100644 --- a/homeassistant/components/mediaroom/media_player.py +++ b/homeassistant/components/mediaroom/media_player.py @@ -134,7 +134,7 @@ class MediaroomDevice(MediaPlayerEntity): state_map = { State.OFF: MediaPlayerState.OFF, - State.STANDBY: MediaPlayerState.STANDBY, + State.STANDBY: MediaPlayerState.IDLE, State.PLAYING_LIVE_TV: MediaPlayerState.PLAYING, State.PLAYING_RECORDED_TV: MediaPlayerState.PLAYING, State.PLAYING_TIMESHIFT_TV: MediaPlayerState.PLAYING, @@ -155,7 +155,7 @@ class MediaroomDevice(MediaPlayerEntity): self._channel = None self._optimistic = optimistic self._attr_state = ( - MediaPlayerState.PLAYING if optimistic else MediaPlayerState.STANDBY + MediaPlayerState.PLAYING if optimistic else MediaPlayerState.IDLE ) self._name = f"Mediaroom {device_id if device_id else host}" self._available = True @@ -254,7 +254,7 @@ class MediaroomDevice(MediaPlayerEntity): try: self.set_state(await self.stb.turn_off()) if self._optimistic: - self._attr_state = MediaPlayerState.STANDBY + self._attr_state = MediaPlayerState.IDLE self._available = True except PyMediaroomError: self._available = False From 04bd1967a7166c9bafb623262ebc8b1a43544610 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 16:31:44 +0200 Subject: [PATCH 025/106] Replace MediaPlayerState.STANDBY with MediaPlayerState.OFF in apple_tv (#148132) --- homeassistant/components/apple_tv/media_player.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/apple_tv/media_player.py b/homeassistant/components/apple_tv/media_player.py index b6d451a9ea0..12a27fb195f 100644 --- a/homeassistant/components/apple_tv/media_player.py +++ b/homeassistant/components/apple_tv/media_player.py @@ -191,7 +191,7 @@ class AppleTvMediaPlayer( self._is_feature_available(FeatureName.PowerState) and self.atv.power.power_state == PowerState.Off ): - return MediaPlayerState.STANDBY + return MediaPlayerState.OFF if self._playing: state = self._playing.device_state if state in (DeviceState.Idle, DeviceState.Loading): @@ -200,7 +200,7 @@ class AppleTvMediaPlayer( return MediaPlayerState.PLAYING if state in (DeviceState.Paused, DeviceState.Seeking, DeviceState.Stopped): return MediaPlayerState.PAUSED - return MediaPlayerState.STANDBY # Bad or unknown state? + return MediaPlayerState.IDLE # Bad or unknown state? return None @callback From cc2aca2c2c093a135f28537dcfd853949f5ce96c Mon Sep 17 00:00:00 2001 From: hanwg Date: Fri, 4 Jul 2025 22:32:46 +0800 Subject: [PATCH 026/106] Fix Telegram bots using plain text parser failing to load on restart (#148050) --- homeassistant/components/telegram_bot/bot.py | 6 +++--- homeassistant/components/telegram_bot/config_flow.py | 2 -- homeassistant/components/telegram_bot/services.yaml | 5 +++++ tests/components/telegram_bot/test_config_flow.py | 2 +- tests/components/telegram_bot/test_telegram_bot.py | 5 ++++- 5 files changed, 13 insertions(+), 7 deletions(-) diff --git a/homeassistant/components/telegram_bot/bot.py b/homeassistant/components/telegram_bot/bot.py index a3feb120460..c57648c9551 100644 --- a/homeassistant/components/telegram_bot/bot.py +++ b/homeassistant/components/telegram_bot/bot.py @@ -374,9 +374,7 @@ class TelegramNotificationService: } if data is not None: if ATTR_PARSER in data: - params[ATTR_PARSER] = self._parsers.get( - data[ATTR_PARSER], self.parse_mode - ) + params[ATTR_PARSER] = data[ATTR_PARSER] if ATTR_TIMEOUT in data: params[ATTR_TIMEOUT] = data[ATTR_TIMEOUT] if ATTR_DISABLE_NOTIF in data: @@ -408,6 +406,8 @@ class TelegramNotificationService: params[ATTR_REPLYMARKUP] = InlineKeyboardMarkup( [_make_row_inline_keyboard(row) for row in keys] ) + if params[ATTR_PARSER] == PARSER_PLAIN_TEXT: + params[ATTR_PARSER] = None return params async def _send_msg( diff --git a/homeassistant/components/telegram_bot/config_flow.py b/homeassistant/components/telegram_bot/config_flow.py index 41f26ccd48d..8d3d9b0cd7b 100644 --- a/homeassistant/components/telegram_bot/config_flow.py +++ b/homeassistant/components/telegram_bot/config_flow.py @@ -159,8 +159,6 @@ class OptionsFlowHandler(OptionsFlow): """Manage the options.""" if user_input is not None: - if user_input[ATTR_PARSER] == PARSER_PLAIN_TEXT: - user_input[ATTR_PARSER] = None return self.async_create_entry(data=user_input) return self.async_show_form( diff --git a/homeassistant/components/telegram_bot/services.yaml b/homeassistant/components/telegram_bot/services.yaml index d5fc0e134d5..b1d94d381ac 100644 --- a/homeassistant/components/telegram_bot/services.yaml +++ b/homeassistant/components/telegram_bot/services.yaml @@ -109,6 +109,7 @@ send_photo: - "markdown" - "markdownv2" - "plain_text" + translation_key: "parse_mode" disable_notification: selector: boolean: @@ -261,6 +262,7 @@ send_animation: - "markdown" - "markdownv2" - "plain_text" + translation_key: "parse_mode" disable_notification: selector: boolean: @@ -341,6 +343,7 @@ send_video: - "markdown" - "markdownv2" - "plain_text" + translation_key: "parse_mode" disable_notification: selector: boolean: @@ -493,6 +496,7 @@ send_document: - "markdown" - "markdownv2" - "plain_text" + translation_key: "parse_mode" disable_notification: selector: boolean: @@ -670,6 +674,7 @@ edit_message: - "markdown" - "markdownv2" - "plain_text" + translation_key: "parse_mode" disable_web_page_preview: selector: boolean: diff --git a/tests/components/telegram_bot/test_config_flow.py b/tests/components/telegram_bot/test_config_flow.py index 2586761b584..9a076016a32 100644 --- a/tests/components/telegram_bot/test_config_flow.py +++ b/tests/components/telegram_bot/test_config_flow.py @@ -63,7 +63,7 @@ async def test_options_flow( await hass.async_block_till_done() assert result["type"] == FlowResultType.CREATE_ENTRY - assert result["data"][ATTR_PARSER] is None + assert result["data"][ATTR_PARSER] == PARSER_PLAIN_TEXT async def test_reconfigure_flow_broadcast( diff --git a/tests/components/telegram_bot/test_telegram_bot.py b/tests/components/telegram_bot/test_telegram_bot.py index 6590bbed1cf..73dd9e27763 100644 --- a/tests/components/telegram_bot/test_telegram_bot.py +++ b/tests/components/telegram_bot/test_telegram_bot.py @@ -50,6 +50,7 @@ from homeassistant.components.telegram_bot.const import ( ATTR_VERIFY_SSL, CONF_CONFIG_ENTRY_ID, DOMAIN, + PARSER_PLAIN_TEXT, PLATFORM_BROADCAST, SECTION_ADVANCED_SETTINGS, SERVICE_ANSWER_CALLBACK_QUERY, @@ -183,6 +184,7 @@ async def test_send_message( ( { ATTR_MESSAGE: "test_message", + ATTR_PARSER: PARSER_PLAIN_TEXT, ATTR_KEYBOARD_INLINE: "command1:/cmd1,/cmd2,mock_link:https://mock_link", }, InlineKeyboardMarkup( @@ -199,6 +201,7 @@ async def test_send_message( ( { ATTR_MESSAGE: "test_message", + ATTR_PARSER: PARSER_PLAIN_TEXT, ATTR_KEYBOARD_INLINE: [ [["command1", "/cmd1"]], [["mock_link", "https://mock_link"]], @@ -250,7 +253,7 @@ async def test_send_message_with_inline_keyboard( mock_send_message.assert_called_once_with( 12345678, "test_message", - parse_mode=ParseMode.MARKDOWN, + parse_mode=None, disable_web_page_preview=None, disable_notification=False, reply_to_message_id=None, From 5d258c2f8216db0788d59ccaf74835446def1644 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Fri, 4 Jul 2025 17:33:16 +0300 Subject: [PATCH 027/106] Bump aioamazondevices to 3.2.3 (#148082) --- homeassistant/components/alexa_devices/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/alexa_devices/manifest.json b/homeassistant/components/alexa_devices/manifest.json index 7c23edd92ce..70281390436 100644 --- a/homeassistant/components/alexa_devices/manifest.json +++ b/homeassistant/components/alexa_devices/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_polling", "loggers": ["aioamazondevices"], "quality_scale": "bronze", - "requirements": ["aioamazondevices==3.2.2"] + "requirements": ["aioamazondevices==3.2.3"] } diff --git a/requirements_all.txt b/requirements_all.txt index 205d5f36ace..63332a285a1 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -185,7 +185,7 @@ aioairzone-cloud==0.6.12 aioairzone==1.0.0 # homeassistant.components.alexa_devices -aioamazondevices==3.2.2 +aioamazondevices==3.2.3 # homeassistant.components.ambient_network # homeassistant.components.ambient_station diff --git a/requirements_test_all.txt b/requirements_test_all.txt index bc1bc2ee792..30a05f2cd53 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -173,7 +173,7 @@ aioairzone-cloud==0.6.12 aioairzone==1.0.0 # homeassistant.components.alexa_devices -aioamazondevices==3.2.2 +aioamazondevices==3.2.3 # homeassistant.components.ambient_network # homeassistant.components.ambient_station From 6235adc69a0bb9832b41a4bbf2a98c70f6071229 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 4 Jul 2025 16:42:24 +0200 Subject: [PATCH 028/106] Fix flaky emulated_roku/test_binding.py::test_events_fired_properly test (#148069) --- tests/components/emulated_roku/test_binding.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/components/emulated_roku/test_binding.py b/tests/components/emulated_roku/test_binding.py index ec3f064dfe0..a05660519c9 100644 --- a/tests/components/emulated_roku/test_binding.py +++ b/tests/components/emulated_roku/test_binding.py @@ -15,7 +15,7 @@ from homeassistant.components.emulated_roku.binding import ( ROKU_COMMAND_LAUNCH, EmulatedRoku, ) -from homeassistant.core import Event, HomeAssistant +from homeassistant.core import Event, HomeAssistant, callback async def test_events_fired_properly(hass: HomeAssistant) -> None: @@ -43,6 +43,7 @@ async def test_events_fired_properly(hass: HomeAssistant) -> None: return Mock(start=AsyncMock(), close=AsyncMock()) + @callback def listener(event: Event) -> None: if event.data[ATTR_SOURCE_NAME] == random_name: events.append(event) From 3250a2fb46096049c7c3e62c944baa6946a07796 Mon Sep 17 00:00:00 2001 From: Thomas55555 <59625598+Thomas55555@users.noreply.github.com> Date: Fri, 4 Jul 2025 16:43:36 +0200 Subject: [PATCH 029/106] Bump aioautomower to 1.2.0 (#148078) --- homeassistant/components/husqvarna_automower/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- .../husqvarna_automower/snapshots/test_diagnostics.ambr | 3 ++- 4 files changed, 5 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/husqvarna_automower/manifest.json b/homeassistant/components/husqvarna_automower/manifest.json index 34ec6693865..046c20c1ddd 100644 --- a/homeassistant/components/husqvarna_automower/manifest.json +++ b/homeassistant/components/husqvarna_automower/manifest.json @@ -8,5 +8,5 @@ "iot_class": "cloud_push", "loggers": ["aioautomower"], "quality_scale": "silver", - "requirements": ["aioautomower==1.0.1"] + "requirements": ["aioautomower==1.2.0"] } diff --git a/requirements_all.txt b/requirements_all.txt index 63332a285a1..391829a25e0 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -204,7 +204,7 @@ aioaseko==1.0.0 aioasuswrt==1.4.0 # homeassistant.components.husqvarna_automower -aioautomower==1.0.1 +aioautomower==1.2.0 # homeassistant.components.azure_devops aioazuredevops==2.2.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 30a05f2cd53..b8da24b1752 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -192,7 +192,7 @@ aioaseko==1.0.0 aioasuswrt==1.4.0 # homeassistant.components.husqvarna_automower -aioautomower==1.0.1 +aioautomower==1.2.0 # homeassistant.components.azure_devops aioazuredevops==2.2.1 diff --git a/tests/components/husqvarna_automower/snapshots/test_diagnostics.ambr b/tests/components/husqvarna_automower/snapshots/test_diagnostics.ambr index 2c3352ecf8e..d1e1f08f867 100644 --- a/tests/components/husqvarna_automower/snapshots/test_diagnostics.ambr +++ b/tests/components/husqvarna_automower/snapshots/test_diagnostics.ambr @@ -63,6 +63,7 @@ 'stay_out_zones': True, 'work_areas': True, }), + 'messages': None, 'metadata': dict({ 'connected': True, 'status_dateteime': '2023-06-05T00:00:00+00:00', @@ -80,7 +81,7 @@ 'work_area_name': 'Front lawn', }), 'planner': dict({ - 'external_reason': 'ifttt_wildlife', + 'external_reason': 'ifttt', 'next_start_datetime': '2023-06-05T19:00:00+02:00', 'override': dict({ 'action': 'not_active', From cf931a75a756c063095d0bd471fb3974f4c3c36f Mon Sep 17 00:00:00 2001 From: Greg Dowling Date: Fri, 4 Jul 2025 16:04:16 +0100 Subject: [PATCH 030/106] Remove incorrect use of via_device in roon component (#146572) --- homeassistant/components/roon/event.py | 7 ++++--- homeassistant/components/roon/media_player.py | 7 ++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/roon/event.py b/homeassistant/components/roon/event.py index 2f2967c5789..b2a491c8d28 100644 --- a/homeassistant/components/roon/event.py +++ b/homeassistant/components/roon/event.py @@ -31,7 +31,7 @@ async def async_setup_entry( if dev_id in event_entities: return # new player! - event_entity = RoonEventEntity(roon_server, player_data) + event_entity = RoonEventEntity(roon_server, player_data, config_entry.entry_id) event_entities.add(dev_id) async_add_entities([event_entity]) @@ -50,13 +50,14 @@ class RoonEventEntity(EventEntity): _attr_event_types = ["volume_up", "volume_down", "mute_toggle"] _attr_translation_key = "volume" - def __init__(self, server, player_data): + def __init__(self, server, player_data, entry_id): """Initialize the entity.""" self._server = server self._player_data = player_data player_name = player_data["display_name"] self._attr_name = f"{player_name} roon volume" self._attr_unique_id = self._player_data["dev_id"] + self._entry_id = entry_id if self._player_data.get("source_controls"): dev_model = self._player_data["source_controls"][0].get("display_name") @@ -69,7 +70,7 @@ class RoonEventEntity(EventEntity): name=cast(str | None, self.name), manufacturer="RoonLabs", model=dev_model, - via_device=(DOMAIN, self._server.roon_id), + via_device=(DOMAIN, self._entry_id), ) def _roonapi_volume_callback( diff --git a/homeassistant/components/roon/media_player.py b/homeassistant/components/roon/media_player.py index 4a87601a24f..0c4f8394989 100644 --- a/homeassistant/components/roon/media_player.py +++ b/homeassistant/components/roon/media_player.py @@ -72,7 +72,7 @@ async def async_setup_entry( dev_id = player_data["dev_id"] if dev_id not in media_players: # new player! - media_player = RoonDevice(roon_server, player_data) + media_player = RoonDevice(roon_server, player_data, config_entry.entry_id) media_players.add(dev_id) async_add_entities([media_player]) else: @@ -106,7 +106,7 @@ class RoonDevice(MediaPlayerEntity): | MediaPlayerEntityFeature.PLAY_MEDIA ) - def __init__(self, server, player_data): + def __init__(self, server, player_data, entry_id): """Initialize Roon device object.""" self._remove_signal_status = None self._server = server @@ -125,6 +125,7 @@ class RoonDevice(MediaPlayerEntity): self._attr_volume_level = 0 self._volume_fixed = True self._volume_incremental = False + self._entry_id = entry_id self.update_data(player_data) async def async_added_to_hass(self) -> None: @@ -166,7 +167,7 @@ class RoonDevice(MediaPlayerEntity): name=cast(str | None, self.name), manufacturer="RoonLabs", model=dev_model, - via_device=(DOMAIN, self._server.roon_id), + via_device=(DOMAIN, self._entry_id), ) def update_data(self, player_data=None): From 8e6b9c04f6fb2aadd720af4be1a3b51d2bd6c40e Mon Sep 17 00:00:00 2001 From: Michael Freeman Date: Fri, 4 Jul 2025 11:46:59 -0400 Subject: [PATCH 031/106] Bump venstarcolortouch to 0.21 (#148152) --- homeassistant/components/venstar/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/venstar/manifest.json b/homeassistant/components/venstar/manifest.json index f3045fe49e8..5991dc8fe51 100644 --- a/homeassistant/components/venstar/manifest.json +++ b/homeassistant/components/venstar/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/venstar", "iot_class": "local_polling", "loggers": ["venstarcolortouch"], - "requirements": ["venstarcolortouch==0.19"] + "requirements": ["venstarcolortouch==0.21"] } diff --git a/requirements_all.txt b/requirements_all.txt index 391829a25e0..862c95d2a47 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -3041,7 +3041,7 @@ vehicle==2.2.2 velbus-aio==2025.5.0 # homeassistant.components.venstar -venstarcolortouch==0.19 +venstarcolortouch==0.21 # homeassistant.components.vilfo vilfo-api-client==0.5.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index b8da24b1752..2515980ccfd 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -2509,7 +2509,7 @@ vehicle==2.2.2 velbus-aio==2025.5.0 # homeassistant.components.venstar -venstarcolortouch==0.19 +venstarcolortouch==0.21 # homeassistant.components.vilfo vilfo-api-client==0.5.0 From bb1e26314995e12ff861eaf08dcbea931441def0 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 18:34:55 +0200 Subject: [PATCH 032/106] Remove cv.SUN_CONDITION_SCHEMA (#148158) --- homeassistant/helpers/config_validation.py | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/homeassistant/helpers/config_validation.py b/homeassistant/helpers/config_validation.py index 5445cb51ac9..ab347e803d6 100644 --- a/homeassistant/helpers/config_validation.py +++ b/homeassistant/helpers/config_validation.py @@ -1537,22 +1537,6 @@ def STATE_CONDITION_SCHEMA(value: Any) -> dict[str, Any]: return key_dependency("for", "state")(validated) -SUN_CONDITION_SCHEMA = vol.All( - vol.Schema( - { - **CONDITION_BASE_SCHEMA, - vol.Required(CONF_CONDITION): "sun", - vol.Optional("before"): sun_event, - vol.Optional("before_offset"): time_period, - vol.Optional("after"): vol.All( - vol.Lower, vol.Any(SUN_EVENT_SUNSET, SUN_EVENT_SUNRISE) - ), - vol.Optional("after_offset"): time_period, - } - ), - has_at_least_one_key("before", "after"), -) - TEMPLATE_CONDITION_SCHEMA = vol.Schema( { **CONDITION_BASE_SCHEMA, From 0b2db2510f1fc0707b752764eec386dd90a1549b Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Fri, 4 Jul 2025 11:06:33 -0700 Subject: [PATCH 033/106] Support translating number selector UoM (#148162) --- homeassistant/components/derivative/config_flow.py | 1 + homeassistant/components/derivative/strings.json | 5 +++++ homeassistant/helpers/selector.py | 2 ++ script/hassfest/translations.py | 4 ++++ tests/helpers/test_selector.py | 8 +++++++- 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/derivative/config_flow.py b/homeassistant/components/derivative/config_flow.py index 37d54e04f7f..dc12e1bbfe2 100644 --- a/homeassistant/components/derivative/config_flow.py +++ b/homeassistant/components/derivative/config_flow.py @@ -94,6 +94,7 @@ async def _get_options_dict(handler: SchemaCommonFlowHandler | None) -> dict: max=6, mode=selector.NumberSelectorMode.BOX, unit_of_measurement="decimals", + translation_key="round", ), ), vol.Required(CONF_TIME_WINDOW): selector.DurationSelector(), diff --git a/homeassistant/components/derivative/strings.json b/homeassistant/components/derivative/strings.json index 5081e7f3b35..551d0912a94 100644 --- a/homeassistant/components/derivative/strings.json +++ b/homeassistant/components/derivative/strings.json @@ -52,6 +52,11 @@ "h": "Hours", "d": "Days" } + }, + "round": { + "unit_of_measurement": { + "decimals": "decimals" + } } } } diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index e4277aac98e..4fa31ee78a2 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -1066,6 +1066,7 @@ class NumberSelectorConfig(BaseSelectorConfig, total=False): step: float | Literal["any"] unit_of_measurement: str mode: NumberSelectorMode + translation_key: str class NumberSelectorMode(StrEnum): @@ -1106,6 +1107,7 @@ class NumberSelector(Selector[NumberSelectorConfig]): vol.Optional(CONF_MODE, default=NumberSelectorMode.SLIDER): vol.All( vol.Coerce(NumberSelectorMode), lambda val: val.value ), + vol.Optional("translation_key"): str, } ), validate_slider, diff --git a/script/hassfest/translations.py b/script/hassfest/translations.py index 4e0cf349aec..974c932ae5c 100644 --- a/script/hassfest/translations.py +++ b/script/hassfest/translations.py @@ -310,6 +310,10 @@ def gen_strings_schema(config: Config, integration: Integration) -> vol.Schema: translation_value_validator, slug_validator=translation_key_validator, ), + vol.Optional("unit_of_measurement"): cv.schema_with_slug_keys( + translation_value_validator, + slug_validator=translation_key_validator, + ), vol.Optional("fields"): cv.schema_with_slug_keys(str), }, slug_validator=vol.Any("_", cv.slug), diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index 8947ea8099c..dd8cd1c1b64 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -396,7 +396,13 @@ def test_assist_pipeline_selector_schema( ({"min": -100, "max": 100, "step": 5}, (), ()), ({"min": -20, "max": -10, "mode": "box"}, (), ()), ( - {"min": 0, "max": 100, "unit_of_measurement": "seconds", "mode": "slider"}, + { + "min": 0, + "max": 100, + "unit_of_measurement": "seconds", + "mode": "slider", + "translation_key": "foo", + }, (), (), ), From c61cd422d191535d3852865c1f486230f0651e6c Mon Sep 17 00:00:00 2001 From: Thomas55555 <59625598+Thomas55555@users.noreply.github.com> Date: Fri, 4 Jul 2025 20:47:32 +0200 Subject: [PATCH 034/106] Delete stale icon translation in Husqvarna Automower (#148168) --- homeassistant/components/husqvarna_automower/icons.json | 3 --- 1 file changed, 3 deletions(-) diff --git a/homeassistant/components/husqvarna_automower/icons.json b/homeassistant/components/husqvarna_automower/icons.json index 14ac5ce4068..e9bc5901b97 100644 --- a/homeassistant/components/husqvarna_automower/icons.json +++ b/homeassistant/components/husqvarna_automower/icons.json @@ -3,9 +3,6 @@ "binary_sensor": { "leaving_dock": { "default": "mdi:debug-step-out" - }, - "returning_to_dock": { - "default": "mdi:debug-step-into" } }, "button": { From 70624f72b65f75a7210c4f044c79e2fe760d9c5c Mon Sep 17 00:00:00 2001 From: Thomas55555 <59625598+Thomas55555@users.noreply.github.com> Date: Fri, 4 Jul 2025 21:51:47 +0200 Subject: [PATCH 035/106] Additional icon translation for Husqvarna Automower (#148167) --- .../components/husqvarna_automower/icons.json | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/homeassistant/components/husqvarna_automower/icons.json b/homeassistant/components/husqvarna_automower/icons.json index e9bc5901b97..e1b355959d9 100644 --- a/homeassistant/components/husqvarna_automower/icons.json +++ b/homeassistant/components/husqvarna_automower/icons.json @@ -45,6 +45,26 @@ "work_area_progress": { "default": "mdi:collage" } + }, + "switch": { + "my_lawn_work_area": { + "default": "mdi:square-outline", + "state": { + "on": "mdi:square" + } + }, + "work_area_work_area": { + "default": "mdi:square-outline", + "state": { + "on": "mdi:square" + } + }, + "stay_out_zones": { + "default": "mdi:rhombus-outline", + "state": { + "on": "mdi:rhombus" + } + } } }, "services": { From b6b6de24aca0ed099db481f564fc777feb8d975a Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Fri, 4 Jul 2025 21:54:11 +0200 Subject: [PATCH 036/106] Replace MediaPlayerState.STANDBY with MediaPlayerState.OFF in cambridge_audio (#148133) --- homeassistant/components/cambridge_audio/media_player.py | 2 +- tests/components/cambridge_audio/test_media_player.py | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/cambridge_audio/media_player.py b/homeassistant/components/cambridge_audio/media_player.py index e8f92c0b25c..75e537e457c 100644 --- a/homeassistant/components/cambridge_audio/media_player.py +++ b/homeassistant/components/cambridge_audio/media_player.py @@ -107,7 +107,7 @@ class CambridgeAudioDevice(CambridgeAudioEntity, MediaPlayerEntity): """Return the state of the device.""" media_state = self.client.play_state.state if media_state == "NETWORK": - return MediaPlayerState.STANDBY + return MediaPlayerState.OFF if self.client.state.power: if media_state == "play": return MediaPlayerState.PLAYING diff --git a/tests/components/cambridge_audio/test_media_player.py b/tests/components/cambridge_audio/test_media_player.py index 10e9311c4b0..7bdc2dddc8d 100644 --- a/tests/components/cambridge_audio/test_media_player.py +++ b/tests/components/cambridge_audio/test_media_player.py @@ -45,7 +45,6 @@ from homeassistant.const import ( STATE_ON, STATE_PAUSED, STATE_PLAYING, - STATE_STANDBY, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError, ServiceValidationError @@ -156,8 +155,8 @@ async def test_entity_supported_features_with_control_bus( @pytest.mark.parametrize( ("power_state", "play_state", "media_player_state"), [ - (True, "NETWORK", STATE_STANDBY), - (False, "NETWORK", STATE_STANDBY), + (True, "NETWORK", STATE_OFF), + (False, "NETWORK", STATE_OFF), (False, "play", STATE_OFF), (True, "play", STATE_PLAYING), (True, "pause", STATE_PAUSED), From bfccee17efffc29163f4e0b200915fd99728aa1f Mon Sep 17 00:00:00 2001 From: Hessel Date: Fri, 4 Jul 2025 21:56:44 +0200 Subject: [PATCH 037/106] Wallbox, Improve test setup (#148036) --- tests/components/wallbox/__init__.py | 386 ----------------- tests/components/wallbox/conftest.py | 254 ++++++++++- tests/components/wallbox/test_config_flow.py | 57 ++- tests/components/wallbox/test_init.py | 115 ++--- tests/components/wallbox/test_lock.py | 162 ++----- tests/components/wallbox/test_number.py | 424 ++++++------------- tests/components/wallbox/test_select.py | 126 +++--- tests/components/wallbox/test_sensor.py | 4 +- tests/components/wallbox/test_switch.py | 93 +--- 9 files changed, 567 insertions(+), 1054 deletions(-) diff --git a/tests/components/wallbox/__init__.py b/tests/components/wallbox/__init__.py index 37e7d5059f0..35bf3cee242 100644 --- a/tests/components/wallbox/__init__.py +++ b/tests/components/wallbox/__init__.py @@ -1,387 +1 @@ """Tests for the Wallbox integration.""" - -from http import HTTPStatus - -import requests -import requests_mock - -from homeassistant.components.wallbox.const import ( - CHARGER_ADDED_ENERGY_KEY, - CHARGER_ADDED_RANGE_KEY, - CHARGER_CHARGING_POWER_KEY, - CHARGER_CHARGING_SPEED_KEY, - CHARGER_CURRENCY_KEY, - CHARGER_CURRENT_VERSION_KEY, - CHARGER_DATA_KEY, - CHARGER_ECO_SMART_KEY, - CHARGER_ECO_SMART_MODE_KEY, - CHARGER_ECO_SMART_STATUS_KEY, - CHARGER_ENERGY_PRICE_KEY, - CHARGER_FEATURES_KEY, - CHARGER_LOCKED_UNLOCKED_KEY, - CHARGER_MAX_AVAILABLE_POWER_KEY, - CHARGER_MAX_CHARGING_CURRENT_KEY, - CHARGER_MAX_ICP_CURRENT_KEY, - CHARGER_NAME_KEY, - CHARGER_PART_NUMBER_KEY, - CHARGER_PLAN_KEY, - CHARGER_POWER_BOOST_KEY, - CHARGER_SERIAL_NUMBER_KEY, - CHARGER_SOFTWARE_KEY, - CHARGER_STATUS_ID_KEY, -) -from homeassistant.core import HomeAssistant - -from .const import ERROR, REFRESH_TOKEN_TTL, STATUS, TTL, USER_ID - -from tests.common import MockConfigEntry - -test_response = { - CHARGER_CHARGING_POWER_KEY: 0, - CHARGER_STATUS_ID_KEY: 193, - CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, - CHARGER_CHARGING_SPEED_KEY: 0, - CHARGER_ADDED_RANGE_KEY: 150, - CHARGER_ADDED_ENERGY_KEY: 44.697, - CHARGER_NAME_KEY: "WallboxName", - CHARGER_DATA_KEY: { - CHARGER_MAX_CHARGING_CURRENT_KEY: 24, - CHARGER_ENERGY_PRICE_KEY: 0.4, - CHARGER_LOCKED_UNLOCKED_KEY: False, - CHARGER_SERIAL_NUMBER_KEY: "20000", - CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", - CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, - CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, - CHARGER_MAX_ICP_CURRENT_KEY: 20, - CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, - CHARGER_ECO_SMART_KEY: { - CHARGER_ECO_SMART_STATUS_KEY: False, - CHARGER_ECO_SMART_MODE_KEY: 0, - }, - }, -} - -test_response_bidir = { - CHARGER_CHARGING_POWER_KEY: 0, - CHARGER_STATUS_ID_KEY: 193, - CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, - CHARGER_CHARGING_SPEED_KEY: 0, - CHARGER_ADDED_RANGE_KEY: 150, - CHARGER_ADDED_ENERGY_KEY: 44.697, - CHARGER_NAME_KEY: "WallboxName", - CHARGER_DATA_KEY: { - CHARGER_MAX_CHARGING_CURRENT_KEY: 24, - CHARGER_ENERGY_PRICE_KEY: 0.4, - CHARGER_LOCKED_UNLOCKED_KEY: False, - CHARGER_SERIAL_NUMBER_KEY: "20000", - CHARGER_PART_NUMBER_KEY: "QSP1-0-2-4-9-002-E", - CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, - CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, - CHARGER_MAX_ICP_CURRENT_KEY: 20, - CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, - CHARGER_ECO_SMART_KEY: { - CHARGER_ECO_SMART_STATUS_KEY: False, - CHARGER_ECO_SMART_MODE_KEY: 0, - }, - }, -} - -test_response_eco_mode = { - CHARGER_CHARGING_POWER_KEY: 0, - CHARGER_STATUS_ID_KEY: 193, - CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, - CHARGER_CHARGING_SPEED_KEY: 0, - CHARGER_ADDED_RANGE_KEY: 150, - CHARGER_ADDED_ENERGY_KEY: 44.697, - CHARGER_NAME_KEY: "WallboxName", - CHARGER_DATA_KEY: { - CHARGER_MAX_CHARGING_CURRENT_KEY: 24, - CHARGER_ENERGY_PRICE_KEY: 0.4, - CHARGER_LOCKED_UNLOCKED_KEY: False, - CHARGER_SERIAL_NUMBER_KEY: "20000", - CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", - CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, - CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, - CHARGER_MAX_ICP_CURRENT_KEY: 20, - CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, - CHARGER_ECO_SMART_KEY: { - CHARGER_ECO_SMART_STATUS_KEY: True, - CHARGER_ECO_SMART_MODE_KEY: 0, - }, - }, -} - - -test_response_full_solar = { - CHARGER_CHARGING_POWER_KEY: 0, - CHARGER_STATUS_ID_KEY: 193, - CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, - CHARGER_CHARGING_SPEED_KEY: 0, - CHARGER_ADDED_RANGE_KEY: 150, - CHARGER_ADDED_ENERGY_KEY: 44.697, - CHARGER_NAME_KEY: "WallboxName", - CHARGER_DATA_KEY: { - CHARGER_MAX_CHARGING_CURRENT_KEY: 24, - CHARGER_ENERGY_PRICE_KEY: 0.4, - CHARGER_LOCKED_UNLOCKED_KEY: False, - CHARGER_SERIAL_NUMBER_KEY: "20000", - CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", - CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, - CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, - CHARGER_MAX_ICP_CURRENT_KEY: 20, - CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, - CHARGER_ECO_SMART_KEY: { - CHARGER_ECO_SMART_STATUS_KEY: True, - CHARGER_ECO_SMART_MODE_KEY: 1, - }, - }, -} - -test_response_no_power_boost = { - CHARGER_CHARGING_POWER_KEY: 0, - CHARGER_STATUS_ID_KEY: 193, - CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, - CHARGER_CHARGING_SPEED_KEY: 0, - CHARGER_ADDED_RANGE_KEY: 150, - CHARGER_ADDED_ENERGY_KEY: 44.697, - CHARGER_NAME_KEY: "WallboxName", - CHARGER_DATA_KEY: { - CHARGER_MAX_CHARGING_CURRENT_KEY: 24, - CHARGER_ENERGY_PRICE_KEY: 0.4, - CHARGER_LOCKED_UNLOCKED_KEY: False, - CHARGER_SERIAL_NUMBER_KEY: "20000", - CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", - CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, - CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, - CHARGER_MAX_ICP_CURRENT_KEY: 20, - CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: []}, - }, -} - - -http_404_error = requests.exceptions.HTTPError() -http_404_error.response = requests.Response() -http_404_error.response.status_code = HTTPStatus.NOT_FOUND -http_429_error = requests.exceptions.HTTPError() -http_429_error.response = requests.Response() -http_429_error.response.status_code = HTTPStatus.TOO_MANY_REQUESTS - -authorisation_response = { - "data": { - "attributes": { - "token": "fakekeyhere", - "refresh_token": "refresh_fakekeyhere", - USER_ID: 12345, - TTL: 145656758, - REFRESH_TOKEN_TTL: 145756758, - ERROR: "false", - STATUS: 200, - } - } -} - - -authorisation_response_unauthorised = { - "data": { - "attributes": { - "token": "fakekeyhere", - "refresh_token": "refresh_fakekeyhere", - USER_ID: 12345, - TTL: 145656758, - REFRESH_TOKEN_TTL: 145756758, - ERROR: "false", - STATUS: 404, - } - } -} - -invalid_reauth_response = { - "jwt": "fakekeyhere", - "refresh_token": "refresh_fakekeyhere", - "user_id": 12345, - "ttl": 145656758, - "refresh_token_ttl": 145756758, - "error": False, - "status": 200, -} - -http_403_error = requests.exceptions.HTTPError() -http_403_error.response = requests.Response() -http_403_error.response.status_code = HTTPStatus.FORBIDDEN - -http_404_error = requests.exceptions.HTTPError() -http_404_error.response = requests.Response() -http_404_error.response.status_code = HTTPStatus.NOT_FOUND - - -async def setup_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None: - """Test wallbox sensor class setup.""" - with requests_mock.Mocker() as mock_request: - mock_request.get( - "https://user-api.wall-box.com/users/signin", - json=authorisation_response, - status_code=HTTPStatus.OK, - ) - mock_request.get( - "https://api.wall-box.com/chargers/status/12345", - json=test_response, - status_code=HTTPStatus.OK, - ) - mock_request.put( - "https://api.wall-box.com/v2/charger/12345", - json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20}, - status_code=HTTPStatus.OK, - ) - - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - -async def setup_integration_no_eco_mode( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class setup.""" - with requests_mock.Mocker() as mock_request: - mock_request.get( - "https://user-api.wall-box.com/users/signin", - json=authorisation_response, - status_code=HTTPStatus.OK, - ) - mock_request.get( - "https://api.wall-box.com/chargers/status/12345", - json=test_response_no_power_boost, - status_code=HTTPStatus.OK, - ) - mock_request.put( - "https://api.wall-box.com/v2/charger/12345", - json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20}, - status_code=HTTPStatus.OK, - ) - - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - -async def setup_integration_select( - hass: HomeAssistant, entry: MockConfigEntry, response -) -> None: - """Test wallbox sensor class setup.""" - with requests_mock.Mocker() as mock_request: - mock_request.get( - "https://user-api.wall-box.com/users/signin", - json=authorisation_response, - status_code=HTTPStatus.OK, - ) - mock_request.get( - "https://api.wall-box.com/chargers/status/12345", - json=response, - status_code=HTTPStatus.OK, - ) - mock_request.put( - "https://api.wall-box.com/v2/charger/12345", - json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20}, - status_code=HTTPStatus.OK, - ) - - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - -async def setup_integration_bidir(hass: HomeAssistant, entry: MockConfigEntry) -> None: - """Test wallbox sensor class setup.""" - with requests_mock.Mocker() as mock_request: - mock_request.get( - "https://user-api.wall-box.com/users/signin", - json=authorisation_response, - status_code=HTTPStatus.OK, - ) - mock_request.get( - "https://api.wall-box.com/chargers/status/12345", - json=test_response_bidir, - status_code=HTTPStatus.OK, - ) - mock_request.put( - "https://api.wall-box.com/v2/charger/12345", - json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20}, - status_code=HTTPStatus.OK, - ) - - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - -async def setup_integration_connection_error( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class setup with a connection error.""" - with requests_mock.Mocker() as mock_request: - mock_request.get( - "https://user-api.wall-box.com/users/signin", - json=authorisation_response, - status_code=HTTPStatus.FORBIDDEN, - ) - mock_request.get( - "https://api.wall-box.com/chargers/status/12345", - json=test_response, - status_code=HTTPStatus.FORBIDDEN, - ) - mock_request.put( - "https://api.wall-box.com/v2/charger/12345", - json={CHARGER_MAX_CHARGING_CURRENT_KEY: 20}, - status_code=HTTPStatus.FORBIDDEN, - ) - - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - -async def setup_integration_read_only( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class setup for read only.""" - - with requests_mock.Mocker() as mock_request: - mock_request.get( - "https://user-api.wall-box.com/users/signin", - json=authorisation_response, - status_code=HTTPStatus.OK, - ) - mock_request.get( - "https://api.wall-box.com/chargers/status/12345", - json=test_response, - status_code=HTTPStatus.OK, - ) - mock_request.put( - "https://api.wall-box.com/v2/charger/12345", - json=test_response, - status_code=HTTPStatus.FORBIDDEN, - ) - - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() - - -async def setup_integration_platform_not_ready( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class setup for read only.""" - - with requests_mock.Mocker() as mock_request: - mock_request.get( - "https://user-api.wall-box.com/users/signin", - json=authorisation_response, - status_code=HTTPStatus.OK, - ) - mock_request.get( - "https://api.wall-box.com/chargers/status/12345", - json=test_response, - status_code=HTTPStatus.OK, - ) - mock_request.put( - "https://api.wall-box.com/v2/charger/12345", - json=test_response, - status_code=HTTPStatus.NOT_FOUND, - ) - - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() diff --git a/tests/components/wallbox/conftest.py b/tests/components/wallbox/conftest.py index 72d493ceb69..ab1032b3816 100644 --- a/tests/components/wallbox/conftest.py +++ b/tests/components/wallbox/conftest.py @@ -1,13 +1,220 @@ """Test fixtures for the Wallbox integration.""" -import pytest +from http import HTTPStatus +from unittest.mock import MagicMock, Mock, patch -from homeassistant.components.wallbox.const import CONF_STATION, DOMAIN +import pytest +import requests + +from homeassistant.components.wallbox.const import ( + CHARGER_ADDED_ENERGY_KEY, + CHARGER_ADDED_RANGE_KEY, + CHARGER_CHARGING_POWER_KEY, + CHARGER_CHARGING_SPEED_KEY, + CHARGER_CURRENCY_KEY, + CHARGER_CURRENT_VERSION_KEY, + CHARGER_DATA_KEY, + CHARGER_DATA_POST_L1_KEY, + CHARGER_DATA_POST_L2_KEY, + CHARGER_ECO_SMART_KEY, + CHARGER_ECO_SMART_MODE_KEY, + CHARGER_ECO_SMART_STATUS_KEY, + CHARGER_ENERGY_PRICE_KEY, + CHARGER_FEATURES_KEY, + CHARGER_LOCKED_UNLOCKED_KEY, + CHARGER_MAX_AVAILABLE_POWER_KEY, + CHARGER_MAX_CHARGING_CURRENT_KEY, + CHARGER_MAX_CHARGING_CURRENT_POST_KEY, + CHARGER_MAX_ICP_CURRENT_KEY, + CHARGER_NAME_KEY, + CHARGER_PART_NUMBER_KEY, + CHARGER_PLAN_KEY, + CHARGER_POWER_BOOST_KEY, + CHARGER_SERIAL_NUMBER_KEY, + CHARGER_SOFTWARE_KEY, + CHARGER_STATUS_ID_KEY, + CONF_STATION, + DOMAIN, +) from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant +from .const import ERROR, REFRESH_TOKEN_TTL, STATUS, TTL, USER_ID + from tests.common import MockConfigEntry +test_response = { + CHARGER_CHARGING_POWER_KEY: 0, + CHARGER_STATUS_ID_KEY: 193, + CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, + CHARGER_CHARGING_SPEED_KEY: 0, + CHARGER_ADDED_RANGE_KEY: 150, + CHARGER_ADDED_ENERGY_KEY: 44.697, + CHARGER_NAME_KEY: "WallboxName", + CHARGER_DATA_KEY: { + CHARGER_MAX_CHARGING_CURRENT_KEY: 24, + CHARGER_ENERGY_PRICE_KEY: 0.4, + CHARGER_LOCKED_UNLOCKED_KEY: False, + CHARGER_SERIAL_NUMBER_KEY: "20000", + CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", + CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, + CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, + CHARGER_ECO_SMART_KEY: { + CHARGER_ECO_SMART_STATUS_KEY: False, + CHARGER_ECO_SMART_MODE_KEY: 0, + }, + }, +} + +test_response_bidir = { + CHARGER_CHARGING_POWER_KEY: 0, + CHARGER_STATUS_ID_KEY: 193, + CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, + CHARGER_CHARGING_SPEED_KEY: 0, + CHARGER_ADDED_RANGE_KEY: 150, + CHARGER_ADDED_ENERGY_KEY: 44.697, + CHARGER_NAME_KEY: "WallboxName", + CHARGER_DATA_KEY: { + CHARGER_MAX_CHARGING_CURRENT_KEY: 24, + CHARGER_ENERGY_PRICE_KEY: 0.4, + CHARGER_LOCKED_UNLOCKED_KEY: False, + CHARGER_SERIAL_NUMBER_KEY: "20000", + CHARGER_PART_NUMBER_KEY: "QSP1-0-2-4-9-002-E", + CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, + CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, + CHARGER_ECO_SMART_KEY: { + CHARGER_ECO_SMART_STATUS_KEY: False, + CHARGER_ECO_SMART_MODE_KEY: 0, + }, + }, +} + +test_response_eco_mode = { + CHARGER_CHARGING_POWER_KEY: 0, + CHARGER_STATUS_ID_KEY: 193, + CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, + CHARGER_CHARGING_SPEED_KEY: 0, + CHARGER_ADDED_RANGE_KEY: 150, + CHARGER_ADDED_ENERGY_KEY: 44.697, + CHARGER_NAME_KEY: "WallboxName", + CHARGER_DATA_KEY: { + CHARGER_MAX_CHARGING_CURRENT_KEY: 24, + CHARGER_ENERGY_PRICE_KEY: 0.4, + CHARGER_LOCKED_UNLOCKED_KEY: False, + CHARGER_SERIAL_NUMBER_KEY: "20000", + CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", + CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, + CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, + CHARGER_ECO_SMART_KEY: { + CHARGER_ECO_SMART_STATUS_KEY: True, + CHARGER_ECO_SMART_MODE_KEY: 0, + }, + }, +} + + +test_response_full_solar = { + CHARGER_CHARGING_POWER_KEY: 0, + CHARGER_STATUS_ID_KEY: 193, + CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, + CHARGER_CHARGING_SPEED_KEY: 0, + CHARGER_ADDED_RANGE_KEY: 150, + CHARGER_ADDED_ENERGY_KEY: 44.697, + CHARGER_NAME_KEY: "WallboxName", + CHARGER_DATA_KEY: { + CHARGER_MAX_CHARGING_CURRENT_KEY: 24, + CHARGER_ENERGY_PRICE_KEY: 0.4, + CHARGER_LOCKED_UNLOCKED_KEY: False, + CHARGER_SERIAL_NUMBER_KEY: "20000", + CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", + CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, + CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, + CHARGER_ECO_SMART_KEY: { + CHARGER_ECO_SMART_STATUS_KEY: True, + CHARGER_ECO_SMART_MODE_KEY: 1, + }, + }, +} + +test_response_no_power_boost = { + CHARGER_CHARGING_POWER_KEY: 0, + CHARGER_STATUS_ID_KEY: 193, + CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, + CHARGER_CHARGING_SPEED_KEY: 0, + CHARGER_ADDED_RANGE_KEY: 150, + CHARGER_ADDED_ENERGY_KEY: 44.697, + CHARGER_NAME_KEY: "WallboxName", + CHARGER_DATA_KEY: { + CHARGER_MAX_CHARGING_CURRENT_KEY: 24, + CHARGER_ENERGY_PRICE_KEY: 0.4, + CHARGER_LOCKED_UNLOCKED_KEY: False, + CHARGER_SERIAL_NUMBER_KEY: "20000", + CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", + CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, + CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: []}, + }, +} + + +http_403_error = requests.exceptions.HTTPError() +http_403_error.response = requests.Response() +http_403_error.response.status_code = HTTPStatus.FORBIDDEN +http_404_error = requests.exceptions.HTTPError() +http_404_error.response = requests.Response() +http_404_error.response.status_code = HTTPStatus.NOT_FOUND +http_429_error = requests.exceptions.HTTPError() +http_429_error.response = requests.Response() +http_429_error.response.status_code = HTTPStatus.TOO_MANY_REQUESTS + +authorisation_response = { + "data": { + "attributes": { + "token": "fakekeyhere", + "refresh_token": "refresh_fakekeyhere", + USER_ID: 12345, + TTL: 145656758, + REFRESH_TOKEN_TTL: 145756758, + ERROR: "false", + STATUS: 200, + } + } +} + + +authorisation_response_unauthorised = { + "data": { + "attributes": { + "token": "fakekeyhere", + "refresh_token": "refresh_fakekeyhere", + USER_ID: 12345, + TTL: 145656758, + REFRESH_TOKEN_TTL: 145756758, + ERROR: "false", + STATUS: 404, + } + } +} + +invalid_reauth_response = { + "jwt": "fakekeyhere", + "refresh_token": "refresh_fakekeyhere", + "user_id": 12345, + "ttl": 145656758, + "refresh_token_ttl": 145756758, + "error": False, + "status": 200, +} + @pytest.fixture def entry(hass: HomeAssistant) -> MockConfigEntry: @@ -23,3 +230,46 @@ def entry(hass: HomeAssistant) -> MockConfigEntry: ) entry.add_to_hass(hass) return entry + + +@pytest.fixture +def mock_wallbox(): + """Patch Wallbox class for tests.""" + with patch("homeassistant.components.wallbox.Wallbox") as mock: + wallbox = MagicMock() + wallbox.authenticate = Mock(return_value=authorisation_response) + wallbox.lockCharger = Mock( + return_value={ + CHARGER_DATA_POST_L1_KEY: { + CHARGER_DATA_POST_L2_KEY: {CHARGER_LOCKED_UNLOCKED_KEY: True} + } + } + ) + wallbox.unlockCharger = Mock( + return_value={ + CHARGER_DATA_POST_L1_KEY: { + CHARGER_DATA_POST_L2_KEY: {CHARGER_LOCKED_UNLOCKED_KEY: True} + } + } + ) + wallbox.setEnergyCost = Mock(return_value={CHARGER_ENERGY_PRICE_KEY: 0.25}) + wallbox.setMaxChargingCurrent = Mock( + return_value={ + CHARGER_DATA_POST_L1_KEY: { + CHARGER_DATA_POST_L2_KEY: { + CHARGER_MAX_CHARGING_CURRENT_POST_KEY: True + } + } + } + ) + wallbox.setIcpMaxCurrent = Mock(return_value={CHARGER_MAX_ICP_CURRENT_KEY: 25}) + wallbox.getChargerStatus = Mock(return_value=test_response) + mock.return_value = wallbox + yield wallbox + + +async def setup_integration(hass: HomeAssistant, entry: MockConfigEntry) -> None: + """Test wallbox sensor class setup.""" + + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/wallbox/test_config_flow.py b/tests/components/wallbox/test_config_flow.py index bdfb4cad18d..d0c34ae0bce 100644 --- a/tests/components/wallbox/test_config_flow.py +++ b/tests/components/wallbox/test_config_flow.py @@ -18,7 +18,7 @@ from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from . import ( +from .conftest import ( authorisation_response, authorisation_response_unauthorised, http_403_error, @@ -38,7 +38,7 @@ test_response = { } -async def test_show_set_form(hass: HomeAssistant) -> None: +async def test_show_set_form(hass: HomeAssistant, mock_wallbox) -> None: """Test that the setup form is served.""" flow = config_flow.WallboxConfigFlow() flow.hass = hass @@ -53,7 +53,6 @@ async def test_form_cannot_authenticate(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with ( patch( "homeassistant.components.wallbox.Wallbox.authenticate", @@ -73,8 +72,8 @@ async def test_form_cannot_authenticate(hass: HomeAssistant) -> None: }, ) - assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "invalid_auth"} + assert result2["type"] is FlowResultType.FORM + assert result2["errors"] == {"base": "invalid_auth"} async def test_form_cannot_connect(hass: HomeAssistant) -> None: @@ -82,7 +81,6 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with ( patch( "homeassistant.components.wallbox.Wallbox.authenticate", @@ -102,8 +100,8 @@ async def test_form_cannot_connect(hass: HomeAssistant) -> None: }, ) - assert result2["type"] is FlowResultType.FORM - assert result2["errors"] == {"base": "cannot_connect"} + assert result2["type"] is FlowResultType.FORM + assert result2["errors"] == {"base": "cannot_connect"} async def test_form_validate_input(hass: HomeAssistant) -> None: @@ -111,15 +109,14 @@ async def test_form_validate_input(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) - with ( patch( "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), + return_value=authorisation_response, ), patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=test_response), + "homeassistant.components.wallbox.Wallbox.pauseChargingSession", + return_value=test_response, ), ): result2 = await hass.config_entries.flow.async_configure( @@ -135,20 +132,20 @@ async def test_form_validate_input(hass: HomeAssistant) -> None: assert result2["data"]["station"] == "12345" -async def test_form_reauth(hass: HomeAssistant, entry: MockConfigEntry) -> None: +async def test_form_reauth( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox +) -> None: """Test we handle reauth flow.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response_unauthorised), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=test_response), + patch.object( + mock_wallbox, + "authenticate", + return_value=authorisation_response_unauthorised, ), + patch.object(mock_wallbox, "getChargerStatus", return_value=test_response), ): result = await entry.start_reauth_flow(hass) @@ -161,27 +158,27 @@ async def test_form_reauth(hass: HomeAssistant, entry: MockConfigEntry) -> None: }, ) - assert result2["type"] is FlowResultType.ABORT - assert result2["reason"] == "reauth_successful" + assert result2["type"] is FlowResultType.ABORT + assert result2["reason"] == "reauth_successful" await hass.async_block_till_done() await hass.config_entries.async_unload(entry.entry_id) -async def test_form_reauth_invalid(hass: HomeAssistant, entry: MockConfigEntry) -> None: +async def test_form_reauth_invalid( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox +) -> None: """Test we handle reauth invalid flow.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response_unauthorised), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=test_response), + patch.object( + mock_wallbox, + "authenticate", + return_value=authorisation_response_unauthorised, ), + patch.object(mock_wallbox, "getChargerStatus", return_value=test_response), ): result = await entry.start_reauth_flow(hass) diff --git a/tests/components/wallbox/test_init.py b/tests/components/wallbox/test_init.py index 5048385aaf6..ef73decea8f 100644 --- a/tests/components/wallbox/test_init.py +++ b/tests/components/wallbox/test_init.py @@ -1,27 +1,23 @@ """Test Wallbox Init Component.""" -from unittest.mock import Mock, patch +from unittest.mock import patch from homeassistant.components.wallbox.const import DOMAIN from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant -from . import ( - authorisation_response, +from .conftest import ( http_403_error, http_429_error, setup_integration, - setup_integration_connection_error, - setup_integration_no_eco_mode, - setup_integration_read_only, - test_response, + test_response_no_power_boost, ) from tests.common import MockConfigEntry async def test_wallbox_setup_unload_entry( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox Unload.""" @@ -33,37 +29,27 @@ async def test_wallbox_setup_unload_entry( async def test_wallbox_unload_entry_connection_error( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox Unload Connection Error.""" + with patch.object(mock_wallbox, "authenticate", side_effect=http_403_error): + await setup_integration(hass, entry) + assert entry.state is ConfigEntryState.SETUP_ERROR - await setup_integration_connection_error(hass, entry) - assert entry.state is ConfigEntryState.SETUP_ERROR - - assert await hass.config_entries.async_unload(entry.entry_id) - assert entry.state is ConfigEntryState.NOT_LOADED + assert await hass.config_entries.async_unload(entry.entry_id) + assert entry.state is ConfigEntryState.NOT_LOADED async def test_wallbox_refresh_failed_connection_error_auth( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with connection error.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(side_effect=http_429_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.pauseChargingSession", - new=Mock(return_value=test_response), - ), - ): + with patch.object(mock_wallbox, "authenticate", side_effect=http_429_error): wallbox = hass.data[DOMAIN][entry.entry_id] - await wallbox.async_refresh() assert await hass.config_entries.async_unload(entry.entry_id) @@ -71,7 +57,7 @@ async def test_wallbox_refresh_failed_connection_error_auth( async def test_wallbox_refresh_failed_invalid_auth( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with authentication error.""" @@ -79,14 +65,8 @@ async def test_wallbox_refresh_failed_invalid_auth( assert entry.state is ConfigEntryState.LOADED with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(side_effect=http_403_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.pauseChargingSession", - new=Mock(side_effect=http_403_error), - ), + patch.object(mock_wallbox, "authenticate", side_effect=http_403_error), + patch.object(mock_wallbox, "pauseChargingSession", side_effect=http_403_error), ): wallbox = hass.data[DOMAIN][entry.entry_id] @@ -97,23 +77,14 @@ async def test_wallbox_refresh_failed_invalid_auth( async def test_wallbox_refresh_failed_http_error( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with authentication error.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(side_effect=http_403_error), - ), - ): + with patch.object(mock_wallbox, "getChargerStatus", side_effect=http_403_error): wallbox = hass.data[DOMAIN][entry.entry_id] await wallbox.async_refresh() @@ -123,23 +94,14 @@ async def test_wallbox_refresh_failed_http_error( async def test_wallbox_refresh_failed_too_many_requests( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with authentication error.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(side_effect=http_429_error), - ), - ): + with patch.object(mock_wallbox, "getChargerStatus", side_effect=http_429_error): wallbox = hass.data[DOMAIN][entry.entry_id] await wallbox.async_refresh() @@ -149,23 +111,14 @@ async def test_wallbox_refresh_failed_too_many_requests( async def test_wallbox_refresh_failed_connection_error( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with connection error.""" await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.pauseChargingSession", - new=Mock(side_effect=http_403_error), - ), - ): + with patch.object(mock_wallbox, "pauseChargingSession", side_effect=http_403_error): wallbox = hass.data[DOMAIN][entry.entry_id] await wallbox.async_refresh() @@ -174,25 +127,15 @@ async def test_wallbox_refresh_failed_connection_error( assert entry.state is ConfigEntryState.NOT_LOADED -async def test_wallbox_refresh_failed_read_only( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test Wallbox setup for read-only user.""" - - await setup_integration_read_only(hass, entry) - assert entry.state is ConfigEntryState.LOADED - - assert await hass.config_entries.async_unload(entry.entry_id) - assert entry.state is ConfigEntryState.NOT_LOADED - - async def test_wallbox_setup_load_entry_no_eco_mode( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox Unload.""" + with patch.object( + mock_wallbox, "getChargerStatus", return_value=test_response_no_power_boost + ): + await setup_integration(hass, entry) + assert entry.state is ConfigEntryState.LOADED - await setup_integration_no_eco_mode(hass, entry) - assert entry.state is ConfigEntryState.LOADED - - assert await hass.config_entries.async_unload(entry.entry_id) - assert entry.state is ConfigEntryState.NOT_LOADED + assert await hass.config_entries.async_unload(entry.entry_id) + assert entry.state is ConfigEntryState.NOT_LOADED diff --git a/tests/components/wallbox/test_lock.py b/tests/components/wallbox/test_lock.py index 7d95aed7a5d..e3c6048e928 100644 --- a/tests/components/wallbox/test_lock.py +++ b/tests/components/wallbox/test_lock.py @@ -1,28 +1,24 @@ """Test Wallbox Lock component.""" -from unittest.mock import Mock, patch +from unittest.mock import patch import pytest from homeassistant.components.lock import SERVICE_LOCK, SERVICE_UNLOCK -from homeassistant.components.wallbox.const import CHARGER_LOCKED_UNLOCKED_KEY +from homeassistant.components.wallbox.coordinator import InvalidAuth from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from . import ( - authorisation_response, - http_403_error, - http_404_error, - http_429_error, - setup_integration, -) +from .conftest import http_403_error, http_404_error, http_429_error, setup_integration from .const import MOCK_LOCK_ENTITY_ID from tests.common import MockConfigEntry -async def test_wallbox_lock_class(hass: HomeAssistant, entry: MockConfigEntry) -> None: +async def test_wallbox_lock_class( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox +) -> None: """Test wallbox lock class.""" await setup_integration(hass, entry) @@ -31,60 +27,35 @@ async def test_wallbox_lock_class(hass: HomeAssistant, entry: MockConfigEntry) - assert state assert state.state == "unlocked" - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.lockCharger", - new=Mock( - return_value={"data": {"chargerData": {CHARGER_LOCKED_UNLOCKED_KEY: 1}}} - ), - ), - patch( - "homeassistant.components.wallbox.Wallbox.unlockCharger", - new=Mock( - return_value={"data": {"chargerData": {CHARGER_LOCKED_UNLOCKED_KEY: 0}}} - ), - ), - ): - await hass.services.async_call( - "lock", - SERVICE_LOCK, - { - ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, - }, - blocking=True, - ) + await hass.services.async_call( + "lock", + SERVICE_LOCK, + { + ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, + }, + blocking=True, + ) - await hass.services.async_call( - "lock", - SERVICE_UNLOCK, - { - ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, - }, - blocking=True, - ) + await hass.services.async_call( + "lock", + SERVICE_UNLOCK, + { + ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, + }, + blocking=True, + ) -async def test_wallbox_lock_class_connection_error( - hass: HomeAssistant, entry: MockConfigEntry +async def test_wallbox_lock_class_error_handling( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox lock class connection error.""" await setup_integration(hass, entry) with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.lockCharger", - new=Mock(side_effect=ConnectionError), - ), - pytest.raises(ConnectionError), + patch.object(mock_wallbox, "lockCharger", side_effect=http_404_error), + pytest.raises(HomeAssistantError), ): await hass.services.async_call( "lock", @@ -96,42 +67,8 @@ async def test_wallbox_lock_class_connection_error( ) with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.lockCharger", - new=Mock(side_effect=ConnectionError), - ), - patch( - "homeassistant.components.wallbox.Wallbox.unlockCharger", - new=Mock(side_effect=ConnectionError), - ), - pytest.raises(ConnectionError), - ): - await hass.services.async_call( - "lock", - SERVICE_UNLOCK, - { - ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, - }, - blocking=True, - ) - - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.lockCharger", - new=Mock(side_effect=http_429_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.unlockCharger", - new=Mock(side_effect=http_429_error), - ), + patch.object(mock_wallbox, "lockCharger", side_effect=http_404_error), + patch.object(mock_wallbox, "unlockCharger", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -143,18 +80,8 @@ async def test_wallbox_lock_class_connection_error( blocking=True, ) with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.lockCharger", - new=Mock(side_effect=http_403_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.unlockCharger", - new=Mock(side_effect=http_403_error), - ), + patch.object(mock_wallbox, "lockCharger", side_effect=http_404_error), + patch.object(mock_wallbox, "unlockCharger", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -165,19 +92,24 @@ async def test_wallbox_lock_class_connection_error( }, blocking=True, ) + with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.lockCharger", - new=Mock(side_effect=http_404_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.unlockCharger", - new=Mock(side_effect=http_404_error), - ), + patch.object(mock_wallbox, "lockCharger", side_effect=http_403_error), + patch.object(mock_wallbox, "unlockCharger", side_effect=http_403_error), + pytest.raises(InvalidAuth), + ): + await hass.services.async_call( + "lock", + SERVICE_UNLOCK, + { + ATTR_ENTITY_ID: MOCK_LOCK_ENTITY_ID, + }, + blocking=True, + ) + + with ( + patch.object(mock_wallbox, "lockCharger", side_effect=http_429_error), + patch.object(mock_wallbox, "unlockCharger", side_effect=http_429_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( diff --git a/tests/components/wallbox/test_number.py b/tests/components/wallbox/test_number.py index 8067917977d..3aba0792baa 100644 --- a/tests/components/wallbox/test_number.py +++ b/tests/components/wallbox/test_number.py @@ -1,28 +1,22 @@ """Test Wallbox Switch component.""" -from unittest.mock import Mock, patch +from unittest.mock import patch import pytest from homeassistant.components.input_number import ATTR_VALUE, SERVICE_SET_VALUE from homeassistant.components.number import DOMAIN as NUMBER_DOMAIN -from homeassistant.components.wallbox.const import ( - CHARGER_ENERGY_PRICE_KEY, - CHARGER_MAX_CHARGING_CURRENT_KEY, - CHARGER_MAX_ICP_CURRENT_KEY, -) from homeassistant.components.wallbox.coordinator import InvalidAuth from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from . import ( - authorisation_response, +from .conftest import ( http_403_error, http_404_error, http_429_error, setup_integration, - setup_integration_bidir, + test_response_bidir, ) from .const import ( MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, @@ -32,105 +26,87 @@ from .const import ( from tests.common import MockConfigEntry -mock_wallbox = Mock() -mock_wallbox.authenticate = Mock(return_value=authorisation_response) -mock_wallbox.setEnergyCost = Mock(return_value={CHARGER_ENERGY_PRICE_KEY: 1.1}) -mock_wallbox.setMaxChargingCurrent = Mock( - return_value={CHARGER_MAX_CHARGING_CURRENT_KEY: 20} -) -mock_wallbox.setIcpMaxCurrent = Mock(return_value={CHARGER_MAX_ICP_CURRENT_KEY: 10}) - -async def test_wallbox_number_class( - hass: HomeAssistant, entry: MockConfigEntry +async def test_wallbox_number_power_class( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox sensor class.""" - await setup_integration(hass, entry) - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setMaxChargingCurrent", - new=Mock( - return_value={"data": {"chargerData": {"maxChargingCurrent": 20}}} - ), - ), - ): - state = hass.states.get(MOCK_NUMBER_ENTITY_ID) - assert state.attributes["min"] == 6 - assert state.attributes["max"] == 25 + state = hass.states.get(MOCK_NUMBER_ENTITY_ID) + assert state.attributes["min"] == 6 + assert state.attributes["max"] == 25 - await hass.services.async_call( - "number", - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ID, - ATTR_VALUE: 20, - }, - blocking=True, - ) + await hass.services.async_call( + "number", + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ID, + ATTR_VALUE: 20, + }, + blocking=True, + ) -async def test_wallbox_number_class_bidir( - hass: HomeAssistant, entry: MockConfigEntry +async def test_wallbox_number_power_class_bidir( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox sensor class.""" + with patch.object( + mock_wallbox, "getChargerStatus", return_value=test_response_bidir + ): + await setup_integration(hass, entry) - await setup_integration_bidir(hass, entry) - - state = hass.states.get(MOCK_NUMBER_ENTITY_ID) - assert state.attributes["min"] == -25 - assert state.attributes["max"] == 25 + state = hass.states.get(MOCK_NUMBER_ENTITY_ID) + assert state.attributes["min"] == -25 + assert state.attributes["max"] == 25 async def test_wallbox_number_energy_class( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox +) -> None: + """Test wallbox sensor class.""" + + await setup_integration(hass, entry) + + await hass.services.async_call( + "number", + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, + ATTR_VALUE: 1.1, + }, + blocking=True, + ) + + +async def test_wallbox_number_icp_power_class( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox +) -> None: + """Test wallbox sensor class.""" + + await setup_integration(hass, entry) + + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID, + ATTR_VALUE: 10, + }, + blocking=True, + ) + + +async def test_wallbox_number_power_class_error_handling( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox sensor class.""" await setup_integration(hass, entry) with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setEnergyCost", - new=Mock(return_value={CHARGER_ENERGY_PRICE_KEY: 1.1}), - ), - ): - await hass.services.async_call( - "number", - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, - ATTR_VALUE: 1.1, - }, - blocking=True, - ) - - -async def test_wallbox_number_class_connection_error( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class.""" - - await setup_integration(hass, entry) - - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setMaxChargingCurrent", - new=Mock(side_effect=http_404_error), - ), + patch.object(mock_wallbox, "setMaxChargingCurrent", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -143,23 +119,8 @@ async def test_wallbox_number_class_connection_error( blocking=True, ) - -async def test_wallbox_number_class_too_many_requests( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class.""" - - await setup_integration(hass, entry) - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setMaxChargingCurrent", - new=Mock(side_effect=http_429_error), - ), + patch.object(mock_wallbox, "setMaxChargingCurrent", side_effect=http_429_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -172,167 +133,8 @@ async def test_wallbox_number_class_too_many_requests( blocking=True, ) - -async def test_wallbox_number_class_energy_price_update_failed( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class.""" - - await setup_integration(hass, entry) - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setEnergyCost", - new=Mock(side_effect=http_429_error), - ), - pytest.raises(HomeAssistantError), - ): - await hass.services.async_call( - "number", - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, - ATTR_VALUE: 1.1, - }, - blocking=True, - ) - - -async def test_wallbox_number_class_energy_price_update_connection_error( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class.""" - - await setup_integration(hass, entry) - - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setEnergyCost", - new=Mock(side_effect=http_404_error), - ), - pytest.raises(HomeAssistantError), - ): - await hass.services.async_call( - "number", - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, - ATTR_VALUE: 1.1, - }, - blocking=True, - ) - - -async def test_wallbox_number_class_energy_price_auth_error( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class.""" - - await setup_integration(hass, entry) - - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setEnergyCost", - new=Mock(side_effect=http_429_error), - ), - pytest.raises(HomeAssistantError), - ): - await hass.services.async_call( - "number", - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, - ATTR_VALUE: 1.1, - }, - blocking=True, - ) - - -async def test_wallbox_number_class_icp_energy( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class.""" - - await setup_integration(hass, entry) - - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setIcpMaxCurrent", - new=Mock(return_value={"icp_max_current": 20}), - ), - ): - await hass.services.async_call( - NUMBER_DOMAIN, - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID, - ATTR_VALUE: 10, - }, - blocking=True, - ) - - -async def test_wallbox_number_class_icp_energy_auth_error( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class.""" - - await setup_integration(hass, entry) - - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setIcpMaxCurrent", - new=Mock(side_effect=http_403_error), - ), - pytest.raises(InvalidAuth), - ): - await hass.services.async_call( - NUMBER_DOMAIN, - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID, - ATTR_VALUE: 10, - }, - blocking=True, - ) - - -async def test_wallbox_number_class_energy_auth_error( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class.""" - - await setup_integration(hass, entry) - - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setMaxChargingCurrent", - new=Mock(side_effect=http_403_error), - ), + patch.object(mock_wallbox, "setMaxChargingCurrent", side_effect=http_403_error), pytest.raises(InvalidAuth), ): await hass.services.async_call( @@ -346,22 +148,79 @@ async def test_wallbox_number_class_energy_auth_error( ) -async def test_wallbox_number_class_icp_energy_connection_error( - hass: HomeAssistant, entry: MockConfigEntry +async def test_wallbox_number_energy_class_error_handling( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox sensor class.""" await setup_integration(hass, entry) with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setIcpMaxCurrent", - new=Mock(side_effect=http_404_error), - ), + patch.object(mock_wallbox, "setEnergyCost", side_effect=http_429_error), + pytest.raises(HomeAssistantError), + ): + await hass.services.async_call( + "number", + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, + ATTR_VALUE: 1.1, + }, + blocking=True, + ) + + with ( + patch.object(mock_wallbox, "setEnergyCost", side_effect=http_404_error), + pytest.raises(HomeAssistantError), + ): + await hass.services.async_call( + "number", + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, + ATTR_VALUE: 1.1, + }, + blocking=True, + ) + + with ( + patch.object(mock_wallbox, "setEnergyCost", side_effect=http_429_error), + pytest.raises(HomeAssistantError), + ): + await hass.services.async_call( + "number", + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, + ATTR_VALUE: 1.1, + }, + blocking=True, + ) + + +async def test_wallbox_number_icp_power_class_error_handling( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox +) -> None: + """Test wallbox sensor class.""" + + await setup_integration(hass, entry) + + with ( + patch.object(mock_wallbox, "setIcpMaxCurrent", side_effect=http_403_error), + pytest.raises(InvalidAuth), + ): + await hass.services.async_call( + NUMBER_DOMAIN, + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ICP_CURRENT_ID, + ATTR_VALUE: 10, + }, + blocking=True, + ) + + with ( + patch.object(mock_wallbox, "setIcpMaxCurrent", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -374,23 +233,8 @@ async def test_wallbox_number_class_icp_energy_connection_error( blocking=True, ) - -async def test_wallbox_number_class_icp_energy_too_many_request( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox sensor class.""" - - await setup_integration(hass, entry) - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.setIcpMaxCurrent", - new=Mock(side_effect=http_429_error), - ), + patch.object(mock_wallbox, "setIcpMaxCurrent", side_effect=http_429_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( diff --git a/tests/components/wallbox/test_select.py b/tests/components/wallbox/test_select.py index e46347bfa5a..f194566dbae 100644 --- a/tests/components/wallbox/test_select.py +++ b/tests/components/wallbox/test_select.py @@ -1,6 +1,6 @@ """Test Wallbox Select component.""" -from unittest.mock import Mock, patch +from unittest.mock import patch import pytest @@ -9,15 +9,14 @@ from homeassistant.components.select import ( DOMAIN as SELECT_DOMAIN, SERVICE_SELECT_OPTION, ) -from homeassistant.components.wallbox.const import CHARGER_STATUS_ID_KEY, EcoSmartMode +from homeassistant.components.wallbox.const import EcoSmartMode from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant, HomeAssistantError -from . import ( - authorisation_response, +from .conftest import ( http_404_error, http_429_error, - setup_integration_select, + setup_integration, test_response, test_response_eco_mode, test_response_full_solar, @@ -34,44 +33,13 @@ TEST_OPTIONS = [ ] -@pytest.fixture -def mock_authenticate(): - """Fixture to patch Wallbox methods.""" - with patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ): - yield - - @pytest.mark.parametrize(("mode", "response"), TEST_OPTIONS) async def test_wallbox_select_solar_charging_class( - hass: HomeAssistant, entry: MockConfigEntry, mode, response, mock_authenticate + hass: HomeAssistant, entry: MockConfigEntry, mode, response, mock_wallbox ) -> None: """Test wallbox select class.""" - - if mode == EcoSmartMode.OFF: - response = test_response - elif mode == EcoSmartMode.ECO_MODE: - response = test_response_eco_mode - elif mode == EcoSmartMode.FULL_SOLAR: - response = test_response_full_solar - - with ( - patch( - "homeassistant.components.wallbox.Wallbox.enableEcoSmart", - new=Mock(return_value={CHARGER_STATUS_ID_KEY: 193}), - ), - patch( - "homeassistant.components.wallbox.Wallbox.disableEcoSmart", - new=Mock(return_value={CHARGER_STATUS_ID_KEY: 193}), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=response), - ), - ): - await setup_integration_select(hass, entry, response) + with patch.object(mock_wallbox, "getChargerStatus", return_value=response): + await setup_integration(hass, entry) await hass.services.async_call( SELECT_DOMAIN, @@ -88,43 +56,35 @@ async def test_wallbox_select_solar_charging_class( async def test_wallbox_select_no_power_boost_class( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox select class.""" - await setup_integration_select(hass, entry, test_response_no_power_boost) + with patch.object( + mock_wallbox, "getChargerStatus", return_value=test_response_no_power_boost + ): + await setup_integration(hass, entry) - state = hass.states.get(MOCK_SELECT_ENTITY_ID) - assert state is None + state = hass.states.get(MOCK_SELECT_ENTITY_ID) + assert state is None @pytest.mark.parametrize(("mode", "response"), TEST_OPTIONS) -@pytest.mark.parametrize("error", [http_404_error, ConnectionError]) async def test_wallbox_select_class_error( hass: HomeAssistant, entry: MockConfigEntry, mode, response, - error, - mock_authenticate, + mock_wallbox, ) -> None: """Test wallbox select class connection error.""" - await setup_integration_select(hass, entry, response) + await setup_integration(hass, entry) with ( - patch( - "homeassistant.components.wallbox.Wallbox.disableEcoSmart", - new=Mock(side_effect=error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.enableEcoSmart", - new=Mock(side_effect=error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=test_response_eco_mode), - ), + patch.object(mock_wallbox, "getChargerStatus", return_value=response), + patch.object(mock_wallbox, "disableEcoSmart", side_effect=http_404_error), + patch.object(mock_wallbox, "enableEcoSmart", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): await hass.services.async_call( @@ -144,25 +104,45 @@ async def test_wallbox_select_too_many_requests_error( entry: MockConfigEntry, mode, response, - mock_authenticate, + mock_wallbox, ) -> None: """Test wallbox select class connection error.""" - await setup_integration_select(hass, entry, response) + await setup_integration(hass, entry) with ( - patch( - "homeassistant.components.wallbox.Wallbox.disableEcoSmart", - new=Mock(side_effect=http_429_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.enableEcoSmart", - new=Mock(side_effect=http_429_error), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=test_response_eco_mode), - ), + patch.object(mock_wallbox, "getChargerStatus", return_value=response), + patch.object(mock_wallbox, "disableEcoSmart", side_effect=http_429_error), + patch.object(mock_wallbox, "enableEcoSmart", side_effect=http_429_error), + pytest.raises(HomeAssistantError), + ): + await hass.services.async_call( + SELECT_DOMAIN, + SERVICE_SELECT_OPTION, + { + ATTR_ENTITY_ID: MOCK_SELECT_ENTITY_ID, + ATTR_OPTION: mode, + }, + blocking=True, + ) + + +@pytest.mark.parametrize(("mode", "response"), TEST_OPTIONS) +async def test_wallbox_select_connection_error( + hass: HomeAssistant, + entry: MockConfigEntry, + mode, + response, + mock_wallbox, +) -> None: + """Test wallbox select class connection error.""" + + await setup_integration(hass, entry) + + with ( + patch.object(mock_wallbox, "getChargerStatus", return_value=response), + patch.object(mock_wallbox, "disableEcoSmart", side_effect=ConnectionError), + patch.object(mock_wallbox, "enableEcoSmart", side_effect=ConnectionError), pytest.raises(HomeAssistantError), ): await hass.services.async_call( diff --git a/tests/components/wallbox/test_sensor.py b/tests/components/wallbox/test_sensor.py index 69d0cc57340..7373b5e70bb 100644 --- a/tests/components/wallbox/test_sensor.py +++ b/tests/components/wallbox/test_sensor.py @@ -3,7 +3,7 @@ from homeassistant.const import CONF_UNIT_OF_MEASUREMENT, UnitOfPower from homeassistant.core import HomeAssistant -from . import setup_integration +from .conftest import setup_integration from .const import ( MOCK_SENSOR_CHARGING_POWER_ID, MOCK_SENSOR_CHARGING_SPEED_ID, @@ -14,7 +14,7 @@ from tests.common import MockConfigEntry async def test_wallbox_sensor_class( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox sensor class.""" diff --git a/tests/components/wallbox/test_switch.py b/tests/components/wallbox/test_switch.py index 98b87828f74..189ce59f55c 100644 --- a/tests/components/wallbox/test_switch.py +++ b/tests/components/wallbox/test_switch.py @@ -1,29 +1,22 @@ """Test Wallbox Lock component.""" -from unittest.mock import Mock, patch +from unittest.mock import patch import pytest from homeassistant.components.switch import SERVICE_TURN_OFF, SERVICE_TURN_ON -from homeassistant.components.wallbox.const import CHARGER_STATUS_ID_KEY from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from . import ( - authorisation_response, - http_404_error, - http_429_error, - setup_integration, - test_response, -) +from .conftest import http_404_error, http_429_error, setup_integration from .const import MOCK_SWITCH_ENTITY_ID from tests.common import MockConfigEntry async def test_wallbox_switch_class( - hass: HomeAssistant, entry: MockConfigEntry + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox switch class.""" @@ -33,59 +26,34 @@ async def test_wallbox_switch_class( assert state assert state.state == "on" - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.pauseChargingSession", - new=Mock(return_value={CHARGER_STATUS_ID_KEY: 193}), - ), - patch( - "homeassistant.components.wallbox.Wallbox.resumeChargingSession", - new=Mock(return_value={CHARGER_STATUS_ID_KEY: 193}), - ), - patch( - "homeassistant.components.wallbox.Wallbox.getChargerStatus", - new=Mock(return_value=test_response), - ), - ): - await hass.services.async_call( - "switch", - SERVICE_TURN_ON, - { - ATTR_ENTITY_ID: MOCK_SWITCH_ENTITY_ID, - }, - blocking=True, - ) + await hass.services.async_call( + "switch", + SERVICE_TURN_ON, + { + ATTR_ENTITY_ID: MOCK_SWITCH_ENTITY_ID, + }, + blocking=True, + ) - await hass.services.async_call( - "switch", - SERVICE_TURN_OFF, - { - ATTR_ENTITY_ID: MOCK_SWITCH_ENTITY_ID, - }, - blocking=True, - ) + await hass.services.async_call( + "switch", + SERVICE_TURN_OFF, + { + ATTR_ENTITY_ID: MOCK_SWITCH_ENTITY_ID, + }, + blocking=True, + ) -async def test_wallbox_switch_class_connection_error( - hass: HomeAssistant, entry: MockConfigEntry +async def test_wallbox_switch_class_error_handling( + hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test wallbox switch class connection error.""" await setup_integration(hass, entry) with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.resumeChargingSession", - new=Mock(side_effect=http_404_error), - ), + patch.object(mock_wallbox, "resumeChargingSession", side_effect=http_404_error), pytest.raises(HomeAssistantError), ): # Test behavior when a connection error occurs @@ -98,23 +66,8 @@ async def test_wallbox_switch_class_connection_error( blocking=True, ) - -async def test_wallbox_switch_class_too_many_requests( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test wallbox switch class connection error.""" - - await setup_integration(hass, entry) - with ( - patch( - "homeassistant.components.wallbox.Wallbox.authenticate", - new=Mock(return_value=authorisation_response), - ), - patch( - "homeassistant.components.wallbox.Wallbox.resumeChargingSession", - new=Mock(side_effect=http_429_error), - ), + patch.object(mock_wallbox, "resumeChargingSession", side_effect=http_429_error), pytest.raises(HomeAssistantError), ): # Test behavior when a connection error occurs From f5b51c6cf0732ff5288f9a4c9049a8bbd000e846 Mon Sep 17 00:00:00 2001 From: Wesley Vos <17592840+Wesley-Vos@users.noreply.github.com> Date: Fri, 4 Jul 2025 22:04:48 +0200 Subject: [PATCH 038/106] Add serial_numbers to device_info of inverters, encharge and enpower (#147964) --- homeassistant/components/enphase_envoy/binary_sensor.py | 2 ++ homeassistant/components/enphase_envoy/number.py | 1 + homeassistant/components/enphase_envoy/select.py | 1 + homeassistant/components/enphase_envoy/sensor.py | 3 +++ homeassistant/components/enphase_envoy/switch.py | 2 ++ .../enphase_envoy/snapshots/test_diagnostics.ambr | 8 ++++---- 6 files changed, 13 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/enphase_envoy/binary_sensor.py b/homeassistant/components/enphase_envoy/binary_sensor.py index dcffef8271b..2628406f56f 100644 --- a/homeassistant/components/enphase_envoy/binary_sensor.py +++ b/homeassistant/components/enphase_envoy/binary_sensor.py @@ -126,6 +126,7 @@ class EnvoyEnchargeBinarySensorEntity(EnvoyBaseBinarySensorEntity): name=f"Encharge {serial_number}", sw_version=str(encharge_inventory[self._serial_number].firmware_version), via_device=(DOMAIN, self.envoy_serial_num), + serial_number=serial_number, ) @property @@ -158,6 +159,7 @@ class EnvoyEnpowerBinarySensorEntity(EnvoyBaseBinarySensorEntity): name=f"Enpower {enpower.serial_number}", sw_version=str(enpower.firmware_version), via_device=(DOMAIN, self.envoy_serial_num), + serial_number=enpower.serial_number, ) @property diff --git a/homeassistant/components/enphase_envoy/number.py b/homeassistant/components/enphase_envoy/number.py index 91e93d9c59b..6e8e48d684b 100644 --- a/homeassistant/components/enphase_envoy/number.py +++ b/homeassistant/components/enphase_envoy/number.py @@ -165,6 +165,7 @@ class EnvoyStorageSettingsNumberEntity(EnvoyBaseEntity, NumberEntity): name=f"Enpower {self._serial_number}", sw_version=str(enpower.firmware_version), via_device=(DOMAIN, self.envoy_serial_num), + serial_number=self._serial_number, ) else: # If no enpower device assign numbers to Envoy itself diff --git a/homeassistant/components/enphase_envoy/select.py b/homeassistant/components/enphase_envoy/select.py index 42b47e5d793..358275942ca 100644 --- a/homeassistant/components/enphase_envoy/select.py +++ b/homeassistant/components/enphase_envoy/select.py @@ -223,6 +223,7 @@ class EnvoyStorageSettingsSelectEntity(EnvoyBaseEntity, SelectEntity): name=f"Enpower {self._serial_number}", sw_version=str(enpower.firmware_version), via_device=(DOMAIN, self.envoy_serial_num), + serial_number=self._serial_number, ) else: # If no enpower device assign selects to Envoy itself diff --git a/homeassistant/components/enphase_envoy/sensor.py b/homeassistant/components/enphase_envoy/sensor.py index c1088252618..63a2a09a6f5 100644 --- a/homeassistant/components/enphase_envoy/sensor.py +++ b/homeassistant/components/enphase_envoy/sensor.py @@ -1313,6 +1313,7 @@ class EnvoyInverterEntity(EnvoySensorBaseEntity): manufacturer="Enphase", model="Inverter", via_device=(DOMAIN, self.envoy_serial_num), + serial_number=serial_number, ) @property @@ -1356,6 +1357,7 @@ class EnvoyEnchargeEntity(EnvoySensorBaseEntity): name=f"Encharge {serial_number}", sw_version=str(encharge_inventory[self._serial_number].firmware_version), via_device=(DOMAIN, self.envoy_serial_num), + serial_number=serial_number, ) @@ -1420,6 +1422,7 @@ class EnvoyEnpowerEntity(EnvoySensorBaseEntity): name=f"Enpower {enpower_data.serial_number}", sw_version=str(enpower_data.firmware_version), via_device=(DOMAIN, self.envoy_serial_num), + serial_number=enpower_data.serial_number, ) @property diff --git a/homeassistant/components/enphase_envoy/switch.py b/homeassistant/components/enphase_envoy/switch.py index bb4ed874b1d..02736979e66 100644 --- a/homeassistant/components/enphase_envoy/switch.py +++ b/homeassistant/components/enphase_envoy/switch.py @@ -138,6 +138,7 @@ class EnvoyEnpowerSwitchEntity(EnvoyBaseEntity, SwitchEntity): name=f"Enpower {self._serial_number}", sw_version=str(enpower.firmware_version), via_device=(DOMAIN, self.envoy_serial_num), + serial_number=self._serial_number, ) @property @@ -235,6 +236,7 @@ class EnvoyStorageSettingsSwitchEntity(EnvoyBaseEntity, SwitchEntity): name=f"Enpower {self._serial_number}", sw_version=str(enpower.firmware_version), via_device=(DOMAIN, self.envoy_serial_num), + serial_number=self._serial_number, ) else: # If no enpower device assign switches to Envoy itself diff --git a/tests/components/enphase_envoy/snapshots/test_diagnostics.ambr b/tests/components/enphase_envoy/snapshots/test_diagnostics.ambr index 7ad45ff51f3..3a7f4e4fb9f 100644 --- a/tests/components/enphase_envoy/snapshots/test_diagnostics.ambr +++ b/tests/components/enphase_envoy/snapshots/test_diagnostics.ambr @@ -307,7 +307,7 @@ 'name': 'Inverter 1', 'name_by_user': None, 'primary_config_entry': '45a36e55aaddb2007c5f6602e0c38e72', - 'serial_number': None, + 'serial_number': '1', 'suggested_area': None, 'sw_version': None, }), @@ -1186,7 +1186,7 @@ 'name': 'Inverter 1', 'name_by_user': None, 'primary_config_entry': '45a36e55aaddb2007c5f6602e0c38e72', - 'serial_number': None, + 'serial_number': '1', 'suggested_area': None, 'sw_version': None, }), @@ -2109,7 +2109,7 @@ 'name': 'Inverter 1', 'name_by_user': None, 'primary_config_entry': '45a36e55aaddb2007c5f6602e0c38e72', - 'serial_number': None, + 'serial_number': '1', 'suggested_area': None, 'sw_version': None, }), @@ -2805,7 +2805,7 @@ 'name': 'Inverter 1', 'name_by_user': None, 'primary_config_entry': '45a36e55aaddb2007c5f6602e0c38e72', - 'serial_number': None, + 'serial_number': '1', 'suggested_area': None, 'sw_version': None, }), From 6e607ffa01bd57c1590a662d6bcca6eec97e7561 Mon Sep 17 00:00:00 2001 From: Sid <27780930+autinerd@users.noreply.github.com> Date: Fri, 4 Jul 2025 22:18:13 +0200 Subject: [PATCH 039/106] Add reconfigure flow to eheimdigital (#147930) --- .../components/eheimdigital/config_flow.py | 56 ++++++++++++- .../eheimdigital/quality_scale.yaml | 2 +- .../components/eheimdigital/strings.json | 12 ++- .../eheimdigital/test_config_flow.py | 81 ++++++++++++++++++- 4 files changed, 147 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/eheimdigital/config_flow.py b/homeassistant/components/eheimdigital/config_flow.py index b0432267c8e..09fbaa601b3 100644 --- a/homeassistant/components/eheimdigital/config_flow.py +++ b/homeassistant/components/eheimdigital/config_flow.py @@ -10,7 +10,12 @@ from eheimdigital.device import EheimDigitalDevice from eheimdigital.hub import EheimDigitalHub import voluptuous as vol -from homeassistant.config_entries import SOURCE_USER, ConfigFlow, ConfigFlowResult +from homeassistant.config_entries import ( + SOURCE_RECONFIGURE, + SOURCE_USER, + ConfigFlow, + ConfigFlowResult, +) from homeassistant.const import CONF_HOST from homeassistant.helpers import selector from homeassistant.helpers.aiohttp_client import async_get_clientsession @@ -126,3 +131,52 @@ class EheimDigitalConfigFlow(ConfigFlow, domain=DOMAIN): data_schema=CONFIG_SCHEMA, errors=errors, ) + + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle reconfiguration of the config entry.""" + if user_input is None: + return self.async_show_form( + step_id=SOURCE_RECONFIGURE, data_schema=CONFIG_SCHEMA + ) + + self._async_abort_entries_match(user_input) + errors: dict[str, str] = {} + hub = EheimDigitalHub( + host=user_input[CONF_HOST], + session=async_get_clientsession(self.hass), + loop=self.hass.loop, + main_device_added_event=self.main_device_added_event, + ) + + try: + await hub.connect() + + async with asyncio.timeout(2): + # This event gets triggered when the first message is received from + # the device, it contains the data necessary to create the main device. + # This removes the race condition where the main device is accessed + # before the response from the device is parsed. + await self.main_device_added_event.wait() + if TYPE_CHECKING: + # At this point the main device is always set + assert isinstance(hub.main, EheimDigitalDevice) + await self.async_set_unique_id(hub.main.mac_address) + await hub.close() + except (ClientError, TimeoutError): + errors["base"] = "cannot_connect" + except Exception: # noqa: BLE001 + errors["base"] = "unknown" + LOGGER.exception("Unknown exception occurred") + else: + self._abort_if_unique_id_mismatch() + return self.async_update_reload_and_abort( + self._get_reconfigure_entry(), + data_updates=user_input, + ) + return self.async_show_form( + step_id=SOURCE_RECONFIGURE, + data_schema=CONFIG_SCHEMA, + errors=errors, + ) diff --git a/homeassistant/components/eheimdigital/quality_scale.yaml b/homeassistant/components/eheimdigital/quality_scale.yaml index c1490b352c2..801e0748310 100644 --- a/homeassistant/components/eheimdigital/quality_scale.yaml +++ b/homeassistant/components/eheimdigital/quality_scale.yaml @@ -60,7 +60,7 @@ rules: entity-translations: done exception-translations: done icon-translations: todo - reconfiguration-flow: todo + reconfiguration-flow: done repair-issues: todo stale-devices: done diff --git a/homeassistant/components/eheimdigital/strings.json b/homeassistant/components/eheimdigital/strings.json index 77cffb4a709..c629ff622cb 100644 --- a/homeassistant/components/eheimdigital/strings.json +++ b/homeassistant/components/eheimdigital/strings.json @@ -4,6 +4,14 @@ "discovery_confirm": { "description": "[%key:common::config_flow::description::confirm_setup%]" }, + "reconfigure": { + "data": { + "host": "[%key:common::config_flow::data::host%]" + }, + "data_description": { + "host": "[%key:component::eheimdigital::config::step::user::data_description::host%]" + } + }, "user": { "data": { "host": "[%key:common::config_flow::data::host%]" @@ -15,7 +23,9 @@ }, "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", - "already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]" + "already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]", + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]", + "unique_id_mismatch": "The identifier does not match the previous identifier" }, "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", diff --git a/tests/components/eheimdigital/test_config_flow.py b/tests/components/eheimdigital/test_config_flow.py index 4bfd45e9259..53c036c802d 100644 --- a/tests/components/eheimdigital/test_config_flow.py +++ b/tests/components/eheimdigital/test_config_flow.py @@ -7,12 +7,20 @@ from aiohttp import ClientConnectionError import pytest from homeassistant.components.eheimdigital.const import DOMAIN -from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF +from homeassistant.config_entries import ( + SOURCE_RECONFIGURE, + SOURCE_USER, + SOURCE_ZEROCONF, +) from homeassistant.const import CONF_HOST from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo +from .conftest import init_integration + +from tests.common import MockConfigEntry + ZEROCONF_DISCOVERY = ZeroconfServiceInfo( ip_address=ip_address("192.0.2.1"), ip_addresses=[ip_address("192.0.2.1")], @@ -210,3 +218,74 @@ async def test_abort(hass: HomeAssistant, eheimdigital_hub_mock: AsyncMock) -> N assert result2["type"] is FlowResultType.ABORT assert result2["reason"] == "already_configured" + + +@patch("homeassistant.components.eheimdigital.config_flow.asyncio.Event", new=AsyncMock) +@pytest.mark.parametrize( + ("side_effect", "error_value"), + [(ClientConnectionError(), "cannot_connect"), (Exception(), "unknown")], +) +async def test_reconfigure( + hass: HomeAssistant, + eheimdigital_hub_mock: AsyncMock, + mock_config_entry: MockConfigEntry, + side_effect: Exception, + error_value: str, +) -> None: + """Test reconfigure flow.""" + await init_integration(hass, mock_config_entry) + + result = await mock_config_entry.start_reconfigure_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == SOURCE_RECONFIGURE + + eheimdigital_hub_mock.return_value.connect.side_effect = side_effect + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + USER_INPUT, + ) + await hass.async_block_till_done() + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": error_value} + + eheimdigital_hub_mock.return_value.connect.side_effect = None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + USER_INPUT, + ) + await hass.async_block_till_done() + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reconfigure_successful" + assert ( + mock_config_entry.unique_id + == eheimdigital_hub_mock.return_value.main.mac_address + ) + + +@patch("homeassistant.components.eheimdigital.config_flow.asyncio.Event", new=AsyncMock) +async def test_reconfigure_different_device( + hass: HomeAssistant, + eheimdigital_hub_mock: AsyncMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test reconfigure flow.""" + + await init_integration(hass, mock_config_entry) + + # Simulate a different device + eheimdigital_hub_mock.return_value.main.mac_address = "00:00:00:00:00:02" + + result = await mock_config_entry.start_reconfigure_flow(hass) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == SOURCE_RECONFIGURE + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + USER_INPUT, + ) + + await hass.async_block_till_done() + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "unique_id_mismatch" From 470baa782e2122d14069b9aaf0e4cb6ba08fb970 Mon Sep 17 00:00:00 2001 From: Joakim Plate Date: Fri, 4 Jul 2025 22:24:40 +0200 Subject: [PATCH 040/106] Add zeroconf discovery to philips_js (#147913) Co-authored-by: Joost Lekkerkerker --- .../components/philips_js/config_flow.py | 118 ++++++++++++------ .../components/philips_js/manifest.json | 3 +- .../components/philips_js/strings.json | 5 + homeassistant/generated/zeroconf.py | 10 ++ tests/components/philips_js/__init__.py | 1 + tests/components/philips_js/conftest.py | 1 + .../components/philips_js/test_config_flow.py | 92 +++++++++++--- 7 files changed, 178 insertions(+), 52 deletions(-) diff --git a/homeassistant/components/philips_js/config_flow.py b/homeassistant/components/philips_js/config_flow.py index 66b4439acd8..a568d51e5ea 100644 --- a/homeassistant/components/philips_js/config_flow.py +++ b/homeassistant/components/philips_js/config_flow.py @@ -6,7 +6,13 @@ from collections.abc import Mapping import platform from typing import Any -from haphilipsjs import ConnectionFailure, PairingFailure, PhilipsTV +from haphilipsjs import ( + DEFAULT_API_VERSION, + ConnectionFailure, + GeneralFailure, + PairingFailure, + PhilipsTV, +) import voluptuous as vol from homeassistant.config_entries import ( @@ -18,16 +24,18 @@ from homeassistant.config_entries import ( from homeassistant.const import ( CONF_API_VERSION, CONF_HOST, + CONF_NAME, CONF_PASSWORD, CONF_PIN, CONF_USERNAME, ) -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import callback from homeassistant.helpers import selector from homeassistant.helpers.schema_config_entry_flow import ( SchemaFlowFormStep, SchemaOptionsFlowHandler, ) +from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo from . import LOGGER from .const import CONF_ALLOW_NOTIFY, CONF_SYSTEM, CONST_APP_ID, CONST_APP_NAME, DOMAIN @@ -54,21 +62,6 @@ OPTIONS_FLOW = { } -async def _validate_input( - hass: HomeAssistant, host: str, api_version: int -) -> PhilipsTV: - """Validate the user input allows us to connect.""" - hub = PhilipsTV(host, api_version) - - await hub.getSystem() - await hub.setTransport(hub.secured_transport) - - if not hub.system: - raise ConnectionFailure("System data is empty") - - return hub - - class PhilipsJSConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for Philips TV.""" @@ -81,6 +74,38 @@ class PhilipsJSConfigFlow(ConfigFlow, domain=DOMAIN): self._hub: PhilipsTV | None = None self._pair_state: Any = None + async def _async_attempt_prepare( + self, host: str, api_version: int, secured_transport: bool + ) -> None: + hub = PhilipsTV( + host, api_version=api_version, secured_transport=secured_transport + ) + + await hub.getSystem() + await hub.setTransport(hub.secured_transport) + + if not hub.system or not hub.name: + raise ConnectionFailure("System data or name is empty") + + self._hub = hub + self._current[CONF_HOST] = host + self._current[CONF_SYSTEM] = hub.system + self._current[CONF_API_VERSION] = hub.api_version + self.context.update({"title_placeholders": {CONF_NAME: hub.name}}) + + if serialnumber := hub.system.get("serialnumber"): + await self.async_set_unique_id(serialnumber) + if self.source != SOURCE_REAUTH: + self._abort_if_unique_id_configured( + updates=self._current, reload_on_update=True + ) + + async def _async_attempt_add(self) -> ConfigFlowResult: + assert self._hub + if self._hub.pairing_type == "digest_auth_pairing": + return await self.async_step_pair() + return await self._async_create_current() + async def _async_create_current(self) -> ConfigFlowResult: system = self._current[CONF_SYSTEM] if self.source == SOURCE_REAUTH: @@ -154,6 +179,43 @@ class PhilipsJSConfigFlow(ConfigFlow, domain=DOMAIN): self._current[CONF_API_VERSION] = entry_data[CONF_API_VERSION] return await self.async_step_user() + async def async_step_zeroconf( + self, discovery_info: ZeroconfServiceInfo + ) -> ConfigFlowResult: + """Handle zeroconf discovery.""" + + LOGGER.debug( + "Checking discovered device: {discovery_info.name} on {discovery_info.host}" + ) + + secured_transport = discovery_info.type == "_philipstv_s_rpc._tcp.local." + api_version = 6 if secured_transport else DEFAULT_API_VERSION + + try: + await self._async_attempt_prepare( + discovery_info.host, api_version, secured_transport + ) + except GeneralFailure: + LOGGER.debug("Failed to get system info from discovery", exc_info=True) + return self.async_abort(reason="discovery_failure") + + return await self.async_step_zeroconf_confirm() + + async def async_step_zeroconf_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle a flow initiated by zeroconf.""" + if user_input is not None: + return await self._async_attempt_add() + + name = self.context.get("title_placeholders", {CONF_NAME: "Philips TV"})[ + CONF_NAME + ] + return self.async_show_form( + step_id="zeroconf_confirm", + description_placeholders={CONF_NAME: name}, + ) + async def async_step_user( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: @@ -162,28 +224,14 @@ class PhilipsJSConfigFlow(ConfigFlow, domain=DOMAIN): if user_input: self._current = user_input try: - hub = await _validate_input( - self.hass, user_input[CONF_HOST], user_input[CONF_API_VERSION] + await self._async_attempt_prepare( + user_input[CONF_HOST], user_input[CONF_API_VERSION], False ) - except ConnectionFailure as exc: + except GeneralFailure as exc: LOGGER.error(exc) errors["base"] = "cannot_connect" - except Exception: # noqa: BLE001 - LOGGER.exception("Unexpected exception") - errors["base"] = "unknown" else: - if serialnumber := hub.system.get("serialnumber"): - await self.async_set_unique_id(serialnumber) - if self.source != SOURCE_REAUTH: - self._abort_if_unique_id_configured() - - self._current[CONF_SYSTEM] = hub.system - self._current[CONF_API_VERSION] = hub.api_version - self._hub = hub - - if hub.pairing_type == "digest_auth_pairing": - return await self.async_step_pair() - return await self._async_create_current() + return await self._async_attempt_add() schema = self.add_suggested_values_to_schema(USER_SCHEMA, self._current) return self.async_show_form(step_id="user", data_schema=schema, errors=errors) diff --git a/homeassistant/components/philips_js/manifest.json b/homeassistant/components/philips_js/manifest.json index bba9a1a8762..0e88d6d44a9 100644 --- a/homeassistant/components/philips_js/manifest.json +++ b/homeassistant/components/philips_js/manifest.json @@ -6,5 +6,6 @@ "documentation": "https://www.home-assistant.io/integrations/philips_js", "iot_class": "local_polling", "loggers": ["haphilipsjs"], - "requirements": ["ha-philipsjs==3.2.2"] + "requirements": ["ha-philipsjs==3.2.2"], + "zeroconf": ["_philipstv_s_rpc._tcp.local.", "_philipstv_rpc._tcp.local."] } diff --git a/homeassistant/components/philips_js/strings.json b/homeassistant/components/philips_js/strings.json index 1f187d89dda..6c5a1fcce0a 100644 --- a/homeassistant/components/philips_js/strings.json +++ b/homeassistant/components/philips_js/strings.json @@ -1,5 +1,6 @@ { "config": { + "flow_title": "{name}", "step": { "user": { "data": { @@ -7,6 +8,10 @@ "api_version": "API Version" } }, + "zeroconf_confirm": { + "title": "Discovered Philips TV", + "description": "Do you want to add the TV ({name}) to Home Assistant? It will turn on and a pairing code will be displayed on it that you will need to enter in the next screen." + }, "pair": { "title": "Pair", "description": "Enter the PIN displayed on your TV", diff --git a/homeassistant/generated/zeroconf.py b/homeassistant/generated/zeroconf.py index 3af4b8caa8d..274fafa51cf 100644 --- a/homeassistant/generated/zeroconf.py +++ b/homeassistant/generated/zeroconf.py @@ -771,6 +771,16 @@ ZEROCONF = { "domain": "onewire", }, ], + "_philipstv_rpc._tcp.local.": [ + { + "domain": "philips_js", + }, + ], + "_philipstv_s_rpc._tcp.local.": [ + { + "domain": "philips_js", + }, + ], "_plexmediasvr._tcp.local.": [ { "domain": "plex", diff --git a/tests/components/philips_js/__init__.py b/tests/components/philips_js/__init__.py index 60e8b238917..4703f3cb430 100644 --- a/tests/components/philips_js/__init__.py +++ b/tests/components/philips_js/__init__.py @@ -5,6 +5,7 @@ MOCK_NAME = "Philips TV" MOCK_USERNAME = "mock_user" MOCK_PASSWORD = "mock_password" +MOCK_HOSTNAME = "mock_hostname" MOCK_SYSTEM = { "menulanguage": "English", diff --git a/tests/components/philips_js/conftest.py b/tests/components/philips_js/conftest.py index 4a79fce85a2..911753a8852 100644 --- a/tests/components/philips_js/conftest.py +++ b/tests/components/philips_js/conftest.py @@ -38,6 +38,7 @@ def mock_tv(): tv.application = None tv.applications = {} tv.system = MOCK_SYSTEM + tv.name = MOCK_NAME tv.api_version = 1 tv.api_version_detected = None tv.on = True diff --git a/tests/components/philips_js/test_config_flow.py b/tests/components/philips_js/test_config_flow.py index 4b8048a8ebe..c4dcc44e619 100644 --- a/tests/components/philips_js/test_config_flow.py +++ b/tests/components/philips_js/test_config_flow.py @@ -1,5 +1,6 @@ """Test the Philips TV config flow.""" +from ipaddress import ip_address from unittest.mock import ANY from haphilipsjs import PairingFailure @@ -9,10 +10,13 @@ from homeassistant import config_entries from homeassistant.components.philips_js.const import CONF_ALLOW_NOTIFY, DOMAIN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType +from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo from . import ( MOCK_CONFIG, MOCK_CONFIG_PAIRED, + MOCK_HOSTNAME, + MOCK_NAME, MOCK_PASSWORD, MOCK_SYSTEM, MOCK_SYSTEM_UNPAIRED, @@ -33,6 +37,7 @@ async def mock_tv_pairable(mock_tv): mock_tv.api_version = 6 mock_tv.api_version_detected = 6 mock_tv.secured_transport = True + mock_tv.name = MOCK_NAME mock_tv.pairRequest.return_value = {} mock_tv.pairGrant.return_value = MOCK_USERNAME, MOCK_PASSWORD @@ -102,21 +107,6 @@ async def test_form_cannot_connect(hass: HomeAssistant, mock_tv) -> None: assert result["errors"] == {"base": "cannot_connect"} -async def test_form_unexpected_error(hass: HomeAssistant, mock_tv) -> None: - """Test we handle unexpected exceptions.""" - result = await hass.config_entries.flow.async_init( - DOMAIN, context={"source": config_entries.SOURCE_USER} - ) - - mock_tv.getSystem.side_effect = Exception("Unexpected exception") - result = await hass.config_entries.flow.async_configure( - result["flow_id"], MOCK_USERINPUT - ) - - assert result["type"] is FlowResultType.FORM - assert result["errors"] == {"base": "unknown"} - - async def test_pairing(hass: HomeAssistant, mock_tv_pairable, mock_setup_entry) -> None: """Test we get the form.""" mock_tv = mock_tv_pairable @@ -143,7 +133,13 @@ async def test_pairing(hass: HomeAssistant, mock_tv_pairable, mock_setup_entry) ) assert result == { - "context": {"source": "user", "unique_id": "ABCDEFGHIJKLF"}, + "context": { + "source": "user", + "unique_id": "ABCDEFGHIJKLF", + "title_placeholders": { + "name": "Philips TV", + }, + }, "flow_id": ANY, "type": "create_entry", "description": None, @@ -258,3 +254,67 @@ async def test_options_flow(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.CREATE_ENTRY assert config_entry.options == {CONF_ALLOW_NOTIFY: True} + + +@pytest.mark.parametrize( + ("secured_transport", "discovery_type"), + [(True, "_philipstv_s_rpc._tcp.local."), (False, "_philipstv_rpc._tcp.local.")], +) +async def test_zeroconf_discovery( + hass: HomeAssistant, mock_tv_pairable, secured_transport, discovery_type +) -> None: + """Test we can setup from zeroconf discovery.""" + + mock_tv_pairable.secured_transport = secured_transport + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_ZEROCONF}, + data=ZeroconfServiceInfo( + ip_address=ip_address("127.0.0.1"), + ip_addresses=[ip_address("127.0.0.1")], + hostname=MOCK_HOSTNAME, + name=MOCK_NAME, + port=None, + properties={}, + type=discovery_type, + ), + ) + assert result["type"] is FlowResultType.FORM + assert result["errors"] is None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {}, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + + mock_tv_pairable.setTransport.assert_called_with(secured_transport) + mock_tv_pairable.pairRequest.assert_called() + + +async def test_zeroconf_probe_failed( + hass: HomeAssistant, + mock_tv_pairable, +) -> None: + """Test we can setup from zeroconf discovery.""" + + mock_tv_pairable.system = None + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_ZEROCONF}, + data=ZeroconfServiceInfo( + ip_address=ip_address("127.0.0.1"), + ip_addresses=[ip_address("127.0.0.1")], + hostname=MOCK_HOSTNAME, + name=MOCK_NAME, + port=None, + properties={}, + type="_philipstv_s_rpc._tcp.local.", + ), + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "discovery_failure" From 6a7f4953cd9b0be3ee10369154a1f30d5c92be7f Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Fri, 4 Jul 2025 22:30:35 +0200 Subject: [PATCH 041/106] Fix media selector validation (#147855) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- homeassistant/helpers/selector.py | 13 +++++++------ tests/helpers/test_selector.py | 24 ++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/homeassistant/helpers/selector.py b/homeassistant/helpers/selector.py index 4fa31ee78a2..bc24113251c 100644 --- a/homeassistant/helpers/selector.py +++ b/homeassistant/helpers/selector.py @@ -1045,16 +1045,17 @@ class MediaSelector(Selector[MediaSelectorConfig]): def __call__(self, data: Any) -> dict[str, str]: """Validate the passed selection.""" - schema = self.DATA_SCHEMA.schema.copy() + schema = { + key: value + for key, value in self.DATA_SCHEMA.schema.items() + if key != "entity_id" + } - if "accept" in self.config: - # If accept is set, the entity_id field will not be present - schema.pop("entity_id", None) - else: + if "accept" not in self.config: # If accept is not set, the entity_id field is required schema[vol.Required("entity_id")] = cv.entity_id_or_uuid - media: dict[str, str] = self.DATA_SCHEMA(data) + media: dict[str, str] = vol.Schema(schema)(data) return media diff --git a/tests/helpers/test_selector.py b/tests/helpers/test_selector.py index dd8cd1c1b64..0e68992d0e4 100644 --- a/tests/helpers/test_selector.py +++ b/tests/helpers/test_selector.py @@ -842,7 +842,16 @@ def test_theme_selector_schema(schema, valid_selections, invalid_selections) -> "metadata": {}, }, ), - (None, "abc", {}), + ( + None, + "abc", + {}, + # We require entity_id when accept is not set + { + "media_content_id": "abc", + "media_content_type": "def", + }, + ), ), ( { @@ -859,7 +868,18 @@ def test_theme_selector_schema(schema, valid_selections, invalid_selections) -> "metadata": {}, }, ), - (None, "abc", {}), + ( + None, + "abc", + {}, + { + # We do not allow entity_id when accept is set + "entity_id": "sensor.abc", + "media_content_id": "abc", + "media_content_type": "def", + "metadata": {}, + }, + ), ), ], ) From c0368f24483c7f99e63b9dc150e3a3d509253d63 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 4 Jul 2025 22:31:11 +0200 Subject: [PATCH 042/106] Add weekdays to time trigger (#147505) Co-authored-by: Claude --- .../components/homeassistant/triggers/time.py | 21 +- .../homeassistant/triggers/test_time.py | 199 +++++++++++++++++- 2 files changed, 218 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homeassistant/triggers/time.py b/homeassistant/components/homeassistant/triggers/time.py index e07d806d3dc..27c63742f7b 100644 --- a/homeassistant/components/homeassistant/triggers/time.py +++ b/homeassistant/components/homeassistant/triggers/time.py @@ -16,6 +16,7 @@ from homeassistant.const import ( CONF_PLATFORM, STATE_UNAVAILABLE, STATE_UNKNOWN, + WEEKDAYS, ) from homeassistant.core import ( CALLBACK_TYPE, @@ -37,6 +38,8 @@ from homeassistant.helpers.trigger import TriggerActionType, TriggerInfo from homeassistant.helpers.typing import ConfigType from homeassistant.util import dt as dt_util +CONF_WEEKDAY = "weekday" + _TIME_TRIGGER_ENTITY = vol.All(str, cv.entity_domain(["input_datetime", "sensor"])) _TIME_AT_SCHEMA = vol.Any(cv.time, _TIME_TRIGGER_ENTITY) @@ -74,6 +77,10 @@ TRIGGER_SCHEMA = cv.TRIGGER_BASE_SCHEMA.extend( { vol.Required(CONF_PLATFORM): "time", vol.Required(CONF_AT): vol.All(cv.ensure_list, [_TIME_TRIGGER_SCHEMA]), + vol.Optional(CONF_WEEKDAY): vol.Any( + vol.In(WEEKDAYS), + vol.All(cv.ensure_list, [vol.In(WEEKDAYS)]), + ), } ) @@ -85,7 +92,7 @@ class TrackEntity(NamedTuple): callback: Callable -async def async_attach_trigger( +async def async_attach_trigger( # noqa: C901 hass: HomeAssistant, config: ConfigType, action: TriggerActionType, @@ -103,6 +110,18 @@ async def async_attach_trigger( description: str, now: datetime, *, entity_id: str | None = None ) -> None: """Listen for time changes and calls action.""" + # Check weekday filter if configured + if CONF_WEEKDAY in config: + weekday_config = config[CONF_WEEKDAY] + current_weekday = WEEKDAYS[now.weekday()] + + # Check if current weekday matches the configuration + if isinstance(weekday_config, str): + if current_weekday != weekday_config: + return + elif current_weekday not in weekday_config: + return + hass.async_run_hass_job( job, { diff --git a/tests/components/homeassistant/triggers/test_time.py b/tests/components/homeassistant/triggers/test_time.py index 9a4f41d08e1..dc9fb1d34c2 100644 --- a/tests/components/homeassistant/triggers/test_time.py +++ b/tests/components/homeassistant/triggers/test_time.py @@ -1,6 +1,6 @@ """The tests for the time automation.""" -from datetime import timedelta +from datetime import datetime, timedelta from unittest.mock import Mock, patch from freezegun.api import FrozenDateTimeFactory @@ -877,3 +877,200 @@ async def test_if_at_template_limited_template( await hass.async_block_till_done() assert "is not supported in limited templates" in caplog.text + + +async def test_if_fires_using_weekday_single( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + service_calls: list[ServiceCall], +) -> None: + """Test for firing on a specific weekday.""" + # Freeze time to Monday, January 2, 2023 at 5:00:00 + monday_trigger = dt_util.as_utc(datetime(2023, 1, 2, 5, 0, 0, 0)) + + freezer.move_to(monday_trigger) + + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": {"platform": "time", "at": "5:00:00", "weekday": "mon"}, + "action": { + "service": "test.automation", + "data_template": { + "some": "{{ trigger.platform }} - {{ trigger.now.strftime('%A') }}", + }, + }, + } + }, + ) + await hass.async_block_till_done() + + # Fire the trigger on Monday + async_fire_time_changed(hass, monday_trigger + timedelta(seconds=1)) + await hass.async_block_till_done() + + assert len(service_calls) == 1 + assert service_calls[0].data["some"] == "time - Monday" + + # Fire on Tuesday at the same time - should not trigger + tuesday_trigger = dt_util.as_utc(datetime(2023, 1, 3, 5, 0, 0, 0)) + async_fire_time_changed(hass, tuesday_trigger) + await hass.async_block_till_done() + + # Should still be only 1 call + assert len(service_calls) == 1 + + +async def test_if_fires_using_weekday_multiple( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + service_calls: list[ServiceCall], +) -> None: + """Test for firing on multiple weekdays.""" + # Freeze time to Monday, January 2, 2023 at 5:00:00 + monday_trigger = dt_util.as_utc(datetime(2023, 1, 2, 5, 0, 0, 0)) + + freezer.move_to(monday_trigger) + + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time", + "at": "5:00:00", + "weekday": ["mon", "wed", "fri"], + }, + "action": { + "service": "test.automation", + "data_template": { + "some": "{{ trigger.platform }} - {{ trigger.now.strftime('%A') }}", + }, + }, + } + }, + ) + await hass.async_block_till_done() + + # Fire on Monday - should trigger + async_fire_time_changed(hass, monday_trigger + timedelta(seconds=1)) + await hass.async_block_till_done() + assert len(service_calls) == 1 + assert "Monday" in service_calls[0].data["some"] + + # Fire on Tuesday - should not trigger + tuesday_trigger = dt_util.as_utc(datetime(2023, 1, 3, 5, 0, 0, 0)) + async_fire_time_changed(hass, tuesday_trigger) + await hass.async_block_till_done() + assert len(service_calls) == 1 + + # Fire on Wednesday - should trigger + wednesday_trigger = dt_util.as_utc(datetime(2023, 1, 4, 5, 0, 0, 0)) + async_fire_time_changed(hass, wednesday_trigger) + await hass.async_block_till_done() + assert len(service_calls) == 2 + assert "Wednesday" in service_calls[1].data["some"] + + # Fire on Friday - should trigger + friday_trigger = dt_util.as_utc(datetime(2023, 1, 6, 5, 0, 0, 0)) + async_fire_time_changed(hass, friday_trigger) + await hass.async_block_till_done() + assert len(service_calls) == 3 + assert "Friday" in service_calls[2].data["some"] + + +async def test_if_fires_using_weekday_with_entity( + hass: HomeAssistant, + freezer: FrozenDateTimeFactory, + service_calls: list[ServiceCall], +) -> None: + """Test for firing on weekday with input_datetime entity.""" + await async_setup_component( + hass, + "input_datetime", + {"input_datetime": {"trigger": {"has_date": False, "has_time": True}}}, + ) + + # Freeze time to Monday, January 2, 2023 at 5:00:00 + monday_trigger = dt_util.as_utc(datetime(2023, 1, 2, 5, 0, 0, 0)) + + await hass.services.async_call( + "input_datetime", + "set_datetime", + { + ATTR_ENTITY_ID: "input_datetime.trigger", + "time": "05:00:00", + }, + blocking=True, + ) + + freezer.move_to(monday_trigger) + + assert await async_setup_component( + hass, + automation.DOMAIN, + { + automation.DOMAIN: { + "trigger": { + "platform": "time", + "at": "input_datetime.trigger", + "weekday": "mon", + }, + "action": { + "service": "test.automation", + "data_template": { + "some": "{{ trigger.platform }} - {{ trigger.now.strftime('%A') }}", + "entity": "{{ trigger.entity_id }}", + }, + }, + } + }, + ) + await hass.async_block_till_done() + + # Fire on Monday - should trigger + async_fire_time_changed(hass, monday_trigger + timedelta(seconds=1)) + await hass.async_block_till_done() + automation_calls = [call for call in service_calls if call.domain == "test"] + assert len(automation_calls) == 1 + assert "Monday" in automation_calls[0].data["some"] + assert automation_calls[0].data["entity"] == "input_datetime.trigger" + + # Fire on Tuesday - should not trigger + tuesday_trigger = dt_util.as_utc(datetime(2023, 1, 3, 5, 0, 0, 0)) + async_fire_time_changed(hass, tuesday_trigger) + await hass.async_block_till_done() + automation_calls = [call for call in service_calls if call.domain == "test"] + assert len(automation_calls) == 1 + + +def test_weekday_validation() -> None: + """Test weekday validation in trigger schema.""" + # Valid single weekday + valid_config = {"platform": "time", "at": "5:00:00", "weekday": "mon"} + time.TRIGGER_SCHEMA(valid_config) + + # Valid multiple weekdays + valid_config = { + "platform": "time", + "at": "5:00:00", + "weekday": ["mon", "wed", "fri"], + } + time.TRIGGER_SCHEMA(valid_config) + + # Invalid weekday + invalid_config = {"platform": "time", "at": "5:00:00", "weekday": "invalid"} + with pytest.raises(vol.Invalid): + time.TRIGGER_SCHEMA(invalid_config) + + # Invalid weekday in list + invalid_config = { + "platform": "time", + "at": "5:00:00", + "weekday": ["mon", "invalid"], + } + with pytest.raises(vol.Invalid): + time.TRIGGER_SCHEMA(invalid_config) From 57c04f3a5635c829718810f65c4f7df10423c3a0 Mon Sep 17 00:00:00 2001 From: TimL Date: Sat, 5 Jul 2025 06:35:44 +1000 Subject: [PATCH 043/106] Bump pysmlight to v0.2.7 (#148101) Co-authored-by: Franck Nijhof --- homeassistant/components/smlight/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/smlight/manifest.json b/homeassistant/components/smlight/manifest.json index 9a37cc554c7..9340573f6ce 100644 --- a/homeassistant/components/smlight/manifest.json +++ b/homeassistant/components/smlight/manifest.json @@ -12,7 +12,7 @@ "integration_type": "device", "iot_class": "local_push", "quality_scale": "silver", - "requirements": ["pysmlight==0.2.6"], + "requirements": ["pysmlight==0.2.7"], "zeroconf": [ { "type": "_slzb-06._tcp.local." diff --git a/requirements_all.txt b/requirements_all.txt index 862c95d2a47..f596a32ed14 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2360,7 +2360,7 @@ pysmhi==1.0.2 pysml==0.1.5 # homeassistant.components.smlight -pysmlight==0.2.6 +pysmlight==0.2.7 # homeassistant.components.snmp pysnmp==6.2.6 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 2515980ccfd..987d16b6a66 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1963,7 +1963,7 @@ pysmhi==1.0.2 pysml==0.1.5 # homeassistant.components.smlight -pysmlight==0.2.6 +pysmlight==0.2.7 # homeassistant.components.snmp pysnmp==6.2.6 From 22e46d9977503dd97ff8a38478b261a8eb4955fe Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Fri, 4 Jul 2025 13:48:48 -0700 Subject: [PATCH 044/106] Make derivative sensor unavailable when source sensor is unavailable (#147468) --- homeassistant/components/derivative/sensor.py | 75 ++++++-- tests/components/derivative/test_init.py | 3 + tests/components/derivative/test_sensor.py | 168 +++++++++++++++++- 3 files changed, 220 insertions(+), 26 deletions(-) diff --git a/homeassistant/components/derivative/sensor.py b/homeassistant/components/derivative/sensor.py index 0639826b1ee..ab09c17673c 100644 --- a/homeassistant/components/derivative/sensor.py +++ b/homeassistant/components/derivative/sensor.py @@ -198,6 +198,7 @@ class DerivativeSensor(RestoreSensor, SensorEntity): self._attr_native_value = round(Decimal(0), round_digits) # List of tuples with (timestamp_start, timestamp_end, derivative) self._state_list: list[tuple[datetime, datetime, Decimal]] = [] + self._last_valid_state_time: tuple[str, datetime] | None = None self._attr_name = name if name is not None else f"{source_entity} derivative" self._attr_extra_state_attributes = {ATTR_SOURCE_ID: source_entity} @@ -242,6 +243,25 @@ class DerivativeSensor(RestoreSensor, SensorEntity): if (current_time - time_end).total_seconds() < self._time_window ] + def _handle_invalid_source_state(self, state: State | None) -> bool: + # Check the source state for unknown/unavailable condition. If unusable, write unknown/unavailable state and return false. + if not state or state.state == STATE_UNAVAILABLE: + self._attr_available = False + self.async_write_ha_state() + return False + if not _is_decimal_state(state.state): + self._attr_available = True + self._write_native_value(None) + return False + self._attr_available = True + return True + + def _write_native_value(self, derivative: Decimal | None) -> None: + self._attr_native_value = ( + None if derivative is None else round(derivative, self._round_digits) + ) + self.async_write_ha_state() + async def async_added_to_hass(self) -> None: """Handle entity which will be added.""" await super().async_added_to_hass() @@ -255,8 +275,8 @@ class DerivativeSensor(RestoreSensor, SensorEntity): Decimal(restored_data.native_value), # type: ignore[arg-type] self._round_digits, ) - except SyntaxError as err: - _LOGGER.warning("Could not restore last state: %s", err) + except (InvalidOperation, TypeError): + self._attr_native_value = None def schedule_max_sub_interval_exceeded(source_state: State | None) -> None: """Schedule calculation using the source state and max_sub_interval. @@ -280,9 +300,7 @@ class DerivativeSensor(RestoreSensor, SensorEntity): self._prune_state_list(now) derivative = self._calc_derivative_from_state_list(now) - self._attr_native_value = round(derivative, self._round_digits) - - self.async_write_ha_state() + self._write_native_value(derivative) # If derivative is now zero, don't schedule another timeout callback, as it will have no effect if derivative != 0: @@ -299,36 +317,46 @@ class DerivativeSensor(RestoreSensor, SensorEntity): """Handle constant sensor state.""" self._cancel_max_sub_interval_exceeded_callback() new_state = event.data["new_state"] + if not self._handle_invalid_source_state(new_state): + return + + assert new_state if self._attr_native_value == Decimal(0): # If the derivative is zero, and the source sensor hasn't # changed state, then we know it will still be zero. return schedule_max_sub_interval_exceeded(new_state) - new_state = event.data["new_state"] - if new_state is not None: - calc_derivative( - new_state, new_state.state, event.data["old_last_reported"] - ) + calc_derivative(new_state, new_state.state, event.data["old_last_reported"]) @callback def on_state_changed(event: Event[EventStateChangedData]) -> None: """Handle changed sensor state.""" self._cancel_max_sub_interval_exceeded_callback() new_state = event.data["new_state"] + if not self._handle_invalid_source_state(new_state): + return + + assert new_state schedule_max_sub_interval_exceeded(new_state) old_state = event.data["old_state"] - if new_state is not None and old_state is not None: + if old_state is not None: calc_derivative(new_state, old_state.state, old_state.last_reported) + else: + # On first state change from none, update availability + self.async_write_ha_state() def calc_derivative( new_state: State, old_value: str, old_last_reported: datetime ) -> None: """Handle the sensor state changes.""" - if old_value in (STATE_UNKNOWN, STATE_UNAVAILABLE) or new_state.state in ( - STATE_UNKNOWN, - STATE_UNAVAILABLE, - ): - return + if not _is_decimal_state(old_value): + if self._last_valid_state_time: + old_value = self._last_valid_state_time[0] + old_last_reported = self._last_valid_state_time[1] + else: + # Sensor becomes valid for the first time, just keep the restored value + self.async_write_ha_state() + return if self.native_unit_of_measurement is None: unit = new_state.attributes.get(ATTR_UNIT_OF_MEASUREMENT) @@ -373,6 +401,10 @@ class DerivativeSensor(RestoreSensor, SensorEntity): self._state_list.append( (old_last_reported, new_state.last_reported, new_derivative) ) + self._last_valid_state_time = ( + new_state.state, + new_state.last_reported, + ) # If outside of time window just report derivative (is the same as modeling it in the window), # otherwise take the weighted average with the previous derivatives @@ -382,11 +414,16 @@ class DerivativeSensor(RestoreSensor, SensorEntity): derivative = self._calc_derivative_from_state_list( new_state.last_reported ) - self._attr_native_value = round(derivative, self._round_digits) - self.async_write_ha_state() + self._write_native_value(derivative) + + source_state = self.hass.states.get(self._sensor_source_id) + if source_state is None or source_state.state in [ + STATE_UNAVAILABLE, + STATE_UNKNOWN, + ]: + self._attr_available = False if self._max_sub_interval is not None: - source_state = self.hass.states.get(self._sensor_source_id) schedule_max_sub_interval_exceeded(source_state) @callback diff --git a/tests/components/derivative/test_init.py b/tests/components/derivative/test_init.py index d237703eb2e..533f91c8a33 100644 --- a/tests/components/derivative/test_init.py +++ b/tests/components/derivative/test_init.py @@ -99,6 +99,9 @@ async def test_setup_and_remove_config_entry( input_sensor_entity_id = "sensor.input" derivative_entity_id = "sensor.my_derivative" + hass.states.async_set(input_sensor_entity_id, "10.0", {}) + await hass.async_block_till_done() + # Setup the config entry config_entry = MockConfigEntry( data={}, diff --git a/tests/components/derivative/test_sensor.py b/tests/components/derivative/test_sensor.py index e4e7097341c..10092e30ca0 100644 --- a/tests/components/derivative/test_sensor.py +++ b/tests/components/derivative/test_sensor.py @@ -6,16 +6,26 @@ import random from typing import Any from freezegun import freeze_time +import pytest from homeassistant.components.derivative.const import DOMAIN from homeassistant.components.sensor import ATTR_STATE_CLASS, SensorStateClass -from homeassistant.const import STATE_UNAVAILABLE, UnitOfPower, UnitOfTime +from homeassistant.const import ( + STATE_UNAVAILABLE, + STATE_UNKNOWN, + UnitOfPower, + UnitOfTime, +) from homeassistant.core import HomeAssistant, State from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.setup import async_setup_component from homeassistant.util import dt as dt_util -from tests.common import MockConfigEntry, async_fire_time_changed +from tests.common import ( + MockConfigEntry, + async_fire_time_changed, + mock_restore_cache_with_extra_data, +) async def test_state(hass: HomeAssistant) -> None: @@ -106,6 +116,7 @@ async def _setup_sensor( config = {"sensor": dict(default_config, **config)} assert await async_setup_component(hass, "sensor", config) + await hass.async_block_till_done() entity_id = config["sensor"]["source"] hass.states.async_set(entity_id, 0, {}) @@ -440,16 +451,14 @@ async def test_sub_intervals_instantaneous(hass: HomeAssistant) -> None: await hass.async_block_till_done() state = hass.states.get("sensor.power") - derivative = round(float(state.state), config["sensor"]["round"]) - assert derivative == -0.29 + assert state.state == STATE_UNAVAILABLE now += timedelta(seconds=60) async_fire_time_changed(hass, now) await hass.async_block_till_done() state = hass.states.get("sensor.power") - derivative = round(float(state.state), config["sensor"]["round"]) - assert derivative == -0.29 + assert state.state == STATE_UNAVAILABLE now += timedelta(seconds=10) freezer.move_to(now) @@ -458,7 +467,7 @@ async def test_sub_intervals_instantaneous(hass: HomeAssistant) -> None: state = hass.states.get("sensor.power") derivative = round(float(state.state), config["sensor"]["round"]) - assert derivative == -0.29 + assert derivative == 0 now += timedelta(seconds=max_sub_interval + 1) async_fire_time_changed(hass, now) @@ -693,3 +702,148 @@ async def test_device_id( derivative_entity = entity_registry.async_get("sensor.derivative") assert derivative_entity is not None assert derivative_entity.device_id == source_entity.device_id + + +@pytest.mark.parametrize("bad_state", [STATE_UNAVAILABLE, STATE_UNKNOWN, "foo"]) +async def test_unavailable( + bad_state: str, + hass: HomeAssistant, +) -> None: + """Test derivative sensor state when unavailable.""" + config, entity_id = await _setup_sensor(hass, {"unit_time": "s"}) + + times = [0, 1, 2, 3] + values = [0, 1, bad_state, 2] + expected_state = [ + 0, + 1, + STATE_UNAVAILABLE if bad_state == STATE_UNAVAILABLE else STATE_UNKNOWN, + 0.5, + ] + + # Testing a energy sensor with non-monotonic intervals and values + base = dt_util.utcnow() + with freeze_time(base) as freezer: + for time, value, expect in zip(times, values, expected_state, strict=False): + freezer.move_to(base + timedelta(seconds=time)) + hass.states.async_set(entity_id, value, {}) + await hass.async_block_till_done() + + state = hass.states.get("sensor.power") + assert state is not None + rounded_state = ( + state.state + if expect in [STATE_UNKNOWN, STATE_UNAVAILABLE] + else round(float(state.state), config["sensor"]["round"]) + ) + assert rounded_state == expect + + +@pytest.mark.parametrize("bad_state", [STATE_UNAVAILABLE, STATE_UNKNOWN, "foo"]) +async def test_unavailable_2( + bad_state: str, + hass: HomeAssistant, +) -> None: + """Test derivative sensor state when unavailable with a time window.""" + config, entity_id = await _setup_sensor( + hass, {"unit_time": "s", "time_window": {"seconds": 10}} + ) + + # Monotonically increasing by 1, with some unavailable holes + times = list(range(21)) + values = list(range(21)) + values[3] = bad_state + values[6] = bad_state + values[7] = bad_state + values[8] = bad_state + + base = dt_util.utcnow() + with freeze_time(base) as freezer: + for time, value in zip(times, values, strict=False): + freezer.move_to(base + timedelta(seconds=time)) + hass.states.async_set(entity_id, value, {}) + await hass.async_block_till_done() + + state = hass.states.get("sensor.power") + assert state is not None + + if value == bad_state: + assert ( + state.state == STATE_UNAVAILABLE + if bad_state is STATE_UNAVAILABLE + else STATE_UNKNOWN + ) + else: + expect = (time / 10) if time < 10 else 1 + assert round(float(state.state), config["sensor"]["round"]) == round( + expect, config["sensor"]["round"] + ) + + +@pytest.mark.parametrize("restore_state", ["3.00", STATE_UNKNOWN]) +async def test_unavailable_boot( + restore_state, + hass: HomeAssistant, +) -> None: + """Test that the booting sequence does not leave derivative in a bad state.""" + + mock_restore_cache_with_extra_data( + hass, + [ + ( + State( + "sensor.power", + restore_state, + { + "unit_of_measurement": "W", + }, + ), + { + "native_value": restore_state, + "native_unit_of_measurement": "W", + }, + ), + ], + ) + + config = { + "platform": "derivative", + "name": "power", + "source": "sensor.energy", + "round": 2, + "unit_time": "s", + } + + config = {"sensor": config} + entity_id = config["sensor"]["source"] + hass.states.async_set(entity_id, STATE_UNAVAILABLE, {}) + await hass.async_block_till_done() + + assert await async_setup_component(hass, "sensor", config) + await hass.async_block_till_done() + + state = hass.states.get("sensor.power") + assert state is not None + # Sensor is unavailable as source is unavailable + assert state.state == STATE_UNAVAILABLE + + base = dt_util.utcnow() + with freeze_time(base) as freezer: + freezer.move_to(base + timedelta(seconds=1)) + hass.states.async_set(entity_id, 10, {}) + await hass.async_block_till_done() + + state = hass.states.get("sensor.power") + assert state is not None + # The source sensor has moved to a valid value, but we need 2 points to derive, + # so just hold until the next tick + assert state.state == restore_state + + freezer.move_to(base + timedelta(seconds=2)) + hass.states.async_set(entity_id, 15, {}) + await hass.async_block_till_done() + + state = hass.states.get("sensor.power") + assert state is not None + # Now that the source sensor has two valid datapoints, we can calculate derivative + assert state.state == "5.00" From 520d92b90265b04a5cfd36ddba6ac1b2af5e8396 Mon Sep 17 00:00:00 2001 From: Andre Lengwenus Date: Fri, 4 Jul 2025 22:53:11 +0200 Subject: [PATCH 045/106] Use brightness stored in hardware device when switching LCN lights (#147375) --- homeassistant/components/lcn/light.py | 50 +++++++++++---------- tests/components/lcn/test_light.py | 63 ++++++++------------------- 2 files changed, 47 insertions(+), 66 deletions(-) diff --git a/homeassistant/components/lcn/light.py b/homeassistant/components/lcn/light.py index cd6b5c7057e..b9dad0aeb19 100644 --- a/homeassistant/components/lcn/light.py +++ b/homeassistant/components/lcn/light.py @@ -18,6 +18,7 @@ from homeassistant.const import CONF_DOMAIN, CONF_ENTITIES from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.typing import ConfigType +from homeassistant.util.color import brightness_to_value, value_to_brightness from .const import ( CONF_DIMMABLE, @@ -29,6 +30,8 @@ from .const import ( from .entity import LcnEntity from .helpers import InputType, LcnConfigEntry +BRIGHTNESS_SCALE = (1, 100) + PARALLEL_UPDATES = 0 @@ -91,8 +94,6 @@ class LcnOutputLight(LcnEntity, LightEntity): ) self.dimmable = config[CONF_DOMAIN_DATA][CONF_DIMMABLE] - self._is_dimming_to_zero = False - if self.dimmable: self._attr_color_mode = ColorMode.BRIGHTNESS else: @@ -113,10 +114,6 @@ class LcnOutputLight(LcnEntity, LightEntity): async def async_turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" - if ATTR_BRIGHTNESS in kwargs: - percent = int(kwargs[ATTR_BRIGHTNESS] / 255.0 * 100) - else: - percent = 100 if ATTR_TRANSITION in kwargs: transition = pypck.lcn_defs.time_to_ramp_value( kwargs[ATTR_TRANSITION] * 1000 @@ -124,12 +121,23 @@ class LcnOutputLight(LcnEntity, LightEntity): else: transition = self._transition - if not await self.device_connection.dim_output( - self.output.value, percent, transition - ): + if ATTR_BRIGHTNESS in kwargs: + percent = int( + brightness_to_value(BRIGHTNESS_SCALE, kwargs[ATTR_BRIGHTNESS]) + ) + if not await self.device_connection.dim_output( + self.output.value, percent, transition + ): + return + elif not self.is_on: + if not await self.device_connection.toggle_output( + self.output.value, transition, to_memory=True + ): + return + else: return + self._attr_is_on = True - self._is_dimming_to_zero = False self.async_write_ha_state() async def async_turn_off(self, **kwargs: Any) -> None: @@ -141,13 +149,13 @@ class LcnOutputLight(LcnEntity, LightEntity): else: transition = self._transition - if not await self.device_connection.dim_output( - self.output.value, 0, transition - ): - return - self._is_dimming_to_zero = bool(transition) - self._attr_is_on = False - self.async_write_ha_state() + if self.is_on: + if not await self.device_connection.toggle_output( + self.output.value, transition, to_memory=True + ): + return + self._attr_is_on = False + self.async_write_ha_state() def input_received(self, input_obj: InputType) -> None: """Set light state when LCN input object (command) is received.""" @@ -157,11 +165,9 @@ class LcnOutputLight(LcnEntity, LightEntity): ): return - self._attr_brightness = int(input_obj.get_percent() / 100.0 * 255) - if self._attr_brightness == 0: - self._is_dimming_to_zero = False - if not self._is_dimming_to_zero and self._attr_brightness is not None: - self._attr_is_on = self._attr_brightness > 0 + percent = input_obj.get_percent() + self._attr_brightness = value_to_brightness(BRIGHTNESS_SCALE, percent) + self._attr_is_on = bool(percent) self.async_write_ha_state() diff --git a/tests/components/lcn/test_light.py b/tests/components/lcn/test_light.py index 00c2341631e..b13e18bbbd1 100644 --- a/tests/components/lcn/test_light.py +++ b/tests/components/lcn/test_light.py @@ -51,9 +51,9 @@ async def test_output_turn_on(hass: HomeAssistant, entry: MockConfigEntry) -> No """Test the output light turns on.""" await init_integration(hass, entry) - with patch.object(MockModuleConnection, "dim_output") as dim_output: + with patch.object(MockModuleConnection, "toggle_output") as toggle_output: # command failed - dim_output.return_value = False + toggle_output.return_value = False await hass.services.async_call( DOMAIN_LIGHT, @@ -62,15 +62,15 @@ async def test_output_turn_on(hass: HomeAssistant, entry: MockConfigEntry) -> No blocking=True, ) - dim_output.assert_awaited_with(0, 100, 9) + toggle_output.assert_awaited_with(0, 9, to_memory=True) state = hass.states.get(LIGHT_OUTPUT1) assert state is not None assert state.state != STATE_ON # command success - dim_output.reset_mock(return_value=True) - dim_output.return_value = True + toggle_output.reset_mock(return_value=True) + toggle_output.return_value = True await hass.services.async_call( DOMAIN_LIGHT, @@ -79,7 +79,7 @@ async def test_output_turn_on(hass: HomeAssistant, entry: MockConfigEntry) -> No blocking=True, ) - dim_output.assert_awaited_with(0, 100, 9) + toggle_output.assert_awaited_with(0, 9, to_memory=True) state = hass.states.get(LIGHT_OUTPUT1) assert state is not None @@ -117,12 +117,16 @@ async def test_output_turn_off(hass: HomeAssistant, entry: MockConfigEntry) -> N """Test the output light turns off.""" await init_integration(hass, entry) - with patch.object(MockModuleConnection, "dim_output") as dim_output: - state = hass.states.get(LIGHT_OUTPUT1) - state.state = STATE_ON + with patch.object(MockModuleConnection, "toggle_output") as toggle_output: + await hass.services.async_call( + DOMAIN_LIGHT, + SERVICE_TURN_ON, + {ATTR_ENTITY_ID: LIGHT_OUTPUT1}, + blocking=True, + ) # command failed - dim_output.return_value = False + toggle_output.return_value = False await hass.services.async_call( DOMAIN_LIGHT, @@ -131,15 +135,15 @@ async def test_output_turn_off(hass: HomeAssistant, entry: MockConfigEntry) -> N blocking=True, ) - dim_output.assert_awaited_with(0, 0, 9) + toggle_output.assert_awaited_with(0, 9, to_memory=True) state = hass.states.get(LIGHT_OUTPUT1) assert state is not None assert state.state != STATE_OFF # command success - dim_output.reset_mock(return_value=True) - dim_output.return_value = True + toggle_output.reset_mock(return_value=True) + toggle_output.return_value = True await hass.services.async_call( DOMAIN_LIGHT, @@ -148,36 +152,7 @@ async def test_output_turn_off(hass: HomeAssistant, entry: MockConfigEntry) -> N blocking=True, ) - dim_output.assert_awaited_with(0, 0, 9) - - state = hass.states.get(LIGHT_OUTPUT1) - assert state is not None - assert state.state == STATE_OFF - - -async def test_output_turn_off_with_attributes( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test the output light turns off.""" - await init_integration(hass, entry) - - with patch.object(MockModuleConnection, "dim_output") as dim_output: - dim_output.return_value = True - - state = hass.states.get(LIGHT_OUTPUT1) - state.state = STATE_ON - - await hass.services.async_call( - DOMAIN_LIGHT, - SERVICE_TURN_OFF, - { - ATTR_ENTITY_ID: LIGHT_OUTPUT1, - ATTR_TRANSITION: 2, - }, - blocking=True, - ) - - dim_output.assert_awaited_with(0, 0, 6) + toggle_output.assert_awaited_with(0, 9, to_memory=True) state = hass.states.get(LIGHT_OUTPUT1) assert state is not None @@ -288,7 +263,7 @@ async def test_pushed_output_status_change( state = hass.states.get(LIGHT_OUTPUT1) assert state is not None assert state.state == STATE_ON - assert state.attributes[ATTR_BRIGHTNESS] == 127 + assert state.attributes[ATTR_BRIGHTNESS] == 128 # push status "off" inp = ModStatusOutput(address, 0, 0) From 8f24ebe96733367cb813ede00d6f0fc93234c12e Mon Sep 17 00:00:00 2001 From: Andre Lengwenus Date: Fri, 4 Jul 2025 22:55:20 +0200 Subject: [PATCH 046/106] Remove deprecated support for lock sensors and corresponding actions in lcn (#147143) --- homeassistant/components/lcn/binary_sensor.py | 136 +----------------- homeassistant/components/lcn/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- .../lcn/fixtures/config_entry_pchk.json | 16 --- .../lcn/snapshots/test_binary_sensor.ambr | 96 ------------- tests/components/lcn/test_binary_sensor.py | 129 +---------------- 7 files changed, 10 insertions(+), 373 deletions(-) diff --git a/homeassistant/components/lcn/binary_sensor.py b/homeassistant/components/lcn/binary_sensor.py index b124b3f6188..a9f194fe1b8 100644 --- a/homeassistant/components/lcn/binary_sensor.py +++ b/homeassistant/components/lcn/binary_sensor.py @@ -5,23 +5,16 @@ from functools import partial import pypck -from homeassistant.components.automation import automations_with_entity from homeassistant.components.binary_sensor import ( DOMAIN as DOMAIN_BINARY_SENSOR, BinarySensorEntity, ) -from homeassistant.components.script import scripts_with_entity from homeassistant.const import CONF_DOMAIN, CONF_ENTITIES, CONF_SOURCE from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from homeassistant.helpers.issue_registry import ( - IssueSeverity, - async_create_issue, - async_delete_issue, -) from homeassistant.helpers.typing import ConfigType -from .const import BINSENSOR_PORTS, CONF_DOMAIN_DATA, DOMAIN, SETPOINTS +from .const import CONF_DOMAIN_DATA from .entity import LcnEntity from .helpers import InputType, LcnConfigEntry @@ -34,15 +27,9 @@ def add_lcn_entities( entity_configs: Iterable[ConfigType], ) -> None: """Add entities for this domain.""" - entities: list[LcnRegulatorLockSensor | LcnBinarySensor | LcnLockKeysSensor] = [] - for entity_config in entity_configs: - if entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in SETPOINTS: - entities.append(LcnRegulatorLockSensor(entity_config, config_entry)) - elif entity_config[CONF_DOMAIN_DATA][CONF_SOURCE] in BINSENSOR_PORTS: - entities.append(LcnBinarySensor(entity_config, config_entry)) - else: # in KEY - entities.append(LcnLockKeysSensor(entity_config, config_entry)) - + entities = [ + LcnBinarySensor(entity_config, config_entry) for entity_config in entity_configs + ] async_add_entities(entities) @@ -71,65 +58,6 @@ async def async_setup_entry( ) -class LcnRegulatorLockSensor(LcnEntity, BinarySensorEntity): - """Representation of a LCN binary sensor for regulator locks.""" - - def __init__(self, config: ConfigType, config_entry: LcnConfigEntry) -> None: - """Initialize the LCN binary sensor.""" - super().__init__(config, config_entry) - - self.setpoint_variable = pypck.lcn_defs.Var[ - config[CONF_DOMAIN_DATA][CONF_SOURCE] - ] - - async def async_added_to_hass(self) -> None: - """Run when entity about to be added to hass.""" - await super().async_added_to_hass() - - if not self.device_connection.is_group: - await self.device_connection.activate_status_request_handler( - self.setpoint_variable - ) - - entity_automations = automations_with_entity(self.hass, self.entity_id) - entity_scripts = scripts_with_entity(self.hass, self.entity_id) - if entity_automations + entity_scripts: - async_create_issue( - self.hass, - DOMAIN, - f"deprecated_binary_sensor_{self.entity_id}", - breaks_in_ha_version="2025.5.0", - is_fixable=False, - severity=IssueSeverity.WARNING, - translation_key="deprecated_regulatorlock_sensor", - translation_placeholders={ - "entity": f"{DOMAIN_BINARY_SENSOR}.{self.name.lower().replace(' ', '_')}", - }, - ) - - async def async_will_remove_from_hass(self) -> None: - """Run when entity will be removed from hass.""" - await super().async_will_remove_from_hass() - if not self.device_connection.is_group: - await self.device_connection.cancel_status_request_handler( - self.setpoint_variable - ) - async_delete_issue( - self.hass, DOMAIN, f"deprecated_binary_sensor_{self.entity_id}" - ) - - def input_received(self, input_obj: InputType) -> None: - """Set sensor value when LCN input object (command) is received.""" - if ( - not isinstance(input_obj, pypck.inputs.ModStatusVar) - or input_obj.get_var() != self.setpoint_variable - ): - return - - self._attr_is_on = input_obj.get_value().is_locked_regulator() - self.async_write_ha_state() - - class LcnBinarySensor(LcnEntity, BinarySensorEntity): """Representation of a LCN binary sensor for binary sensor ports.""" @@ -164,59 +92,3 @@ class LcnBinarySensor(LcnEntity, BinarySensorEntity): self._attr_is_on = input_obj.get_state(self.bin_sensor_port.value) self.async_write_ha_state() - - -class LcnLockKeysSensor(LcnEntity, BinarySensorEntity): - """Representation of a LCN sensor for key locks.""" - - def __init__(self, config: ConfigType, config_entry: LcnConfigEntry) -> None: - """Initialize the LCN sensor.""" - super().__init__(config, config_entry) - - self.source = pypck.lcn_defs.Key[config[CONF_DOMAIN_DATA][CONF_SOURCE]] - - async def async_added_to_hass(self) -> None: - """Run when entity about to be added to hass.""" - await super().async_added_to_hass() - - if not self.device_connection.is_group: - await self.device_connection.activate_status_request_handler(self.source) - - entity_automations = automations_with_entity(self.hass, self.entity_id) - entity_scripts = scripts_with_entity(self.hass, self.entity_id) - if entity_automations + entity_scripts: - async_create_issue( - self.hass, - DOMAIN, - f"deprecated_binary_sensor_{self.entity_id}", - breaks_in_ha_version="2025.5.0", - is_fixable=False, - severity=IssueSeverity.WARNING, - translation_key="deprecated_keylock_sensor", - translation_placeholders={ - "entity": f"{DOMAIN_BINARY_SENSOR}.{self.name.lower().replace(' ', '_')}", - }, - ) - - async def async_will_remove_from_hass(self) -> None: - """Run when entity will be removed from hass.""" - await super().async_will_remove_from_hass() - if not self.device_connection.is_group: - await self.device_connection.cancel_status_request_handler(self.source) - async_delete_issue( - self.hass, DOMAIN, f"deprecated_binary_sensor_{self.entity_id}" - ) - - def input_received(self, input_obj: InputType) -> None: - """Set sensor value when LCN input object (command) is received.""" - if ( - not isinstance(input_obj, pypck.inputs.ModStatusKeyLocks) - or self.source not in pypck.lcn_defs.Key - ): - return - - table_id = ord(self.source.name[0]) - 65 - key_id = int(self.source.name[1]) - 1 - - self._attr_is_on = input_obj.get_state(table_id, key_id) - self.async_write_ha_state() diff --git a/homeassistant/components/lcn/manifest.json b/homeassistant/components/lcn/manifest.json index 8a47f1c1359..234178d3e3b 100644 --- a/homeassistant/components/lcn/manifest.json +++ b/homeassistant/components/lcn/manifest.json @@ -9,5 +9,5 @@ "iot_class": "local_push", "loggers": ["pypck"], "quality_scale": "bronze", - "requirements": ["pypck==0.8.10", "lcn-frontend==0.2.5"] + "requirements": ["pypck==0.8.10", "lcn-frontend==0.2.6"] } diff --git a/requirements_all.txt b/requirements_all.txt index f596a32ed14..d1a2c2a21ef 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1319,7 +1319,7 @@ lakeside==0.13 laundrify-aio==1.2.2 # homeassistant.components.lcn -lcn-frontend==0.2.5 +lcn-frontend==0.2.6 # homeassistant.components.ld2410_ble ld2410-ble==0.1.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 987d16b6a66..b4bc9a82158 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1138,7 +1138,7 @@ lacrosse-view==1.1.1 laundrify-aio==1.2.2 # homeassistant.components.lcn -lcn-frontend==0.2.5 +lcn-frontend==0.2.6 # homeassistant.components.ld2410_ble ld2410-ble==0.1.1 diff --git a/tests/components/lcn/fixtures/config_entry_pchk.json b/tests/components/lcn/fixtures/config_entry_pchk.json index 5ded11d619a..c0a52821d5a 100644 --- a/tests/components/lcn/fixtures/config_entry_pchk.json +++ b/tests/components/lcn/fixtures/config_entry_pchk.json @@ -187,14 +187,6 @@ "transition": 10.0 } }, - { - "address": [0, 7, false], - "name": "Sensor_LockRegulator1", - "domain": "binary_sensor", - "domain_data": { - "source": "R1VARSETPOINT" - } - }, { "address": [0, 7, false], "name": "Binary_Sensor1", @@ -203,14 +195,6 @@ "source": "BINSENSOR1" } }, - { - "address": [0, 7, false], - "name": "Sensor_KeyLock", - "domain": "binary_sensor", - "domain_data": { - "source": "A5" - } - }, { "address": [0, 7, false], "name": "Sensor_Var1", diff --git a/tests/components/lcn/snapshots/test_binary_sensor.ambr b/tests/components/lcn/snapshots/test_binary_sensor.ambr index d1a76b98bf1..1317150b19e 100644 --- a/tests/components/lcn/snapshots/test_binary_sensor.ambr +++ b/tests/components/lcn/snapshots/test_binary_sensor.ambr @@ -47,99 +47,3 @@ 'state': 'unknown', }) # --- -# name: test_setup_lcn_binary_sensor[binary_sensor.testmodule_sensor_keylock-entry] - EntityRegistryEntrySnapshot({ - 'aliases': set({ - }), - 'area_id': None, - 'capabilities': None, - 'config_entry_id': , - 'config_subentry_id': , - 'device_class': None, - 'device_id': , - 'disabled_by': None, - 'domain': 'binary_sensor', - 'entity_category': None, - 'entity_id': 'binary_sensor.testmodule_sensor_keylock', - 'has_entity_name': True, - 'hidden_by': None, - 'icon': None, - 'id': , - 'labels': set({ - }), - 'name': None, - 'options': dict({ - }), - 'original_device_class': None, - 'original_icon': None, - 'original_name': 'Sensor_KeyLock', - 'platform': 'lcn', - 'previous_unique_id': None, - 'suggested_object_id': None, - 'supported_features': 0, - 'translation_key': None, - 'unique_id': 'lcn/config_entry_pchk_json-m000007-a5', - 'unit_of_measurement': None, - }) -# --- -# name: test_setup_lcn_binary_sensor[binary_sensor.testmodule_sensor_keylock-state] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'friendly_name': 'TestModule Sensor_KeyLock', - }), - 'context': , - 'entity_id': 'binary_sensor.testmodule_sensor_keylock', - 'last_changed': , - 'last_reported': , - 'last_updated': , - 'state': 'unknown', - }) -# --- -# name: test_setup_lcn_binary_sensor[binary_sensor.testmodule_sensor_lockregulator1-entry] - EntityRegistryEntrySnapshot({ - 'aliases': set({ - }), - 'area_id': None, - 'capabilities': None, - 'config_entry_id': , - 'config_subentry_id': , - 'device_class': None, - 'device_id': , - 'disabled_by': None, - 'domain': 'binary_sensor', - 'entity_category': None, - 'entity_id': 'binary_sensor.testmodule_sensor_lockregulator1', - 'has_entity_name': True, - 'hidden_by': None, - 'icon': None, - 'id': , - 'labels': set({ - }), - 'name': None, - 'options': dict({ - }), - 'original_device_class': None, - 'original_icon': None, - 'original_name': 'Sensor_LockRegulator1', - 'platform': 'lcn', - 'previous_unique_id': None, - 'suggested_object_id': None, - 'supported_features': 0, - 'translation_key': None, - 'unique_id': 'lcn/config_entry_pchk_json-m000007-r1varsetpoint', - 'unit_of_measurement': None, - }) -# --- -# name: test_setup_lcn_binary_sensor[binary_sensor.testmodule_sensor_lockregulator1-state] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'friendly_name': 'TestModule Sensor_LockRegulator1', - }), - 'context': , - 'entity_id': 'binary_sensor.testmodule_sensor_lockregulator1', - 'last_changed': , - 'last_reported': , - 'last_updated': , - 'state': 'unknown', - }) -# --- diff --git a/tests/components/lcn/test_binary_sensor.py b/tests/components/lcn/test_binary_sensor.py index b9362dcd242..a4712459e78 100644 --- a/tests/components/lcn/test_binary_sensor.py +++ b/tests/components/lcn/test_binary_sensor.py @@ -2,29 +2,20 @@ from unittest.mock import patch -from pypck.inputs import ModStatusBinSensors, ModStatusKeyLocks, ModStatusVar +from pypck.inputs import ModStatusBinSensors from pypck.lcn_addr import LcnAddr -from pypck.lcn_defs import Var, VarValue -import pytest from syrupy.assertion import SnapshotAssertion -from homeassistant.components import automation, script -from homeassistant.components.automation import automations_with_entity -from homeassistant.components.lcn import DOMAIN from homeassistant.components.lcn.helpers import get_device_connection -from homeassistant.components.script import scripts_with_entity from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNAVAILABLE, Platform -from homeassistant.core import HomeAssistant, ServiceCall -from homeassistant.helpers import entity_registry as er, issue_registry as ir -from homeassistant.setup import async_setup_component +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er from .conftest import MockConfigEntry, init_integration from tests.common import snapshot_platform -BINARY_SENSOR_LOCKREGULATOR1 = "binary_sensor.testmodule_sensor_lockregulator1" BINARY_SENSOR_SENSOR1 = "binary_sensor.testmodule_binary_sensor1" -BINARY_SENSOR_KEYLOCK = "binary_sensor.testmodule_sensor_keylock" async def test_setup_lcn_binary_sensor( @@ -40,35 +31,6 @@ async def test_setup_lcn_binary_sensor( await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id) -async def test_pushed_lock_setpoint_status_change( - hass: HomeAssistant, - entry: MockConfigEntry, -) -> None: - """Test the lock setpoint sensor changes its state on status received.""" - await init_integration(hass, entry) - - device_connection = get_device_connection(hass, (0, 7, False), entry) - address = LcnAddr(0, 7, False) - - # push status lock setpoint - inp = ModStatusVar(address, Var.R1VARSETPOINT, VarValue(0x8000)) - await device_connection.async_process_input(inp) - await hass.async_block_till_done() - - state = hass.states.get(BINARY_SENSOR_LOCKREGULATOR1) - assert state is not None - assert state.state == STATE_ON - - # push status unlock setpoint - inp = ModStatusVar(address, Var.R1VARSETPOINT, VarValue(0x7FFF)) - await device_connection.async_process_input(inp) - await hass.async_block_till_done() - - state = hass.states.get(BINARY_SENSOR_LOCKREGULATOR1) - assert state is not None - assert state.state == STATE_OFF - - async def test_pushed_binsensor_status_change( hass: HomeAssistant, entry: MockConfigEntry ) -> None: @@ -99,94 +61,9 @@ async def test_pushed_binsensor_status_change( assert state.state == STATE_ON -async def test_pushed_keylock_status_change( - hass: HomeAssistant, entry: MockConfigEntry -) -> None: - """Test the keylock sensor changes its state on status received.""" - await init_integration(hass, entry) - - device_connection = get_device_connection(hass, (0, 7, False), entry) - address = LcnAddr(0, 7, False) - states = [[False] * 8 for i in range(4)] - - # push status keylock "off" - inp = ModStatusKeyLocks(address, states) - await device_connection.async_process_input(inp) - await hass.async_block_till_done() - - state = hass.states.get(BINARY_SENSOR_KEYLOCK) - assert state is not None - assert state.state == STATE_OFF - - # push status keylock "on" - states[0][4] = True - inp = ModStatusKeyLocks(address, states) - await device_connection.async_process_input(inp) - await hass.async_block_till_done() - - state = hass.states.get(BINARY_SENSOR_KEYLOCK) - assert state is not None - assert state.state == STATE_ON - - async def test_unload_config_entry(hass: HomeAssistant, entry: MockConfigEntry) -> None: """Test the binary sensor is removed when the config entry is unloaded.""" await init_integration(hass, entry) await hass.config_entries.async_unload(entry.entry_id) - assert hass.states.get(BINARY_SENSOR_LOCKREGULATOR1).state == STATE_UNAVAILABLE assert hass.states.get(BINARY_SENSOR_SENSOR1).state == STATE_UNAVAILABLE - assert hass.states.get(BINARY_SENSOR_KEYLOCK).state == STATE_UNAVAILABLE - - -@pytest.mark.parametrize( - "entity_id", - [ - "binary_sensor.testmodule_sensor_lockregulator1", - "binary_sensor.testmodule_sensor_keylock", - ], -) -async def test_create_issue( - hass: HomeAssistant, - service_calls: list[ServiceCall], - issue_registry: ir.IssueRegistry, - entry: MockConfigEntry, - entity_id, -) -> None: - """Test we create an issue when an automation or script is using a deprecated entity.""" - assert await async_setup_component( - hass, - automation.DOMAIN, - { - automation.DOMAIN: { - "alias": "test", - "trigger": {"platform": "state", "entity_id": entity_id}, - "action": {"action": "test.automation"}, - } - }, - ) - - assert await async_setup_component( - hass, - script.DOMAIN, - { - script.DOMAIN: { - "test": { - "sequence": { - "condition": "state", - "entity_id": entity_id, - "state": STATE_ON, - } - } - } - }, - ) - - await init_integration(hass, entry) - - assert automations_with_entity(hass, entity_id)[0] == "automation.test" - assert scripts_with_entity(hass, entity_id)[0] == "script.test" - - assert issue_registry.async_get_issue( - DOMAIN, f"deprecated_binary_sensor_{entity_id}" - ) From 79683c8267bf5321c0f40468df6d83d2a5835567 Mon Sep 17 00:00:00 2001 From: Guido Schmitz Date: Fri, 4 Jul 2025 22:59:38 +0200 Subject: [PATCH 047/106] Log availability of devices in devolo Home Control (#147091) --- .../components/devolo_home_control/entity.py | 17 ++++++++++++++++- .../devolo_home_control/test_switch.py | 14 +++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/devolo_home_control/entity.py b/homeassistant/components/devolo_home_control/entity.py index 9edc7d54145..dade8d6a2f9 100644 --- a/homeassistant/components/devolo_home_control/entity.py +++ b/homeassistant/components/devolo_home_control/entity.py @@ -87,7 +87,22 @@ class DevoloDeviceEntity(Entity): self._value = message[1] elif len(message) == 3 and message[2] == "status": # Maybe the API wants to tell us, that the device went on- or offline. - self._attr_available = self._device_instance.is_online() + state = self._device_instance.is_online() + if state != self.available and not state: + _LOGGER.info( + "Device %s is unavailable", + self._device_instance.settings_property[ + "general_device_settings" + ].name, + ) + if state != self.available and state: + _LOGGER.info( + "Device %s is back online", + self._device_instance.settings_property[ + "general_device_settings" + ].name, + ) + self._attr_available = state elif message[1] == "del" and self.platform.config_entry: device_registry = dr.async_get(self.hass) device = device_registry.async_get_device( diff --git a/tests/components/devolo_home_control/test_switch.py b/tests/components/devolo_home_control/test_switch.py index 46adaf8c8b0..0a66760bc81 100644 --- a/tests/components/devolo_home_control/test_switch.py +++ b/tests/components/devolo_home_control/test_switch.py @@ -2,6 +2,7 @@ from unittest.mock import patch +import pytest from syrupy.assertion import SnapshotAssertion from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN @@ -20,7 +21,10 @@ from .mocks import HomeControlMock, HomeControlMockSwitch async def test_switch( - hass: HomeAssistant, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion + hass: HomeAssistant, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, + caplog: pytest.LogCaptureFixture, ) -> None: """Test setup and state change of a switch device.""" entry = configure_integration(hass) @@ -69,6 +73,14 @@ async def test_switch( test_gateway.publisher.dispatch("Test", ("Status", False, "status")) await hass.async_block_till_done() assert hass.states.get(f"{SWITCH_DOMAIN}.test").state == STATE_UNAVAILABLE + assert "Device Test is unavailable" in caplog.text + + # Emulate websocket message: device went back online + test_gateway.devices["Test"].status = 0 + test_gateway.publisher.dispatch("Test", ("Status", False, "status")) + await hass.async_block_till_done() + assert hass.states.get(f"{SWITCH_DOMAIN}.test").state == STATE_ON + assert "Device Test is back online" in caplog.text async def test_remove_from_hass(hass: HomeAssistant) -> None: From be7735964b92b170f1bb68cdfc4813dfd799becf Mon Sep 17 00:00:00 2001 From: Pete Sage <76050312+PeteRager@users.noreply.github.com> Date: Fri, 4 Jul 2025 17:02:38 -0400 Subject: [PATCH 048/106] Sonos remove unneeded mocking from test (#147064) --- tests/components/sonos/test_init.py | 34 ++++++++++++----------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/tests/components/sonos/test_init.py b/tests/components/sonos/test_init.py index 1bc8baff752..c1b98b2ec60 100644 --- a/tests/components/sonos/test_init.py +++ b/tests/components/sonos/test_init.py @@ -1,7 +1,6 @@ """Tests for the Sonos config flow.""" import asyncio -from datetime import timedelta import logging from unittest.mock import Mock, PropertyMock, patch @@ -330,29 +329,24 @@ async def test_async_poll_manual_hosts_5( soco_2.renderingControl = Mock() soco_2.renderingControl.GetVolume = Mock() speaker_2_activity = SpeakerActivity(hass, soco_2) - with patch( - "homeassistant.components.sonos.DISCOVERY_INTERVAL" - ) as mock_discovery_interval: - # Speed up manual discovery interval so second iteration runs sooner - mock_discovery_interval.total_seconds = Mock(side_effect=[0.5, 60]) - with caplog.at_level(logging.DEBUG): - caplog.clear() + with caplog.at_level(logging.DEBUG): + caplog.clear() - await _setup_hass(hass) + await _setup_hass(hass) - assert "media_player.bedroom" in entity_registry.entities - assert "media_player.living_room" in entity_registry.entities + assert "media_player.bedroom" in entity_registry.entities + assert "media_player.living_room" in entity_registry.entities - async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=0.5)) - await hass.async_block_till_done() - await asyncio.gather( - *[speaker_1_activity.event.wait(), speaker_2_activity.event.wait()] - ) - assert speaker_1_activity.call_count == 1 - assert speaker_2_activity.call_count == 1 - assert "Activity on Living Room" in caplog.text - assert "Activity on Bedroom" in caplog.text + async_fire_time_changed(hass, dt_util.utcnow() + DISCOVERY_INTERVAL) + await hass.async_block_till_done() + await asyncio.gather( + *[speaker_1_activity.event.wait(), speaker_2_activity.event.wait()] + ) + assert speaker_1_activity.call_count == 1 + assert speaker_2_activity.call_count == 1 + assert "Activity on Living Room" in caplog.text + assert "Activity on Bedroom" in caplog.text await hass.async_block_till_done(wait_background_tasks=True) From 9a5cbe483bf409f8f2f20e32791517875a3f995d Mon Sep 17 00:00:00 2001 From: Kevin Stillhammer Date: Fri, 4 Jul 2025 23:06:47 +0200 Subject: [PATCH 049/106] Remove obsolete string unit_system in here_travel_time (#146656) --- homeassistant/components/here_travel_time/strings.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/here_travel_time/strings.json b/homeassistant/components/here_travel_time/strings.json index c0534fa7154..89350261299 100644 --- a/homeassistant/components/here_travel_time/strings.json +++ b/homeassistant/components/here_travel_time/strings.json @@ -61,8 +61,7 @@ "init": { "data": { "traffic_mode": "Traffic mode", - "route_mode": "Route mode", - "unit_system": "Unit system" + "route_mode": "Route mode" } }, "time_menu": { From ca85ffc06885d81a5052af8d218dd7020ec06e1b Mon Sep 17 00:00:00 2001 From: Michael Podhorodecki Date: Sat, 5 Jul 2025 07:07:13 +1000 Subject: [PATCH 050/106] Add Deadlock (SecureMode) support to the Yale Access Bluetooth integration (#144107) Co-authored-by: J. Nick Koston Co-authored-by: J. Nick Koston --- homeassistant/components/august/manifest.json | 2 +- homeassistant/components/yale/manifest.json | 2 +- .../components/yalexs_ble/__init__.py | 6 +++- .../components/yalexs_ble/icons.json | 11 ++++++ homeassistant/components/yalexs_ble/lock.py | 36 ++++++++++++++++--- .../components/yalexs_ble/manifest.json | 2 +- .../components/yalexs_ble/strings.json | 5 +++ requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 9 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 homeassistant/components/yalexs_ble/icons.json diff --git a/homeassistant/components/august/manifest.json b/homeassistant/components/august/manifest.json index a6b2961c2a0..9dc66084a45 100644 --- a/homeassistant/components/august/manifest.json +++ b/homeassistant/components/august/manifest.json @@ -28,5 +28,5 @@ "documentation": "https://www.home-assistant.io/integrations/august", "iot_class": "cloud_push", "loggers": ["pubnub", "yalexs"], - "requirements": ["yalexs==8.10.0", "yalexs-ble==2.6.0"] + "requirements": ["yalexs==8.10.0", "yalexs-ble==3.0.0"] } diff --git a/homeassistant/components/yale/manifest.json b/homeassistant/components/yale/manifest.json index 4d9ea9ec2c9..fee5b0b8310 100644 --- a/homeassistant/components/yale/manifest.json +++ b/homeassistant/components/yale/manifest.json @@ -13,5 +13,5 @@ "documentation": "https://www.home-assistant.io/integrations/yale", "iot_class": "cloud_push", "loggers": ["socketio", "engineio", "yalexs"], - "requirements": ["yalexs==8.10.0", "yalexs-ble==2.6.0"] + "requirements": ["yalexs==8.10.0", "yalexs-ble==3.0.0"] } diff --git a/homeassistant/components/yalexs_ble/__init__.py b/homeassistant/components/yalexs_ble/__init__.py index c5183623660..68d64494e41 100644 --- a/homeassistant/components/yalexs_ble/__init__.py +++ b/homeassistant/components/yalexs_ble/__init__.py @@ -32,7 +32,11 @@ from .util import async_find_existing_service_info, bluetooth_callback_matcher type YALEXSBLEConfigEntry = ConfigEntry[YaleXSBLEData] -PLATFORMS: list[Platform] = [Platform.BINARY_SENSOR, Platform.LOCK, Platform.SENSOR] +PLATFORMS: list[Platform] = [ + Platform.BINARY_SENSOR, + Platform.LOCK, + Platform.SENSOR, +] async def async_setup_entry(hass: HomeAssistant, entry: YALEXSBLEConfigEntry) -> bool: diff --git a/homeassistant/components/yalexs_ble/icons.json b/homeassistant/components/yalexs_ble/icons.json new file mode 100644 index 00000000000..0b4929cd778 --- /dev/null +++ b/homeassistant/components/yalexs_ble/icons.json @@ -0,0 +1,11 @@ +{ + "entity": { + "lock": { + "secure_mode": { + "state": { + "locked": "mdi:shield-lock" + } + } + } + } +} diff --git a/homeassistant/components/yalexs_ble/lock.py b/homeassistant/components/yalexs_ble/lock.py index 78b92ab9eb1..3d822714fb5 100644 --- a/homeassistant/components/yalexs_ble/lock.py +++ b/homeassistant/components/yalexs_ble/lock.py @@ -12,6 +12,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from . import YALEXSBLEConfigEntry from .entity import YALEXSBLEEntity +from .models import YaleXSBLEData async def async_setup_entry( @@ -20,13 +21,15 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up locks.""" - async_add_entities([YaleXSBLELock(entry.runtime_data)]) + async_add_entities( + [YaleXSBLELock(entry.runtime_data), YaleXSBLESecureModeLock(entry.runtime_data)] + ) -class YaleXSBLELock(YALEXSBLEEntity, LockEntity): +class YaleXSBLEBaseLock(YALEXSBLEEntity, LockEntity): """A yale xs ble lock.""" - _attr_name = None + _secure_mode: bool = False @callback def _async_update_state( @@ -39,11 +42,13 @@ class YaleXSBLELock(YALEXSBLEEntity, LockEntity): self._attr_is_jammed = False lock_state = new_state.lock if lock_state is LockStatus.LOCKED: - self._attr_is_locked = True + self._attr_is_locked = not self._secure_mode elif lock_state is LockStatus.LOCKING: self._attr_is_locking = True elif lock_state is LockStatus.UNLOCKING: self._attr_is_unlocking = True + elif lock_state is LockStatus.SECUREMODE: + self._attr_is_locked = True elif lock_state in ( LockStatus.UNKNOWN_01, LockStatus.UNKNOWN_06, @@ -57,6 +62,29 @@ class YaleXSBLELock(YALEXSBLEEntity, LockEntity): """Unlock the lock.""" await self._device.unlock() + +class YaleXSBLELock(YaleXSBLEBaseLock, LockEntity): + """A yale xs ble lock not in secure mode.""" + + _attr_name = None + async def async_lock(self, **kwargs: Any) -> None: """Lock the lock.""" await self._device.lock() + + +class YaleXSBLESecureModeLock(YaleXSBLEBaseLock): + """A yale xs ble lock in secure mode.""" + + _attr_entity_registry_enabled_default = False + _attr_translation_key = "secure_mode" + _secure_mode = True + + def __init__(self, data: YaleXSBLEData) -> None: + """Initialize the entity.""" + super().__init__(data) + self._attr_unique_id = f"{self._device.address}_secure_mode" + + async def async_lock(self, **kwargs: Any) -> None: + """Lock the lock.""" + await self._device.securemode() diff --git a/homeassistant/components/yalexs_ble/manifest.json b/homeassistant/components/yalexs_ble/manifest.json index 2387f5dc15f..b3021bd908e 100644 --- a/homeassistant/components/yalexs_ble/manifest.json +++ b/homeassistant/components/yalexs_ble/manifest.json @@ -12,5 +12,5 @@ "dependencies": ["bluetooth_adapters"], "documentation": "https://www.home-assistant.io/integrations/yalexs_ble", "iot_class": "local_push", - "requirements": ["yalexs-ble==2.6.0"] + "requirements": ["yalexs-ble==3.0.0"] } diff --git a/homeassistant/components/yalexs_ble/strings.json b/homeassistant/components/yalexs_ble/strings.json index c79830be3a9..92d807d01f6 100644 --- a/homeassistant/components/yalexs_ble/strings.json +++ b/homeassistant/components/yalexs_ble/strings.json @@ -51,6 +51,11 @@ "battery_voltage": { "name": "Battery voltage" } + }, + "lock": { + "secure_mode": { + "name": "Secure mode" + } } } } diff --git a/requirements_all.txt b/requirements_all.txt index d1a2c2a21ef..8749d653a2f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -3150,7 +3150,7 @@ yalesmartalarmclient==0.4.3 # homeassistant.components.august # homeassistant.components.yale # homeassistant.components.yalexs_ble -yalexs-ble==2.6.0 +yalexs-ble==3.0.0 # homeassistant.components.august # homeassistant.components.yale diff --git a/requirements_test_all.txt b/requirements_test_all.txt index b4bc9a82158..9e98ca26b18 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -2600,7 +2600,7 @@ yalesmartalarmclient==0.4.3 # homeassistant.components.august # homeassistant.components.yale # homeassistant.components.yalexs_ble -yalexs-ble==2.6.0 +yalexs-ble==3.0.0 # homeassistant.components.august # homeassistant.components.yale From dcad5bbe04e40083d24f78c84005a17a67e11355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Fri, 4 Jul 2025 21:26:36 +0000 Subject: [PATCH 051/106] Simplify unnecessary re.findall calls (#147907) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- homeassistant/components/downloader/services.py | 8 +++----- homeassistant/components/qbus/entity.py | 7 ++----- 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/downloader/services.py b/homeassistant/components/downloader/services.py index cce8c9d65b0..bb1b968dd99 100644 --- a/homeassistant/components/downloader/services.py +++ b/homeassistant/components/downloader/services.py @@ -65,12 +65,10 @@ def download_file(service: ServiceCall) -> None: else: if filename is None and "content-disposition" in req.headers: - match = re.findall( + if match := re.search( r"filename=(\S+)", req.headers["content-disposition"] - ) - - if match: - filename = match[0].strip("'\" ") + ): + filename = match.group(1).strip("'\" ") if not filename: filename = os.path.basename(url).strip() diff --git a/homeassistant/components/qbus/entity.py b/homeassistant/components/qbus/entity.py index ec800c15afa..91e4d83b548 100644 --- a/homeassistant/components/qbus/entity.py +++ b/homeassistant/components/qbus/entity.py @@ -48,11 +48,8 @@ def add_new_outputs( def format_ref_id(ref_id: str) -> str | None: """Format the Qbus ref_id.""" - matches: list[str] = re.findall(_REFID_REGEX, ref_id) - - if len(matches) > 0: - if ref_id := matches[0]: - return ref_id.replace("/", "-") + if match := _REFID_REGEX.search(ref_id): + return match.group(1).replace("/", "-") return None From 528daad8545813129b10adda91ea7cc2415916fa Mon Sep 17 00:00:00 2001 From: Thomas55555 <59625598+Thomas55555@users.noreply.github.com> Date: Fri, 4 Jul 2025 23:42:17 +0200 Subject: [PATCH 052/106] Constant polling for Husqvarna Automower (#147957) --- .../husqvarna_automower/coordinator.py | 20 ++++- .../husqvarna_automower/test_init.py | 73 ++++++++++++++++++- 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/husqvarna_automower/coordinator.py b/homeassistant/components/husqvarna_automower/coordinator.py index dc653d8ce80..70af5219d04 100644 --- a/homeassistant/components/husqvarna_automower/coordinator.py +++ b/homeassistant/components/husqvarna_automower/coordinator.py @@ -74,7 +74,7 @@ class AutomowerDataUpdateCoordinator(DataUpdateCoordinator[MowerDictionary]): """Subscribe for websocket and poll data from the API.""" if not self.ws_connected: await self.api.connect() - self.api.register_data_callback(self.callback) + self.api.register_data_callback(self.handle_websocket_updates) self.ws_connected = True try: data = await self.api.get_status() @@ -86,11 +86,27 @@ class AutomowerDataUpdateCoordinator(DataUpdateCoordinator[MowerDictionary]): return data @callback - def callback(self, ws_data: MowerDictionary) -> None: + def handle_websocket_updates(self, ws_data: MowerDictionary) -> None: """Process websocket callbacks and write them to the DataUpdateCoordinator.""" self.async_set_updated_data(ws_data) self._async_add_remove_devices_and_entities(ws_data) + @callback + def async_set_updated_data(self, data: MowerDictionary) -> None: + """Override DataUpdateCoordinator to preserve fixed polling interval. + + The built-in implementation resets the polling timer on every websocket + update. Since websockets do not deliver all required data (e.g. statistics + or work area details), we enforce a constant REST polling cadence. + """ + self.data = data + self.last_update_success = True + self.logger.debug( + "Manually updated %s data", + self.name, + ) + self.async_update_listeners() + async def client_listen( self, hass: HomeAssistant, diff --git a/tests/components/husqvarna_automower/test_init.py b/tests/components/husqvarna_automower/test_init.py index ecb92bb39cf..f2b468c4faf 100644 --- a/tests/components/husqvarna_automower/test_init.py +++ b/tests/components/husqvarna_automower/test_init.py @@ -1,7 +1,9 @@ """Tests for init module.""" from asyncio import Event -from datetime import datetime +from collections.abc import Callable +from copy import deepcopy +from datetime import datetime, timedelta import http import time from unittest.mock import AsyncMock, patch @@ -20,7 +22,7 @@ from syrupy.assertion import SnapshotAssertion from homeassistant.components.husqvarna_automower.const import DOMAIN, OAUTH2_TOKEN from homeassistant.components.husqvarna_automower.coordinator import SCAN_INTERVAL from homeassistant.config_entries import ConfigEntryState -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util import dt as dt_util @@ -221,6 +223,73 @@ async def test_device_info( assert reg_device == snapshot +async def test_constant_polling( + hass: HomeAssistant, + mock_automower_client: AsyncMock, + mock_config_entry: MockConfigEntry, + values: dict[str, MowerAttributes], + freezer: FrozenDateTimeFactory, +) -> None: + """Verify that receiving a WebSocket update does not interrupt the regular polling cycle. + + The test simulates a WebSocket update that changes an entity's state, then advances time + to trigger a scheduled poll to confirm polled data also arrives. + """ + test_values = deepcopy(values) + callback_holder: dict[str, Callable] = {} + + @callback + def fake_register_websocket_response( + cb: Callable[[dict[str, MowerAttributes]], None], + ) -> None: + callback_holder["cb"] = cb + + mock_automower_client.register_data_callback.side_effect = ( + fake_register_websocket_response + ) + await setup_integration(hass, mock_config_entry) + await hass.async_block_till_done() + + assert mock_automower_client.register_data_callback.called + assert "cb" in callback_holder + + state = hass.states.get("sensor.test_mower_1_battery") + assert state is not None + assert state.state == "100" + state = hass.states.get("sensor.test_mower_1_front_lawn_progress") + assert state is not None + assert state.state == "40" + + test_values[TEST_MOWER_ID].battery.battery_percent = 77 + + freezer.tick(SCAN_INTERVAL - timedelta(seconds=1)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + + callback_holder["cb"](test_values) + await hass.async_block_till_done() + + state = hass.states.get("sensor.test_mower_1_battery") + assert state is not None + assert state.state == "77" + state = hass.states.get("sensor.test_mower_1_front_lawn_progress") + assert state is not None + assert state.state == "40" + + test_values[TEST_MOWER_ID].work_areas[123456].progress = 50 + mock_automower_client.get_status.return_value = test_values + freezer.tick(timedelta(seconds=4)) + async_fire_time_changed(hass) + await hass.async_block_till_done() + mock_automower_client.get_status.assert_awaited() + state = hass.states.get("sensor.test_mower_1_battery") + assert state is not None + assert state.state == "77" + state = hass.states.get("sensor.test_mower_1_front_lawn_progress") + assert state is not None + assert state.state == "50" + + async def test_coordinator_automatic_registry_cleanup( hass: HomeAssistant, mock_automower_client: AsyncMock, From 76be2fdba1431b2d77ef9e86f2f8b441d3374233 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 5 Jul 2025 00:02:36 +0200 Subject: [PATCH 053/106] Improve (and align) deprecation messages (#147948) --- homeassistant/helpers/deprecation.py | 18 +++++++------- tests/common.py | 12 +++++----- tests/components/hassio/test_init.py | 8 +++++-- tests/helpers/test_deprecation.py | 35 ++++++++++++++-------------- tests/helpers/test_json.py | 4 ++-- tests/test_const.py | 4 ++-- tests/util/test_dt.py | 4 ++-- 7 files changed, 44 insertions(+), 41 deletions(-) diff --git a/homeassistant/helpers/deprecation.py b/homeassistant/helpers/deprecation.py index 20b5b7ebab9..29d9237de05 100644 --- a/homeassistant/helpers/deprecation.py +++ b/homeassistant/helpers/deprecation.py @@ -197,7 +197,7 @@ def _print_deprecation_warning_internal_impl( logger = logging.getLogger(module_name) if breaks_in_ha_version: - breaks_in = f" which will be removed in HA Core {breaks_in_ha_version}" + breaks_in = f" It will be removed in HA Core {breaks_in_ha_version}." else: breaks_in = "" try: @@ -205,9 +205,10 @@ def _print_deprecation_warning_internal_impl( except MissingIntegrationFrame: if log_when_no_integration_is_found: logger.warning( - "%s is a deprecated %s%s. Use %s instead", - obj_name, + "The deprecated %s %s was %s.%s Use %s instead", description, + obj_name, + verb, breaks_in, replacement, ) @@ -219,25 +220,22 @@ def _print_deprecation_warning_internal_impl( module=integration_frame.module, ) logger.warning( - ( - "%s was %s from %s, this is a deprecated %s%s. Use %s instead," - " please %s" - ), + ("The deprecated %s %s was %s from %s.%s Use %s instead, please %s"), + description, obj_name, verb, integration_frame.integration, - description, breaks_in, replacement, report_issue, ) else: logger.warning( - "%s was %s from %s, this is a deprecated %s%s. Use %s instead", + "The deprecated %s %s was %s from %s.%s Use %s instead", + description, obj_name, verb, integration_frame.integration, - description, breaks_in, replacement, ) diff --git a/tests/common.py b/tests/common.py index 7652a020117..e43e4bf5fee 100644 --- a/tests/common.py +++ b/tests/common.py @@ -1826,9 +1826,9 @@ def import_and_test_deprecated_constant( module.__name__, logging.WARNING, ( - f"{constant_name} was used from test_constant_deprecation," - f" this is a deprecated constant which will be removed in HA Core {breaks_in_ha_version}. " - f"Use {replacement_name} instead, please report " + f"The deprecated constant {constant_name} was used from " + "test_constant_deprecation. It will be removed in HA Core " + f"{breaks_in_ha_version}. Use {replacement_name} instead, please report " "it to the author of the 'test_constant_deprecation' custom integration" ), ) in caplog.record_tuples @@ -1860,9 +1860,9 @@ def import_and_test_deprecated_alias( module.__name__, logging.WARNING, ( - f"{alias_name} was used from test_constant_deprecation," - f" this is a deprecated alias which will be removed in HA Core {breaks_in_ha_version}. " - f"Use {replacement_name} instead, please report " + f"The deprecated alias {alias_name} was used from " + "test_constant_deprecation. It will be removed in HA Core " + f"{breaks_in_ha_version}. Use {replacement_name} instead, please report " "it to the author of the 'test_constant_deprecation' custom integration" ), ) in caplog.record_tuples diff --git a/tests/components/hassio/test_init.py b/tests/components/hassio/test_init.py index 2874ea726dc..f96ab8aca2a 100644 --- a/tests/components/hassio/test_init.py +++ b/tests/components/hassio/test_init.py @@ -1098,7 +1098,9 @@ def test_deprecated_function_is_hassio( ( "homeassistant.components.hassio", logging.WARNING, - "is_hassio is a deprecated function which will be removed in HA Core 2025.11. Use homeassistant.helpers.hassio.is_hassio instead", + "The deprecated function is_hassio was called. It will be " + "removed in HA Core 2025.11. Use homeassistant.helpers" + ".hassio.is_hassio instead", ) ] @@ -1114,7 +1116,9 @@ def test_deprecated_function_get_supervisor_ip( ( "homeassistant.helpers.hassio", logging.WARNING, - "get_supervisor_ip is a deprecated function which will be removed in HA Core 2025.11. Use homeassistant.helpers.hassio.get_supervisor_ip instead", + "The deprecated function get_supervisor_ip was called. It will " + "be removed in HA Core 2025.11. Use homeassistant.helpers" + ".hassio.get_supervisor_ip instead", ) ] diff --git a/tests/helpers/test_deprecation.py b/tests/helpers/test_deprecation.py index a74055c59ec..d45c9ce1546 100644 --- a/tests/helpers/test_deprecation.py +++ b/tests/helpers/test_deprecation.py @@ -135,7 +135,7 @@ def test_deprecated_class(mock_get_logger) -> None: ("breaks_in_ha_version", "extra_msg"), [ (None, ""), - ("2099.1", " which will be removed in HA Core 2099.1"), + ("2099.1", " It will be removed in HA Core 2099.1."), ], ) def test_deprecated_function( @@ -154,8 +154,9 @@ def test_deprecated_function( mock_deprecated_function() assert ( - f"mock_deprecated_function is a deprecated function{extra_msg}. " - "Use new_function instead" + "The deprecated function mock_deprecated_function was called." + f"{extra_msg}" + " Use new_function instead" ) in caplog.text @@ -163,7 +164,7 @@ def test_deprecated_function( ("breaks_in_ha_version", "extra_msg"), [ (None, ""), - ("2099.1", " which will be removed in HA Core 2099.1"), + ("2099.1", " It will be removed in HA Core 2099.1."), ], ) def test_deprecated_function_called_from_built_in_integration( @@ -210,9 +211,9 @@ def test_deprecated_function_called_from_built_in_integration( ): mock_deprecated_function() assert ( - "mock_deprecated_function was called from hue, " - f"this is a deprecated function{extra_msg}. " - "Use new_function instead" + "The deprecated function mock_deprecated_function was called from hue." + f"{extra_msg}" + " Use new_function instead" ) in caplog.text @@ -220,7 +221,7 @@ def test_deprecated_function_called_from_built_in_integration( ("breaks_in_ha_version", "extra_msg"), [ (None, ""), - ("2099.1", " which will be removed in HA Core 2099.1"), + ("2099.1", " It will be removed in HA Core 2099.1."), ], ) def test_deprecated_function_called_from_custom_integration( @@ -270,9 +271,9 @@ def test_deprecated_function_called_from_custom_integration( ): mock_deprecated_function() assert ( - "mock_deprecated_function was called from hue, " - f"this is a deprecated function{extra_msg}. " - "Use new_function instead, please report it to the author of the " + "The deprecated function mock_deprecated_function was called from hue." + f"{extra_msg}" + " Use new_function instead, please report it to the author of the " "'hue' custom integration" ) in caplog.text @@ -316,7 +317,7 @@ def _get_value( ), ( DeprecatedConstant(1, "NEW_CONSTANT", "2099.1"), - " which will be removed in HA Core 2099.1. Use NEW_CONSTANT instead", + ". It will be removed in HA Core 2099.1. Use NEW_CONSTANT instead", "constant", ), ( @@ -326,7 +327,7 @@ def _get_value( ), ( DeprecatedConstantEnum(TestDeprecatedConstantEnum.TEST, "2099.1"), - " which will be removed in HA Core 2099.1. Use TestDeprecatedConstantEnum.TEST instead", + ". It will be removed in HA Core 2099.1. Use TestDeprecatedConstantEnum.TEST instead", "constant", ), ( @@ -336,7 +337,7 @@ def _get_value( ), ( DeprecatedAlias(1, "new_alias", "2099.1"), - " which will be removed in HA Core 2099.1. Use new_alias instead", + ". It will be removed in HA Core 2099.1. Use new_alias instead", "alias", ), ], @@ -405,7 +406,7 @@ def test_check_if_deprecated_constant( assert ( module_name, logging.WARNING, - f"TEST_CONSTANT was used from hue, this is a deprecated {description}{extra_msg}{extra_extra_msg}", + f"The deprecated {description} TEST_CONSTANT was used from hue{extra_msg}{extra_extra_msg}", ) in caplog.record_tuples @@ -594,7 +595,7 @@ def test_enum_with_deprecated_members( "tests.helpers.test_deprecation", logging.WARNING, ( - "TestEnum.CATS was used from hue, this is a deprecated enum member which " + "The deprecated enum member TestEnum.CATS was used from hue. It " "will be removed in HA Core 2025.11.0. Use TestEnum.CATS_PER_CM instead" f"{extra_extra_msg}" ), @@ -603,7 +604,7 @@ def test_enum_with_deprecated_members( "tests.helpers.test_deprecation", logging.WARNING, ( - "TestEnum.DOGS was used from hue, this is a deprecated enum member. Use " + "The deprecated enum member TestEnum.DOGS was used from hue. Use " f"TestEnum.DOGS_PER_CM instead{extra_extra_msg}" ), ) in caplog.record_tuples diff --git a/tests/helpers/test_json.py b/tests/helpers/test_json.py index 94f21da1781..413e7e0dc9d 100644 --- a/tests/helpers/test_json.py +++ b/tests/helpers/test_json.py @@ -359,8 +359,8 @@ def test_deprecated_json_loads(caplog: pytest.LogCaptureFixture) -> None: """ json_helper.json_loads("{}") assert ( - "json_loads is a deprecated function which will be removed in " - "HA Core 2025.8. Use homeassistant.util.json.json_loads instead" + "The deprecated function json_loads was called. It will be removed " + "in HA Core 2025.8. Use homeassistant.util.json.json_loads instead" ) in caplog.text diff --git a/tests/test_const.py b/tests/test_const.py index a039545a004..f1ceaad6a08 100644 --- a/tests/test_const.py +++ b/tests/test_const.py @@ -166,8 +166,8 @@ def test_deprecated_unit_of_conductivity_members( def deprecation_message(member: str, replacement: str) -> str: return ( - f"UnitOfConductivity.{member} was used from hue, this is a deprecated enum " - "member which will be removed in HA Core 2025.11.0. Use UnitOfConductivity." + f"The deprecated enum member UnitOfConductivity.{member} was used from hue. " + "It will be removed in HA Core 2025.11.0. Use UnitOfConductivity." f"{replacement} instead, please report it to the author of the 'hue' custom" " integration" ) diff --git a/tests/util/test_dt.py b/tests/util/test_dt.py index 3f288962009..c357f5cf39c 100644 --- a/tests/util/test_dt.py +++ b/tests/util/test_dt.py @@ -121,8 +121,8 @@ def test_timestamp_to_utc(caplog: pytest.LogCaptureFixture) -> None: utc_now = dt_util.utcnow() assert dt_util.utc_to_timestamp(utc_now) == utc_now.timestamp() assert ( - "utc_to_timestamp is a deprecated function which will be removed " - "in HA Core 2026.1. Use datetime.timestamp instead" in caplog.text + "The deprecated function utc_to_timestamp was called. It will be " + "removed in HA Core 2026.1. Use datetime.timestamp instead" in caplog.text ) From 12b90f3c8ecbe6d2129fe495ac1543cf93f48cc1 Mon Sep 17 00:00:00 2001 From: Arie Catsman <120491684+catsmanac@users.noreply.github.com> Date: Sat, 5 Jul 2025 00:14:51 +0200 Subject: [PATCH 054/106] Add debug logs to trace enphase auth process at load. (#148117) --- .../components/enphase_envoy/coordinator.py | 8 ++++ tests/components/enphase_envoy/test_init.py | 39 +++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/homeassistant/components/enphase_envoy/coordinator.py b/homeassistant/components/enphase_envoy/coordinator.py index cfff0777af5..57ce924733c 100644 --- a/homeassistant/components/enphase_envoy/coordinator.py +++ b/homeassistant/components/enphase_envoy/coordinator.py @@ -220,6 +220,7 @@ class EnphaseUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): await envoy.setup() assert envoy.serial_number is not None self.envoy_serial_number = envoy.serial_number + _LOGGER.debug("Envoy setup complete for serial: %s", self.envoy_serial_number) if token := self.config_entry.data.get(CONF_TOKEN): with contextlib.suppress(*INVALID_AUTH_ERRORS): # Always set the username and password @@ -227,6 +228,7 @@ class EnphaseUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): await envoy.authenticate( username=self.username, password=self.password, token=token ) + _LOGGER.debug("Authorized, validating token lifetime") # The token is valid, but we still want # to refresh it if it's stale right away self._async_refresh_token_if_needed(dt_util.utcnow()) @@ -234,6 +236,8 @@ class EnphaseUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): # token likely expired or firmware changed # so we fall through to authenticate with # username/password + _LOGGER.debug("setup and auth got INVALID_AUTH_ERRORS") + _LOGGER.debug("Authenticate with username/password only") await self.envoy.authenticate(username=self.username, password=self.password) # Password auth succeeded, so we can update the token # if we are using EnvoyTokenAuth @@ -262,13 +266,16 @@ class EnphaseUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): for tries in range(2): try: if not self._setup_complete: + _LOGGER.debug("update on try %s, setup not complete", tries) await self._async_setup_and_authenticate() self._async_mark_setup_complete() # dump all received data in debug mode to assist troubleshooting envoy_data = await envoy.update() except INVALID_AUTH_ERRORS as err: + _LOGGER.debug("update on try %s, INVALID_AUTH_ERRORS %s", tries, err) if self._setup_complete and tries == 0: # token likely expired or firmware changed, try to re-authenticate + _LOGGER.debug("update on try %s, setup was complete, retry", tries) self._setup_complete = False continue raise ConfigEntryAuthFailed( @@ -280,6 +287,7 @@ class EnphaseUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): }, ) from err except EnvoyError as err: + _LOGGER.debug("update on try %s, EnvoyError %s", tries, err) raise UpdateFailed( translation_domain=DOMAIN, translation_key="envoy_error", diff --git a/tests/components/enphase_envoy/test_init.py b/tests/components/enphase_envoy/test_init.py index a738b31c183..c43be96d8b1 100644 --- a/tests/components/enphase_envoy/test_init.py +++ b/tests/components/enphase_envoy/test_init.py @@ -229,6 +229,45 @@ async def test_coordinator_token_refresh_error( assert entity_state.state == "116" +@respx.mock +@pytest.mark.freeze_time("2024-07-23 00:00:00+00:00") +async def test_coordinator_first_update_auth_error( + hass: HomeAssistant, + mock_envoy: AsyncMock, +) -> None: + """Test coordinator update error handling.""" + current_token = encode( + # some time in future + payload={"name": "envoy", "exp": 1927314600}, + key="secret", + algorithm="HS256", + ) + + # mock envoy with expired token in config + entry = MockConfigEntry( + domain=DOMAIN, + entry_id="45a36e55aaddb2007c5f6602e0c38e72", + title="Envoy 1234", + unique_id="1234", + data={ + CONF_HOST: "1.1.1.1", + CONF_NAME: "Envoy 1234", + CONF_USERNAME: "test-username", + CONF_PASSWORD: "test-password", + CONF_TOKEN: current_token, + }, + ) + mock_envoy.auth = EnvoyTokenAuth( + "127.0.0.1", + token=current_token, + envoy_serial="1234", + cloud_username="test_username", + cloud_password="test_password", + ) + mock_envoy.authenticate.side_effect = EnvoyAuthenticationError("Failing test") + await setup_integration(hass, entry, ConfigEntryState.SETUP_ERROR) + + async def test_config_no_unique_id( hass: HomeAssistant, mock_envoy: AsyncMock, From e592e565c0947c291abd6df7d5ccba42bfcd9ec7 Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Sat, 5 Jul 2025 07:20:42 +0200 Subject: [PATCH 055/106] Make ready time sensors unavailable instead in lamarzocco (#147985) --- homeassistant/components/lamarzocco/sensor.py | 16 +++++++++++++++- .../lamarzocco/snapshots/test_sensor.ambr | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/lamarzocco/sensor.py b/homeassistant/components/lamarzocco/sensor.py index a432f5b8dae..1f4983a03a8 100644 --- a/homeassistant/components/lamarzocco/sensor.py +++ b/homeassistant/components/lamarzocco/sensor.py @@ -56,6 +56,13 @@ ENTITIES: tuple[LaMarzoccoSensorEntityDescription, ...] = ( CoffeeBoiler, config[WidgetType.CM_COFFEE_BOILER] ).ready_start_time ), + available_fn=( + lambda coordinator: cast( + CoffeeBoiler, + coordinator.device.dashboard.config[WidgetType.CM_COFFEE_BOILER], + ).ready_start_time + is not None + ), entity_category=EntityCategory.DIAGNOSTIC, ), LaMarzoccoSensorEntityDescription( @@ -67,11 +74,18 @@ ENTITIES: tuple[LaMarzoccoSensorEntityDescription, ...] = ( SteamBoilerLevel, config[WidgetType.CM_STEAM_BOILER_LEVEL] ).ready_start_time ), - entity_category=EntityCategory.DIAGNOSTIC, supported_fn=( lambda coordinator: coordinator.device.dashboard.model_name in (ModelName.LINEA_MICRA, ModelName.LINEA_MINI_R) ), + available_fn=( + lambda coordinator: cast( + SteamBoilerLevel, + coordinator.device.dashboard.config[WidgetType.CM_STEAM_BOILER_LEVEL], + ).ready_start_time + is not None + ), + entity_category=EntityCategory.DIAGNOSTIC, ), LaMarzoccoSensorEntityDescription( key="brewing_start_time", diff --git a/tests/components/lamarzocco/snapshots/test_sensor.ambr b/tests/components/lamarzocco/snapshots/test_sensor.ambr index eea4616d0ff..3dd1ff9b665 100644 --- a/tests/components/lamarzocco/snapshots/test_sensor.ambr +++ b/tests/components/lamarzocco/snapshots/test_sensor.ambr @@ -94,7 +94,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'unknown', + 'state': 'unavailable', }) # --- # name: test_sensors[sensor.gs012345_last_cleaning_time-entry] From e63e6a6072f024e7f3edd59af6f1693228e42b8e Mon Sep 17 00:00:00 2001 From: Matt Zimmerman Date: Fri, 4 Jul 2025 23:08:52 -0700 Subject: [PATCH 056/106] Bump python-smarttub to 0.0.43 (#147317) --- homeassistant/components/smarttub/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/smarttub/manifest.json b/homeassistant/components/smarttub/manifest.json index b8d81db0ea5..086446c4c66 100644 --- a/homeassistant/components/smarttub/manifest.json +++ b/homeassistant/components/smarttub/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/smarttub", "iot_class": "cloud_polling", "loggers": ["smarttub"], - "requirements": ["python-smarttub==0.0.39"] + "requirements": ["python-smarttub==0.0.44"] } diff --git a/requirements_all.txt b/requirements_all.txt index 8749d653a2f..80a824cf44f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2502,7 +2502,7 @@ python-ripple-api==0.0.3 python-roborock==2.18.2 # homeassistant.components.smarttub -python-smarttub==0.0.39 +python-smarttub==0.0.44 # homeassistant.components.snoo python-snoo==0.6.6 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 9e98ca26b18..88dc21a0e07 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -2072,7 +2072,7 @@ python-rabbitair==0.0.8 python-roborock==2.18.2 # homeassistant.components.smarttub -python-smarttub==0.0.39 +python-smarttub==0.0.44 # homeassistant.components.snoo python-snoo==0.6.6 From 275d390a6c9d4b15edbbc0c0b8d3c02ed1b065ce Mon Sep 17 00:00:00 2001 From: Andrey Kupreychik Date: Sat, 5 Jul 2025 10:52:43 +0400 Subject: [PATCH 057/106] Add reconfiguration support for keenetic_ndms2 integration (#142191) Co-authored-by: Franck Nijhof Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Franck Nijhof --- .../components/keenetic_ndms2/config_flow.py | 32 +++++++++++++--- .../components/keenetic_ndms2/strings.json | 3 +- tests/components/keenetic_ndms2/__init__.py | 6 +++ .../keenetic_ndms2/test_config_flow.py | 37 ++++++++++++++++++- 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/keenetic_ndms2/config_flow.py b/homeassistant/components/keenetic_ndms2/config_flow.py index 3862d34398f..c6095968c07 100644 --- a/homeassistant/components/keenetic_ndms2/config_flow.py +++ b/homeassistant/components/keenetic_ndms2/config_flow.py @@ -8,7 +8,12 @@ from urllib.parse import urlparse from ndms2_client import Client, ConnectionException, InterfaceInfo, TelnetConnection import voluptuous as vol -from homeassistant.config_entries import ConfigFlow, ConfigFlowResult, OptionsFlow +from homeassistant.config_entries import ( + SOURCE_RECONFIGURE, + ConfigFlow, + ConfigFlowResult, + OptionsFlow, +) from homeassistant.const import ( CONF_HOST, CONF_PASSWORD, @@ -45,7 +50,7 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): VERSION = 1 - host: str | bytes | None = None + _host: str | bytes | None = None @staticmethod @callback @@ -61,8 +66,9 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): """Handle a flow initialized by the user.""" errors = {} if user_input is not None: - host = self.host or user_input[CONF_HOST] - self._async_abort_entries_match({CONF_HOST: host}) + host = self._host or user_input[CONF_HOST] + if self.source != SOURCE_RECONFIGURE: + self._async_abort_entries_match({CONF_HOST: host}) _client = Client( TelnetConnection( @@ -81,12 +87,17 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): except ConnectionException: errors["base"] = "cannot_connect" else: + if self.source == SOURCE_RECONFIGURE: + return self.async_update_reload_and_abort( + self._get_reconfigure_entry(), + data={CONF_HOST: host, **user_input}, + ) return self.async_create_entry( title=router_info.name, data={CONF_HOST: host, **user_input} ) host_schema: VolDictType = ( - {vol.Required(CONF_HOST): str} if not self.host else {} + {vol.Required(CONF_HOST): str} if not self._host else {} ) return self.async_show_form( @@ -102,6 +113,15 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): errors=errors, ) + async def async_step_reconfigure( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Handle reconfiguration.""" + existing_entry_data = dict(self._get_reconfigure_entry().data) + self._host = existing_entry_data[CONF_HOST] + + return await self.async_step_user(user_input) + async def async_step_ssdp( self, discovery_info: SsdpServiceInfo ) -> ConfigFlowResult: @@ -124,7 +144,7 @@ class KeeneticFlowHandler(ConfigFlow, domain=DOMAIN): self._async_abort_entries_match({CONF_HOST: host}) - self.host = host + self._host = host self.context["title_placeholders"] = { "name": friendly_name, "host": host, diff --git a/homeassistant/components/keenetic_ndms2/strings.json b/homeassistant/components/keenetic_ndms2/strings.json index 93b59be122d..3098996d48f 100644 --- a/homeassistant/components/keenetic_ndms2/strings.json +++ b/homeassistant/components/keenetic_ndms2/strings.json @@ -21,7 +21,8 @@ "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_account%]", "no_udn": "SSDP discovery info has no UDN", - "not_keenetic_ndms2": "Discovered device is not a Keenetic router" + "not_keenetic_ndms2": "Discovered device is not a Keenetic router", + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]" } }, "options": { diff --git a/tests/components/keenetic_ndms2/__init__.py b/tests/components/keenetic_ndms2/__init__.py index dc0c89e8ea6..dc812af6d01 100644 --- a/tests/components/keenetic_ndms2/__init__.py +++ b/tests/components/keenetic_ndms2/__init__.py @@ -25,6 +25,12 @@ MOCK_DATA = { CONF_PORT: 23, } +MOCK_RECONFIGURE = { + CONF_USERNAME: "user1", + CONF_PASSWORD: "pass1", + CONF_PORT: 123, +} + MOCK_OPTIONS = { CONF_SCAN_INTERVAL: 15, const.CONF_CONSIDER_HOME: 150, diff --git a/tests/components/keenetic_ndms2/test_config_flow.py b/tests/components/keenetic_ndms2/test_config_flow.py index 3293bd3d4da..1b86e6c265c 100644 --- a/tests/components/keenetic_ndms2/test_config_flow.py +++ b/tests/components/keenetic_ndms2/test_config_flow.py @@ -19,7 +19,14 @@ from homeassistant.helpers.service_info.ssdp import ( ATTR_UPNP_UDN, ) -from . import MOCK_DATA, MOCK_NAME, MOCK_OPTIONS, MOCK_SSDP_DISCOVERY_INFO +from . import ( + MOCK_DATA, + MOCK_IP, + MOCK_NAME, + MOCK_OPTIONS, + MOCK_RECONFIGURE, + MOCK_SSDP_DISCOVERY_INFO, +) from tests.common import MockConfigEntry @@ -75,6 +82,34 @@ async def test_flow_works(hass: HomeAssistant, connect) -> None: assert len(mock_setup_entry.mock_calls) == 1 +async def test_reconfigure(hass: HomeAssistant, connect) -> None: + """Test reconfigure flow.""" + entry = MockConfigEntry(domain=keenetic.DOMAIN, data=MOCK_DATA) + entry.add_to_hass(hass) + + result = await entry.start_reconfigure_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + with patch( + "homeassistant.components.keenetic_ndms2.async_setup_entry", return_value=True + ) as mock_setup_entry: + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input=MOCK_RECONFIGURE, + ) + await hass.async_block_till_done() + + assert result2["type"] is FlowResultType.ABORT + assert result2["reason"] == "reconfigure_successful" + assert entry.data == { + CONF_HOST: MOCK_IP, + **MOCK_RECONFIGURE, + } + assert len(mock_setup_entry.mock_calls) == 1 + + async def test_options(hass: HomeAssistant) -> None: """Test updating options.""" entry = MockConfigEntry(domain=keenetic.DOMAIN, data=MOCK_DATA) From 3cfff4de3a4846f07a73f1294506fe86b7735cb4 Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Sat, 5 Jul 2025 00:09:02 -0700 Subject: [PATCH 058/106] Add a preview to history_stats options flow (#145721) --- .../components/history_stats/config_flow.py | 130 ++++++- .../components/history_stats/coordinator.py | 7 + .../components/history_stats/data.py | 6 +- .../components/history_stats/helpers.py | 13 +- .../components/history_stats/sensor.py | 32 +- .../history_stats/test_config_flow.py | 358 +++++++++++++++++- 6 files changed, 536 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/history_stats/config_flow.py b/homeassistant/components/history_stats/config_flow.py index ca3d5229b6b..996c7ba0d0c 100644 --- a/homeassistant/components/history_stats/config_flow.py +++ b/homeassistant/components/history_stats/config_flow.py @@ -3,11 +3,15 @@ from __future__ import annotations from collections.abc import Mapping +from datetime import timedelta from typing import Any, cast import voluptuous as vol +from homeassistant.components import websocket_api from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_STATE, CONF_TYPE +from homeassistant.core import HomeAssistant, callback +from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.schema_config_entry_flow import ( SchemaCommonFlowHandler, SchemaConfigFlowHandler, @@ -26,6 +30,7 @@ from homeassistant.helpers.selector import ( TextSelector, TextSelectorConfig, ) +from homeassistant.helpers.template import Template from .const import ( CONF_DURATION, @@ -37,14 +42,21 @@ from .const import ( DEFAULT_NAME, DOMAIN, ) +from .coordinator import HistoryStatsUpdateCoordinator +from .data import HistoryStats +from .sensor import HistoryStatsSensor + + +def _validate_two_period_keys(user_input: dict[str, Any]) -> None: + if sum(param in user_input for param in CONF_PERIOD_KEYS) != 2: + raise SchemaFlowError("only_two_keys_allowed") async def validate_options( handler: SchemaCommonFlowHandler, user_input: dict[str, Any] ) -> dict[str, Any]: """Validate options selected.""" - if sum(param in user_input for param in CONF_PERIOD_KEYS) != 2: - raise SchemaFlowError("only_two_keys_allowed") + _validate_two_period_keys(user_input) handler.parent_handler._async_abort_entries_match({**handler.options, **user_input}) # noqa: SLF001 @@ -97,12 +109,14 @@ CONFIG_FLOW = { "options": SchemaFlowFormStep( schema=DATA_SCHEMA_OPTIONS, validate_user_input=validate_options, + preview="history_stats", ), } OPTIONS_FLOW = { "init": SchemaFlowFormStep( DATA_SCHEMA_OPTIONS, validate_user_input=validate_options, + preview="history_stats", ), } @@ -116,3 +130,115 @@ class HistoryStatsConfigFlowHandler(SchemaConfigFlowHandler, domain=DOMAIN): def async_config_entry_title(self, options: Mapping[str, Any]) -> str: """Return config entry title.""" return cast(str, options[CONF_NAME]) + + @staticmethod + async def async_setup_preview(hass: HomeAssistant) -> None: + """Set up preview WS API.""" + websocket_api.async_register_command(hass, ws_start_preview) + + +@websocket_api.websocket_command( + { + vol.Required("type"): "history_stats/start_preview", + vol.Required("flow_id"): str, + vol.Required("flow_type"): vol.Any("config_flow", "options_flow"), + vol.Required("user_input"): dict, + } +) +@websocket_api.async_response +async def ws_start_preview( + hass: HomeAssistant, + connection: websocket_api.ActiveConnection, + msg: dict[str, Any], +) -> None: + """Generate a preview.""" + if msg["flow_type"] == "config_flow": + flow_status = hass.config_entries.flow.async_get(msg["flow_id"]) + flow_sets = hass.config_entries.flow._handler_progress_index.get( # noqa: SLF001 + flow_status["handler"] + ) + options = {} + assert flow_sets + for active_flow in flow_sets: + options = active_flow._common_handler.options # type: ignore [attr-defined] # noqa: SLF001 + config_entry = hass.config_entries.async_get_entry(flow_status["handler"]) + entity_id = options[CONF_ENTITY_ID] + name = options[CONF_NAME] + else: + flow_status = hass.config_entries.options.async_get(msg["flow_id"]) + config_entry = hass.config_entries.async_get_entry(flow_status["handler"]) + if not config_entry: + raise HomeAssistantError("Config entry not found") + entity_id = config_entry.options[CONF_ENTITY_ID] + name = config_entry.options[CONF_NAME] + + @callback + def async_preview_updated( + last_exception: Exception | None, state: str, attributes: Mapping[str, Any] + ) -> None: + """Forward config entry state events to websocket.""" + if last_exception: + connection.send_message( + websocket_api.event_message( + msg["id"], {"error": str(last_exception) or "Unknown error"} + ) + ) + else: + connection.send_message( + websocket_api.event_message( + msg["id"], {"attributes": attributes, "state": state} + ) + ) + + for param in CONF_PERIOD_KEYS: + if param in msg["user_input"] and not bool(msg["user_input"][param]): + del msg["user_input"][param] # Remove falsy values before counting keys + + validated_data: Any = None + try: + validated_data = DATA_SCHEMA_OPTIONS(msg["user_input"]) + except vol.Invalid as ex: + connection.send_error(msg["id"], "invalid_schema", str(ex)) + return + + try: + _validate_two_period_keys(validated_data) + except SchemaFlowError: + connection.send_error( + msg["id"], + "invalid_schema", + f"Exactly two of {', '.join(CONF_PERIOD_KEYS)} required", + ) + return + + sensor_type = validated_data.get(CONF_TYPE) + entity_states = validated_data.get(CONF_STATE) + start = validated_data.get(CONF_START) + end = validated_data.get(CONF_END) + duration = validated_data.get(CONF_DURATION) + + history_stats = HistoryStats( + hass, + entity_id, + entity_states, + Template(start, hass) if start else None, + Template(end, hass) if end else None, + timedelta(**duration) if duration else None, + True, + ) + coordinator = HistoryStatsUpdateCoordinator(hass, history_stats, None, name, True) + await coordinator.async_refresh() + preview_entity = HistoryStatsSensor( + hass, coordinator, sensor_type, name, None, entity_id + ) + preview_entity.hass = hass + + connection.send_result(msg["id"]) + cancel_listener = coordinator.async_setup_state_listener() + cancel_preview = await preview_entity.async_start_preview(async_preview_updated) + + def unsub() -> None: + cancel_listener() + cancel_preview() + + connection.subscriptions[msg["id"]] = unsub diff --git a/homeassistant/components/history_stats/coordinator.py b/homeassistant/components/history_stats/coordinator.py index fafbb5d3ce0..091e1da6ad8 100644 --- a/homeassistant/components/history_stats/coordinator.py +++ b/homeassistant/components/history_stats/coordinator.py @@ -36,12 +36,14 @@ class HistoryStatsUpdateCoordinator(DataUpdateCoordinator[HistoryStatsState]): history_stats: HistoryStats, config_entry: ConfigEntry | None, name: str, + preview: bool = False, ) -> None: """Initialize DataUpdateCoordinator.""" self._history_stats = history_stats self._subscriber_count = 0 self._at_start_listener: CALLBACK_TYPE | None = None self._track_events_listener: CALLBACK_TYPE | None = None + self._preview = preview super().__init__( hass, _LOGGER, @@ -104,3 +106,8 @@ class HistoryStatsUpdateCoordinator(DataUpdateCoordinator[HistoryStatsState]): return await self._history_stats.async_update(None) except (TemplateError, TypeError, ValueError) as ex: raise UpdateFailed(ex) from ex + + async def async_refresh(self) -> None: + """Refresh data and log errors.""" + log_failures = not self._preview + await self._async_refresh(log_failures) diff --git a/homeassistant/components/history_stats/data.py b/homeassistant/components/history_stats/data.py index fd950dbba23..569483df687 100644 --- a/homeassistant/components/history_stats/data.py +++ b/homeassistant/components/history_stats/data.py @@ -47,6 +47,7 @@ class HistoryStats: start: Template | None, end: Template | None, duration: datetime.timedelta | None, + preview: bool = False, ) -> None: """Init the history stats manager.""" self.hass = hass @@ -59,6 +60,7 @@ class HistoryStats: self._duration = duration self._start = start self._end = end + self._preview = preview self._pending_events: list[Event[EventStateChangedData]] = [] self._query_count = 0 @@ -70,7 +72,9 @@ class HistoryStats: # Get previous values of start and end previous_period_start, previous_period_end = self._period # Parse templates - self._period = async_calculate_period(self._duration, self._start, self._end) + self._period = async_calculate_period( + self._duration, self._start, self._end, log_errors=not self._preview + ) # Get the current period current_period_start, current_period_end = self._period diff --git a/homeassistant/components/history_stats/helpers.py b/homeassistant/components/history_stats/helpers.py index 99214a51369..b0ed132c1ef 100644 --- a/homeassistant/components/history_stats/helpers.py +++ b/homeassistant/components/history_stats/helpers.py @@ -23,6 +23,7 @@ def async_calculate_period( duration: datetime.timedelta | None, start_template: Template | None, end_template: Template | None, + log_errors: bool = True, ) -> tuple[datetime.datetime, datetime.datetime]: """Parse the templates and return the period.""" bounds: dict[str, datetime.datetime | None] = { @@ -37,13 +38,17 @@ def async_calculate_period( if template is None: continue try: - rendered = template.async_render() + rendered = template.async_render( + log_fn=None if log_errors else lambda *args, **kwargs: None + ) except (TemplateError, TypeError) as ex: - if ex.args and not ex.args[0].startswith( - "UndefinedError: 'None' has no attribute" + if ( + log_errors + and ex.args + and not ex.args[0].startswith("UndefinedError: 'None' has no attribute") ): _LOGGER.error("Error parsing template for field %s", bound, exc_info=ex) - raise + raise type(ex)(f"Error parsing template for field {bound}: {ex}") from ex if isinstance(rendered, str): bounds[bound] = dt_util.parse_datetime(rendered) if bounds[bound] is not None: diff --git a/homeassistant/components/history_stats/sensor.py b/homeassistant/components/history_stats/sensor.py index 6935b13bc3d..780bff14eb1 100644 --- a/homeassistant/components/history_stats/sensor.py +++ b/homeassistant/components/history_stats/sensor.py @@ -3,6 +3,7 @@ from __future__ import annotations from abc import abstractmethod +from collections.abc import Callable, Mapping import datetime from typing import Any @@ -23,7 +24,7 @@ from homeassistant.const import ( PERCENTAGE, UnitOfTime, ) -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import CALLBACK_TYPE, HomeAssistant, callback from homeassistant.exceptions import PlatformNotReady from homeassistant.helpers import config_validation as cv from homeassistant.helpers.device import async_device_info_to_link_from_entity @@ -183,6 +184,9 @@ class HistoryStatsSensor(HistoryStatsSensorBase): ) -> None: """Initialize the HistoryStats sensor.""" super().__init__(coordinator, name) + self._preview_callback: ( + Callable[[Exception | None, str, Mapping[str, Any]], None] | None + ) = None self._attr_native_unit_of_measurement = UNITS[sensor_type] self._type = sensor_type self._attr_unique_id = unique_id @@ -212,3 +216,29 @@ class HistoryStatsSensor(HistoryStatsSensorBase): self._attr_native_value = pretty_ratio(state.seconds_matched, state.period) elif self._type == CONF_TYPE_COUNT: self._attr_native_value = state.match_count + + if self._preview_callback: + calculated_state = self._async_calculate_state() + self._preview_callback( + None, calculated_state.state, calculated_state.attributes + ) + + async def async_start_preview( + self, + preview_callback: Callable[[Exception | None, str, Mapping[str, Any]], None], + ) -> CALLBACK_TYPE: + """Render a preview.""" + + self.async_on_remove( + self.coordinator.async_add_listener(self._process_update, None) + ) + + self._preview_callback = preview_callback + calculated_state = self._async_calculate_state() + preview_callback( + self.coordinator.last_exception, + calculated_state.state, + calculated_state.attributes, + ) + + return self._call_on_remove_callbacks diff --git a/tests/components/history_stats/test_config_flow.py b/tests/components/history_stats/test_config_flow.py index a695a06995e..a1f0a080b8a 100644 --- a/tests/components/history_stats/test_config_flow.py +++ b/tests/components/history_stats/test_config_flow.py @@ -2,22 +2,28 @@ from __future__ import annotations -from unittest.mock import AsyncMock +import logging +from unittest.mock import AsyncMock, patch + +from freezegun import freeze_time from homeassistant import config_entries from homeassistant.components.history_stats.const import ( CONF_DURATION, CONF_END, CONF_START, + CONF_TYPE_COUNT, DEFAULT_NAME, DOMAIN, ) from homeassistant.components.recorder import Recorder from homeassistant.const import CONF_ENTITY_ID, CONF_NAME, CONF_STATE, CONF_TYPE -from homeassistant.core import HomeAssistant +from homeassistant.core import HomeAssistant, State from homeassistant.data_entry_flow import FlowResultType +from homeassistant.util import dt as dt_util from tests.common import MockConfigEntry +from tests.typing import WebSocketGenerator async def test_form( @@ -193,3 +199,351 @@ async def test_entry_already_exist( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured" + + +async def test_config_flow_preview_success( + recorder_mock: Recorder, + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, +) -> None: + """Test the config flow preview.""" + client = await hass_ws_client(hass) + + # add state for the tests + await hass.config.async_set_time_zone("UTC") + utcnow = dt_util.utcnow() + start_time = utcnow.replace(hour=0, minute=0, second=0, microsecond=0) + t1 = start_time.replace(hour=3) + t2 = start_time.replace(hour=4) + t3 = start_time.replace(hour=5) + + monitored_entity = "binary_sensor.state" + + def _fake_states(*args, **kwargs): + return { + monitored_entity: [ + State( + monitored_entity, + "on", + last_changed=start_time, + last_updated=start_time, + ), + State( + monitored_entity, + "off", + last_changed=t1, + last_updated=t1, + ), + State( + monitored_entity, + "on", + last_changed=t2, + last_updated=t2, + ), + ] + } + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] is None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_NAME: DEFAULT_NAME, + CONF_ENTITY_ID: monitored_entity, + CONF_TYPE: CONF_TYPE_COUNT, + CONF_STATE: ["on"], + }, + ) + await hass.async_block_till_done() + assert result["type"] == FlowResultType.FORM + assert result["step_id"] == "options" + assert result["errors"] is None + assert result["preview"] == "history_stats" + + with ( + patch( + "homeassistant.components.recorder.history.state_changes_during_period", + _fake_states, + ), + freeze_time(t3), + ): + await client.send_json_auto_id( + { + "type": "history_stats/start_preview", + "flow_id": result["flow_id"], + "flow_type": "config_flow", + "user_input": { + CONF_ENTITY_ID: monitored_entity, + CONF_TYPE: CONF_TYPE_COUNT, + CONF_STATE: ["on"], + CONF_END: "{{now()}}", + CONF_START: "{{ today_at() }}", + }, + } + ) + msg = await client.receive_json() + assert msg["success"] + assert msg["result"] is None + + msg = await client.receive_json() + assert msg["event"]["state"] == "2" + + +async def test_options_flow_preview( + recorder_mock: Recorder, + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, +) -> None: + """Test the options flow preview.""" + logging.getLogger("sqlalchemy.engine").setLevel(logging.ERROR) + client = await hass_ws_client(hass) + + # add state for the tests + await hass.config.async_set_time_zone("UTC") + utcnow = dt_util.utcnow() + start_time = utcnow.replace(hour=0, minute=0, second=0, microsecond=0) + t1 = start_time.replace(hour=3) + t2 = start_time.replace(hour=4) + t3 = start_time.replace(hour=5) + + monitored_entity = "binary_sensor.state" + + def _fake_states(*args, **kwargs): + return { + monitored_entity: [ + State( + monitored_entity, + "on", + last_changed=start_time, + last_updated=start_time, + ), + State( + monitored_entity, + "off", + last_changed=t1, + last_updated=t1, + ), + State( + monitored_entity, + "on", + last_changed=t2, + last_updated=t2, + ), + State( + monitored_entity, + "off", + last_changed=t2, + last_updated=t2, + ), + ] + } + + # Setup the config entry + config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={ + CONF_NAME: DEFAULT_NAME, + CONF_ENTITY_ID: monitored_entity, + CONF_TYPE: CONF_TYPE_COUNT, + CONF_STATE: ["on"], + CONF_END: "{{ now() }}", + CONF_START: "{{ today_at() }}", + }, + title=DEFAULT_NAME, + ) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + result = await hass.config_entries.options.async_init(config_entry.entry_id) + assert result["type"] == FlowResultType.FORM + assert result["errors"] is None + assert result["preview"] == "history_stats" + + with ( + patch( + "homeassistant.components.recorder.history.state_changes_during_period", + _fake_states, + ), + freeze_time(t3), + ): + for end, exp_count in ( + ("{{now()}}", "2"), + ("{{today_at('2:00')}}", "1"), + ("{{today_at('23:00')}}", "2"), + ): + await client.send_json_auto_id( + { + "type": "history_stats/start_preview", + "flow_id": result["flow_id"], + "flow_type": "options_flow", + "user_input": { + CONF_ENTITY_ID: monitored_entity, + CONF_TYPE: CONF_TYPE_COUNT, + CONF_STATE: ["on"], + CONF_END: end, + CONF_START: "{{ today_at() }}", + }, + } + ) + + msg = await client.receive_json() + assert msg["success"] + assert msg["result"] is None + + msg = await client.receive_json() + assert msg["event"]["state"] == exp_count + + hass.states.async_set(monitored_entity, "on") + + msg = await client.receive_json() + assert msg["event"]["state"] == "3" + + +async def test_options_flow_preview_errors( + recorder_mock: Recorder, + hass: HomeAssistant, + hass_ws_client: WebSocketGenerator, +) -> None: + """Test the options flow preview.""" + logging.getLogger("sqlalchemy.engine").setLevel(logging.ERROR) + client = await hass_ws_client(hass) + + # add state for the tests + monitored_entity = "binary_sensor.state" + + # Setup the config entry + config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={ + CONF_NAME: DEFAULT_NAME, + CONF_ENTITY_ID: monitored_entity, + CONF_TYPE: CONF_TYPE_COUNT, + CONF_STATE: ["on"], + CONF_END: "{{ now() }}", + CONF_START: "{{ today_at() }}", + }, + title=DEFAULT_NAME, + ) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + result = await hass.config_entries.options.async_init(config_entry.entry_id) + assert result["type"] == FlowResultType.FORM + assert result["errors"] is None + assert result["preview"] == "history_stats" + + for schema in ( + {CONF_END: "{{ now() }"}, # Missing '}' at end of template + {CONF_START: "{{ today_at( }}"}, # Missing ')' in template function + {CONF_DURATION: {"hours": 1}}, # Specified 3 period keys (1 too many) + {CONF_START: ""}, # Specified 1 period keys (1 too few) + ): + await client.send_json_auto_id( + { + "type": "history_stats/start_preview", + "flow_id": result["flow_id"], + "flow_type": "options_flow", + "user_input": { + CONF_ENTITY_ID: monitored_entity, + CONF_TYPE: CONF_TYPE_COUNT, + CONF_STATE: ["on"], + CONF_END: "{{ now() }}", + CONF_START: "{{ today_at() }}", + **schema, + }, + } + ) + + msg = await client.receive_json() + assert not msg["success"] + assert msg["error"]["code"] == "invalid_schema" + + for schema in ( + {CONF_END: "{{ nowwww() }}"}, # Unknown jinja function + {CONF_START: "{{ today_at('abcde') }}"}, # Invalid value passed to today_at + {CONF_END: '"{{ now() }}"'}, # Invalid quotes around template + ): + await client.send_json_auto_id( + { + "type": "history_stats/start_preview", + "flow_id": result["flow_id"], + "flow_type": "options_flow", + "user_input": { + CONF_ENTITY_ID: monitored_entity, + CONF_TYPE: CONF_TYPE_COUNT, + CONF_STATE: ["on"], + CONF_END: "{{ now() }}", + CONF_START: "{{ today_at() }}", + **schema, + }, + } + ) + + msg = await client.receive_json() + assert msg["success"] + assert msg["result"] is None + + msg = await client.receive_json() + assert msg["event"]["error"] + + +async def test_options_flow_sensor_preview_config_entry_removed( + recorder_mock: Recorder, hass: HomeAssistant, hass_ws_client: WebSocketGenerator +) -> None: + """Test the option flow preview where the config entry is removed.""" + client = await hass_ws_client(hass) + + # Setup the config entry + config_entry = MockConfigEntry( + data={}, + domain=DOMAIN, + options={ + CONF_NAME: DEFAULT_NAME, + CONF_ENTITY_ID: "sensor.test_monitored", + CONF_TYPE: CONF_TYPE_COUNT, + CONF_STATE: ["on"], + CONF_START: "0", + CONF_END: "1", + }, + title=DEFAULT_NAME, + ) + config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + result = await hass.config_entries.options.async_init(config_entry.entry_id) + assert result["type"] == FlowResultType.FORM + assert result["errors"] is None + assert result["preview"] == "history_stats" + + await hass.config_entries.async_remove(config_entry.entry_id) + + await client.send_json_auto_id( + { + "type": "history_stats/start_preview", + "flow_id": result["flow_id"], + "flow_type": "options_flow", + "user_input": { + CONF_ENTITY_ID: "sensor.test_monitored", + CONF_TYPE: CONF_TYPE_COUNT, + CONF_STATE: ["on"], + CONF_START: "0", + CONF_END: "1", + }, + } + ) + msg = await client.receive_json() + assert not msg["success"] + assert msg["error"] == { + "code": "home_assistant_error", + "message": "Config entry not found", + } From 0d54e759400241184f998659749c0a95834454cc Mon Sep 17 00:00:00 2001 From: Norbert Rittel Date: Sat, 5 Jul 2025 09:34:24 +0200 Subject: [PATCH 059/106] Fix spelling of "auto" prefixes in `zha` (#148022) --- homeassistant/components/zha/strings.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/zha/strings.json b/homeassistant/components/zha/strings.json index 9694388e784..48bdfc6bb62 100644 --- a/homeassistant/components/zha/strings.json +++ b/homeassistant/components/zha/strings.json @@ -1118,7 +1118,7 @@ "name": "Comfort temperature" }, "valve_state_auto_shutdown": { - "name": "Valve state auto shutdown" + "name": "Valve state autoshutdown" }, "shutdown_timer": { "name": "Shutdown timer" @@ -1996,7 +1996,7 @@ "name": "Schedule mode" }, "auto_clean": { - "name": "Auto clean" + "name": "Autoclean" }, "test_mode": { "name": "Test mode" @@ -2005,7 +2005,7 @@ "name": "External temperature sensor" }, "auto_relock": { - "name": "Auto relock" + "name": "Autorelock" } } } From 7898e3f0fbe7baad71f3eadac2e6367210cde89c Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 5 Jul 2025 09:54:54 +0200 Subject: [PATCH 060/106] Add initial tuya snapshot tests (#148034) Co-authored-by: Franck Nijhof --- tests/components/tuya/__init__.py | 32 ++++++ tests/components/tuya/conftest.py | 93 ++++++++++++++- ...ete_two_12l_dehumidifier_air_purifier.json | 53 +++++++++ .../tuya/fixtures/mcs_door_sensor.json | 20 ++++ .../tuya/snapshots/test_config_flow.ambr | 4 +- tests/components/tuya/snapshots/test_fan.ambr | 51 +++++++++ .../tuya/snapshots/test_humidifier.ambr | 58 ++++++++++ .../tuya/snapshots/test_select.ambr | 62 ++++++++++ .../tuya/snapshots/test_sensor.ambr | 107 ++++++++++++++++++ tests/components/tuya/test_fan.py | 36 ++++++ tests/components/tuya/test_humidifier.py | 36 ++++++ tests/components/tuya/test_select.py | 36 ++++++ tests/components/tuya/test_sensor.py | 37 ++++++ 13 files changed, 618 insertions(+), 7 deletions(-) create mode 100644 tests/components/tuya/fixtures/cs_arete_two_12l_dehumidifier_air_purifier.json create mode 100644 tests/components/tuya/fixtures/mcs_door_sensor.json create mode 100644 tests/components/tuya/snapshots/test_fan.ambr create mode 100644 tests/components/tuya/snapshots/test_humidifier.ambr create mode 100644 tests/components/tuya/snapshots/test_select.ambr create mode 100644 tests/components/tuya/snapshots/test_sensor.ambr create mode 100644 tests/components/tuya/test_fan.py create mode 100644 tests/components/tuya/test_humidifier.py create mode 100644 tests/components/tuya/test_select.py create mode 100644 tests/components/tuya/test_sensor.py diff --git a/tests/components/tuya/__init__.py b/tests/components/tuya/__init__.py index 56bfc0867c6..1d468a46814 100644 --- a/tests/components/tuya/__init__.py +++ b/tests/components/tuya/__init__.py @@ -1 +1,33 @@ """Tests for the Tuya component.""" + +from __future__ import annotations + +from unittest.mock import patch + +from tuya_sharing import CustomerDevice + +from homeassistant.components.tuya import ManagerCompat +from homeassistant.core import HomeAssistant + +from tests.common import MockConfigEntry + + +async def initialize_entry( + hass: HomeAssistant, + mock_manager: ManagerCompat, + mock_config_entry: MockConfigEntry, + mock_device: CustomerDevice, +) -> None: + """Initialize the Tuya component with a mock manager and config entry.""" + # Setup + mock_manager.device_map = { + mock_device.id: mock_device, + } + mock_config_entry.add_to_hass(hass) + + # Initialize the component + with patch( + "homeassistant.components.tuya.ManagerCompat", return_value=mock_manager + ): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() diff --git a/tests/components/tuya/conftest.py b/tests/components/tuya/conftest.py index 4fffb3ae389..017c6f00241 100644 --- a/tests/components/tuya/conftest.py +++ b/tests/components/tuya/conftest.py @@ -6,10 +6,20 @@ from collections.abc import Generator from unittest.mock import MagicMock, patch import pytest +from tuya_sharing import CustomerDevice -from homeassistant.components.tuya.const import CONF_APP_TYPE, CONF_USER_CODE, DOMAIN +from homeassistant.components.tuya import ManagerCompat +from homeassistant.components.tuya.const import ( + CONF_APP_TYPE, + CONF_ENDPOINT, + CONF_TERMINAL_ID, + CONF_TOKEN_INFO, + CONF_USER_CODE, + DOMAIN, +) +from homeassistant.core import HomeAssistant -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, async_load_json_object_fixture @pytest.fixture @@ -25,15 +35,44 @@ def mock_old_config_entry() -> MockConfigEntry: @pytest.fixture def mock_config_entry() -> MockConfigEntry: - """Mock an config entry.""" + """Mock a config entry.""" return MockConfigEntry( - title="12345", + title="Test Tuya entry", domain=DOMAIN, - data={CONF_USER_CODE: "12345"}, + data={ + CONF_ENDPOINT: "test_endpoint", + CONF_TERMINAL_ID: "test_terminal", + CONF_TOKEN_INFO: "test_token", + CONF_USER_CODE: "test_user_code", + }, unique_id="12345", ) +@pytest.fixture +async def mock_loaded_entry( + hass: HomeAssistant, + mock_manager: ManagerCompat, + mock_config_entry: MockConfigEntry, + mock_device: CustomerDevice, +) -> MockConfigEntry: + """Mock a config entry.""" + # Setup + mock_manager.device_map = { + mock_device.id: mock_device, + } + mock_config_entry.add_to_hass(hass) + + # Initialize the component + with ( + patch("homeassistant.components.tuya.ManagerCompat", return_value=mock_manager), + ): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + return mock_config_entry + + @pytest.fixture def mock_setup_entry() -> Generator[None]: """Mock setting up a config entry.""" @@ -68,3 +107,47 @@ def mock_tuya_login_control() -> Generator[MagicMock]: }, ) yield login_control + + +@pytest.fixture +def mock_manager() -> ManagerCompat: + """Mock Tuya Manager.""" + manager = MagicMock(spec=ManagerCompat) + manager.device_map = {} + manager.mq = MagicMock() + return manager + + +@pytest.fixture +def mock_device_code() -> str: + """Fixture to parametrize the type of the mock device. + + To set a configuration, tests can be marked with: + @pytest.mark.parametrize("mock_device_code", ["device_code_1", "device_code_2"]) + """ + return None + + +@pytest.fixture +async def mock_device(hass: HomeAssistant, mock_device_code: str) -> CustomerDevice: + """Mock a Tuya CustomerDevice.""" + details = await async_load_json_object_fixture( + hass, f"{mock_device_code}.json", DOMAIN + ) + device = MagicMock(spec=CustomerDevice) + device.id = details["id"] + device.name = details["name"] + device.category = details["category"] + device.product_id = details["product_id"] + device.product_name = details["product_name"] + device.online = details["online"] + device.function = { + key: MagicMock(type=value["type"], values=value["values"]) + for key, value in details["function"].items() + } + device.status_range = { + key: MagicMock(type=value["type"], values=value["values"]) + for key, value in details["status_range"].items() + } + device.status = details["status"] + return device diff --git a/tests/components/tuya/fixtures/cs_arete_two_12l_dehumidifier_air_purifier.json b/tests/components/tuya/fixtures/cs_arete_two_12l_dehumidifier_air_purifier.json new file mode 100644 index 00000000000..1e50e7e3fec --- /dev/null +++ b/tests/components/tuya/fixtures/cs_arete_two_12l_dehumidifier_air_purifier.json @@ -0,0 +1,53 @@ +{ + "id": "bf3fce6af592f12df3gbgq", + "name": "Dehumidifier", + "category": "cs", + "product_id": "zibqa9dutqyaxym2", + "product_name": "Arete\u00ae Two 12L Dehumidifier/Air Purifier", + "online": true, + "function": { + "switch": { "type": "Boolean", "values": "{}" }, + "dehumidify_set_value": { + "type": "Integer", + "values": "{\"unit\": \"%\", \"min\": 35, \"max\": 70, \"scale\": 0, \"step\": 5}" + }, + "child_lock": { "type": "Boolean", "values": "{}" }, + "countdown_set": { + "type": "Enum", + "values": "{\"range\": [\"cancel\", \"1h\", \"2h\", \"3h\"]}" + } + }, + "status_range": { + "switch": { "type": "Boolean", "values": "{}" }, + "dehumidify_set_value": { + "type": "Integer", + "values": "{\"unit\": \"%\", \"min\": 35, \"max\": 70, \"scale\": 0, \"step\": 5}" + }, + "child_lock": { "type": "Boolean", "values": "{}" }, + "humidity_indoor": { + "type": "Integer", + "values": "{\"unit\": \"%\", \"min\": 0, \"max\": 100, \"scale\": 0, \"step\": 1}" + }, + "countdown_set": { + "type": "Enum", + "values": "{\"range\": [\"cancel\", \"1h\", \"2h\", \"3h\"]}" + }, + "countdown_left": { + "type": "Integer", + "values": "{\"unit\": \"h\", \"min\": 0, \"max\": 24, \"scale\": 0, \"step\": 1}" + }, + "fault": { + "type": "Bitmap", + "values": "{\"label\": [\"tankfull\", \"defrost\", \"E1\", \"E2\", \"L2\", \"L3\", \"L4\", \"wet\"]}" + } + }, + "status": { + "switch": true, + "dehumidify_set_value": 50, + "child_lock": false, + "humidity_indoor": 47, + "countdown_set": "cancel", + "countdown_left": 0, + "fault": 0 + } +} diff --git a/tests/components/tuya/fixtures/mcs_door_sensor.json b/tests/components/tuya/fixtures/mcs_door_sensor.json new file mode 100644 index 00000000000..cec9547c2ea --- /dev/null +++ b/tests/components/tuya/fixtures/mcs_door_sensor.json @@ -0,0 +1,20 @@ +{ + "id": "bf5cccf9027080e2dbb9w3", + "name": "Door Sensor", + "category": "mcs", + "product_id": "7jIGJAymiH8OsFFb", + "product_name": "Door Sensor", + "online": true, + "function": {}, + "status_range": { + "switch": { "type": "Boolean", "values": "{}" }, + "battery": { + "type": "Integer", + "values": "{\"unit\": \"\", \"min\": 0, \"max\": 500, \"scale\": 0, \"step\": 1}" + } + }, + "status": { + "switch": false, + "battery": 100 + } +} diff --git a/tests/components/tuya/snapshots/test_config_flow.ambr b/tests/components/tuya/snapshots/test_config_flow.ambr index 90d83d69814..ba5b4f4bb8d 100644 --- a/tests/components/tuya/snapshots/test_config_flow.ambr +++ b/tests/components/tuya/snapshots/test_config_flow.ambr @@ -11,7 +11,7 @@ 't': 'mocked_t', 'uid': 'mocked_uid', }), - 'user_code': '12345', + 'user_code': 'test_user_code', }), 'disabled_by': None, 'discovery_keys': dict({ @@ -26,7 +26,7 @@ 'source': 'user', 'subentries': list([ ]), - 'title': '12345', + 'title': 'Test Tuya entry', 'unique_id': '12345', 'version': 1, }) diff --git a/tests/components/tuya/snapshots/test_fan.ambr b/tests/components/tuya/snapshots/test_fan.ambr new file mode 100644 index 00000000000..399056e7665 --- /dev/null +++ b/tests/components/tuya/snapshots/test_fan.ambr @@ -0,0 +1,51 @@ +# serializer version: 1 +# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][fan.dehumidifier-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'fan', + 'entity_category': None, + 'entity_id': 'fan.dehumidifier', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': None, + 'platform': 'tuya', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': , + 'translation_key': None, + 'unique_id': 'tuya.bf3fce6af592f12df3gbgq', + 'unit_of_measurement': None, + }) +# --- +# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][fan.dehumidifier-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Dehumidifier', + 'supported_features': , + }), + 'context': , + 'entity_id': 'fan.dehumidifier', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/tuya/snapshots/test_humidifier.ambr b/tests/components/tuya/snapshots/test_humidifier.ambr new file mode 100644 index 00000000000..c22005e123d --- /dev/null +++ b/tests/components/tuya/snapshots/test_humidifier.ambr @@ -0,0 +1,58 @@ +# serializer version: 1 +# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][humidifier.dehumidifier-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'max_humidity': 70, + 'min_humidity': 35, + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'humidifier', + 'entity_category': None, + 'entity_id': 'humidifier.dehumidifier', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': None, + 'platform': 'tuya', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'tuya.bf3fce6af592f12df3gbgqswitch', + 'unit_of_measurement': None, + }) +# --- +# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][humidifier.dehumidifier-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'current_humidity': 47, + 'device_class': 'dehumidifier', + 'friendly_name': 'Dehumidifier', + 'humidity': 50, + 'max_humidity': 70, + 'min_humidity': 35, + 'supported_features': , + }), + 'context': , + 'entity_id': 'humidifier.dehumidifier', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/tuya/snapshots/test_select.ambr b/tests/components/tuya/snapshots/test_select.ambr new file mode 100644 index 00000000000..a9daca637b5 --- /dev/null +++ b/tests/components/tuya/snapshots/test_select.ambr @@ -0,0 +1,62 @@ +# serializer version: 1 +# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][select.dehumidifier_countdown-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'options': list([ + 'cancel', + '1h', + '2h', + '3h', + ]), + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'select', + 'entity_category': , + 'entity_id': 'select.dehumidifier_countdown', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Countdown', + 'platform': 'tuya', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'countdown', + 'unique_id': 'tuya.bf3fce6af592f12df3gbgqcountdown_set', + 'unit_of_measurement': None, + }) +# --- +# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][select.dehumidifier_countdown-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Dehumidifier Countdown', + 'options': list([ + 'cancel', + '1h', + '2h', + '3h', + ]), + }), + 'context': , + 'entity_id': 'select.dehumidifier_countdown', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'cancel', + }) +# --- diff --git a/tests/components/tuya/snapshots/test_sensor.ambr b/tests/components/tuya/snapshots/test_sensor.ambr new file mode 100644 index 00000000000..47709b03a5e --- /dev/null +++ b/tests/components/tuya/snapshots/test_sensor.ambr @@ -0,0 +1,107 @@ +# serializer version: 1 +# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][sensor.dehumidifier_humidity-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.dehumidifier_humidity', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Humidity', + 'platform': 'tuya', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'humidity', + 'unique_id': 'tuya.bf3fce6af592f12df3gbgqhumidity_indoor', + 'unit_of_measurement': '%', + }) +# --- +# name: test_platform_setup_and_discovery[cs_arete_two_12l_dehumidifier_air_purifier][sensor.dehumidifier_humidity-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'humidity', + 'friendly_name': 'Dehumidifier Humidity', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.dehumidifier_humidity', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '47.0', + }) +# --- +# name: test_platform_setup_and_discovery[mcs_door_sensor][sensor.door_sensor_battery-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'state_class': , + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': , + 'entity_id': 'sensor.door_sensor_battery', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Battery', + 'platform': 'tuya', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'battery', + 'unique_id': 'tuya.bf5cccf9027080e2dbb9w3battery', + 'unit_of_measurement': '%', + }) +# --- +# name: test_platform_setup_and_discovery[mcs_door_sensor][sensor.door_sensor_battery-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'battery', + 'friendly_name': 'Door Sensor Battery', + 'state_class': , + 'unit_of_measurement': '%', + }), + 'context': , + 'entity_id': 'sensor.door_sensor_battery', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '100.0', + }) +# --- diff --git a/tests/components/tuya/test_fan.py b/tests/components/tuya/test_fan.py new file mode 100644 index 00000000000..f8a2c5bbee8 --- /dev/null +++ b/tests/components/tuya/test_fan.py @@ -0,0 +1,36 @@ +"""Test Tuya fan platform.""" + +from __future__ import annotations + +from unittest.mock import patch + +import pytest +from syrupy.assertion import SnapshotAssertion +from tuya_sharing import CustomerDevice + +from homeassistant.components.tuya import ManagerCompat +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import initialize_entry + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.parametrize( + "mock_device_code", ["cs_arete_two_12l_dehumidifier_air_purifier"] +) +@patch("homeassistant.components.tuya.PLATFORMS", [Platform.FAN]) +async def test_platform_setup_and_discovery( + hass: HomeAssistant, + mock_manager: ManagerCompat, + mock_config_entry: MockConfigEntry, + mock_device: CustomerDevice, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test platform setup and discovery.""" + await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) diff --git a/tests/components/tuya/test_humidifier.py b/tests/components/tuya/test_humidifier.py new file mode 100644 index 00000000000..aad5782ee13 --- /dev/null +++ b/tests/components/tuya/test_humidifier.py @@ -0,0 +1,36 @@ +"""Test Tuya humidifier platform.""" + +from __future__ import annotations + +from unittest.mock import patch + +import pytest +from syrupy.assertion import SnapshotAssertion +from tuya_sharing import CustomerDevice + +from homeassistant.components.tuya import ManagerCompat +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import initialize_entry + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.parametrize( + "mock_device_code", ["cs_arete_two_12l_dehumidifier_air_purifier"] +) +@patch("homeassistant.components.tuya.PLATFORMS", [Platform.HUMIDIFIER]) +async def test_platform_setup_and_discovery( + hass: HomeAssistant, + mock_manager: ManagerCompat, + mock_config_entry: MockConfigEntry, + mock_device: CustomerDevice, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test platform setup and discovery.""" + await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) diff --git a/tests/components/tuya/test_select.py b/tests/components/tuya/test_select.py new file mode 100644 index 00000000000..5f1111a0fd3 --- /dev/null +++ b/tests/components/tuya/test_select.py @@ -0,0 +1,36 @@ +"""Test Tuya select platform.""" + +from __future__ import annotations + +from unittest.mock import patch + +import pytest +from syrupy.assertion import SnapshotAssertion +from tuya_sharing import CustomerDevice + +from homeassistant.components.tuya import ManagerCompat +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import initialize_entry + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.parametrize( + "mock_device_code", ["cs_arete_two_12l_dehumidifier_air_purifier"] +) +@patch("homeassistant.components.tuya.PLATFORMS", [Platform.SELECT]) +async def test_platform_setup_and_discovery( + hass: HomeAssistant, + mock_manager: ManagerCompat, + mock_config_entry: MockConfigEntry, + mock_device: CustomerDevice, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test platform setup and discovery.""" + await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) diff --git a/tests/components/tuya/test_sensor.py b/tests/components/tuya/test_sensor.py new file mode 100644 index 00000000000..bf424e289ef --- /dev/null +++ b/tests/components/tuya/test_sensor.py @@ -0,0 +1,37 @@ +"""Test Tuya sensor platform.""" + +from __future__ import annotations + +from unittest.mock import patch + +import pytest +from syrupy.assertion import SnapshotAssertion +from tuya_sharing import CustomerDevice + +from homeassistant.components.tuya import ManagerCompat +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from . import initialize_entry + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.mark.parametrize( + "mock_device_code", + ["cs_arete_two_12l_dehumidifier_air_purifier", "mcs_door_sensor"], +) +@patch("homeassistant.components.tuya.PLATFORMS", [Platform.SENSOR]) +async def test_platform_setup_and_discovery( + hass: HomeAssistant, + mock_manager: ManagerCompat, + mock_config_entry: MockConfigEntry, + mock_device: CustomerDevice, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test platform setup and discovery.""" + await initialize_entry(hass, mock_manager, mock_config_entry, mock_device) + + await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) From 1e164c94b152178de4dce4ac5ba6c5683952e42e Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 5 Jul 2025 10:14:52 +0200 Subject: [PATCH 061/106] Include path when media source file can be accessed on disk (#148180) --- .../components/media_source/local_source.py | 2 +- homeassistant/components/media_source/models.py | 6 +++++- tests/components/media_source/test_local_source.py | 14 ++++++++++++-- .../system_bridge/snapshots/test_media_source.ambr | 2 ++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/media_source/local_source.py b/homeassistant/components/media_source/local_source.py index c9b81e6534e..fa30dc9baf3 100644 --- a/homeassistant/components/media_source/local_source.py +++ b/homeassistant/components/media_source/local_source.py @@ -80,7 +80,7 @@ class LocalSource(MediaSource): path = self.async_full_path(source_dir_id, location) mime_type, _ = mimetypes.guess_type(str(path)) assert isinstance(mime_type, str) - return PlayMedia(f"/media/{item.identifier}", mime_type) + return PlayMedia(f"/media/{item.identifier}", mime_type, path=path) async def async_browse_media(self, item: MediaSourceItem) -> BrowseMediaSource: """Return media.""" diff --git a/homeassistant/components/media_source/models.py b/homeassistant/components/media_source/models.py index 8588c5bcacc..2cf5d231741 100644 --- a/homeassistant/components/media_source/models.py +++ b/homeassistant/components/media_source/models.py @@ -2,7 +2,7 @@ from __future__ import annotations -from dataclasses import dataclass +from dataclasses import dataclass, field from typing import TYPE_CHECKING, Any from homeassistant.components.media_player import BrowseMedia, MediaClass, MediaType @@ -10,6 +10,9 @@ from homeassistant.core import HomeAssistant, callback from .const import MEDIA_SOURCE_DATA, URI_SCHEME, URI_SCHEME_REGEX +if TYPE_CHECKING: + from pathlib import Path + @dataclass(slots=True) class PlayMedia: @@ -17,6 +20,7 @@ class PlayMedia: url: str mime_type: str + path: Path | None = field(kw_only=True, default=None) class BrowseMediaSource(BrowseMedia): diff --git a/tests/components/media_source/test_local_source.py b/tests/components/media_source/test_local_source.py index 1823165d906..259407bfb5a 100644 --- a/tests/components/media_source/test_local_source.py +++ b/tests/components/media_source/test_local_source.py @@ -167,13 +167,23 @@ async def test_upload_view( res = await client.post( "/api/media_source/local_source/upload", data={ - "media_content_id": "media-source://media_source/test_dir/.", + "media_content_id": "media-source://media_source/test_dir", "file": get_file("logo.png"), }, ) assert res.status == 200 - assert (Path(temp_dir) / "logo.png").is_file() + data = await res.json() + assert data["media_content_id"] == "media-source://media_source/test_dir/logo.png" + uploaded_path = Path(temp_dir) / "logo.png" + assert uploaded_path.is_file() + + resolved = await media_source.async_resolve_media( + hass, data["media_content_id"], target_media_player=None + ) + assert resolved.url == "/media/test_dir/logo.png" + assert resolved.mime_type == "image/png" + assert resolved.path == uploaded_path # Test with bad media source ID for bad_id in ( diff --git a/tests/components/system_bridge/snapshots/test_media_source.ambr b/tests/components/system_bridge/snapshots/test_media_source.ambr index 954332c932a..695a35f17d9 100644 --- a/tests/components/system_bridge/snapshots/test_media_source.ambr +++ b/tests/components/system_bridge/snapshots/test_media_source.ambr @@ -28,12 +28,14 @@ # name: test_file[system_bridge_media_source_file_image] dict({ 'mime_type': 'image/jpeg', + 'path': None, 'url': 'http://127.0.0.1:9170/api/media/file/data?token=abc-123-def-456-ghi&base=documents&path=testimage.jpg', }) # --- # name: test_file[system_bridge_media_source_file_text] dict({ 'mime_type': 'text/plain', + 'path': None, 'url': 'http://127.0.0.1:9170/api/media/file/data?token=abc-123-def-456-ghi&base=documents&path=testfile.txt', }) # --- From 1b21c986e8a4550a000cafcb763c20cfc9c01665 Mon Sep 17 00:00:00 2001 From: HarvsG <11440490+HarvsG@users.noreply.github.com> Date: Sat, 5 Jul 2025 09:21:32 +0100 Subject: [PATCH 062/106] Enable Pihole API v6 (#145890) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Josef Zweck --- homeassistant/components/pi_hole/__init__.py | 143 ++++++++++-- .../components/pi_hole/binary_sensor.py | 2 +- .../components/pi_hole/config_flow.py | 88 ++++--- homeassistant/components/pi_hole/const.py | 7 + homeassistant/components/pi_hole/entity.py | 5 +- homeassistant/components/pi_hole/icons.json | 9 + .../components/pi_hole/manifest.json | 2 +- homeassistant/components/pi_hole/sensor.py | 108 ++++++++- homeassistant/components/pi_hole/strings.json | 27 ++- homeassistant/components/pi_hole/switch.py | 2 +- homeassistant/components/pi_hole/update.py | 30 +-- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- script/hassfest/requirements.py | 1 - tests/components/pi_hole/__init__.py | 215 ++++++++++++++++-- .../pi_hole/snapshots/test_diagnostics.ambr | 1 + tests/components/pi_hole/test_config_flow.py | 129 ++++++++--- tests/components/pi_hole/test_diagnostics.py | 5 +- tests/components/pi_hole/test_init.py | 155 +++++++++++-- tests/components/pi_hole/test_repairs.py | 136 +++++++++++ tests/components/pi_hole/test_sensor.py | 79 +++++++ tests/components/pi_hole/test_update.py | 8 +- 22 files changed, 979 insertions(+), 177 deletions(-) create mode 100644 tests/components/pi_hole/test_repairs.py create mode 100644 tests/components/pi_hole/test_sensor.py diff --git a/homeassistant/components/pi_hole/__init__.py b/homeassistant/components/pi_hole/__init__.py index 5cc21cef3a9..f211d646c0b 100644 --- a/homeassistant/components/pi_hole/__init__.py +++ b/homeassistant/components/pi_hole/__init__.py @@ -4,13 +4,15 @@ from __future__ import annotations from dataclasses import dataclass import logging +from typing import Any, Literal -from hole import Hole -from hole.exceptions import HoleError +from hole import Hole, HoleV5, HoleV6 +from hole.exceptions import HoleConnectionError, HoleError from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( CONF_API_KEY, + CONF_API_VERSION, CONF_HOST, CONF_LOCATION, CONF_NAME, @@ -24,7 +26,12 @@ from homeassistant.helpers import entity_registry as er from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed -from .const import CONF_STATISTICS_ONLY, DOMAIN, MIN_TIME_BETWEEN_UPDATES +from .const import ( + CONF_STATISTICS_ONLY, + DOMAIN, + MIN_TIME_BETWEEN_UPDATES, + VERSION_6_RESPONSE_TO_5_ERROR, +) _LOGGER = logging.getLogger(__name__) @@ -51,10 +58,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: PiHoleConfigEntry) -> bo """Set up Pi-hole entry.""" name = entry.data[CONF_NAME] host = entry.data[CONF_HOST] - use_tls = entry.data[CONF_SSL] - verify_tls = entry.data[CONF_VERIFY_SSL] - location = entry.data[CONF_LOCATION] - api_key = entry.data.get(CONF_API_KEY, "") + version = entry.data.get(CONF_API_VERSION) # remove obsolet CONF_STATISTICS_ONLY from entry.data if CONF_STATISTICS_ONLY in entry.data: @@ -96,21 +100,42 @@ async def async_setup_entry(hass: HomeAssistant, entry: PiHoleConfigEntry) -> bo await er.async_migrate_entries(hass, entry.entry_id, update_unique_id) - session = async_get_clientsession(hass, verify_tls) - api = Hole( - host, - session, - location=location, - tls=use_tls, - api_token=api_key, - ) + if version is None: + _LOGGER.debug( + "No API version specified, determining Pi-hole API version for %s", host + ) + version = await determine_api_version(hass, dict(entry.data)) + _LOGGER.debug("Pi-hole API version determined: %s", version) + hass.config_entries.async_update_entry( + entry, data={**entry.data, CONF_API_VERSION: version} + ) + # Once API version 5 is deprecated we should instantiate Hole directly + api = api_by_version(hass, dict(entry.data), version) async def async_update_data() -> None: """Fetch data from API endpoint.""" try: await api.get_data() await api.get_versions() + if "error" in (response := api.data): + match response["error"]: + case { + "key": key, + "message": message, + "hint": hint, + } if ( + key == VERSION_6_RESPONSE_TO_5_ERROR["key"] + and message == VERSION_6_RESPONSE_TO_5_ERROR["message"] + and hint.startswith("The API is hosted at ") + and "/admin/api" in hint + ): + _LOGGER.warning( + "Pi-hole API v6 returned an error that is expected when using v5 endpoints please re-configure your authentication" + ) + raise ConfigEntryAuthFailed except HoleError as err: + if str(err) == "Authentication failed: Invalid password": + raise ConfigEntryAuthFailed from err raise UpdateFailed(f"Failed to communicate with API: {err}") from err if not isinstance(api.data, dict): raise ConfigEntryAuthFailed @@ -136,3 +161,91 @@ async def async_setup_entry(hass: HomeAssistant, entry: PiHoleConfigEntry) -> bo async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload Pi-hole entry.""" return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) + + +def api_by_version( + hass: HomeAssistant, + entry: dict[str, Any], + version: int, + password: str | None = None, +) -> HoleV5 | HoleV6: + """Create a pi-hole API object by API version number. Once V5 is deprecated this function can be removed.""" + + if password is None: + password = entry.get(CONF_API_KEY, "") + session = async_get_clientsession(hass, entry[CONF_VERIFY_SSL]) + hole_kwargs = { + "host": entry[CONF_HOST], + "session": session, + "location": entry[CONF_LOCATION], + "verify_tls": entry[CONF_VERIFY_SSL], + "version": version, + } + if version == 5: + hole_kwargs["tls"] = entry.get(CONF_SSL) + hole_kwargs["api_token"] = password + elif version == 6: + hole_kwargs["protocol"] = "https" if entry.get(CONF_SSL) else "http" + hole_kwargs["password"] = password + + return Hole(**hole_kwargs) + + +async def determine_api_version( + hass: HomeAssistant, entry: dict[str, Any] +) -> Literal[5, 6]: + """Determine the API version of the Pi-hole instance without requiring authentication. + + Neither API v5 or v6 provides an endpoint to check the version without authentication. + Version 6 provides other enddpoints that do not require authentication, so we can use those to determine the version + version 5 returns an empty list in response to unauthenticated requests. + Because we are using endpoints that are not designed for this purpose, we should log liberally to help with debugging. + """ + + holeV6 = api_by_version(hass, entry, 6, password="wrong_password") + try: + await holeV6.authenticate() + except HoleConnectionError as err: + _LOGGER.error( + "Unexpected error connecting to Pi-hole v6 API at %s: %s. Trying version 5 API", + holeV6.base_url, + err, + ) + # Ideally python-hole would raise a specific exception for authentication failures + except HoleError as ex_v6: + if str(ex_v6) == "Authentication failed: Invalid password": + _LOGGER.debug( + "Success connecting to Pi-hole at %s without auth, API version is : %s", + holeV6.base_url, + 6, + ) + return 6 + _LOGGER.debug( + "Connection to %s failed: %s, trying API version 5", holeV6.base_url, ex_v6 + ) + holeV5 = api_by_version(hass, entry, 5, password="wrong_token") + try: + await holeV5.get_data() + + except HoleConnectionError as err: + _LOGGER.error( + "Failed to connect to Pi-hole v5 API at %s: %s", holeV5.base_url, err + ) + else: + # V5 API returns [] to unauthenticated requests + if not holeV5.data: + _LOGGER.debug( + "Response '[]' from API without auth, pihole API version 5 probably detected at %s", + holeV5.base_url, + ) + return 5 + _LOGGER.debug( + "Unexpected response from Pi-hole API at %s: %s", + holeV5.base_url, + str(holeV5.data), + ) + _LOGGER.debug( + "Could not determine pi-hole API version at: %s", + holeV6.base_url, + ) + raise HoleError("Could not determine Pi-hole API version") diff --git a/homeassistant/components/pi_hole/binary_sensor.py b/homeassistant/components/pi_hole/binary_sensor.py index 1d12307b6e5..049195d01b1 100644 --- a/homeassistant/components/pi_hole/binary_sensor.py +++ b/homeassistant/components/pi_hole/binary_sensor.py @@ -33,7 +33,7 @@ BINARY_SENSOR_TYPES: tuple[PiHoleBinarySensorEntityDescription, ...] = ( PiHoleBinarySensorEntityDescription( key="status", translation_key="status", - state_value=lambda api: bool(api.data.get("status") == "enabled"), + state_value=lambda api: bool(api.status == "enabled"), ), ) diff --git a/homeassistant/components/pi_hole/config_flow.py b/homeassistant/components/pi_hole/config_flow.py index e50b018caa4..da994b74e6d 100644 --- a/homeassistant/components/pi_hole/config_flow.py +++ b/homeassistant/components/pi_hole/config_flow.py @@ -6,13 +6,13 @@ from collections.abc import Mapping import logging from typing import Any -from hole import Hole from hole.exceptions import HoleError import voluptuous as vol from homeassistant.config_entries import ConfigFlow, ConfigFlowResult from homeassistant.const import ( CONF_API_KEY, + CONF_API_VERSION, CONF_HOST, CONF_LOCATION, CONF_NAME, @@ -20,8 +20,8 @@ from homeassistant.const import ( CONF_SSL, CONF_VERIFY_SSL, ) -from homeassistant.helpers.aiohttp_client import async_get_clientsession +from . import Hole, api_by_version, determine_api_version from .const import ( DEFAULT_LOCATION, DEFAULT_NAME, @@ -55,6 +55,7 @@ class PiHoleFlowHandler(ConfigFlow, domain=DOMAIN): CONF_LOCATION: user_input[CONF_LOCATION], CONF_SSL: user_input[CONF_SSL], CONF_VERIFY_SSL: user_input[CONF_VERIFY_SSL], + CONF_API_KEY: user_input[CONF_API_KEY], } self._async_abort_entries_match( @@ -69,9 +70,6 @@ class PiHoleFlowHandler(ConfigFlow, domain=DOMAIN): title=user_input[CONF_NAME], data=self._config ) - if CONF_API_KEY in errors: - return await self.async_step_api_key() - user_input = user_input or {} return self.async_show_form( step_id="user", @@ -88,6 +86,10 @@ class PiHoleFlowHandler(ConfigFlow, domain=DOMAIN): CONF_LOCATION, default=user_input.get(CONF_LOCATION, DEFAULT_LOCATION), ): str, + vol.Required( + CONF_API_KEY, + default=user_input.get(CONF_API_KEY), + ): str, vol.Required( CONF_SSL, default=user_input.get(CONF_SSL, DEFAULT_SSL), @@ -101,25 +103,6 @@ class PiHoleFlowHandler(ConfigFlow, domain=DOMAIN): errors=errors, ) - async def async_step_api_key( - self, user_input: dict[str, Any] | None = None - ) -> ConfigFlowResult: - """Handle step to setup API key.""" - errors = {} - if user_input is not None: - self._config[CONF_API_KEY] = user_input[CONF_API_KEY] - if not (errors := await self._async_try_connect()): - return self.async_create_entry( - title=self._config[CONF_NAME], - data=self._config, - ) - - return self.async_show_form( - step_id="api_key", - data_schema=vol.Schema({vol.Required(CONF_API_KEY): str}), - errors=errors, - ) - async def async_step_reauth( self, entry_data: Mapping[str, Any] ) -> ConfigFlowResult: @@ -151,19 +134,50 @@ class PiHoleFlowHandler(ConfigFlow, domain=DOMAIN): ) async def _async_try_connect(self) -> dict[str, str]: - session = async_get_clientsession(self.hass, self._config[CONF_VERIFY_SSL]) - pi_hole = Hole( - self._config[CONF_HOST], - session, - location=self._config[CONF_LOCATION], - tls=self._config[CONF_SSL], - api_token=self._config.get(CONF_API_KEY), - ) + """Try to connect to the Pi-hole API and determine the version.""" try: - await pi_hole.get_data() - except HoleError as ex: - _LOGGER.debug("Connection failed: %s", ex) + version = await determine_api_version(hass=self.hass, entry=self._config) + except HoleError: return {"base": "cannot_connect"} - if not isinstance(pi_hole.data, dict): - return {CONF_API_KEY: "invalid_auth"} + pi_hole: Hole = api_by_version(self.hass, self._config, version) + + if version == 6: + try: + await pi_hole.authenticate() + _LOGGER.debug("Success authenticating with pihole API version: %s", 6) + self._config[CONF_API_VERSION] = 6 + except HoleError: + _LOGGER.debug("Failed authenticating with pihole API version: %s", 6) + return {CONF_API_KEY: "invalid_auth"} + + elif version == 5: + try: + await pi_hole.get_data() + if pi_hole.data is not None and "error" in pi_hole.data: + _LOGGER.debug( + "API version %s returned an unexpected error: %s", + 5, + str(pi_hole.data), + ) + raise HoleError(pi_hole.data) # noqa: TRY301 + except HoleError as ex_v5: + _LOGGER.error( + "Connection to API version 5 failed: %s", + ex_v5, + ) + return {"base": "cannot_connect"} + else: + _LOGGER.debug( + "Success connecting to, but necessarily authenticating with, pihole, API version is: %s", + 5, + ) + self._config[CONF_API_VERSION] = 5 + # the v5 API returns an empty list to unauthenticated requests. + if not isinstance(pi_hole.data, dict): + _LOGGER.debug( + "API version %s returned %s, '[]' is expected for unauthenticated requests", + 5, + pi_hole.data, + ) + return {CONF_API_KEY: "invalid_auth"} return {} diff --git a/homeassistant/components/pi_hole/const.py b/homeassistant/components/pi_hole/const.py index c81e6504dff..5e91f348ce9 100644 --- a/homeassistant/components/pi_hole/const.py +++ b/homeassistant/components/pi_hole/const.py @@ -17,3 +17,10 @@ SERVICE_DISABLE = "disable" SERVICE_DISABLE_ATTR_DURATION = "duration" MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=5) + +# See https://github.com/pi-hole/FTL/blob/88737f6248cd3df3202eed72aeec89b9fb572631/src/webserver/lua_web.c#L83 +VERSION_6_RESPONSE_TO_5_ERROR = { + "key": "bad_request", + "message": "Bad request", + "hint": "The API is hosted at pi.hole/api, not pi.hole/admin/api", +} diff --git a/homeassistant/components/pi_hole/entity.py b/homeassistant/components/pi_hole/entity.py index 0f5c6039232..f29aa819139 100644 --- a/homeassistant/components/pi_hole/entity.py +++ b/homeassistant/components/pi_hole/entity.py @@ -32,7 +32,10 @@ class PiHoleEntity(CoordinatorEntity[DataUpdateCoordinator[None]]): @property def device_info(self) -> DeviceInfo: """Return the device information of the entity.""" - if self.api.tls: + if ( + getattr(self.api, "tls", None) # API version 5 + or getattr(self.api, "protocol", None) == "https" # API version 6 + ): config_url = f"https://{self.api.host}/{self.api.location}" else: config_url = f"http://{self.api.host}/{self.api.location}" diff --git a/homeassistant/components/pi_hole/icons.json b/homeassistant/components/pi_hole/icons.json index 3a45f8ab454..d5c2e9a2d43 100644 --- a/homeassistant/components/pi_hole/icons.json +++ b/homeassistant/components/pi_hole/icons.json @@ -9,15 +9,24 @@ "ads_blocked_today": { "default": "mdi:close-octagon-outline" }, + "ads_blocked": { + "default": "mdi:close-octagon-outline" + }, "ads_percentage_today": { "default": "mdi:close-octagon-outline" }, + "percent_ads_blocked": { + "default": "mdi:close-octagon-outline" + }, "clients_ever_seen": { "default": "mdi:account-outline" }, "dns_queries_today": { "default": "mdi:comment-question-outline" }, + "dns_queries": { + "default": "mdi:comment-question-outline" + }, "domains_being_blocked": { "default": "mdi:block-helper" }, diff --git a/homeassistant/components/pi_hole/manifest.json b/homeassistant/components/pi_hole/manifest.json index 975d8a1494c..aa8af024c5a 100644 --- a/homeassistant/components/pi_hole/manifest.json +++ b/homeassistant/components/pi_hole/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/pi_hole", "iot_class": "local_polling", "loggers": ["hole"], - "requirements": ["hole==0.8.0"] + "requirements": ["hole==0.9.0"] } diff --git a/homeassistant/components/pi_hole/sensor.py b/homeassistant/components/pi_hole/sensor.py index 54a9cb23d02..aa79805cc2d 100644 --- a/homeassistant/components/pi_hole/sensor.py +++ b/homeassistant/components/pi_hole/sensor.py @@ -2,10 +2,13 @@ from __future__ import annotations +from collections.abc import Mapping +from typing import Any + from hole import Hole from homeassistant.components.sensor import SensorEntity, SensorEntityDescription -from homeassistant.const import CONF_NAME, PERCENTAGE +from homeassistant.const import CONF_API_VERSION, CONF_NAME, PERCENTAGE from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.typing import StateType @@ -18,29 +21,98 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( SensorEntityDescription( key="ads_blocked_today", translation_key="ads_blocked_today", + suggested_display_precision=0, ), SensorEntityDescription( key="ads_percentage_today", translation_key="ads_percentage_today", native_unit_of_measurement=PERCENTAGE, + suggested_display_precision=1, ), SensorEntityDescription( key="clients_ever_seen", translation_key="clients_ever_seen", + suggested_display_precision=0, ), SensorEntityDescription( - key="dns_queries_today", translation_key="dns_queries_today" + key="dns_queries_today", + translation_key="dns_queries_today", + suggested_display_precision=0, ), SensorEntityDescription( key="domains_being_blocked", translation_key="domains_being_blocked", + suggested_display_precision=0, ), - SensorEntityDescription(key="queries_cached", translation_key="queries_cached"), SensorEntityDescription( - key="queries_forwarded", translation_key="queries_forwarded" + key="queries_cached", + translation_key="queries_cached", + suggested_display_precision=0, + ), + SensorEntityDescription( + key="queries_forwarded", + translation_key="queries_forwarded", + suggested_display_precision=0, + ), + SensorEntityDescription( + key="unique_clients", + translation_key="unique_clients", + suggested_display_precision=0, + ), + SensorEntityDescription( + key="unique_domains", + translation_key="unique_domains", + suggested_display_precision=0, + ), +) + +SENSOR_TYPES_V6: tuple[SensorEntityDescription, ...] = ( + SensorEntityDescription( + key="queries.blocked", + translation_key="ads_blocked", + suggested_display_precision=0, + ), + SensorEntityDescription( + key="queries.percent_blocked", + translation_key="percent_ads_blocked", + native_unit_of_measurement=PERCENTAGE, + suggested_display_precision=2, + ), + SensorEntityDescription( + key="clients.total", + translation_key="clients_ever_seen", + suggested_display_precision=0, + ), + SensorEntityDescription( + key="queries.total", + translation_key="dns_queries", + suggested_display_precision=0, + ), + SensorEntityDescription( + key="gravity.domains_being_blocked", + translation_key="domains_being_blocked", + suggested_display_precision=0, + ), + SensorEntityDescription( + key="queries.cached", + translation_key="queries_cached", + suggested_display_precision=0, + ), + SensorEntityDescription( + key="queries.forwarded", + translation_key="queries_forwarded", + suggested_display_precision=0, + ), + SensorEntityDescription( + key="clients.active", + translation_key="unique_clients", + suggested_display_precision=0, + ), + SensorEntityDescription( + key="queries.unique_domains", + translation_key="unique_domains", + suggested_display_precision=0, ), - SensorEntityDescription(key="unique_clients", translation_key="unique_clients"), - SensorEntityDescription(key="unique_domains", translation_key="unique_domains"), ) @@ -60,7 +132,9 @@ async def async_setup_entry( entry.entry_id, description, ) - for description in SENSOR_TYPES + for description in ( + SENSOR_TYPES if entry.data[CONF_API_VERSION] == 5 else SENSOR_TYPES_V6 + ) ] async_add_entities(sensors, True) @@ -88,7 +162,19 @@ class PiHoleSensor(PiHoleEntity, SensorEntity): @property def native_value(self) -> StateType: """Return the state of the device.""" - try: - return round(self.api.data[self.entity_description.key], 2) # type: ignore[no-any-return] - except TypeError: - return self.api.data[self.entity_description.key] # type: ignore[no-any-return] + return get_nested(self.api.data, self.entity_description.key) + + +def get_nested(data: Mapping[str, Any], key: str) -> float | int: + """Get a value from a nested dictionary using a dot-separated key. + + Ensures type safety as it iterates into the dict. + """ + current: Any = data + for part in key.split("."): + if not isinstance(current, Mapping): + raise KeyError(f"Cannot access '{part}' in non-dict {current!r}") + current = current[part] + if not isinstance(current, (float, int)): + raise TypeError(f"Value at '{key}' is not a float or int: {current!r}") + return current diff --git a/homeassistant/components/pi_hole/strings.json b/homeassistant/components/pi_hole/strings.json index 504be7a62dd..069f8a576d4 100644 --- a/homeassistant/components/pi_hole/strings.json +++ b/homeassistant/components/pi_hole/strings.json @@ -8,14 +8,11 @@ "name": "[%key:common::config_flow::data::name%]", "location": "[%key:common::config_flow::data::location%]", "ssl": "[%key:common::config_flow::data::ssl%]", - "verify_ssl": "[%key:common::config_flow::data::verify_ssl%]" - } - }, - "api_key": { - "data": { - "api_key": "[%key:common::config_flow::data::api_key%]" + "verify_ssl": "[%key:common::config_flow::data::verify_ssl%]", + "api_key": "App Password or API Key" } }, + "reauth_confirm": { "title": "Reauthenticate Pi-hole", "description": "Please enter a new API key for Pi-hole at {host}/{location}", @@ -33,6 +30,12 @@ "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" } }, + "issues": { + "v5_to_v6_migration": { + "title": "Recent migration from Pi-hole API v5 to v6", + "description": "You've likely updated your Pi-hole to API v6 from v5. Some sensors changed in the new API, the daily sensors were removed, and your old API token is invalid. Provide your new app password by re-authenticating in repairs or in **Settings -> Devices & services -> Pi-hole**." + } + }, "entity": { "binary_sensor": { "status": { @@ -44,9 +47,17 @@ "name": "Ads blocked today", "unit_of_measurement": "ads" }, + "ads_blocked": { + "name": "Ads blocked", + "unit_of_measurement": "ads" + }, "ads_percentage_today": { "name": "Ads percentage blocked today" }, + + "percent_ads_blocked": { + "name": "Ads percentage blocked" + }, "clients_ever_seen": { "name": "Seen clients", "unit_of_measurement": "clients" @@ -55,6 +66,10 @@ "name": "DNS queries today", "unit_of_measurement": "queries" }, + "dns_queries": { + "name": "DNS queries", + "unit_of_measurement": "queries" + }, "domains_being_blocked": { "name": "Domains blocked", "unit_of_measurement": "domains" diff --git a/homeassistant/components/pi_hole/switch.py b/homeassistant/components/pi_hole/switch.py index 84ffe7e51a4..5fdb39bf9eb 100644 --- a/homeassistant/components/pi_hole/switch.py +++ b/homeassistant/components/pi_hole/switch.py @@ -70,7 +70,7 @@ class PiHoleSwitch(PiHoleEntity, SwitchEntity): @property def is_on(self) -> bool: """Return if the service is on.""" - return self.api.data.get("status") == "enabled" # type: ignore[no-any-return] + return self.api.status == "enabled" # type: ignore[no-any-return] async def async_turn_on(self, **kwargs: Any) -> None: """Turn on the service.""" diff --git a/homeassistant/components/pi_hole/update.py b/homeassistant/components/pi_hole/update.py index 56e92b47289..90fdefd306b 100644 --- a/homeassistant/components/pi_hole/update.py +++ b/homeassistant/components/pi_hole/update.py @@ -21,9 +21,9 @@ from .entity import PiHoleEntity class PiHoleUpdateEntityDescription(UpdateEntityDescription): """Describes PiHole update entity.""" - installed_version: Callable[[dict], str | None] = lambda api: None - latest_version: Callable[[dict], str | None] = lambda api: None - has_update: Callable[[dict], bool | None] = lambda api: None + installed_version: Callable[[Hole], str | None] = lambda api: None + latest_version: Callable[[Hole], str | None] = lambda api: None + has_update: Callable[[Hole], bool | None] = lambda api: None release_base_url: str | None = None title: str | None = None @@ -34,9 +34,9 @@ UPDATE_ENTITY_TYPES: tuple[PiHoleUpdateEntityDescription, ...] = ( translation_key="core_update_available", title="Pi-hole Core", entity_category=EntityCategory.DIAGNOSTIC, - installed_version=lambda versions: versions.get("core_current"), - latest_version=lambda versions: versions.get("core_latest"), - has_update=lambda versions: versions.get("core_update"), + installed_version=lambda api: api.core_current, + latest_version=lambda api: api.core_latest, + has_update=lambda api: api.core_update, release_base_url="https://github.com/pi-hole/pi-hole/releases/tag", ), PiHoleUpdateEntityDescription( @@ -44,9 +44,9 @@ UPDATE_ENTITY_TYPES: tuple[PiHoleUpdateEntityDescription, ...] = ( translation_key="web_update_available", title="Pi-hole Web interface", entity_category=EntityCategory.DIAGNOSTIC, - installed_version=lambda versions: versions.get("web_current"), - latest_version=lambda versions: versions.get("web_latest"), - has_update=lambda versions: versions.get("web_update"), + installed_version=lambda api: api.web_current, + latest_version=lambda api: api.web_latest, + has_update=lambda api: api.web_update, release_base_url="https://github.com/pi-hole/AdminLTE/releases/tag", ), PiHoleUpdateEntityDescription( @@ -54,9 +54,9 @@ UPDATE_ENTITY_TYPES: tuple[PiHoleUpdateEntityDescription, ...] = ( translation_key="ftl_update_available", title="Pi-hole FTL DNS", entity_category=EntityCategory.DIAGNOSTIC, - installed_version=lambda versions: versions.get("FTL_current"), - latest_version=lambda versions: versions.get("FTL_latest"), - has_update=lambda versions: versions.get("FTL_update"), + installed_version=lambda api: api.ftl_current, + latest_version=lambda api: api.ftl_latest, + has_update=lambda api: api.ftl_update, release_base_url="https://github.com/pi-hole/FTL/releases/tag", ), ) @@ -108,15 +108,15 @@ class PiHoleUpdateEntity(PiHoleEntity, UpdateEntity): def installed_version(self) -> str | None: """Version installed and in use.""" if isinstance(self.api.versions, dict): - return self.entity_description.installed_version(self.api.versions) + return self.entity_description.installed_version(self.api) return None @property def latest_version(self) -> str | None: """Latest version available for install.""" if isinstance(self.api.versions, dict): - if self.entity_description.has_update(self.api.versions): - return self.entity_description.latest_version(self.api.versions) + if self.entity_description.has_update(self.api): + return self.entity_description.latest_version(self.api) return self.installed_version return None diff --git a/requirements_all.txt b/requirements_all.txt index 80a824cf44f..2655e6f9a90 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1161,7 +1161,7 @@ hko==0.3.2 hlk-sw16==0.0.9 # homeassistant.components.pi_hole -hole==0.8.0 +hole==0.9.0 # homeassistant.components.holiday # homeassistant.components.workday diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 88dc21a0e07..3ec700ccf1e 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1010,7 +1010,7 @@ hko==0.3.2 hlk-sw16==0.0.9 # homeassistant.components.pi_hole -hole==0.8.0 +hole==0.9.0 # homeassistant.components.holiday # homeassistant.components.workday diff --git a/script/hassfest/requirements.py b/script/hassfest/requirements.py index a2115ae5591..d7d064fff28 100644 --- a/script/hassfest/requirements.py +++ b/script/hassfest/requirements.py @@ -246,7 +246,6 @@ FORBIDDEN_PACKAGE_EXCEPTIONS: dict[str, dict[str, set[str]]] = { # opower > arrow > types-python-dateutil "arrow": {"types-python-dateutil"} }, - "pi_hole": {"hole": {"async-timeout"}}, "pvpc_hourly_pricing": {"aiopvpc": {"async-timeout"}}, "remote_rpi_gpio": { # https://github.com/waveform80/colorzero/issues/9 diff --git a/tests/components/pi_hole/__init__.py b/tests/components/pi_hole/__init__.py index 993f6a2571c..36ee963a16f 100644 --- a/tests/components/pi_hole/__init__.py +++ b/tests/components/pi_hole/__init__.py @@ -1,8 +1,9 @@ """Tests for the pi_hole component.""" +from typing import Any from unittest.mock import AsyncMock, MagicMock, patch -from hole.exceptions import HoleError +from hole.exceptions import HoleConnectionError, HoleError from homeassistant.components.pi_hole.const import ( DEFAULT_LOCATION, @@ -12,6 +13,7 @@ from homeassistant.components.pi_hole.const import ( ) from homeassistant.const import ( CONF_API_KEY, + CONF_API_VERSION, CONF_HOST, CONF_LOCATION, CONF_NAME, @@ -32,6 +34,82 @@ ZERO_DATA = { "unique_clients": 0, "unique_domains": 0, } +ZERO_DATA_V6 = { + "queries": { + "total": 0, + "blocked": 0, + "percent_blocked": 0, + "unique_domains": 0, + "forwarded": 0, + "cached": 0, + "frequency": 0, + "types": { + "A": 0, + "AAAA": 0, + "ANY": 0, + "SRV": 0, + "SOA": 0, + "PTR": 0, + "TXT": 0, + "NAPTR": 0, + "MX": 0, + "DS": 0, + "RRSIG": 0, + "DNSKEY": 0, + "NS": 0, + "SVCB": 0, + "HTTPS": 0, + "OTHER": 0, + }, + "status": { + "UNKNOWN": 0, + "GRAVITY": 0, + "FORWARDED": 0, + "CACHE": 0, + "REGEX": 0, + "DENYLIST": 0, + "EXTERNAL_BLOCKED_IP": 0, + "EXTERNAL_BLOCKED_NULL": 0, + "EXTERNAL_BLOCKED_NXRA": 0, + "GRAVITY_CNAME": 0, + "REGEX_CNAME": 0, + "DENYLIST_CNAME": 0, + "RETRIED": 0, + "RETRIED_DNSSEC": 0, + "IN_PROGRESS": 0, + "DBBUSY": 0, + "SPECIAL_DOMAIN": 0, + "CACHE_STALE": 0, + "EXTERNAL_BLOCKED_EDE15": 0, + }, + "replies": { + "UNKNOWN": 0, + "NODATA": 0, + "NXDOMAIN": 0, + "CNAME": 0, + "IP": 0, + "DOMAIN": 0, + "RRNAME": 0, + "SERVFAIL": 0, + "REFUSED": 0, + "NOTIMP": 0, + "OTHER": 0, + "DNSSEC": 0, + "NONE": 0, + "BLOB": 0, + }, + }, + "clients": {"active": 0, "total": 0}, + "gravity": {"domains_being_blocked": 0, "last_update": 0}, + "took": 0, +} + +FTL_ERROR = { + "error": { + "key": "FTLnotrunning", + "message": "FTL not running", + } +} SAMPLE_VERSIONS_WITH_UPDATES = { "core_current": "v5.5", @@ -62,6 +140,7 @@ PORT = 80 LOCATION = "location" NAME = "Pi hole" API_KEY = "apikey" +API_VERSION = 6 SSL = False VERIFY_SSL = True @@ -72,6 +151,7 @@ CONFIG_DATA_DEFAULTS = { CONF_SSL: DEFAULT_SSL, CONF_VERIFY_SSL: DEFAULT_VERIFY_SSL, CONF_API_KEY: API_KEY, + CONF_API_VERSION: API_VERSION, } CONFIG_DATA = { @@ -81,12 +161,14 @@ CONFIG_DATA = { CONF_API_KEY: API_KEY, CONF_SSL: SSL, CONF_VERIFY_SSL: VERIFY_SSL, + CONF_API_VERSION: API_VERSION, } CONFIG_FLOW_USER = { CONF_HOST: HOST, CONF_PORT: PORT, CONF_LOCATION: LOCATION, + CONF_API_KEY: API_KEY, CONF_NAME: NAME, CONF_SSL: SSL, CONF_VERIFY_SSL: VERIFY_SSL, @@ -103,6 +185,7 @@ CONFIG_ENTRY_WITH_API_KEY = { CONF_API_KEY: API_KEY, CONF_SSL: SSL, CONF_VERIFY_SSL: VERIFY_SSL, + CONF_API_VERSION: API_VERSION, } CONFIG_ENTRY_WITHOUT_API_KEY = { @@ -111,47 +194,129 @@ CONFIG_ENTRY_WITHOUT_API_KEY = { CONF_NAME: NAME, CONF_SSL: SSL, CONF_VERIFY_SSL: VERIFY_SSL, + CONF_API_VERSION: API_VERSION, } SWITCH_ENTITY_ID = "switch.pi_hole" def _create_mocked_hole( - raise_exception=False, has_versions=True, has_update=True, has_data=True -): - mocked_hole = MagicMock() - type(mocked_hole).get_data = AsyncMock( - side_effect=HoleError("") if raise_exception else None - ) - type(mocked_hole).get_versions = AsyncMock( - side_effect=HoleError("") if raise_exception else None - ) - type(mocked_hole).enable = AsyncMock() - type(mocked_hole).disable = AsyncMock() - if has_data: - mocked_hole.data = ZERO_DATA - else: - mocked_hole.data = [] - if has_versions: - if has_update: - mocked_hole.versions = SAMPLE_VERSIONS_WITH_UPDATES + raise_exception: bool = False, + has_versions: bool = True, + has_update: bool = True, + has_data: bool = True, + api_version: int = 5, + incorrect_app_password: bool = False, + wrong_host: bool = False, + ftl_error: bool = False, +) -> MagicMock: + """Return a mocked Hole API object with side effects based on constructor args.""" + + instances = [] + + def make_mock(**kwargs: Any) -> MagicMock: + mocked_hole = MagicMock() + # Set constructor kwargs as attributes + for key, value in kwargs.items(): + setattr(mocked_hole, key, value) + + async def authenticate_side_effect(*_args, **_kwargs): + if wrong_host: + raise HoleConnectionError("Cannot authenticate with Pi-hole: err") + password = getattr(mocked_hole, "password", None) + if ( + raise_exception + or incorrect_app_password + or (api_version == 6 and password not in ["newkey", "apikey"]) + ): + if api_version == 6: + raise HoleError("Authentication failed: Invalid password") + raise HoleConnectionError + + async def get_data_side_effect(*_args, **_kwargs): + """Return data based on the mocked Hole instance state.""" + if wrong_host: + raise HoleConnectionError("Cannot fetch data from Pi-hole: err") + password = getattr(mocked_hole, "password", None) + api_token = getattr(mocked_hole, "api_token", None) + if ( + raise_exception + or incorrect_app_password + or (api_version == 5 and (not api_token or api_token == "wrong_token")) + or (api_version == 6 and password not in ["newkey", "apikey"]) + ): + mocked_hole.data = [] if api_version == 5 else {} + elif password in ["newkey", "apikey"] or api_token in ["newkey", "apikey"]: + mocked_hole.data = ZERO_DATA_V6 if api_version == 6 else ZERO_DATA + + async def ftl_side_effect(): + mocked_hole.data = FTL_ERROR + + mocked_hole.authenticate = AsyncMock(side_effect=authenticate_side_effect) + mocked_hole.get_data = AsyncMock(side_effect=get_data_side_effect) + + if ftl_error: + # two unauthenticated instances are created in `determine_api_version` before aync_try_connect is called + if len(instances) > 1: + mocked_hole.get_data = AsyncMock(side_effect=ftl_side_effect) + mocked_hole.get_versions = AsyncMock(return_value=None) + mocked_hole.enable = AsyncMock() + mocked_hole.disable = AsyncMock() + + # Set versions and version properties + if has_versions: + versions = ( + SAMPLE_VERSIONS_WITH_UPDATES + if has_update + else SAMPLE_VERSIONS_NO_UPDATES + ) + mocked_hole.versions = versions + mocked_hole.ftl_current = versions["FTL_current"] + mocked_hole.ftl_latest = versions["FTL_latest"] + mocked_hole.ftl_update = versions["FTL_update"] + mocked_hole.core_current = versions["core_current"] + mocked_hole.core_latest = versions["core_latest"] + mocked_hole.core_update = versions["core_update"] + mocked_hole.web_current = versions["web_current"] + mocked_hole.web_latest = versions["web_latest"] + mocked_hole.web_update = versions["web_update"] else: - mocked_hole.versions = SAMPLE_VERSIONS_NO_UPDATES - else: - mocked_hole.versions = None - return mocked_hole + mocked_hole.versions = None + + # Set initial data + if has_data: + mocked_hole.data = ZERO_DATA_V6 if api_version == 6 else ZERO_DATA + else: + mocked_hole.data = [] if api_version == 5 else {} + instances.append(mocked_hole) + return mocked_hole + + # Return a factory function for patching + make_mock.instances = instances + return make_mock def _patch_init_hole(mocked_hole): - return patch("homeassistant.components.pi_hole.Hole", return_value=mocked_hole) + """Patch the Hole class in the main integration.""" + + def side_effect(*args, **kwargs): + return mocked_hole(**kwargs) + + return patch("homeassistant.components.pi_hole.Hole", side_effect=side_effect) def _patch_config_flow_hole(mocked_hole): + """Patch the Hole class in the config flow.""" + + def side_effect(*args, **kwargs): + return mocked_hole(**kwargs) + return patch( - "homeassistant.components.pi_hole.config_flow.Hole", return_value=mocked_hole + "homeassistant.components.pi_hole.config_flow.Hole", side_effect=side_effect ) def _patch_setup_hole(): + """Patch async_setup_entry for the integration.""" return patch( "homeassistant.components.pi_hole.async_setup_entry", return_value=True ) diff --git a/tests/components/pi_hole/snapshots/test_diagnostics.ambr b/tests/components/pi_hole/snapshots/test_diagnostics.ambr index 2d6f6687d04..58f4302f226 100644 --- a/tests/components/pi_hole/snapshots/test_diagnostics.ambr +++ b/tests/components/pi_hole/snapshots/test_diagnostics.ambr @@ -16,6 +16,7 @@ 'entry': dict({ 'data': dict({ 'api_key': '**REDACTED**', + 'api_version': 5, 'host': '1.2.3.4:80', 'location': 'admin', 'name': 'Pi-Hole', diff --git a/tests/components/pi_hole/test_config_flow.py b/tests/components/pi_hole/test_config_flow.py index d13712d6f76..e92a845ce1e 100644 --- a/tests/components/pi_hole/test_config_flow.py +++ b/tests/components/pi_hole/test_config_flow.py @@ -3,16 +3,15 @@ from homeassistant.components import pi_hole from homeassistant.components.pi_hole.const import DOMAIN from homeassistant.config_entries import SOURCE_USER -from homeassistant.const import CONF_API_KEY +from homeassistant.const import CONF_API_KEY, CONF_API_VERSION from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from . import ( CONFIG_DATA_DEFAULTS, CONFIG_ENTRY_WITH_API_KEY, - CONFIG_ENTRY_WITHOUT_API_KEY, - CONFIG_FLOW_API_KEY, CONFIG_FLOW_USER, + FTL_ERROR, NAME, ZERO_DATA, _create_mocked_hole, @@ -24,10 +23,14 @@ from . import ( from tests.common import MockConfigEntry -async def test_flow_user_with_api_key(hass: HomeAssistant) -> None: +async def test_flow_user_with_api_key_v6(hass: HomeAssistant) -> None: """Test user initialized flow with api key needed.""" - mocked_hole = _create_mocked_hole(has_data=False) - with _patch_config_flow_hole(mocked_hole), _patch_setup_hole() as mock_setup: + mocked_hole = _create_mocked_hole(has_data=False, api_version=6) + with ( + _patch_init_hole(mocked_hole), + _patch_config_flow_hole(mocked_hole), + _patch_setup_hole() as mock_setup, + ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, @@ -38,27 +41,19 @@ async def test_flow_user_with_api_key(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_configure( result["flow_id"], - user_input=CONFIG_FLOW_USER, + user_input={**CONFIG_FLOW_USER, CONF_API_KEY: "invalid_password"}, ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "api_key" - assert result["errors"] == {} - - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={CONF_API_KEY: "some_key"}, - ) - assert result["type"] is FlowResultType.FORM - assert result["step_id"] == "api_key" + # we have had no response from the server yet, so we expect an error assert result["errors"] == {CONF_API_KEY: "invalid_auth"} - mocked_hole.data = ZERO_DATA + # now we have a valid passiword result = await hass.config_entries.flow.async_configure( result["flow_id"], - user_input=CONFIG_FLOW_API_KEY, + user_input=CONFIG_FLOW_USER, ) + + # form should be complete with a valid config entry assert result["type"] is FlowResultType.CREATE_ENTRY - assert result["title"] == NAME assert result["data"] == CONFIG_ENTRY_WITH_API_KEY mock_setup.assert_called_once() @@ -72,10 +67,15 @@ async def test_flow_user_with_api_key(hass: HomeAssistant) -> None: assert result["reason"] == "already_configured" -async def test_flow_user_without_api_key(hass: HomeAssistant) -> None: - """Test user initialized flow without api key needed.""" - mocked_hole = _create_mocked_hole() - with _patch_config_flow_hole(mocked_hole), _patch_setup_hole() as mock_setup: +async def test_flow_user_with_api_key_v5(hass: HomeAssistant) -> None: + """Test user initialized flow with api key needed.""" + mocked_hole = _create_mocked_hole(api_version=5) + with ( + _patch_init_hole(mocked_hole), + _patch_config_flow_hole(mocked_hole), + _patch_setup_hole() as mock_setup, + ): + # start the flow as a user initiated flow result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, @@ -84,32 +84,72 @@ async def test_flow_user_without_api_key(hass: HomeAssistant) -> None: assert result["step_id"] == "user" assert result["errors"] == {} + # configure the flow with an invalid api key + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={**CONFIG_FLOW_USER, CONF_API_KEY: "wrong_token"}, + ) + + # confirm an invalid authentication error + assert result["errors"] == {CONF_API_KEY: "invalid_auth"} + + # configure the flow with a valid api key result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=CONFIG_FLOW_USER, ) + + # in API V5 we get data to confirm authentication + assert mocked_hole.instances[-1].data == ZERO_DATA + assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == NAME - assert result["data"] == CONFIG_ENTRY_WITHOUT_API_KEY + assert result["data"] == {**CONFIG_ENTRY_WITH_API_KEY, CONF_API_VERSION: 5} mock_setup.assert_called_once() + # duplicated server + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_USER}, + data=CONFIG_FLOW_USER, + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + async def test_flow_user_invalid(hass: HomeAssistant) -> None: - """Test user initialized flow with invalid server.""" + """Test user initialized flow with completely invalid server.""" mocked_hole = _create_mocked_hole(raise_exception=True) - with _patch_config_flow_hole(mocked_hole): + with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data=CONFIG_FLOW_USER ) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" - assert result["errors"] == {"base": "cannot_connect"} + assert result["errors"] == {"api_key": "invalid_auth"} + + +async def test_flow_user_invalid_v6(hass: HomeAssistant) -> None: + """Test user initialized flow with invalid server - typically a V6 API and a incorrect app password.""" + mocked_hole = _create_mocked_hole( + has_data=True, api_version=6, incorrect_app_password=True + ) + with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole): + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data=CONFIG_FLOW_USER + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {"api_key": "invalid_auth"} async def test_flow_reauth(hass: HomeAssistant) -> None: """Test reauth flow.""" - mocked_hole = _create_mocked_hole(has_data=False) - entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA_DEFAULTS) + mocked_hole = _create_mocked_hole(has_data=False, api_version=5) + entry = MockConfigEntry( + domain=pi_hole.DOMAIN, + data={**CONFIG_DATA_DEFAULTS, CONF_API_VERSION: 5, CONF_API_KEY: "oldkey"}, + ) entry.add_to_hass(hass) with _patch_init_hole(mocked_hole), _patch_config_flow_hole(mocked_hole): assert not await hass.config_entries.async_setup(entry.entry_id) @@ -120,9 +160,7 @@ async def test_flow_reauth(hass: HomeAssistant) -> None: assert len(flows) == 1 assert flows[0]["step_id"] == "reauth_confirm" assert flows[0]["context"]["entry_id"] == entry.entry_id - - mocked_hole.data = ZERO_DATA - + mocked_hole.instances[-1].api_token = "newkey" result = await hass.config_entries.flow.async_configure( flows[0]["flow_id"], user_input={CONF_API_KEY: "newkey"}, @@ -131,3 +169,28 @@ async def test_flow_reauth(hass: HomeAssistant) -> None: assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reauth_successful" assert entry.data[CONF_API_KEY] == "newkey" + + +async def test_flow_user_invalid_host(hass: HomeAssistant) -> None: + """Test user initialized flow with invalid server host address.""" + mocked_hole = _create_mocked_hole(api_version=6, wrong_host=True) + with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole): + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data=CONFIG_FLOW_USER + ) + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {"base": "cannot_connect"} + + +async def test_flow_error_response(hass: HomeAssistant) -> None: + """Test user initialized flow but dataotherbase errors occur.""" + mocked_hole = _create_mocked_hole(api_version=5, ftl_error=True, has_data=False) + with _patch_config_flow_hole(mocked_hole), _patch_init_hole(mocked_hole): + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER}, data=CONFIG_FLOW_USER + ) + assert mocked_hole.instances[-1].data == FTL_ERROR + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + assert result["errors"] == {"base": "cannot_connect"} diff --git a/tests/components/pi_hole/test_diagnostics.py b/tests/components/pi_hole/test_diagnostics.py index 8d5a83e4622..678efdf078e 100644 --- a/tests/components/pi_hole/test_diagnostics.py +++ b/tests/components/pi_hole/test_diagnostics.py @@ -19,9 +19,10 @@ async def test_diagnostics( snapshot: SnapshotAssertion, ) -> None: """Tests diagnostics.""" - mocked_hole = _create_mocked_hole() + mocked_hole = _create_mocked_hole(api_version=5) + config_entry = {**CONFIG_DATA_DEFAULTS, "api_version": 5} entry = MockConfigEntry( - domain=pi_hole.DOMAIN, data=CONFIG_DATA_DEFAULTS, entry_id="pi_hole_mock_entry" + domain=pi_hole.DOMAIN, data=config_entry, entry_id="pi_hole_mock_entry" ) entry.add_to_hass(hass) with _patch_init_hole(mocked_hole): diff --git a/tests/components/pi_hole/test_init.py b/tests/components/pi_hole/test_init.py index 72b48e3d572..b4cc11529d9 100644 --- a/tests/components/pi_hole/test_init.py +++ b/tests/components/pi_hole/test_init.py @@ -16,6 +16,7 @@ from homeassistant.components.pi_hole.const import ( from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ( ATTR_ENTITY_ID, + CONF_API_VERSION, CONF_HOST, CONF_LOCATION, CONF_NAME, @@ -27,7 +28,7 @@ from . import ( API_KEY, CONFIG_DATA, CONFIG_DATA_DEFAULTS, - CONFIG_ENTRY_WITHOUT_API_KEY, + DEFAULT_VERIFY_SSL, SWITCH_ENTITY_ID, _create_mocked_hole, _patch_init_hole, @@ -38,32 +39,62 @@ from tests.common import MockConfigEntry @pytest.mark.parametrize( ("config_entry_data", "expected_api_token"), - [(CONFIG_DATA_DEFAULTS, API_KEY), (CONFIG_ENTRY_WITHOUT_API_KEY, "")], + [(CONFIG_DATA_DEFAULTS, API_KEY)], ) -async def test_setup_api( +async def test_setup_api_v6( hass: HomeAssistant, config_entry_data: dict, expected_api_token: str ) -> None: """Tests the API object is created with the expected parameters.""" - mocked_hole = _create_mocked_hole() + mocked_hole = _create_mocked_hole(api_version=6) + config_entry_data = {**config_entry_data} + entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=config_entry_data) + entry.add_to_hass(hass) + with _patch_init_hole(mocked_hole) as patched_init_hole: + assert await hass.config_entries.async_setup(entry.entry_id) + patched_init_hole.assert_called_once_with( + host=config_entry_data[CONF_HOST], + session=ANY, + password=expected_api_token, + location=config_entry_data[CONF_LOCATION], + protocol="http", + version=6, + verify_tls=DEFAULT_VERIFY_SSL, + ) + + +@pytest.mark.parametrize( + ("config_entry_data", "expected_api_token"), + [({**CONFIG_DATA_DEFAULTS}, API_KEY)], +) +async def test_setup_api_v5( + hass: HomeAssistant, config_entry_data: dict, expected_api_token: str +) -> None: + """Tests the API object is created with the expected parameters.""" + mocked_hole = _create_mocked_hole(api_version=5) + config_entry_data = {**config_entry_data} + config_entry_data[CONF_API_VERSION] = 5 config_entry_data = {**config_entry_data, CONF_STATISTICS_ONLY: True} entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=config_entry_data) entry.add_to_hass(hass) with _patch_init_hole(mocked_hole) as patched_init_hole: assert await hass.config_entries.async_setup(entry.entry_id) patched_init_hole.assert_called_once_with( - config_entry_data[CONF_HOST], - ANY, + host=config_entry_data[CONF_HOST], + session=ANY, api_token=expected_api_token, location=config_entry_data[CONF_LOCATION], tls=config_entry_data[CONF_SSL], + version=5, + verify_tls=DEFAULT_VERIFY_SSL, ) -async def test_setup_with_defaults(hass: HomeAssistant) -> None: +async def test_setup_with_defaults_v5(hass: HomeAssistant) -> None: """Tests component setup with default config.""" - mocked_hole = _create_mocked_hole() + mocked_hole = _create_mocked_hole(api_version=5) entry = MockConfigEntry( - domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS, CONF_STATISTICS_ONLY: True} + domain=pi_hole.DOMAIN, + data={**CONFIG_DATA_DEFAULTS, CONF_API_VERSION: 5, CONF_STATISTICS_ONLY: True}, ) entry.add_to_hass(hass) with _patch_init_hole(mocked_hole): @@ -110,9 +141,87 @@ async def test_setup_with_defaults(hass: HomeAssistant) -> None: assert state.state == "off" +async def test_setup_with_defaults_v6(hass: HomeAssistant) -> None: + """Tests component setup with default config.""" + mocked_hole = _create_mocked_hole( + api_version=6, has_data=True, incorrect_app_password=False + ) + entry = MockConfigEntry( + domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS, CONF_STATISTICS_ONLY: True} + ) + entry.add_to_hass(hass) + with _patch_init_hole(mocked_hole): + assert await hass.config_entries.async_setup(entry.entry_id) + + state = hass.states.get("sensor.pi_hole_ads_blocked") + assert state is not None + assert state.name == "Pi-Hole Ads blocked" + assert state.state == "0" + + state = hass.states.get("sensor.pi_hole_ads_percentage_blocked") + assert state.name == "Pi-Hole Ads percentage blocked" + assert state.state == "0" + + state = hass.states.get("sensor.pi_hole_dns_queries_cached") + assert state.name == "Pi-Hole DNS queries cached" + assert state.state == "0" + + state = hass.states.get("sensor.pi_hole_dns_queries_forwarded") + assert state.name == "Pi-Hole DNS queries forwarded" + assert state.state == "0" + + state = hass.states.get("sensor.pi_hole_dns_queries") + assert state.name == "Pi-Hole DNS queries" + assert state.state == "0" + + state = hass.states.get("sensor.pi_hole_dns_unique_clients") + assert state.name == "Pi-Hole DNS unique clients" + assert state.state == "0" + + state = hass.states.get("sensor.pi_hole_dns_unique_domains") + assert state.name == "Pi-Hole DNS unique domains" + assert state.state == "0" + + state = hass.states.get("sensor.pi_hole_domains_blocked") + assert state.name == "Pi-Hole Domains blocked" + assert state.state == "0" + + state = hass.states.get("sensor.pi_hole_seen_clients") + assert state.name == "Pi-Hole Seen clients" + assert state.state == "0" + + state = hass.states.get("binary_sensor.pi_hole_status") + assert state.name == "Pi-Hole Status" + assert state.state == "off" + + +async def test_setup_without_api_version(hass: HomeAssistant) -> None: + """Tests component setup without API version.""" + + mocked_hole = _create_mocked_hole(api_version=6) + config = {**CONFIG_DATA_DEFAULTS} + config.pop(CONF_API_VERSION) + entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=config) + entry.add_to_hass(hass) + with _patch_init_hole(mocked_hole): + assert await hass.config_entries.async_setup(entry.entry_id) + + assert entry.data[CONF_API_VERSION] == 6 + + mocked_hole = _create_mocked_hole(api_version=5) + config = {**CONFIG_DATA_DEFAULTS} + config.pop(CONF_API_VERSION) + entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=config) + entry.add_to_hass(hass) + with _patch_init_hole(mocked_hole): + assert await hass.config_entries.async_setup(entry.entry_id) + + assert entry.data[CONF_API_VERSION] == 5 + + async def test_setup_name_config(hass: HomeAssistant) -> None: """Tests component setup with a custom name.""" - mocked_hole = _create_mocked_hole() + mocked_hole = _create_mocked_hole(api_version=6) entry = MockConfigEntry( domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS, CONF_NAME: "Custom"} ) @@ -122,16 +231,15 @@ async def test_setup_name_config(hass: HomeAssistant) -> None: await hass.async_block_till_done() - assert ( - hass.states.get("sensor.custom_ads_blocked_today").name - == "Custom Ads blocked today" - ) + assert hass.states.get("sensor.custom_ads_blocked").name == "Custom Ads blocked" async def test_switch(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> None: """Test Pi-hole switch.""" mocked_hole = _create_mocked_hole() - entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA) + entry = MockConfigEntry( + domain=pi_hole.DOMAIN, data={**CONFIG_DATA, CONF_API_VERSION: 5} + ) entry.add_to_hass(hass) with _patch_init_hole(mocked_hole): @@ -145,7 +253,7 @@ async def test_switch(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> {"entity_id": SWITCH_ENTITY_ID}, blocking=True, ) - mocked_hole.enable.assert_called_once() + mocked_hole.instances[-1].enable.assert_called_once() await hass.services.async_call( switch.DOMAIN, @@ -153,17 +261,17 @@ async def test_switch(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> {"entity_id": SWITCH_ENTITY_ID}, blocking=True, ) - mocked_hole.disable.assert_called_once_with(True) + mocked_hole.instances[-1].disable.assert_called_once_with(True) # Failed calls - type(mocked_hole).enable = AsyncMock(side_effect=HoleError("Error1")) + mocked_hole.instances[-1].enable = AsyncMock(side_effect=HoleError("Error1")) await hass.services.async_call( switch.DOMAIN, switch.SERVICE_TURN_ON, {"entity_id": SWITCH_ENTITY_ID}, blocking=True, ) - type(mocked_hole).disable = AsyncMock(side_effect=HoleError("Error2")) + mocked_hole.instances[-1].disable = AsyncMock(side_effect=HoleError("Error2")) await hass.services.async_call( switch.DOMAIN, switch.SERVICE_TURN_OFF, @@ -171,6 +279,7 @@ async def test_switch(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> blocking=True, ) errors = [x for x in caplog.records if x.levelno == logging.ERROR] + assert errors[-2].message == "Unable to enable Pi-hole: Error1" assert errors[-1].message == "Unable to disable Pi-hole: Error2" @@ -178,7 +287,7 @@ async def test_switch(hass: HomeAssistant, caplog: pytest.LogCaptureFixture) -> async def test_disable_service_call(hass: HomeAssistant) -> None: """Test disable service call with no Pi-hole named.""" - mocked_hole = _create_mocked_hole() + mocked_hole = _create_mocked_hole(api_version=6) with _patch_init_hole(mocked_hole): entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA) entry.add_to_hass(hass) @@ -199,7 +308,7 @@ async def test_disable_service_call(hass: HomeAssistant) -> None: blocking=True, ) - mocked_hole.disable.assert_called_with(1) + mocked_hole.instances[-1].disable.assert_called_with(1) async def test_unload(hass: HomeAssistant) -> None: @@ -209,7 +318,7 @@ async def test_unload(hass: HomeAssistant) -> None: data={**CONFIG_DATA_DEFAULTS, CONF_HOST: "pi.hole"}, ) entry.add_to_hass(hass) - mocked_hole = _create_mocked_hole() + mocked_hole = _create_mocked_hole(api_version=6) with _patch_init_hole(mocked_hole): await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() @@ -222,7 +331,7 @@ async def test_unload(hass: HomeAssistant) -> None: async def test_remove_obsolete(hass: HomeAssistant) -> None: """Test removing obsolete config entry parameters.""" - mocked_hole = _create_mocked_hole() + mocked_hole = _create_mocked_hole(api_version=6) entry = MockConfigEntry( domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS, CONF_STATISTICS_ONLY: True} ) diff --git a/tests/components/pi_hole/test_repairs.py b/tests/components/pi_hole/test_repairs.py new file mode 100644 index 00000000000..4982b1544c7 --- /dev/null +++ b/tests/components/pi_hole/test_repairs.py @@ -0,0 +1,136 @@ +"""Test pi_hole component.""" + +from datetime import timedelta +from unittest.mock import AsyncMock + +from hole.exceptions import HoleConnectionError, HoleError +import pytest + +import homeassistant +from homeassistant.components import pi_hole +from homeassistant.components.pi_hole.const import VERSION_6_RESPONSE_TO_5_ERROR +from homeassistant.const import CONF_API_VERSION, STATE_UNAVAILABLE +from homeassistant.core import HomeAssistant +from homeassistant.helpers import issue_registry as ir +from homeassistant.util import dt as dt_util + +from . import CONFIG_DATA_DEFAULTS, ZERO_DATA, _create_mocked_hole, _patch_init_hole + +from tests.common import MockConfigEntry, async_fire_time_changed + + +async def test_change_api_5_to_6( + hass: HomeAssistant, issue_registry: ir.IssueRegistry +) -> None: + """Tests a user with an API version 5 config entry that is updated to API version 6.""" + mocked_hole = _create_mocked_hole(api_version=5) + + # setu up a valid API version 5 config entry + entry = MockConfigEntry( + domain=pi_hole.DOMAIN, + data={**CONFIG_DATA_DEFAULTS, CONF_API_VERSION: 5}, + ) + entry.add_to_hass(hass) + with _patch_init_hole(mocked_hole): + assert await hass.config_entries.async_setup(entry.entry_id) + + assert mocked_hole.instances[-1].data == ZERO_DATA + # Change the mock's state after setup + mocked_hole.instances[-1].hole_version = 6 + mocked_hole.instances[-1].api_token = "wrong_token" + + # Patch the method on the coordinator's api reference directly + pihole_data = entry.runtime_data + assert pihole_data.api == mocked_hole.instances[-1] + pihole_data.api.get_data = AsyncMock( + side_effect=lambda: setattr( + pihole_data.api, + "data", + {"error": VERSION_6_RESPONSE_TO_5_ERROR, "took": 0.0001430511474609375}, + ) + ) + + # Now trigger the update + with pytest.raises(homeassistant.exceptions.ConfigEntryAuthFailed): + await pihole_data.coordinator.update_method() + assert pihole_data.api.data == { + "error": VERSION_6_RESPONSE_TO_5_ERROR, + "took": 0.0001430511474609375, + } + + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10)) + await hass.async_block_till_done() + # ensure a re-auth flow is created + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + assert flows[0]["step_id"] == "reauth_confirm" + assert flows[0]["context"]["entry_id"] == entry.entry_id + + +async def test_app_password_changing( + hass: HomeAssistant, issue_registry: ir.IssueRegistry +) -> None: + """Tests a user with an API version 5 config entry that is updated to API version 6.""" + mocked_hole = _create_mocked_hole( + api_version=6, has_data=True, incorrect_app_password=False + ) + entry = MockConfigEntry(domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS}) + entry.add_to_hass(hass) + with _patch_init_hole(mocked_hole): + assert await hass.config_entries.async_setup(entry.entry_id) + + state = hass.states.get("sensor.pi_hole_ads_blocked") + assert state is not None + assert state.name == "Pi-Hole Ads blocked" + assert state.state == "0" + + # Test app password changing + async def fail_auth(): + """Set mocked data to bad_data.""" + raise HoleError("Authentication failed: Invalid password") + + mocked_hole.instances[-1].get_data = AsyncMock(side_effect=fail_auth) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10)) + await hass.async_block_till_done() + + flows = hass.config_entries.flow.async_progress() + assert len(flows) == 1 + assert flows[0]["step_id"] == "reauth_confirm" + assert flows[0]["context"]["entry_id"] == entry.entry_id + + # Test app password changing + async def fail_fetch(): + """Set mocked data to bad_data.""" + raise HoleConnectionError("Cannot fetch data from Pi-hole: 200") + + mocked_hole.instances[-1].get_data = AsyncMock(side_effect=fail_fetch) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10)) + await hass.async_block_till_done() + + +async def test_app_failed_fetch( + hass: HomeAssistant, issue_registry: ir.IssueRegistry +) -> None: + """Tests a user with an API version 5 config entry that is updated to API version 6.""" + mocked_hole = _create_mocked_hole( + api_version=6, has_data=True, incorrect_app_password=False + ) + entry = MockConfigEntry(domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS}) + entry.add_to_hass(hass) + with _patch_init_hole(mocked_hole): + assert await hass.config_entries.async_setup(entry.entry_id) + + state = hass.states.get("sensor.pi_hole_ads_blocked") + assert state.state == "0" + + # Test fetch failing changing + async def fail_fetch(): + """Set mocked data to bad_data.""" + raise HoleConnectionError("Cannot fetch data from Pi-hole: 200") + + mocked_hole.instances[-1].get_data = AsyncMock(side_effect=fail_fetch) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=10)) + await hass.async_block_till_done() + + state = hass.states.get("sensor.pi_hole_ads_blocked") + assert state.state == STATE_UNAVAILABLE diff --git a/tests/components/pi_hole/test_sensor.py b/tests/components/pi_hole/test_sensor.py new file mode 100644 index 00000000000..7d3efd938fe --- /dev/null +++ b/tests/components/pi_hole/test_sensor.py @@ -0,0 +1,79 @@ +"""Test pi_hole component.""" + +import copy +from datetime import timedelta +from unittest.mock import AsyncMock + +import pytest + +from homeassistant.components import pi_hole +from homeassistant.components.pi_hole.const import CONF_STATISTICS_ONLY +from homeassistant.core import HomeAssistant +from homeassistant.util import dt as dt_util + +from . import CONFIG_DATA_DEFAULTS, ZERO_DATA_V6, _create_mocked_hole, _patch_init_hole + +from tests.common import MockConfigEntry, async_fire_time_changed + + +async def test_bad_data_type( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test handling of bad data. Mostly for code coverage, rather than simulating known error states.""" + mocked_hole = _create_mocked_hole( + api_version=6, has_data=True, incorrect_app_password=False + ) + entry = MockConfigEntry( + domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS, CONF_STATISTICS_ONLY: True} + ) + entry.add_to_hass(hass) + with _patch_init_hole(mocked_hole): + assert await hass.config_entries.async_setup(entry.entry_id) + + bad_data = copy.deepcopy(ZERO_DATA_V6) + bad_data["queries"]["total"] = "error string" + assert bad_data != ZERO_DATA_V6 + + async def set_bad_data(): + """Set mocked data to bad_data.""" + mocked_hole.instances[-1].data = bad_data + + mocked_hole.instances[-1].get_data = AsyncMock(side_effect=set_bad_data) + + # Wait a minute + future = dt_util.utcnow() + timedelta(minutes=1) + async_fire_time_changed(hass, future) + await hass.async_block_till_done() + + assert "TypeError" in caplog.text + + +async def test_bad_data_key( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test handling of bad data. Mostly for code coverage, rather than simulating known error states.""" + mocked_hole = _create_mocked_hole( + api_version=6, has_data=True, incorrect_app_password=False + ) + entry = MockConfigEntry( + domain=pi_hole.DOMAIN, data={**CONFIG_DATA_DEFAULTS, CONF_STATISTICS_ONLY: True} + ) + entry.add_to_hass(hass) + with _patch_init_hole(mocked_hole): + assert await hass.config_entries.async_setup(entry.entry_id) + + bad_data = copy.deepcopy(ZERO_DATA_V6) + # remove a whole part of the dict tree now + bad_data["queries"] = "error string" + assert bad_data != ZERO_DATA_V6 + + async def set_bad_data(): + """Set mocked data to bad_data.""" + mocked_hole.instances[-1].data = bad_data + + mocked_hole.instances[-1].get_data = AsyncMock(side_effect=set_bad_data) + async_fire_time_changed(hass, dt_util.utcnow() + timedelta(minutes=1)) + await hass.async_block_till_done() + assert mocked_hole.instances[-1].data != ZERO_DATA_V6 + + assert "KeyError" in caplog.text diff --git a/tests/components/pi_hole/test_update.py b/tests/components/pi_hole/test_update.py index 705e9f9c08d..5e81d91b5bd 100644 --- a/tests/components/pi_hole/test_update.py +++ b/tests/components/pi_hole/test_update.py @@ -11,7 +11,7 @@ from tests.common import MockConfigEntry async def test_update(hass: HomeAssistant) -> None: """Tests update entity.""" - mocked_hole = _create_mocked_hole() + mocked_hole = _create_mocked_hole(api_version=6) entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA_DEFAULTS) entry.add_to_hass(hass) with _patch_init_hole(mocked_hole): @@ -52,7 +52,7 @@ async def test_update(hass: HomeAssistant) -> None: async def test_update_no_versions(hass: HomeAssistant) -> None: """Tests update entity when no version data available.""" - mocked_hole = _create_mocked_hole(has_versions=False) + mocked_hole = _create_mocked_hole(has_versions=False, api_version=6) entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA_DEFAULTS) entry.add_to_hass(hass) with _patch_init_hole(mocked_hole): @@ -84,7 +84,9 @@ async def test_update_no_versions(hass: HomeAssistant) -> None: async def test_update_no_updates(hass: HomeAssistant) -> None: """Tests update entity when no latest data available.""" - mocked_hole = _create_mocked_hole(has_versions=True, has_update=False) + mocked_hole = _create_mocked_hole( + has_versions=True, has_update=False, api_version=6 + ) entry = MockConfigEntry(domain=pi_hole.DOMAIN, data=CONFIG_DATA_DEFAULTS) entry.add_to_hass(hass) with _patch_init_hole(mocked_hole): From f1698cdb75d45fff7621cc368740261d8d50988f Mon Sep 17 00:00:00 2001 From: Markus Adrario Date: Sat, 5 Jul 2025 10:26:04 +0200 Subject: [PATCH 063/106] Add reauth flow to homee (#147258) --- homeassistant/components/homee/__init__.py | 10 +- homeassistant/components/homee/config_flow.py | 60 ++++++++ homeassistant/components/homee/strings.json | 16 ++- tests/components/homee/conftest.py | 2 + tests/components/homee/test_config_flow.py | 136 +++++++++++++++++- 5 files changed, 214 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/homee/__init__.py b/homeassistant/components/homee/__init__.py index 0f90752733d..d748d1dd809 100644 --- a/homeassistant/components/homee/__init__.py +++ b/homeassistant/components/homee/__init__.py @@ -7,7 +7,7 @@ from pyHomee import Homee, HomeeAuthFailedException, HomeeConnectionFailedExcept from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant -from homeassistant.exceptions import ConfigEntryNotReady +from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import device_registry as dr from .const import DOMAIN @@ -53,12 +53,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: HomeeConfigEntry) -> boo try: await homee.get_access_token() except HomeeConnectionFailedException as exc: - raise ConfigEntryNotReady( - f"Connection to Homee failed: {exc.__cause__}" - ) from exc + raise ConfigEntryNotReady(f"Connection to Homee failed: {exc.reason}") from exc except HomeeAuthFailedException as exc: - raise ConfigEntryNotReady( - f"Authentication to Homee failed: {exc.__cause__}" + raise ConfigEntryAuthFailed( + f"Authentication to Homee failed: {exc.reason}" ) from exc hass.loop.create_task(homee.run()) diff --git a/homeassistant/components/homee/config_flow.py b/homeassistant/components/homee/config_flow.py index fcf03322d0d..7030752f4c3 100644 --- a/homeassistant/components/homee/config_flow.py +++ b/homeassistant/components/homee/config_flow.py @@ -1,5 +1,6 @@ """Config flow for homee integration.""" +from collections.abc import Mapping import logging from typing import Any @@ -32,6 +33,8 @@ class HomeeConfigFlow(ConfigFlow, domain=DOMAIN): VERSION = 1 homee: Homee + _reauth_host: str + _reauth_username: str async def async_step_user( self, user_input: dict[str, Any] | None = None @@ -84,6 +87,63 @@ class HomeeConfigFlow(ConfigFlow, domain=DOMAIN): errors=errors, ) + async def async_step_reauth( + self, entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: + """Perform reauthentication upon an API authentication error.""" + self._reauth_host = entry_data[CONF_HOST] + self._reauth_username = entry_data[CONF_USERNAME] + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, user_input: dict[str, Any] | None = None + ) -> ConfigFlowResult: + """Confirm reauthentication dialog.""" + errors: dict[str, str] = {} + + if user_input: + self.homee = Homee( + self._reauth_host, user_input[CONF_USERNAME], user_input[CONF_PASSWORD] + ) + try: + await self.homee.get_access_token() + except HomeeConnectionFailedException: + errors["base"] = "cannot_connect" + except HomeeAuthenticationFailedException: + errors["base"] = "invalid_auth" + except Exception: + _LOGGER.exception("Unexpected exception") + errors["base"] = "unknown" + else: + self.hass.loop.create_task(self.homee.run()) + await self.homee.wait_until_connected() + self.homee.disconnect() + await self.homee.wait_until_disconnected() + + await self.async_set_unique_id(self.homee.settings.uid) + self._abort_if_unique_id_mismatch(reason="wrong_hub") + + _LOGGER.debug( + "Reauthenticated homee entry with ID %s", self.homee.settings.uid + ) + return self.async_update_reload_and_abort( + self._get_reauth_entry(), data_updates=user_input + ) + + return self.async_show_form( + step_id="reauth_confirm", + data_schema=vol.Schema( + { + vol.Required(CONF_USERNAME, default=self._reauth_username): str, + vol.Required(CONF_PASSWORD): str, + } + ), + description_placeholders={ + "host": self._reauth_host, + }, + errors=errors, + ) + async def async_step_reconfigure( self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: diff --git a/homeassistant/components/homee/strings.json b/homeassistant/components/homee/strings.json index 8b10b3ebb8a..9523d62c671 100644 --- a/homeassistant/components/homee/strings.json +++ b/homeassistant/components/homee/strings.json @@ -3,8 +3,9 @@ "flow_title": "homee {name} ({host})", "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]", "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]", - "wrong_hub": "Address belongs to a different homee." + "wrong_hub": "IP-Address belongs to a different homee than the configured one." }, "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", @@ -25,6 +26,17 @@ "password": "The password for your homee." } }, + "reauth_confirm": { + "title": "[%key:common::config_flow::title::reauth%]", + "data": { + "username": "[%key:common::config_flow::data::username%]", + "password": "[%key:common::config_flow::data::password%]" + }, + "data_description": { + "username": "[%key:component::homee::config::step::user::data_description::username%]", + "password": "[%key:component::homee::config::step::user::data_description::password%]" + } + }, "reconfigure": { "title": "Reconfigure homee {name}", "description": "Reconfigure the IP address of your homee.", @@ -32,7 +44,7 @@ "host": "[%key:common::config_flow::data::host%]" }, "data_description": { - "host": "The IP address of your homee." + "host": "[%key:component::homee::config::step::user::data_description::host%]" } } } diff --git a/tests/components/homee/conftest.py b/tests/components/homee/conftest.py index f9fa95c593f..3db3e809374 100644 --- a/tests/components/homee/conftest.py +++ b/tests/components/homee/conftest.py @@ -15,7 +15,9 @@ HOMEE_IP = "192.168.1.11" NEW_HOMEE_IP = "192.168.1.12" HOMEE_NAME = "TestHomee" TESTUSER = "testuser" +NEW_TESTUSER = "testuser2" TESTPASS = "testpass" +NEW_TESTPASS = "testpass2" @pytest.fixture diff --git a/tests/components/homee/test_config_flow.py b/tests/components/homee/test_config_flow.py index 70d34ced91c..6f45dcbdb0d 100644 --- a/tests/components/homee/test_config_flow.py +++ b/tests/components/homee/test_config_flow.py @@ -11,7 +11,16 @@ from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from .conftest import HOMEE_ID, HOMEE_IP, HOMEE_NAME, NEW_HOMEE_IP, TESTPASS, TESTUSER +from .conftest import ( + HOMEE_ID, + HOMEE_IP, + HOMEE_NAME, + NEW_HOMEE_IP, + NEW_TESTPASS, + NEW_TESTUSER, + TESTPASS, + TESTUSER, +) from tests.common import MockConfigEntry @@ -113,7 +122,6 @@ async def test_flow_already_configured( ) -> None: """Test config flow aborts when already configured.""" mock_config_entry.add_to_hass(hass) - result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER} ) @@ -132,6 +140,130 @@ async def test_flow_already_configured( assert result["reason"] == "already_configured" +@pytest.mark.usefixtures("mock_homee", "mock_setup_entry") +async def test_reauth_success( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the reauth flow.""" + mock_config_entry.add_to_hass(hass) + result = await mock_config_entry.start_reauth_flow(hass) + + assert result["step_id"] == "reauth_confirm" + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {} + assert result["handler"] == DOMAIN + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_USERNAME: NEW_TESTUSER, + CONF_PASSWORD: NEW_TESTPASS, + }, + ) + + assert result2["type"] is FlowResultType.ABORT + assert result2["reason"] == "reauth_successful" + + # Confirm that the config entry has been updated + assert mock_config_entry.data[CONF_HOST] == HOMEE_IP + assert mock_config_entry.data[CONF_USERNAME] == NEW_TESTUSER + assert mock_config_entry.data[CONF_PASSWORD] == NEW_TESTPASS + + +@pytest.mark.parametrize( + ("side_eff", "error"), + [ + ( + HomeeConnectionFailedException("connection timed out"), + {"base": "cannot_connect"}, + ), + ( + HomeeAuthFailedException("wrong username or password"), + {"base": "invalid_auth"}, + ), + ( + Exception, + {"base": "unknown"}, + ), + ], +) +async def test_reauth_errors( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_homee: AsyncMock, + side_eff: Exception, + error: dict[str, str], +) -> None: + """Test reconfigure flow errors.""" + mock_config_entry.add_to_hass(hass) + result = await mock_config_entry.start_reauth_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + + mock_homee.get_access_token.side_effect = side_eff + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_USERNAME: NEW_TESTUSER, + CONF_PASSWORD: NEW_TESTPASS, + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == error + + # Confirm that the config entry is unchanged + assert mock_config_entry.data[CONF_USERNAME] == TESTUSER + assert mock_config_entry.data[CONF_PASSWORD] == TESTPASS + + mock_homee.get_access_token.side_effect = None + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_USERNAME: NEW_TESTUSER, + CONF_PASSWORD: NEW_TESTPASS, + }, + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reauth_successful" + + # Confirm that the config entry has been updated + assert mock_config_entry.data[CONF_HOST] == HOMEE_IP + assert mock_config_entry.data[CONF_USERNAME] == NEW_TESTUSER + assert mock_config_entry.data[CONF_PASSWORD] == NEW_TESTPASS + + +async def test_reauth_wrong_uid( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_homee: AsyncMock, +) -> None: + """Test reauth flow with wrong UID.""" + mock_homee.settings.uid = "wrong_uid" + mock_config_entry.add_to_hass(hass) + result = await mock_config_entry.start_reauth_flow(hass) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_USERNAME: NEW_TESTUSER, + CONF_PASSWORD: NEW_TESTPASS, + }, + ) + + assert result2["type"] is FlowResultType.ABORT + assert result2["reason"] == "wrong_hub" + + # Confirm that the config entry is unchanged + assert mock_config_entry.data[CONF_HOST] == HOMEE_IP + + @pytest.mark.usefixtures("mock_setup_entry") async def test_reconfigure_success( hass: HomeAssistant, From fea7dc7eba402c42b31d104707eaa3097217830a Mon Sep 17 00:00:00 2001 From: tronikos Date: Sat, 5 Jul 2025 01:26:15 -0700 Subject: [PATCH 064/106] Remember Opower utility and username on config flow errors (#148097) --- homeassistant/components/opower/config_flow.py | 11 +++++++++-- tests/components/opower/test_config_flow.py | 11 ++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/opower/config_flow.py b/homeassistant/components/opower/config_flow.py index 4753a77894e..e7f2534e1ad 100644 --- a/homeassistant/components/opower/config_flow.py +++ b/homeassistant/components/opower/config_flow.py @@ -26,6 +26,7 @@ from .const import CONF_TOTP_SECRET, CONF_UTILITY, DOMAIN _LOGGER = logging.getLogger(__name__) + STEP_USER_DATA_SCHEMA = vol.Schema( { vol.Required(CONF_UTILITY): vol.In(get_supported_utility_names()), @@ -88,9 +89,15 @@ class OpowerConfigFlow(ConfigFlow, domain=DOMAIN): errors = await _validate_login(self.hass, user_input) if not errors: return self._async_create_opower_entry(user_input) - + else: + user_input = {} + user_input.pop(CONF_PASSWORD, None) return self.async_show_form( - step_id="user", data_schema=STEP_USER_DATA_SCHEMA, errors=errors + step_id="user", + data_schema=self.add_suggested_values_to_schema( + STEP_USER_DATA_SCHEMA, user_input + ), + errors=errors, ) async def async_step_mfa( diff --git a/tests/components/opower/test_config_flow.py b/tests/components/opower/test_config_flow.py index 8134539b0a5..c9edfc6808f 100644 --- a/tests/components/opower/test_config_flow.py +++ b/tests/components/opower/test_config_flow.py @@ -13,7 +13,7 @@ from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, get_schema_suggested_value @pytest.fixture(autouse=True, name="mock_setup_entry") @@ -203,6 +203,15 @@ async def test_form_exceptions( assert result2["type"] is FlowResultType.FORM assert result2["errors"] == {"base": expected_error} + # On error, the form should have the previous user input, except password, + # as suggested values. + data_schema = result2["data_schema"].schema + assert ( + get_schema_suggested_value(data_schema, "utility") + == "Pacific Gas and Electric Company (PG&E)" + ) + assert get_schema_suggested_value(data_schema, "username") == "test-username" + assert get_schema_suggested_value(data_schema, "password") is None assert mock_login.call_count == 1 From b72536acfa81a5e7330a9f510856f0414b03ca6a Mon Sep 17 00:00:00 2001 From: Norbert Rittel Date: Sat, 5 Jul 2025 10:59:57 +0200 Subject: [PATCH 065/106] Make "autorelock" consistent across integrations in `matter` (#148023) --- homeassistant/components/matter/strings.json | 2 +- .../matter/snapshots/test_number.ambr | 24 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/matter/strings.json b/homeassistant/components/matter/strings.json index df1cbc5adb0..0ac44c006ab 100644 --- a/homeassistant/components/matter/strings.json +++ b/homeassistant/components/matter/strings.json @@ -193,7 +193,7 @@ "name": "Occupied to unoccupied delay" }, "auto_relock_timer": { - "name": "Automatic relock timer" + "name": "Autorelock time" } }, "light": { diff --git a/tests/components/matter/snapshots/test_number.ambr b/tests/components/matter/snapshots/test_number.ambr index c1d08dba8a1..8d27c4b4691 100644 --- a/tests/components/matter/snapshots/test_number.ambr +++ b/tests/components/matter/snapshots/test_number.ambr @@ -402,7 +402,7 @@ 'state': '1.0', }) # --- -# name: test_numbers[door_lock][number.mock_door_lock_automatic_relock_timer-entry] +# name: test_numbers[door_lock][number.mock_door_lock_autorelock_time-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -420,7 +420,7 @@ 'disabled_by': None, 'domain': 'number', 'entity_category': , - 'entity_id': 'number.mock_door_lock_automatic_relock_timer', + 'entity_id': 'number.mock_door_lock_autorelock_time', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -432,7 +432,7 @@ }), 'original_device_class': None, 'original_icon': None, - 'original_name': 'Automatic relock timer', + 'original_name': 'Autorelock time', 'platform': 'matter', 'previous_unique_id': None, 'suggested_object_id': None, @@ -442,10 +442,10 @@ 'unit_of_measurement': , }) # --- -# name: test_numbers[door_lock][number.mock_door_lock_automatic_relock_timer-state] +# name: test_numbers[door_lock][number.mock_door_lock_autorelock_time-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'Mock Door Lock Automatic relock timer', + 'friendly_name': 'Mock Door Lock Autorelock time', 'max': 65534, 'min': 0, 'mode': , @@ -453,14 +453,14 @@ 'unit_of_measurement': , }), 'context': , - 'entity_id': 'number.mock_door_lock_automatic_relock_timer', + 'entity_id': 'number.mock_door_lock_autorelock_time', 'last_changed': , 'last_reported': , 'last_updated': , 'state': '60', }) # --- -# name: test_numbers[door_lock_with_unbolt][number.mock_door_lock_automatic_relock_timer-entry] +# name: test_numbers[door_lock_with_unbolt][number.mock_door_lock_autorelock_time-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -478,7 +478,7 @@ 'disabled_by': None, 'domain': 'number', 'entity_category': , - 'entity_id': 'number.mock_door_lock_automatic_relock_timer', + 'entity_id': 'number.mock_door_lock_autorelock_time', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -490,7 +490,7 @@ }), 'original_device_class': None, 'original_icon': None, - 'original_name': 'Automatic relock timer', + 'original_name': 'Autorelock time', 'platform': 'matter', 'previous_unique_id': None, 'suggested_object_id': None, @@ -500,10 +500,10 @@ 'unit_of_measurement': , }) # --- -# name: test_numbers[door_lock_with_unbolt][number.mock_door_lock_automatic_relock_timer-state] +# name: test_numbers[door_lock_with_unbolt][number.mock_door_lock_autorelock_time-state] StateSnapshot({ 'attributes': ReadOnlyDict({ - 'friendly_name': 'Mock Door Lock Automatic relock timer', + 'friendly_name': 'Mock Door Lock Autorelock time', 'max': 65534, 'min': 0, 'mode': , @@ -511,7 +511,7 @@ 'unit_of_measurement': , }), 'context': , - 'entity_id': 'number.mock_door_lock_automatic_relock_timer', + 'entity_id': 'number.mock_door_lock_autorelock_time', 'last_changed': , 'last_reported': , 'last_updated': , From ef255788d2a593d1ca95de851048b2a9433a4ccf Mon Sep 17 00:00:00 2001 From: Norbert Rittel Date: Sat, 5 Jul 2025 11:01:27 +0200 Subject: [PATCH 066/106] Make lat/long attribute names localizable in `dwd_weather_warnings` (#147988) --- homeassistant/components/dwd_weather_warnings/strings.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/dwd_weather_warnings/strings.json b/homeassistant/components/dwd_weather_warnings/strings.json index 3f421d338a7..4e0ee2d2016 100644 --- a/homeassistant/components/dwd_weather_warnings/strings.json +++ b/homeassistant/components/dwd_weather_warnings/strings.json @@ -2,7 +2,7 @@ "config": { "step": { "user": { - "description": "To identify the desired region, either the warncell ID / name or device tracker is required. The provided device tracker has to contain the attributes 'latitude' and 'longitude'.", + "description": "To identify the desired region, either the warncell ID / name or device tracker is required. The provided device tracker has to contain the attributes 'Latitude' and 'Longitude'.", "data": { "region_identifier": "Warncell ID or name", "region_device_tracker": "Device tracker entity" @@ -14,7 +14,7 @@ "ambiguous_identifier": "The region identifier and device tracker can not be specified together.", "invalid_identifier": "The specified region identifier / device tracker is invalid.", "entity_not_found": "The specified device tracker entity was not found.", - "attribute_not_found": "The required `latitude` or `longitude` attribute was not found in the specified device tracker." + "attribute_not_found": "The required attributes 'Latitude' and 'Longitude' were not found in the specified device tracker." }, "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", From 23773759ea3b9b59662cb114943dd60e12893802 Mon Sep 17 00:00:00 2001 From: David Rapan Date: Sat, 5 Jul 2025 11:18:54 +0200 Subject: [PATCH 067/106] Starlink's last boot time occasional, back and forth changes by 1 s fix (#147969) --- homeassistant/components/starlink/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/starlink/sensor.py b/homeassistant/components/starlink/sensor.py index 14cbf6fe876..b353051a074 100644 --- a/homeassistant/components/starlink/sensor.py +++ b/homeassistant/components/starlink/sensor.py @@ -114,7 +114,7 @@ SENSORS: tuple[StarlinkSensorEntityDescription, ...] = ( device_class=SensorDeviceClass.TIMESTAMP, entity_category=EntityCategory.DIAGNOSTIC, value_fn=lambda data: ( - now() - timedelta(seconds=data.status["uptime"]) + now() - timedelta(seconds=data.status["uptime"], milliseconds=-500) ).replace(microsecond=0), ), StarlinkSensorEntityDescription( From 3151713a346e169614b4bb3523db72542719c1f8 Mon Sep 17 00:00:00 2001 From: Denis Shulyaka Date: Sat, 5 Jul 2025 12:27:27 +0300 Subject: [PATCH 068/106] Replace dot with underscores for NamespacedTool and ActionTool (#147764) --- homeassistant/helpers/llm.py | 4 ++-- tests/helpers/test_llm.py | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/homeassistant/helpers/llm.py b/homeassistant/helpers/llm.py index bf89e693870..a8a598c79f8 100644 --- a/homeassistant/helpers/llm.py +++ b/homeassistant/helpers/llm.py @@ -331,7 +331,7 @@ class NamespacedTool(Tool): def __init__(self, namespace: str, tool: Tool) -> None: """Init the class.""" self.namespace = namespace - self.name = f"{namespace}.{tool.name}" + self.name = f"{namespace}__{tool.name}" self.description = tool.description self.parameters = tool.parameters self.tool = tool @@ -915,7 +915,7 @@ class ActionTool(Tool): """Init the class.""" self._domain = domain self._action = action - self.name = f"{domain}.{action}" + self.name = f"{domain}__{action}" # Note: _get_cached_action_parameters only works for services which # add their description directly to the service description cache. # This is not the case for most services, but it is for scripts. diff --git a/tests/helpers/test_llm.py b/tests/helpers/test_llm.py index b978559130c..78ff675f0b6 100644 --- a/tests/helpers/test_llm.py +++ b/tests/helpers/test_llm.py @@ -1542,18 +1542,18 @@ This is prompt 2 """ ) assert [(tool.name, tool.description) for tool in instance.tools] == [ - ("api-1.Tool_1", "Description 1"), - ("api-2.Tool_2", "Description 2"), + ("api-1__Tool_1", "Description 1"), + ("api-2__Tool_2", "Description 2"), ] # The test tool returns back the provided arguments so we can verify # the original tool is invoked with the correct tool name and args. result = await instance.async_call_tool( - llm.ToolInput(tool_name="api-1.Tool_1", tool_args={"arg1": "value1"}) + llm.ToolInput(tool_name="api-1__Tool_1", tool_args={"arg1": "value1"}) ) assert result == {"result": {"Tool_1": {"arg1": "value1"}}} result = await instance.async_call_tool( - llm.ToolInput(tool_name="api-2.Tool_2", tool_args={"arg2": "value2"}) + llm.ToolInput(tool_name="api-2__Tool_2", tool_args={"arg2": "value2"}) ) assert result == {"result": {"Tool_2": {"arg2": "value2"}}} From 676567f471f163449e90ab878be02eb2f387537f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Beye?= Date: Sat, 5 Jul 2025 11:31:30 +0200 Subject: [PATCH 069/106] Squeezebox: Fix tracks not having thumbnails (#147187) --- homeassistant/components/squeezebox/browse_media.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/squeezebox/browse_media.py b/homeassistant/components/squeezebox/browse_media.py index 03df289a2fd..03dcd116a6d 100644 --- a/homeassistant/components/squeezebox/browse_media.py +++ b/homeassistant/components/squeezebox/browse_media.py @@ -221,12 +221,16 @@ def _get_item_thumbnail( ) -> str | None: """Construct path to thumbnail image.""" item_thumbnail: str | None = None - if artwork_track_id := item.get("artwork_track_id"): + track_id = item.get("artwork_track_id") or ( + item.get("id") if item_type == "track" else None + ) + + if track_id: if internal_request: - item_thumbnail = player.generate_image_url_from_track_id(artwork_track_id) + item_thumbnail = player.generate_image_url_from_track_id(track_id) elif item_type is not None: item_thumbnail = entity.get_browse_image_url( - item_type, item["id"], artwork_track_id + item_type, item["id"], track_id ) elif search_type in ["apps", "radios"]: From 2ea09ff37a77069c98d411d538a4deba985938b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Beye?= Date: Sat, 5 Jul 2025 11:36:45 +0200 Subject: [PATCH 070/106] Squeezebox: Fix track selection in media browser (#147185) --- homeassistant/components/squeezebox/browse_media.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homeassistant/components/squeezebox/browse_media.py b/homeassistant/components/squeezebox/browse_media.py index 03dcd116a6d..bab4f90c6d1 100644 --- a/homeassistant/components/squeezebox/browse_media.py +++ b/homeassistant/components/squeezebox/browse_media.py @@ -315,8 +315,7 @@ async def build_item_response( title=item["title"], media_content_type=item_type, media_class=CONTENT_TYPE_MEDIA_CLASS[item_type]["item"], - can_expand=CONTENT_TYPE_MEDIA_CLASS[item_type]["children"] - is not None, + can_expand=bool(CONTENT_TYPE_MEDIA_CLASS[item_type]["children"]), can_play=True, ) From 8d82e34ba55462808594107ef87946b7c8a3264b Mon Sep 17 00:00:00 2001 From: Guido Schmitz Date: Sat, 5 Jul 2025 11:42:15 +0200 Subject: [PATCH 071/106] Make connected stations coordinator a dict in devolo Home Network (#147042) --- .../devolo_home_network/coordinator.py | 7 ++-- .../devolo_home_network/device_tracker.py | 35 +++++++------------ .../components/devolo_home_network/entity.py | 2 +- .../components/devolo_home_network/sensor.py | 8 +++-- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/homeassistant/components/devolo_home_network/coordinator.py b/homeassistant/components/devolo_home_network/coordinator.py index d23aa0e935e..5af9afb12ae 100644 --- a/homeassistant/components/devolo_home_network/coordinator.py +++ b/homeassistant/components/devolo_home_network/coordinator.py @@ -207,7 +207,7 @@ class DevoloUptimeGetCoordinator(DevoloDataUpdateCoordinator[int]): class DevoloWifiConnectedStationsGetCoordinator( - DevoloDataUpdateCoordinator[list[ConnectedStationInfo]] + DevoloDataUpdateCoordinator[dict[str, ConnectedStationInfo]] ): """Class to manage fetching data from the WifiGuestAccessGet endpoint.""" @@ -230,10 +230,11 @@ class DevoloWifiConnectedStationsGetCoordinator( ) self.update_method = self.async_get_wifi_connected_station - async def async_get_wifi_connected_station(self) -> list[ConnectedStationInfo]: + async def async_get_wifi_connected_station(self) -> dict[str, ConnectedStationInfo]: """Fetch data from API endpoint.""" assert self.device.device - return await self.device.device.async_get_wifi_connected_station() + clients = await self.device.device.async_get_wifi_connected_station() + return {client.mac_address: client for client in clients} class DevoloWifiGuestAccessGetCoordinator( diff --git a/homeassistant/components/devolo_home_network/device_tracker.py b/homeassistant/components/devolo_home_network/device_tracker.py index ad3d3e1cffa..a0cdd381261 100644 --- a/homeassistant/components/devolo_home_network/device_tracker.py +++ b/homeassistant/components/devolo_home_network/device_tracker.py @@ -28,9 +28,9 @@ async def async_setup_entry( ) -> None: """Get all devices and sensors and setup them via config entry.""" device = entry.runtime_data.device - coordinators: dict[str, DevoloDataUpdateCoordinator[list[ConnectedStationInfo]]] = ( - entry.runtime_data.coordinators - ) + coordinators: dict[ + str, DevoloDataUpdateCoordinator[dict[str, ConnectedStationInfo]] + ] = entry.runtime_data.coordinators registry = er.async_get(hass) tracked = set() @@ -38,16 +38,16 @@ async def async_setup_entry( def new_device_callback() -> None: """Add new devices if needed.""" new_entities = [] - for station in coordinators[CONNECTED_WIFI_CLIENTS].data: - if station.mac_address in tracked: + for mac_address in coordinators[CONNECTED_WIFI_CLIENTS].data: + if mac_address in tracked: continue new_entities.append( DevoloScannerEntity( - coordinators[CONNECTED_WIFI_CLIENTS], device, station.mac_address + coordinators[CONNECTED_WIFI_CLIENTS], device, mac_address ) ) - tracked.add(station.mac_address) + tracked.add(mac_address) async_add_entities(new_entities) @callback @@ -82,7 +82,7 @@ async def async_setup_entry( # The pylint disable is needed because of https://github.com/pylint-dev/pylint/issues/9138 class DevoloScannerEntity( # pylint: disable=hass-enforce-class-module - CoordinatorEntity[DevoloDataUpdateCoordinator[list[ConnectedStationInfo]]], + CoordinatorEntity[DevoloDataUpdateCoordinator[dict[str, ConnectedStationInfo]]], ScannerEntity, ): """Representation of a devolo device tracker.""" @@ -92,7 +92,7 @@ class DevoloScannerEntity( # pylint: disable=hass-enforce-class-module def __init__( self, - coordinator: DevoloDataUpdateCoordinator[list[ConnectedStationInfo]], + coordinator: DevoloDataUpdateCoordinator[dict[str, ConnectedStationInfo]], device: Device, mac: str, ) -> None: @@ -109,14 +109,8 @@ class DevoloScannerEntity( # pylint: disable=hass-enforce-class-module if not self.coordinator.data: return {} - station = next( - ( - station - for station in self.coordinator.data - if station.mac_address == self.mac_address - ), - None, - ) + assert self.mac_address + station = self.coordinator.data.get(self.mac_address) if station: attrs["wifi"] = WIFI_APTYPE.get(station.vap_type, STATE_UNKNOWN) attrs["band"] = ( @@ -129,11 +123,8 @@ class DevoloScannerEntity( # pylint: disable=hass-enforce-class-module @property def is_connected(self) -> bool: """Return true if the device is connected to the network.""" - return any( - station - for station in self.coordinator.data - if station.mac_address == self.mac_address - ) + assert self.mac_address + return self.coordinator.data.get(self.mac_address) is not None @property def unique_id(self) -> str: diff --git a/homeassistant/components/devolo_home_network/entity.py b/homeassistant/components/devolo_home_network/entity.py index be437314ae4..79b9b846463 100644 --- a/homeassistant/components/devolo_home_network/entity.py +++ b/homeassistant/components/devolo_home_network/entity.py @@ -21,7 +21,7 @@ from .coordinator import DevoloDataUpdateCoordinator, DevoloHomeNetworkConfigEnt type _DataType = ( LogicalNetwork | DataRate - | list[ConnectedStationInfo] + | dict[str, ConnectedStationInfo] | list[NeighborAPInfo] | WifiGuestAccessGet | bool diff --git a/homeassistant/components/devolo_home_network/sensor.py b/homeassistant/components/devolo_home_network/sensor.py index f4c911bf787..941eec4215d 100644 --- a/homeassistant/components/devolo_home_network/sensor.py +++ b/homeassistant/components/devolo_home_network/sensor.py @@ -47,7 +47,11 @@ def _last_restart(runtime: int) -> datetime: type _CoordinatorDataType = ( - LogicalNetwork | DataRate | list[ConnectedStationInfo] | list[NeighborAPInfo] | int + LogicalNetwork + | DataRate + | dict[str, ConnectedStationInfo] + | list[NeighborAPInfo] + | int ) type _SensorDataType = int | float | datetime @@ -79,7 +83,7 @@ SENSOR_TYPES: dict[str, DevoloSensorEntityDescription[Any, Any]] = { ), ), CONNECTED_WIFI_CLIENTS: DevoloSensorEntityDescription[ - list[ConnectedStationInfo], int + dict[str, ConnectedStationInfo], int ]( key=CONNECTED_WIFI_CLIENTS, state_class=SensorStateClass.MEASUREMENT, From 33d05d99ebfa81503bc114f9b43602e9343c8ef1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luka=20Matijevi=C4=87?= Date: Sat, 5 Jul 2025 16:44:41 +0200 Subject: [PATCH 072/106] Fix Miele hob plate power step typo (#148214) --- homeassistant/components/miele/const.py | 2 +- tests/components/miele/snapshots/test_sensor.ambr | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/miele/const.py b/homeassistant/components/miele/const.py index fd2f8631cd2..a40df909e14 100644 --- a/homeassistant/components/miele/const.py +++ b/homeassistant/components/miele/const.py @@ -1314,7 +1314,7 @@ class PlatePowerStep(MieleEnum): plate_step_11 = 11 plate_step_12 = 12 plate_step_13 = 13 - plate_step_14 = 4 + plate_step_14 = 14 plate_step_15 = 15 plate_step_16 = 16 plate_step_17 = 17 diff --git a/tests/components/miele/snapshots/test_sensor.ambr b/tests/components/miele/snapshots/test_sensor.ambr index b1691c28b19..dfc12a52c08 100644 --- a/tests/components/miele/snapshots/test_sensor.ambr +++ b/tests/components/miele/snapshots/test_sensor.ambr @@ -103,6 +103,7 @@ 'plate_step_11', 'plate_step_12', 'plate_step_13', + 'plate_step_14', 'plate_step_15', 'plate_step_16', 'plate_step_17', @@ -160,6 +161,7 @@ 'plate_step_11', 'plate_step_12', 'plate_step_13', + 'plate_step_14', 'plate_step_15', 'plate_step_16', 'plate_step_17', @@ -197,6 +199,7 @@ 'plate_step_11', 'plate_step_12', 'plate_step_13', + 'plate_step_14', 'plate_step_15', 'plate_step_16', 'plate_step_17', @@ -254,6 +257,7 @@ 'plate_step_11', 'plate_step_12', 'plate_step_13', + 'plate_step_14', 'plate_step_15', 'plate_step_16', 'plate_step_17', @@ -291,6 +295,7 @@ 'plate_step_11', 'plate_step_12', 'plate_step_13', + 'plate_step_14', 'plate_step_15', 'plate_step_16', 'plate_step_17', @@ -348,6 +353,7 @@ 'plate_step_11', 'plate_step_12', 'plate_step_13', + 'plate_step_14', 'plate_step_15', 'plate_step_16', 'plate_step_17', @@ -385,6 +391,7 @@ 'plate_step_11', 'plate_step_12', 'plate_step_13', + 'plate_step_14', 'plate_step_15', 'plate_step_16', 'plate_step_17', @@ -442,6 +449,7 @@ 'plate_step_11', 'plate_step_12', 'plate_step_13', + 'plate_step_14', 'plate_step_15', 'plate_step_16', 'plate_step_17', @@ -479,6 +487,7 @@ 'plate_step_11', 'plate_step_12', 'plate_step_13', + 'plate_step_14', 'plate_step_15', 'plate_step_16', 'plate_step_17', @@ -536,6 +545,7 @@ 'plate_step_11', 'plate_step_12', 'plate_step_13', + 'plate_step_14', 'plate_step_15', 'plate_step_16', 'plate_step_17', From 4f4ec6f41a11e1868b8ff44e0f3a08eddc567ef1 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 5 Jul 2025 17:22:17 +0200 Subject: [PATCH 073/106] Add Google Gen AI structured data support (#148143) --- .../ai_task.py | 25 ++++++- .../entity.py | 14 ++++ homeassistant/helpers/llm.py | 6 +- .../conftest.py | 25 ++++--- .../test_ai_task.py | 74 ++++++++++++++++++- 5 files changed, 127 insertions(+), 17 deletions(-) diff --git a/homeassistant/components/google_generative_ai_conversation/ai_task.py b/homeassistant/components/google_generative_ai_conversation/ai_task.py index ab34af71ebe..b4f9d73e38d 100644 --- a/homeassistant/components/google_generative_ai_conversation/ai_task.py +++ b/homeassistant/components/google_generative_ai_conversation/ai_task.py @@ -2,11 +2,14 @@ from __future__ import annotations +from json import JSONDecodeError + from homeassistant.components import ai_task, conversation from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from homeassistant.util.json import json_loads from .const import LOGGER from .entity import ERROR_GETTING_RESPONSE, GoogleGenerativeAILLMBaseEntity @@ -42,7 +45,7 @@ class GoogleGenerativeAITaskEntity( chat_log: conversation.ChatLog, ) -> ai_task.GenDataTaskResult: """Handle a generate data task.""" - await self._async_handle_chat_log(chat_log) + await self._async_handle_chat_log(chat_log, task.structure) if not isinstance(chat_log.content[-1], conversation.AssistantContent): LOGGER.error( @@ -51,7 +54,25 @@ class GoogleGenerativeAITaskEntity( ) raise HomeAssistantError(ERROR_GETTING_RESPONSE) + text = chat_log.content[-1].content or "" + + if not task.structure: + return ai_task.GenDataTaskResult( + conversation_id=chat_log.conversation_id, + data=text, + ) + + try: + data = json_loads(text) + except JSONDecodeError as err: + LOGGER.error( + "Failed to parse JSON response: %s. Response: %s", + err, + text, + ) + raise HomeAssistantError(ERROR_GETTING_RESPONSE) from err + return ai_task.GenDataTaskResult( conversation_id=chat_log.conversation_id, - data=chat_log.content[-1].content or "", + data=data, ) diff --git a/homeassistant/components/google_generative_ai_conversation/entity.py b/homeassistant/components/google_generative_ai_conversation/entity.py index dea875212ef..d471da36a8c 100644 --- a/homeassistant/components/google_generative_ai_conversation/entity.py +++ b/homeassistant/components/google_generative_ai_conversation/entity.py @@ -21,6 +21,7 @@ from google.genai.types import ( Schema, Tool, ) +import voluptuous as vol from voluptuous_openapi import convert from homeassistant.components import conversation @@ -324,6 +325,7 @@ class GoogleGenerativeAILLMBaseEntity(Entity): async def _async_handle_chat_log( self, chat_log: conversation.ChatLog, + structure: vol.Schema | None = None, ) -> None: """Generate an answer for the chat log.""" options = self.subentry.data @@ -402,6 +404,18 @@ class GoogleGenerativeAILLMBaseEntity(Entity): generateContentConfig.automatic_function_calling = ( AutomaticFunctionCallingConfig(disable=True, maximum_remote_calls=None) ) + if structure: + generateContentConfig.response_mime_type = "application/json" + generateContentConfig.response_schema = _format_schema( + convert( + structure, + custom_serializer=( + chat_log.llm_api.custom_serializer + if chat_log.llm_api + else llm.selector_serializer + ), + ) + ) if not supports_system_instruction: messages = [ diff --git a/homeassistant/helpers/llm.py b/homeassistant/helpers/llm.py index a8a598c79f8..b239ad99119 100644 --- a/homeassistant/helpers/llm.py +++ b/homeassistant/helpers/llm.py @@ -458,7 +458,7 @@ class AssistAPI(API): api_prompt=self._async_get_api_prompt(llm_context, exposed_entities), llm_context=llm_context, tools=self._async_get_tools(llm_context, exposed_entities), - custom_serializer=_selector_serializer, + custom_serializer=selector_serializer, ) @callback @@ -701,7 +701,7 @@ def _get_exposed_entities( return data -def _selector_serializer(schema: Any) -> Any: # noqa: C901 +def selector_serializer(schema: Any) -> Any: # noqa: C901 """Convert selectors into OpenAPI schema.""" if not isinstance(schema, selector.Selector): return UNSUPPORTED @@ -782,7 +782,7 @@ def _selector_serializer(schema: Any) -> Any: # noqa: C901 result["properties"] = { field: convert( selector.selector(field_schema["selector"]), - custom_serializer=_selector_serializer, + custom_serializer=selector_serializer, ) for field, field_schema in fields.items() } diff --git a/tests/components/google_generative_ai_conversation/conftest.py b/tests/components/google_generative_ai_conversation/conftest.py index 244ac518fbd..da5976f46c4 100644 --- a/tests/components/google_generative_ai_conversation/conftest.py +++ b/tests/components/google_generative_ai_conversation/conftest.py @@ -112,19 +112,26 @@ async def setup_ha(hass: HomeAssistant) -> None: @pytest.fixture -def mock_send_message_stream() -> Generator[AsyncMock]: +def mock_chat_create() -> Generator[AsyncMock]: """Mock stream response.""" async def mock_generator(stream): for value in stream: yield value - with patch( - "google.genai.chats.AsyncChat.send_message_stream", - AsyncMock(), - ) as mock_send_message_stream: - mock_send_message_stream.side_effect = lambda **kwargs: mock_generator( - mock_send_message_stream.return_value.pop(0) - ) + mock_send_message_stream = AsyncMock() + mock_send_message_stream.side_effect = lambda **kwargs: mock_generator( + mock_send_message_stream.return_value.pop(0) + ) - yield mock_send_message_stream + with patch( + "google.genai.chats.AsyncChats.create", + return_value=AsyncMock(send_message_stream=mock_send_message_stream), + ) as mock_create: + yield mock_create + + +@pytest.fixture +def mock_send_message_stream(mock_chat_create) -> Generator[AsyncMock]: + """Mock stream response.""" + return mock_chat_create.return_value.send_message_stream diff --git a/tests/components/google_generative_ai_conversation/test_ai_task.py b/tests/components/google_generative_ai_conversation/test_ai_task.py index 72b62b64615..b2b44aa1cd6 100644 --- a/tests/components/google_generative_ai_conversation/test_ai_task.py +++ b/tests/components/google_generative_ai_conversation/test_ai_task.py @@ -4,10 +4,12 @@ from unittest.mock import AsyncMock from google.genai.types import GenerateContentResponse import pytest +import voluptuous as vol from homeassistant.components import ai_task from homeassistant.core import HomeAssistant -from homeassistant.helpers import entity_registry as er +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import entity_registry as er, selector from tests.common import MockConfigEntry from tests.components.conversation import ( @@ -17,14 +19,15 @@ from tests.components.conversation import ( @pytest.mark.usefixtures("mock_init_component") -async def test_run_task( +async def test_generate_data( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_chat_log: MockChatLog, # noqa: F811 mock_send_message_stream: AsyncMock, + mock_chat_create: AsyncMock, entity_registry: er.EntityRegistry, ) -> None: - """Test empty response.""" + """Test generating data.""" entity_id = "ai_task.google_ai_task" # Ensure it's linked to the subentry @@ -60,3 +63,68 @@ async def test_run_task( instructions="Test prompt", ) assert result.data == "Hi there!" + + mock_send_message_stream.return_value = [ + [ + GenerateContentResponse( + candidates=[ + { + "content": { + "parts": [{"text": '{"characters": ["Mario", "Luigi"]}'}], + "role": "model", + }, + } + ], + ), + ], + ] + result = await ai_task.async_generate_data( + hass, + task_name="Test Task", + entity_id=entity_id, + instructions="Give me 2 mario characters", + structure=vol.Schema( + { + vol.Required("characters"): selector.selector( + { + "text": { + "multiple": True, + } + } + ) + }, + ), + ) + assert result.data == {"characters": ["Mario", "Luigi"]} + + assert len(mock_chat_create.mock_calls) == 2 + config = mock_chat_create.mock_calls[-1][2]["config"] + assert config.response_mime_type == "application/json" + assert config.response_schema == { + "properties": {"characters": {"items": {"type": "STRING"}, "type": "ARRAY"}}, + "required": ["characters"], + "type": "OBJECT", + } + # Raise error on invalid JSON response + mock_send_message_stream.return_value = [ + [ + GenerateContentResponse( + candidates=[ + { + "content": { + "parts": [{"text": "INVALID JSON RESPONSE"}], + "role": "model", + }, + } + ], + ), + ], + ] + with pytest.raises(HomeAssistantError): + result = await ai_task.async_generate_data( + hass, + task_name="Test Task", + entity_id=entity_id, + instructions="Test prompt", + structure=vol.Schema({vol.Required("bla"): str}), + ) From 736865c130ee33c68464667524c74d68f976b8dc Mon Sep 17 00:00:00 2001 From: Jack Powell Date: Sat, 5 Jul 2025 11:27:23 -0400 Subject: [PATCH 074/106] Add binary sensor platform to PlayStation Network Integration (#147639) --- .../playstation_network/__init__.py | 6 +- .../playstation_network/binary_sensor.py | 71 +++++++++++++++++++ .../components/playstation_network/entity.py | 36 ++++++++++ .../components/playstation_network/icons.json | 5 ++ .../components/playstation_network/sensor.py | 35 ++------- .../playstation_network/strings.json | 5 ++ .../snapshots/test_binary_sensor.ambr | 49 +++++++++++++ .../playstation_network/test_binary_sensor.py | 42 +++++++++++ 8 files changed, 217 insertions(+), 32 deletions(-) create mode 100644 homeassistant/components/playstation_network/binary_sensor.py create mode 100644 homeassistant/components/playstation_network/entity.py create mode 100644 tests/components/playstation_network/snapshots/test_binary_sensor.ambr create mode 100644 tests/components/playstation_network/test_binary_sensor.py diff --git a/homeassistant/components/playstation_network/__init__.py b/homeassistant/components/playstation_network/__init__.py index 72ce0b9cfc2..feb598a646a 100644 --- a/homeassistant/components/playstation_network/__init__.py +++ b/homeassistant/components/playstation_network/__init__.py @@ -9,7 +9,11 @@ from .const import CONF_NPSSO from .coordinator import PlaystationNetworkConfigEntry, PlaystationNetworkCoordinator from .helpers import PlaystationNetwork -PLATFORMS: list[Platform] = [Platform.MEDIA_PLAYER, Platform.SENSOR] +PLATFORMS: list[Platform] = [ + Platform.BINARY_SENSOR, + Platform.MEDIA_PLAYER, + Platform.SENSOR, +] async def async_setup_entry( diff --git a/homeassistant/components/playstation_network/binary_sensor.py b/homeassistant/components/playstation_network/binary_sensor.py new file mode 100644 index 00000000000..fcecd1d1ee1 --- /dev/null +++ b/homeassistant/components/playstation_network/binary_sensor.py @@ -0,0 +1,71 @@ +"""Binary Sensor platform for PlayStation Network integration.""" + +from __future__ import annotations + +from collections.abc import Callable +from dataclasses import dataclass +from enum import StrEnum + +from homeassistant.components.binary_sensor import ( + BinarySensorEntity, + BinarySensorEntityDescription, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from .coordinator import PlaystationNetworkConfigEntry, PlaystationNetworkData +from .entity import PlaystationNetworkServiceEntity + +PARALLEL_UPDATES = 0 + + +@dataclass(kw_only=True, frozen=True) +class PlaystationNetworkBinarySensorEntityDescription(BinarySensorEntityDescription): + """PlayStation Network binary sensor description.""" + + is_on_fn: Callable[[PlaystationNetworkData], bool] + + +class PlaystationNetworkBinarySensor(StrEnum): + """PlayStation Network binary sensors.""" + + PS_PLUS_STATUS = "ps_plus_status" + + +BINARY_SENSOR_DESCRIPTIONS: tuple[ + PlaystationNetworkBinarySensorEntityDescription, ... +] = ( + PlaystationNetworkBinarySensorEntityDescription( + key=PlaystationNetworkBinarySensor.PS_PLUS_STATUS, + translation_key=PlaystationNetworkBinarySensor.PS_PLUS_STATUS, + is_on_fn=lambda psn: psn.profile["isPlus"], + ), +) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: PlaystationNetworkConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the binary sensor platform.""" + coordinator = config_entry.runtime_data + async_add_entities( + PlaystationNetworkBinarySensorEntity(coordinator, description) + for description in BINARY_SENSOR_DESCRIPTIONS + ) + + +class PlaystationNetworkBinarySensorEntity( + PlaystationNetworkServiceEntity, + BinarySensorEntity, +): + """Representation of a PlayStation Network binary sensor entity.""" + + entity_description: PlaystationNetworkBinarySensorEntityDescription + + @property + def is_on(self) -> bool: + """Return the state of the binary sensor.""" + + return self.entity_description.is_on_fn(self.coordinator.data) diff --git a/homeassistant/components/playstation_network/entity.py b/homeassistant/components/playstation_network/entity.py new file mode 100644 index 00000000000..54f5fd5db70 --- /dev/null +++ b/homeassistant/components/playstation_network/entity.py @@ -0,0 +1,36 @@ +"""Base entity for PlayStation Network Integration.""" + +from typing import TYPE_CHECKING + +from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo +from homeassistant.helpers.entity import EntityDescription +from homeassistant.helpers.update_coordinator import CoordinatorEntity + +from .const import DOMAIN +from .coordinator import PlaystationNetworkCoordinator + + +class PlaystationNetworkServiceEntity(CoordinatorEntity[PlaystationNetworkCoordinator]): + """Common entity class for PlayStationNetwork Service entities.""" + + _attr_has_entity_name = True + + def __init__( + self, + coordinator: PlaystationNetworkCoordinator, + entity_description: EntityDescription, + ) -> None: + """Initialize PlayStation Network Service Entity.""" + super().__init__(coordinator) + if TYPE_CHECKING: + assert coordinator.config_entry.unique_id + self.entity_description = entity_description + self._attr_unique_id = ( + f"{coordinator.config_entry.unique_id}_{entity_description.key}" + ) + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, coordinator.config_entry.unique_id)}, + name=coordinator.data.username, + entry_type=DeviceEntryType.SERVICE, + manufacturer="Sony Interactive Entertainment", + ) diff --git a/homeassistant/components/playstation_network/icons.json b/homeassistant/components/playstation_network/icons.json index 612427c9a1d..2742ab1c989 100644 --- a/homeassistant/components/playstation_network/icons.json +++ b/homeassistant/components/playstation_network/icons.json @@ -5,6 +5,11 @@ "default": "mdi:sony-playstation" } }, + "binary_sensor": { + "ps_plus_status": { + "default": "mdi:shape-plus-outline" + } + }, "sensor": { "trophy_level": { "default": "mdi:trophy-award" diff --git a/homeassistant/components/playstation_network/sensor.py b/homeassistant/components/playstation_network/sensor.py index 305f252f31d..f4a634d5fb5 100644 --- a/homeassistant/components/playstation_network/sensor.py +++ b/homeassistant/components/playstation_network/sensor.py @@ -6,7 +6,6 @@ from collections.abc import Callable from dataclasses import dataclass from datetime import datetime from enum import StrEnum -from typing import TYPE_CHECKING from homeassistant.components.sensor import ( SensorDeviceClass, @@ -15,18 +14,12 @@ from homeassistant.components.sensor import ( ) from homeassistant.const import PERCENTAGE from homeassistant.core import HomeAssistant -from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.typing import StateType -from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.util import dt as dt_util -from .const import DOMAIN -from .coordinator import ( - PlaystationNetworkConfigEntry, - PlaystationNetworkCoordinator, - PlaystationNetworkData, -) +from .coordinator import PlaystationNetworkConfigEntry, PlaystationNetworkData +from .entity import PlaystationNetworkServiceEntity PARALLEL_UPDATES = 0 @@ -146,32 +139,12 @@ async def async_setup_entry( class PlaystationNetworkSensorEntity( - CoordinatorEntity[PlaystationNetworkCoordinator], SensorEntity + PlaystationNetworkServiceEntity, + SensorEntity, ): """Representation of a PlayStation Network sensor entity.""" entity_description: PlaystationNetworkSensorEntityDescription - coordinator: PlaystationNetworkCoordinator - - _attr_has_entity_name = True - - def __init__( - self, - coordinator: PlaystationNetworkCoordinator, - description: PlaystationNetworkSensorEntityDescription, - ) -> None: - """Initialize a sensor entity.""" - super().__init__(coordinator) - self.entity_description = description - if TYPE_CHECKING: - assert coordinator.config_entry.unique_id - self._attr_unique_id = f"{coordinator.config_entry.unique_id}_{description.key}" - self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, coordinator.config_entry.unique_id)}, - name=coordinator.data.username, - entry_type=DeviceEntryType.SERVICE, - manufacturer="Sony Interactive Entertainment", - ) @property def native_value(self) -> StateType | datetime: diff --git a/homeassistant/components/playstation_network/strings.json b/homeassistant/components/playstation_network/strings.json index f68d69417fb..360687f97c8 100644 --- a/homeassistant/components/playstation_network/strings.json +++ b/homeassistant/components/playstation_network/strings.json @@ -53,6 +53,11 @@ } }, "entity": { + "binary_sensor": { + "ps_plus_status": { + "name": "Subscribed to PlayStation Plus" + } + }, "sensor": { "trophy_level": { "name": "Trophy level" diff --git a/tests/components/playstation_network/snapshots/test_binary_sensor.ambr b/tests/components/playstation_network/snapshots/test_binary_sensor.ambr new file mode 100644 index 00000000000..f380f91e9b9 --- /dev/null +++ b/tests/components/playstation_network/snapshots/test_binary_sensor.ambr @@ -0,0 +1,49 @@ +# serializer version: 1 +# name: test_sensors[binary_sensor.testuser_subscribed_to_playstation_plus-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'binary_sensor', + 'entity_category': None, + 'entity_id': 'binary_sensor.testuser_subscribed_to_playstation_plus', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Subscribed to PlayStation Plus', + 'platform': 'playstation_network', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': , + 'unique_id': 'my-psn-id_ps_plus_status', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[binary_sensor.testuser_subscribed_to_playstation_plus-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'testuser Subscribed to PlayStation Plus', + }), + 'context': , + 'entity_id': 'binary_sensor.testuser_subscribed_to_playstation_plus', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- diff --git a/tests/components/playstation_network/test_binary_sensor.py b/tests/components/playstation_network/test_binary_sensor.py new file mode 100644 index 00000000000..de7ef630b76 --- /dev/null +++ b/tests/components/playstation_network/test_binary_sensor.py @@ -0,0 +1,42 @@ +"""Test the Playstation Network binary sensor platform.""" + +from collections.abc import Generator +from unittest.mock import patch + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er + +from tests.common import MockConfigEntry, snapshot_platform + + +@pytest.fixture(autouse=True) +def binary_sensor_only() -> Generator[None]: + """Enable only the binary sensor platform.""" + with patch( + "homeassistant.components.playstation_network.PLATFORMS", + [Platform.BINARY_SENSOR], + ): + yield + + +@pytest.mark.usefixtures("mock_psnawpapi") +async def test_sensors( + hass: HomeAssistant, + config_entry: MockConfigEntry, + snapshot: SnapshotAssertion, + entity_registry: er.EntityRegistry, +) -> None: + """Test setup of the PlayStation Network binary sensor platform.""" + + config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(config_entry.entry_id) + await hass.async_block_till_done() + + assert config_entry.state is ConfigEntryState.LOADED + + await snapshot_platform(hass, entity_registry, snapshot, config_entry.entry_id) From d997efc500efba308c780e4c8f7c0d316a15555d Mon Sep 17 00:00:00 2001 From: Pete Sage <76050312+PeteRager@users.noreply.github.com> Date: Sat, 5 Jul 2025 11:39:52 -0400 Subject: [PATCH 075/106] Add tests for Sonos Alarms (#146308) --- tests/components/sonos/conftest.py | 28 +++++++++++++- tests/components/sonos/test_init.py | 5 +++ tests/components/sonos/test_switch.py | 54 ++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 3 deletions(-) diff --git a/tests/components/sonos/conftest.py b/tests/components/sonos/conftest.py index d3de2a889d5..a2a4e53cae4 100644 --- a/tests/components/sonos/conftest.py +++ b/tests/components/sonos/conftest.py @@ -214,12 +214,25 @@ class MockSoCo(MagicMock): surround_level = 3 music_surround_level = 4 soundbar_audio_input_format = "Dolby 5.1" + factory: SoCoMockFactory | None = None @property def visible_zones(self): """Return visible zones and allow property to be overridden by device classes.""" return {self} + @property + def all_zones(self) -> set[MockSoCo]: + """Return a set of all mock zones, or just self if no factory or zones.""" + if self.factory is not None: + if zones := self.factory.mock_all_zones: + return zones + return {self} + + def set_factory(self, factory: SoCoMockFactory) -> None: + """Set the factory for this mock.""" + self.factory = factory + class SoCoMockFactory: """Factory for creating SoCo Mocks.""" @@ -244,11 +257,19 @@ class SoCoMockFactory: self.sonos_playlists = sonos_playlists self.sonos_queue = sonos_queue + @property + def mock_all_zones(self) -> set[MockSoCo]: + """Return a set of all mock zones.""" + return { + mock for mock in self.mock_list.values() if mock.mock_include_in_all_zones + } + def cache_mock( self, mock_soco: MockSoCo, ip_address: str, name: str = "Zone A" ) -> MockSoCo: """Put a user created mock into the cache.""" mock_soco.mock_add_spec(SoCo) + mock_soco.set_factory(self) mock_soco.ip_address = ip_address if ip_address != "192.168.42.2": mock_soco.uid += f"_{ip_address}" @@ -260,6 +281,11 @@ class SoCoMockFactory: my_speaker_info = self.speaker_info.copy() my_speaker_info["zone_name"] = name my_speaker_info["uid"] = mock_soco.uid + # Generate a different MAC for the non-default speakers. + # otherwise new devices will not be created. + if ip_address != "192.168.42.2": + last_octet = ip_address.split(".")[-1] + my_speaker_info["mac_address"] = f"00-00-00-00-00-{last_octet.zfill(2)}" mock_soco.get_speaker_info = Mock(return_value=my_speaker_info) mock_soco.add_to_queue = Mock(return_value=10) mock_soco.add_uri_to_queue = Mock(return_value=10) @@ -278,7 +304,7 @@ class SoCoMockFactory: mock_soco.alarmClock = self.alarm_clock mock_soco.get_battery_info.return_value = self.battery_info - mock_soco.all_zones = {mock_soco} + mock_soco.mock_include_in_all_zones = True mock_soco.group.coordinator = mock_soco mock_soco.household_id = "test_household_id" self.mock_list[ip_address] = mock_soco diff --git a/tests/components/sonos/test_init.py b/tests/components/sonos/test_init.py index c1b98b2ec60..901ae359917 100644 --- a/tests/components/sonos/test_init.py +++ b/tests/components/sonos/test_init.py @@ -324,10 +324,15 @@ async def test_async_poll_manual_hosts_5( soco_1 = soco_factory.cache_mock(MockSoCo(), "10.10.10.1", "Living Room") soco_1.renderingControl = Mock() soco_1.renderingControl.GetVolume = Mock() + # Unavailable speakers should not be included in all zones + soco_1.mock_include_in_all_zones = False + speaker_1_activity = SpeakerActivity(hass, soco_1) soco_2 = soco_factory.cache_mock(MockSoCo(), "10.10.10.2", "Bedroom") soco_2.renderingControl = Mock() soco_2.renderingControl.GetVolume = Mock() + soco_2.mock_include_in_all_zones = False + speaker_2_activity = SpeakerActivity(hass, soco_2) with caplog.at_level(logging.DEBUG): diff --git a/tests/components/sonos/test_switch.py b/tests/components/sonos/test_switch.py index 04457ee95c7..56dd96b0caf 100644 --- a/tests/components/sonos/test_switch.py +++ b/tests/components/sonos/test_switch.py @@ -26,10 +26,10 @@ from homeassistant.const import ( STATE_ON, ) from homeassistant.core import HomeAssistant -from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util import dt as dt_util -from .conftest import MockSoCo, SonosMockEvent +from .conftest import MockSoCo, SonosMockEvent, SonosMockService from tests.common import async_fire_time_changed @@ -211,3 +211,53 @@ async def test_alarm_create_delete( assert "switch.sonos_alarm_14" in entity_registry.entities assert "switch.sonos_alarm_15" not in entity_registry.entities + + +async def test_alarm_change_device( + hass: HomeAssistant, + async_setup_sonos, + soco: MockSoCo, + alarm_clock: SonosMockService, + alarm_clock_extended: SonosMockService, + alarm_event: SonosMockEvent, + entity_registry: er.EntityRegistry, + device_registry: dr.DeviceRegistry, + sonos_setup_two_speakers: list[MockSoCo], +) -> None: + """Test Sonos Alarm being moved to a different speaker. + + This test simulates a scenario where an alarm is created on one speaker + and then moved to another speaker. It checks that the entity is correctly + created on the new speaker and removed from the old one. + """ + entity_id = "switch.sonos_alarm_14" + soco_lr = sonos_setup_two_speakers[0] + + await async_setup_sonos() + + # Initially, the alarm is created on the soco mock + assert entity_id in entity_registry.entities + entity = entity_registry.async_get(entity_id) + device = device_registry.async_get(entity.device_id) + assert device.name == soco.get_speaker_info()["zone_name"] + + # Simulate the alarm being moved to the soco_lr speaker + alarm_update = copy(alarm_clock_extended.ListAlarms.return_value) + alarm_update["CurrentAlarmList"] = alarm_update["CurrentAlarmList"].replace( + "RINCON_test", f"{soco_lr.uid}" + ) + alarm_clock.ListAlarms.return_value = alarm_update + + # Update the alarm_list_version so it gets processed. + alarm_event.variables["alarm_list_version"] = f"{soco_lr.uid}:1000" + alarm_update["CurrentAlarmListVersion"] = alarm_event.increment_variable( + "alarm_list_version" + ) + + alarm_clock.subscribe.return_value.callback(event=alarm_event) + await hass.async_block_till_done(wait_background_tasks=True) + + assert entity_id in entity_registry.entities + alarm_14 = entity_registry.async_get(entity_id) + device = device_registry.async_get(alarm_14.device_id) + assert device.name == soco_lr.get_speaker_info()["zone_name"] From 295b15ace928bb8cce046dca7b8cb1b9547447d8 Mon Sep 17 00:00:00 2001 From: TheJulianJES Date: Sat, 5 Jul 2025 20:23:03 +0200 Subject: [PATCH 076/106] Change ZHA string "autoshutdown" to "auto-shutdown" (#148230) --- homeassistant/components/zha/strings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/zha/strings.json b/homeassistant/components/zha/strings.json index 48bdfc6bb62..87c3903b342 100644 --- a/homeassistant/components/zha/strings.json +++ b/homeassistant/components/zha/strings.json @@ -1118,7 +1118,7 @@ "name": "Comfort temperature" }, "valve_state_auto_shutdown": { - "name": "Valve state autoshutdown" + "name": "Valve state auto-shutdown" }, "shutdown_timer": { "name": "Shutdown timer" From eb0f11a8597488fb8249646b49f8dce37f9d6734 Mon Sep 17 00:00:00 2001 From: Noah Husby <32528627+noahhusby@users.noreply.github.com> Date: Sat, 5 Jul 2025 14:13:48 -0500 Subject: [PATCH 077/106] Bump aiorussound to 4.8.0 (#148235) --- homeassistant/components/russound_rio/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/russound_rio/manifest.json b/homeassistant/components/russound_rio/manifest.json index 955ab451d3d..aad9b9425aa 100644 --- a/homeassistant/components/russound_rio/manifest.json +++ b/homeassistant/components/russound_rio/manifest.json @@ -7,6 +7,6 @@ "iot_class": "local_push", "loggers": ["aiorussound"], "quality_scale": "silver", - "requirements": ["aiorussound==4.7.0"], + "requirements": ["aiorussound==4.8.0"], "zeroconf": ["_rio._tcp.local."] } diff --git a/requirements_all.txt b/requirements_all.txt index 2655e6f9a90..83e7693822e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -372,7 +372,7 @@ aioridwell==2024.01.0 aioruckus==0.42 # homeassistant.components.russound_rio -aiorussound==4.7.0 +aiorussound==4.8.0 # homeassistant.components.ruuvi_gateway aioruuvigateway==0.1.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 3ec700ccf1e..a7fb3353938 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -354,7 +354,7 @@ aioridwell==2024.01.0 aioruckus==0.42 # homeassistant.components.russound_rio -aiorussound==4.7.0 +aiorussound==4.8.0 # homeassistant.components.ruuvi_gateway aioruuvigateway==0.1.0 From 160e4e4d054a3266204a5ccb6fe0e77331d27a5f Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sat, 5 Jul 2025 21:36:15 +0200 Subject: [PATCH 078/106] Block options flow for default hostname in dnsip (#148221) --- homeassistant/components/dnsip/config_flow.py | 3 ++ homeassistant/components/dnsip/strings.json | 3 +- tests/components/dnsip/test_config_flow.py | 34 +++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/dnsip/config_flow.py b/homeassistant/components/dnsip/config_flow.py index 6b86f1627bc..ab1ca42acd3 100644 --- a/homeassistant/components/dnsip/config_flow.py +++ b/homeassistant/components/dnsip/config_flow.py @@ -172,6 +172,9 @@ class DnsIPOptionsFlowHandler(OptionsFlow): self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Manage the options.""" + if self.config_entry.data[CONF_HOSTNAME] == DEFAULT_HOSTNAME: + return self.async_abort(reason="no_options") + errors = {} if user_input is not None: resolver = user_input.get(CONF_RESOLVER, DEFAULT_RESOLVER) diff --git a/homeassistant/components/dnsip/strings.json b/homeassistant/components/dnsip/strings.json index 39a0fbf7cd3..70472d37917 100644 --- a/homeassistant/components/dnsip/strings.json +++ b/homeassistant/components/dnsip/strings.json @@ -30,7 +30,8 @@ } }, "abort": { - "already_configured": "[%key:common::config_flow::abort::already_configured_service%]" + "already_configured": "[%key:common::config_flow::abort::already_configured_service%]", + "no_options": "The myip hostname requires the default resolvers and therefore cannot be configured." }, "error": { "invalid_resolver": "Invalid IP address or port for resolver" diff --git a/tests/components/dnsip/test_config_flow.py b/tests/components/dnsip/test_config_flow.py index 1a565345275..d9420afaa8c 100644 --- a/tests/components/dnsip/test_config_flow.py +++ b/tests/components/dnsip/test_config_flow.py @@ -16,6 +16,7 @@ from homeassistant.components.dnsip.const import ( CONF_PORT_IPV6, CONF_RESOLVER, CONF_RESOLVER_IPV6, + DEFAULT_HOSTNAME, DOMAIN, ) from homeassistant.config_entries import ConfigEntryState @@ -379,3 +380,36 @@ async def test_options_error(hass: HomeAssistant, p_input: dict[str, str]) -> No assert result2["errors"] == {"resolver": "invalid_resolver"} if p_input[CONF_IPV6]: assert result2["errors"] == {"resolver_ipv6": "invalid_resolver"} + + +async def test_cannot_configure_options_for_myip(hass: HomeAssistant) -> None: + """Test options config flow aborts for default myip hostname.""" + entry = MockConfigEntry( + domain=DOMAIN, + unique_id="12345", + data={ + CONF_HOSTNAME: DEFAULT_HOSTNAME, + CONF_NAME: "myip", + CONF_IPV4: True, + CONF_IPV6: False, + }, + options={ + CONF_RESOLVER: "208.67.222.222", + CONF_RESOLVER_IPV6: "2620:119:53::5", + CONF_PORT: 53, + CONF_PORT_IPV6: 53, + }, + ) + entry.add_to_hass(hass) + + with patch( + "homeassistant.components.dnsip.config_flow.aiodns.DNSResolver", + return_value=RetrieveDNS(), + ): + assert await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() + + result = await hass.config_entries.options.async_init(entry.entry_id) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "no_options" From e304022560bdcc79089d728a7c1b72cd1f16b557 Mon Sep 17 00:00:00 2001 From: G Johansson Date: Sat, 5 Jul 2025 21:39:48 +0200 Subject: [PATCH 079/106] Add service in Nord Pool for fetching normalized price indices (#147979) --- homeassistant/components/nordpool/const.py | 1 + homeassistant/components/nordpool/icons.json | 3 + homeassistant/components/nordpool/services.py | 79 +- .../components/nordpool/services.yaml | 56 ++ .../components/nordpool/strings.json | 26 + .../nordpool/fixtures/indices_15.json | 689 ++++++++++++++++++ .../nordpool/fixtures/indices_60.json | 185 +++++ .../nordpool/snapshots/test_services.ambr | 612 ++++++++++++++++ tests/components/nordpool/test_services.py | 84 ++- 9 files changed, 1726 insertions(+), 9 deletions(-) create mode 100644 tests/components/nordpool/fixtures/indices_15.json create mode 100644 tests/components/nordpool/fixtures/indices_60.json diff --git a/homeassistant/components/nordpool/const.py b/homeassistant/components/nordpool/const.py index 19a978d946c..1fd3009321b 100644 --- a/homeassistant/components/nordpool/const.py +++ b/homeassistant/components/nordpool/const.py @@ -12,3 +12,4 @@ PLATFORMS = [Platform.SENSOR] DEFAULT_NAME = "Nord Pool" CONF_AREAS = "areas" +ATTR_RESOLUTION = "resolution" diff --git a/homeassistant/components/nordpool/icons.json b/homeassistant/components/nordpool/icons.json index 5a1a3df3d92..42449b7a1a5 100644 --- a/homeassistant/components/nordpool/icons.json +++ b/homeassistant/components/nordpool/icons.json @@ -42,6 +42,9 @@ "services": { "get_prices_for_date": { "service": "mdi:cash-multiple" + }, + "get_price_indices_for_date": { + "service": "mdi:cash-multiple" } } } diff --git a/homeassistant/components/nordpool/services.py b/homeassistant/components/nordpool/services.py index 9bb97d0737b..e568764871a 100644 --- a/homeassistant/components/nordpool/services.py +++ b/homeassistant/components/nordpool/services.py @@ -2,16 +2,21 @@ from __future__ import annotations +from collections.abc import Callable from datetime import date, datetime +from functools import partial import logging from typing import TYPE_CHECKING from pynordpool import ( AREAS, Currency, + DeliveryPeriodData, NordPoolAuthenticationError, + NordPoolClient, NordPoolEmptyResponseError, NordPoolError, + PriceIndicesData, ) import voluptuous as vol @@ -32,7 +37,7 @@ from homeassistant.util.json import JsonValueType if TYPE_CHECKING: from . import NordPoolConfigEntry -from .const import DOMAIN +from .const import ATTR_RESOLUTION, DOMAIN _LOGGER = logging.getLogger(__name__) ATTR_CONFIG_ENTRY = "config_entry" @@ -40,6 +45,7 @@ ATTR_AREAS = "areas" ATTR_CURRENCY = "currency" SERVICE_GET_PRICES_FOR_DATE = "get_prices_for_date" +SERVICE_GET_PRICE_INDICES_FOR_DATE = "get_price_indices_for_date" SERVICE_GET_PRICES_SCHEMA = vol.Schema( { vol.Required(ATTR_CONFIG_ENTRY): ConfigEntrySelector({"integration": DOMAIN}), @@ -50,6 +56,13 @@ SERVICE_GET_PRICES_SCHEMA = vol.Schema( ), } ) +SERVICE_GET_PRICE_INDICES_SCHEMA = SERVICE_GET_PRICES_SCHEMA.extend( + { + vol.Optional(ATTR_RESOLUTION, default=60): vol.All( + cv.positive_int, vol.All(vol.Coerce(int), vol.In((15, 30, 60))) + ), + } +) def get_config_entry(hass: HomeAssistant, entry_id: str) -> NordPoolConfigEntry: @@ -71,11 +84,13 @@ def get_config_entry(hass: HomeAssistant, entry_id: str) -> NordPoolConfigEntry: def async_setup_services(hass: HomeAssistant) -> None: """Set up services for Nord Pool integration.""" - async def get_prices_for_date(call: ServiceCall) -> ServiceResponse: - """Get price service.""" + def get_service_params( + call: ServiceCall, + ) -> tuple[NordPoolClient, date, str, list[str], int]: + """Return the parameters for the service.""" entry = get_config_entry(hass, call.data[ATTR_CONFIG_ENTRY]) - asked_date: date = call.data[ATTR_DATE] client = entry.runtime_data.client + asked_date: date = call.data[ATTR_DATE] areas: list[str] = entry.data[ATTR_AREAS] if _areas := call.data.get(ATTR_AREAS): @@ -85,14 +100,55 @@ def async_setup_services(hass: HomeAssistant) -> None: if _currency := call.data.get(ATTR_CURRENCY): currency = _currency + resolution: int = 60 + if _resolution := call.data.get(ATTR_RESOLUTION): + resolution = _resolution + areas = [area.upper() for area in areas] currency = currency.upper() + return (client, asked_date, currency, areas, resolution) + + async def get_prices_for_date( + client: NordPoolClient, + asked_date: date, + currency: str, + areas: list[str], + resolution: int, + ) -> DeliveryPeriodData: + """Get prices.""" + return await client.async_get_delivery_period( + datetime.combine(asked_date, dt_util.utcnow().time()), + Currency(currency), + areas, + ) + + async def get_price_indices_for_date( + client: NordPoolClient, + asked_date: date, + currency: str, + areas: list[str], + resolution: int, + ) -> PriceIndicesData: + """Get prices.""" + return await client.async_get_price_indices( + datetime.combine(asked_date, dt_util.utcnow().time()), + Currency(currency), + areas, + resolution=resolution, + ) + + async def get_prices(func: Callable, call: ServiceCall) -> ServiceResponse: + """Get price service.""" + client, asked_date, currency, areas, resolution = get_service_params(call) + try: - price_data = await client.async_get_delivery_period( - datetime.combine(asked_date, dt_util.utcnow().time()), - Currency(currency), + price_data = await func( + client, + asked_date, + currency, areas, + resolution, ) except NordPoolAuthenticationError as error: raise ServiceValidationError( @@ -122,7 +178,14 @@ def async_setup_services(hass: HomeAssistant) -> None: hass.services.async_register( DOMAIN, SERVICE_GET_PRICES_FOR_DATE, - get_prices_for_date, + partial(get_prices, get_prices_for_date), schema=SERVICE_GET_PRICES_SCHEMA, supports_response=SupportsResponse.ONLY, ) + hass.services.async_register( + DOMAIN, + SERVICE_GET_PRICE_INDICES_FOR_DATE, + partial(get_prices, get_price_indices_for_date), + schema=SERVICE_GET_PRICE_INDICES_SCHEMA, + supports_response=SupportsResponse.ONLY, + ) diff --git a/homeassistant/components/nordpool/services.yaml b/homeassistant/components/nordpool/services.yaml index dded8482c6f..f18d705f54b 100644 --- a/homeassistant/components/nordpool/services.yaml +++ b/homeassistant/components/nordpool/services.yaml @@ -46,3 +46,59 @@ get_prices_for_date: - "PLN" - "SEK" mode: dropdown +get_price_indices_for_date: + fields: + config_entry: + required: true + selector: + config_entry: + integration: nordpool + date: + required: true + selector: + date: + areas: + selector: + select: + options: + - "EE" + - "LT" + - "LV" + - "AT" + - "BE" + - "FR" + - "GER" + - "NL" + - "PL" + - "DK1" + - "DK2" + - "FI" + - "NO1" + - "NO2" + - "NO3" + - "NO4" + - "NO5" + - "SE1" + - "SE2" + - "SE3" + - "SE4" + - "SYS" + mode: dropdown + currency: + selector: + select: + options: + - "DKK" + - "EUR" + - "NOK" + - "PLN" + - "SEK" + mode: dropdown + resolution: + selector: + select: + options: + - "15" + - "30" + - "60" + mode: dropdown diff --git a/homeassistant/components/nordpool/strings.json b/homeassistant/components/nordpool/strings.json index 73c35673826..06bd74e78a6 100644 --- a/homeassistant/components/nordpool/strings.json +++ b/homeassistant/components/nordpool/strings.json @@ -114,6 +114,32 @@ "description": "Currency to get prices in. If left empty it will use the currency already configured." } } + }, + "get_price_indices_for_date": { + "name": "Get price indices for date", + "description": "Retrieves the price indices for a specific date.", + "fields": { + "config_entry": { + "name": "Config entry", + "description": "The Nord Pool configuration entry for this action." + }, + "date": { + "name": "Date", + "description": "Only dates two months in the past and one day in the future is allowed." + }, + "areas": { + "name": "Areas", + "description": "One or multiple areas to get prices for. If left empty it will use the areas already configured." + }, + "currency": { + "name": "Currency", + "description": "Currency to get prices in. If left empty it will use the currency already configured." + }, + "resolution": { + "name": "Resolution", + "description": "Resolution time for the prices, can be any of 15, 30 and 60 minutes." + } + } } }, "exceptions": { diff --git a/tests/components/nordpool/fixtures/indices_15.json b/tests/components/nordpool/fixtures/indices_15.json new file mode 100644 index 00000000000..63af9840098 --- /dev/null +++ b/tests/components/nordpool/fixtures/indices_15.json @@ -0,0 +1,689 @@ +{ + "deliveryDateCET": "2025-07-06", + "version": 2, + "updatedAt": "2025-07-05T10:56:42.3755929Z", + "market": "DayAhead", + "indexNames": ["SE3"], + "currency": "SEK", + "resolutionInMinutes": 15, + "areaStates": [ + { + "state": "Preliminary", + "areas": ["SE3"] + } + ], + "multiIndexEntries": [ + { + "deliveryStart": "2025-07-05T22:00:00Z", + "deliveryEnd": "2025-07-05T22:15:00Z", + "entryPerArea": { + "SE3": 43.57 + } + }, + { + "deliveryStart": "2025-07-05T22:15:00Z", + "deliveryEnd": "2025-07-05T22:30:00Z", + "entryPerArea": { + "SE3": 43.57 + } + }, + { + "deliveryStart": "2025-07-05T22:30:00Z", + "deliveryEnd": "2025-07-05T22:45:00Z", + "entryPerArea": { + "SE3": 43.57 + } + }, + { + "deliveryStart": "2025-07-05T22:45:00Z", + "deliveryEnd": "2025-07-05T23:00:00Z", + "entryPerArea": { + "SE3": 43.57 + } + }, + { + "deliveryStart": "2025-07-05T23:00:00Z", + "deliveryEnd": "2025-07-05T23:15:00Z", + "entryPerArea": { + "SE3": 36.47 + } + }, + { + "deliveryStart": "2025-07-05T23:15:00Z", + "deliveryEnd": "2025-07-05T23:30:00Z", + "entryPerArea": { + "SE3": 36.47 + } + }, + { + "deliveryStart": "2025-07-05T23:30:00Z", + "deliveryEnd": "2025-07-05T23:45:00Z", + "entryPerArea": { + "SE3": 36.47 + } + }, + { + "deliveryStart": "2025-07-05T23:45:00Z", + "deliveryEnd": "2025-07-06T00:00:00Z", + "entryPerArea": { + "SE3": 36.47 + } + }, + { + "deliveryStart": "2025-07-06T00:00:00Z", + "deliveryEnd": "2025-07-06T00:15:00Z", + "entryPerArea": { + "SE3": 35.57 + } + }, + { + "deliveryStart": "2025-07-06T00:15:00Z", + "deliveryEnd": "2025-07-06T00:30:00Z", + "entryPerArea": { + "SE3": 35.57 + } + }, + { + "deliveryStart": "2025-07-06T00:30:00Z", + "deliveryEnd": "2025-07-06T00:45:00Z", + "entryPerArea": { + "SE3": 35.57 + } + }, + { + "deliveryStart": "2025-07-06T00:45:00Z", + "deliveryEnd": "2025-07-06T01:00:00Z", + "entryPerArea": { + "SE3": 35.57 + } + }, + { + "deliveryStart": "2025-07-06T01:00:00Z", + "deliveryEnd": "2025-07-06T01:15:00Z", + "entryPerArea": { + "SE3": 30.73 + } + }, + { + "deliveryStart": "2025-07-06T01:15:00Z", + "deliveryEnd": "2025-07-06T01:30:00Z", + "entryPerArea": { + "SE3": 30.73 + } + }, + { + "deliveryStart": "2025-07-06T01:30:00Z", + "deliveryEnd": "2025-07-06T01:45:00Z", + "entryPerArea": { + "SE3": 30.73 + } + }, + { + "deliveryStart": "2025-07-06T01:45:00Z", + "deliveryEnd": "2025-07-06T02:00:00Z", + "entryPerArea": { + "SE3": 30.73 + } + }, + { + "deliveryStart": "2025-07-06T02:00:00Z", + "deliveryEnd": "2025-07-06T02:15:00Z", + "entryPerArea": { + "SE3": 32.42 + } + }, + { + "deliveryStart": "2025-07-06T02:15:00Z", + "deliveryEnd": "2025-07-06T02:30:00Z", + "entryPerArea": { + "SE3": 32.42 + } + }, + { + "deliveryStart": "2025-07-06T02:30:00Z", + "deliveryEnd": "2025-07-06T02:45:00Z", + "entryPerArea": { + "SE3": 32.42 + } + }, + { + "deliveryStart": "2025-07-06T02:45:00Z", + "deliveryEnd": "2025-07-06T03:00:00Z", + "entryPerArea": { + "SE3": 32.42 + } + }, + { + "deliveryStart": "2025-07-06T03:00:00Z", + "deliveryEnd": "2025-07-06T03:15:00Z", + "entryPerArea": { + "SE3": 38.73 + } + }, + { + "deliveryStart": "2025-07-06T03:15:00Z", + "deliveryEnd": "2025-07-06T03:30:00Z", + "entryPerArea": { + "SE3": 38.73 + } + }, + { + "deliveryStart": "2025-07-06T03:30:00Z", + "deliveryEnd": "2025-07-06T03:45:00Z", + "entryPerArea": { + "SE3": 38.73 + } + }, + { + "deliveryStart": "2025-07-06T03:45:00Z", + "deliveryEnd": "2025-07-06T04:00:00Z", + "entryPerArea": { + "SE3": 38.73 + } + }, + { + "deliveryStart": "2025-07-06T04:00:00Z", + "deliveryEnd": "2025-07-06T04:15:00Z", + "entryPerArea": { + "SE3": 42.78 + } + }, + { + "deliveryStart": "2025-07-06T04:15:00Z", + "deliveryEnd": "2025-07-06T04:30:00Z", + "entryPerArea": { + "SE3": 42.78 + } + }, + { + "deliveryStart": "2025-07-06T04:30:00Z", + "deliveryEnd": "2025-07-06T04:45:00Z", + "entryPerArea": { + "SE3": 42.78 + } + }, + { + "deliveryStart": "2025-07-06T04:45:00Z", + "deliveryEnd": "2025-07-06T05:00:00Z", + "entryPerArea": { + "SE3": 42.78 + } + }, + { + "deliveryStart": "2025-07-06T05:00:00Z", + "deliveryEnd": "2025-07-06T05:15:00Z", + "entryPerArea": { + "SE3": 54.71 + } + }, + { + "deliveryStart": "2025-07-06T05:15:00Z", + "deliveryEnd": "2025-07-06T05:30:00Z", + "entryPerArea": { + "SE3": 54.71 + } + }, + { + "deliveryStart": "2025-07-06T05:30:00Z", + "deliveryEnd": "2025-07-06T05:45:00Z", + "entryPerArea": { + "SE3": 54.71 + } + }, + { + "deliveryStart": "2025-07-06T05:45:00Z", + "deliveryEnd": "2025-07-06T06:00:00Z", + "entryPerArea": { + "SE3": 54.71 + } + }, + { + "deliveryStart": "2025-07-06T06:00:00Z", + "deliveryEnd": "2025-07-06T06:15:00Z", + "entryPerArea": { + "SE3": 83.87 + } + }, + { + "deliveryStart": "2025-07-06T06:15:00Z", + "deliveryEnd": "2025-07-06T06:30:00Z", + "entryPerArea": { + "SE3": 83.87 + } + }, + { + "deliveryStart": "2025-07-06T06:30:00Z", + "deliveryEnd": "2025-07-06T06:45:00Z", + "entryPerArea": { + "SE3": 83.87 + } + }, + { + "deliveryStart": "2025-07-06T06:45:00Z", + "deliveryEnd": "2025-07-06T07:00:00Z", + "entryPerArea": { + "SE3": 83.87 + } + }, + { + "deliveryStart": "2025-07-06T07:00:00Z", + "deliveryEnd": "2025-07-06T07:15:00Z", + "entryPerArea": { + "SE3": 78.8 + } + }, + { + "deliveryStart": "2025-07-06T07:15:00Z", + "deliveryEnd": "2025-07-06T07:30:00Z", + "entryPerArea": { + "SE3": 78.8 + } + }, + { + "deliveryStart": "2025-07-06T07:30:00Z", + "deliveryEnd": "2025-07-06T07:45:00Z", + "entryPerArea": { + "SE3": 78.8 + } + }, + { + "deliveryStart": "2025-07-06T07:45:00Z", + "deliveryEnd": "2025-07-06T08:00:00Z", + "entryPerArea": { + "SE3": 78.8 + } + }, + { + "deliveryStart": "2025-07-06T08:00:00Z", + "deliveryEnd": "2025-07-06T08:15:00Z", + "entryPerArea": { + "SE3": 92.09 + } + }, + { + "deliveryStart": "2025-07-06T08:15:00Z", + "deliveryEnd": "2025-07-06T08:30:00Z", + "entryPerArea": { + "SE3": 92.09 + } + }, + { + "deliveryStart": "2025-07-06T08:30:00Z", + "deliveryEnd": "2025-07-06T08:45:00Z", + "entryPerArea": { + "SE3": 92.09 + } + }, + { + "deliveryStart": "2025-07-06T08:45:00Z", + "deliveryEnd": "2025-07-06T09:00:00Z", + "entryPerArea": { + "SE3": 92.09 + } + }, + { + "deliveryStart": "2025-07-06T09:00:00Z", + "deliveryEnd": "2025-07-06T09:15:00Z", + "entryPerArea": { + "SE3": 104.92 + } + }, + { + "deliveryStart": "2025-07-06T09:15:00Z", + "deliveryEnd": "2025-07-06T09:30:00Z", + "entryPerArea": { + "SE3": 104.92 + } + }, + { + "deliveryStart": "2025-07-06T09:30:00Z", + "deliveryEnd": "2025-07-06T09:45:00Z", + "entryPerArea": { + "SE3": 104.92 + } + }, + { + "deliveryStart": "2025-07-06T09:45:00Z", + "deliveryEnd": "2025-07-06T10:00:00Z", + "entryPerArea": { + "SE3": 104.92 + } + }, + { + "deliveryStart": "2025-07-06T10:00:00Z", + "deliveryEnd": "2025-07-06T10:15:00Z", + "entryPerArea": { + "SE3": 72.5 + } + }, + { + "deliveryStart": "2025-07-06T10:15:00Z", + "deliveryEnd": "2025-07-06T10:30:00Z", + "entryPerArea": { + "SE3": 72.5 + } + }, + { + "deliveryStart": "2025-07-06T10:30:00Z", + "deliveryEnd": "2025-07-06T10:45:00Z", + "entryPerArea": { + "SE3": 72.5 + } + }, + { + "deliveryStart": "2025-07-06T10:45:00Z", + "deliveryEnd": "2025-07-06T11:00:00Z", + "entryPerArea": { + "SE3": 72.5 + } + }, + { + "deliveryStart": "2025-07-06T11:00:00Z", + "deliveryEnd": "2025-07-06T11:15:00Z", + "entryPerArea": { + "SE3": 63.49 + } + }, + { + "deliveryStart": "2025-07-06T11:15:00Z", + "deliveryEnd": "2025-07-06T11:30:00Z", + "entryPerArea": { + "SE3": 63.49 + } + }, + { + "deliveryStart": "2025-07-06T11:30:00Z", + "deliveryEnd": "2025-07-06T11:45:00Z", + "entryPerArea": { + "SE3": 63.49 + } + }, + { + "deliveryStart": "2025-07-06T11:45:00Z", + "deliveryEnd": "2025-07-06T12:00:00Z", + "entryPerArea": { + "SE3": 63.49 + } + }, + { + "deliveryStart": "2025-07-06T12:00:00Z", + "deliveryEnd": "2025-07-06T12:15:00Z", + "entryPerArea": { + "SE3": 91.64 + } + }, + { + "deliveryStart": "2025-07-06T12:15:00Z", + "deliveryEnd": "2025-07-06T12:30:00Z", + "entryPerArea": { + "SE3": 91.64 + } + }, + { + "deliveryStart": "2025-07-06T12:30:00Z", + "deliveryEnd": "2025-07-06T12:45:00Z", + "entryPerArea": { + "SE3": 91.64 + } + }, + { + "deliveryStart": "2025-07-06T12:45:00Z", + "deliveryEnd": "2025-07-06T13:00:00Z", + "entryPerArea": { + "SE3": 91.64 + } + }, + { + "deliveryStart": "2025-07-06T13:00:00Z", + "deliveryEnd": "2025-07-06T13:15:00Z", + "entryPerArea": { + "SE3": 111.79 + } + }, + { + "deliveryStart": "2025-07-06T13:15:00Z", + "deliveryEnd": "2025-07-06T13:30:00Z", + "entryPerArea": { + "SE3": 111.79 + } + }, + { + "deliveryStart": "2025-07-06T13:30:00Z", + "deliveryEnd": "2025-07-06T13:45:00Z", + "entryPerArea": { + "SE3": 111.79 + } + }, + { + "deliveryStart": "2025-07-06T13:45:00Z", + "deliveryEnd": "2025-07-06T14:00:00Z", + "entryPerArea": { + "SE3": 111.79 + } + }, + { + "deliveryStart": "2025-07-06T14:00:00Z", + "deliveryEnd": "2025-07-06T14:15:00Z", + "entryPerArea": { + "SE3": 234.04 + } + }, + { + "deliveryStart": "2025-07-06T14:15:00Z", + "deliveryEnd": "2025-07-06T14:30:00Z", + "entryPerArea": { + "SE3": 234.04 + } + }, + { + "deliveryStart": "2025-07-06T14:30:00Z", + "deliveryEnd": "2025-07-06T14:45:00Z", + "entryPerArea": { + "SE3": 234.04 + } + }, + { + "deliveryStart": "2025-07-06T14:45:00Z", + "deliveryEnd": "2025-07-06T15:00:00Z", + "entryPerArea": { + "SE3": 234.04 + } + }, + { + "deliveryStart": "2025-07-06T15:00:00Z", + "deliveryEnd": "2025-07-06T15:15:00Z", + "entryPerArea": { + "SE3": 435.33 + } + }, + { + "deliveryStart": "2025-07-06T15:15:00Z", + "deliveryEnd": "2025-07-06T15:30:00Z", + "entryPerArea": { + "SE3": 435.33 + } + }, + { + "deliveryStart": "2025-07-06T15:30:00Z", + "deliveryEnd": "2025-07-06T15:45:00Z", + "entryPerArea": { + "SE3": 435.33 + } + }, + { + "deliveryStart": "2025-07-06T15:45:00Z", + "deliveryEnd": "2025-07-06T16:00:00Z", + "entryPerArea": { + "SE3": 435.33 + } + }, + { + "deliveryStart": "2025-07-06T16:00:00Z", + "deliveryEnd": "2025-07-06T16:15:00Z", + "entryPerArea": { + "SE3": 431.84 + } + }, + { + "deliveryStart": "2025-07-06T16:15:00Z", + "deliveryEnd": "2025-07-06T16:30:00Z", + "entryPerArea": { + "SE3": 431.84 + } + }, + { + "deliveryStart": "2025-07-06T16:30:00Z", + "deliveryEnd": "2025-07-06T16:45:00Z", + "entryPerArea": { + "SE3": 431.84 + } + }, + { + "deliveryStart": "2025-07-06T16:45:00Z", + "deliveryEnd": "2025-07-06T17:00:00Z", + "entryPerArea": { + "SE3": 431.84 + } + }, + { + "deliveryStart": "2025-07-06T17:00:00Z", + "deliveryEnd": "2025-07-06T17:15:00Z", + "entryPerArea": { + "SE3": 423.73 + } + }, + { + "deliveryStart": "2025-07-06T17:15:00Z", + "deliveryEnd": "2025-07-06T17:30:00Z", + "entryPerArea": { + "SE3": 423.73 + } + }, + { + "deliveryStart": "2025-07-06T17:30:00Z", + "deliveryEnd": "2025-07-06T17:45:00Z", + "entryPerArea": { + "SE3": 423.73 + } + }, + { + "deliveryStart": "2025-07-06T17:45:00Z", + "deliveryEnd": "2025-07-06T18:00:00Z", + "entryPerArea": { + "SE3": 423.73 + } + }, + { + "deliveryStart": "2025-07-06T18:00:00Z", + "deliveryEnd": "2025-07-06T18:15:00Z", + "entryPerArea": { + "SE3": 437.92 + } + }, + { + "deliveryStart": "2025-07-06T18:15:00Z", + "deliveryEnd": "2025-07-06T18:30:00Z", + "entryPerArea": { + "SE3": 437.92 + } + }, + { + "deliveryStart": "2025-07-06T18:30:00Z", + "deliveryEnd": "2025-07-06T18:45:00Z", + "entryPerArea": { + "SE3": 437.92 + } + }, + { + "deliveryStart": "2025-07-06T18:45:00Z", + "deliveryEnd": "2025-07-06T19:00:00Z", + "entryPerArea": { + "SE3": 437.92 + } + }, + { + "deliveryStart": "2025-07-06T19:00:00Z", + "deliveryEnd": "2025-07-06T19:15:00Z", + "entryPerArea": { + "SE3": 416.42 + } + }, + { + "deliveryStart": "2025-07-06T19:15:00Z", + "deliveryEnd": "2025-07-06T19:30:00Z", + "entryPerArea": { + "SE3": 416.42 + } + }, + { + "deliveryStart": "2025-07-06T19:30:00Z", + "deliveryEnd": "2025-07-06T19:45:00Z", + "entryPerArea": { + "SE3": 416.42 + } + }, + { + "deliveryStart": "2025-07-06T19:45:00Z", + "deliveryEnd": "2025-07-06T20:00:00Z", + "entryPerArea": { + "SE3": 416.42 + } + }, + { + "deliveryStart": "2025-07-06T20:00:00Z", + "deliveryEnd": "2025-07-06T20:15:00Z", + "entryPerArea": { + "SE3": 414.39 + } + }, + { + "deliveryStart": "2025-07-06T20:15:00Z", + "deliveryEnd": "2025-07-06T20:30:00Z", + "entryPerArea": { + "SE3": 414.39 + } + }, + { + "deliveryStart": "2025-07-06T20:30:00Z", + "deliveryEnd": "2025-07-06T20:45:00Z", + "entryPerArea": { + "SE3": 414.39 + } + }, + { + "deliveryStart": "2025-07-06T20:45:00Z", + "deliveryEnd": "2025-07-06T21:00:00Z", + "entryPerArea": { + "SE3": 414.39 + } + }, + { + "deliveryStart": "2025-07-06T21:00:00Z", + "deliveryEnd": "2025-07-06T21:15:00Z", + "entryPerArea": { + "SE3": 396.38 + } + }, + { + "deliveryStart": "2025-07-06T21:15:00Z", + "deliveryEnd": "2025-07-06T21:30:00Z", + "entryPerArea": { + "SE3": 396.38 + } + }, + { + "deliveryStart": "2025-07-06T21:30:00Z", + "deliveryEnd": "2025-07-06T21:45:00Z", + "entryPerArea": { + "SE3": 396.38 + } + }, + { + "deliveryStart": "2025-07-06T21:45:00Z", + "deliveryEnd": "2025-07-06T22:00:00Z", + "entryPerArea": { + "SE3": 396.38 + } + } + ] +} diff --git a/tests/components/nordpool/fixtures/indices_60.json b/tests/components/nordpool/fixtures/indices_60.json new file mode 100644 index 00000000000..97bbe554b13 --- /dev/null +++ b/tests/components/nordpool/fixtures/indices_60.json @@ -0,0 +1,185 @@ +{ + "deliveryDateCET": "2025-07-06", + "version": 2, + "updatedAt": "2025-07-05T10:56:44.6936838Z", + "market": "DayAhead", + "indexNames": ["SE3"], + "currency": "SEK", + "resolutionInMinutes": 60, + "areaStates": [ + { + "state": "Preliminary", + "areas": ["SE3"] + } + ], + "multiIndexEntries": [ + { + "deliveryStart": "2025-07-05T22:00:00Z", + "deliveryEnd": "2025-07-05T23:00:00Z", + "entryPerArea": { + "SE3": 43.57 + } + }, + { + "deliveryStart": "2025-07-05T23:00:00Z", + "deliveryEnd": "2025-07-06T00:00:00Z", + "entryPerArea": { + "SE3": 36.47 + } + }, + { + "deliveryStart": "2025-07-06T00:00:00Z", + "deliveryEnd": "2025-07-06T01:00:00Z", + "entryPerArea": { + "SE3": 35.57 + } + }, + { + "deliveryStart": "2025-07-06T01:00:00Z", + "deliveryEnd": "2025-07-06T02:00:00Z", + "entryPerArea": { + "SE3": 30.73 + } + }, + { + "deliveryStart": "2025-07-06T02:00:00Z", + "deliveryEnd": "2025-07-06T03:00:00Z", + "entryPerArea": { + "SE3": 32.42 + } + }, + { + "deliveryStart": "2025-07-06T03:00:00Z", + "deliveryEnd": "2025-07-06T04:00:00Z", + "entryPerArea": { + "SE3": 38.73 + } + }, + { + "deliveryStart": "2025-07-06T04:00:00Z", + "deliveryEnd": "2025-07-06T05:00:00Z", + "entryPerArea": { + "SE3": 42.78 + } + }, + { + "deliveryStart": "2025-07-06T05:00:00Z", + "deliveryEnd": "2025-07-06T06:00:00Z", + "entryPerArea": { + "SE3": 54.71 + } + }, + { + "deliveryStart": "2025-07-06T06:00:00Z", + "deliveryEnd": "2025-07-06T07:00:00Z", + "entryPerArea": { + "SE3": 83.87 + } + }, + { + "deliveryStart": "2025-07-06T07:00:00Z", + "deliveryEnd": "2025-07-06T08:00:00Z", + "entryPerArea": { + "SE3": 78.8 + } + }, + { + "deliveryStart": "2025-07-06T08:00:00Z", + "deliveryEnd": "2025-07-06T09:00:00Z", + "entryPerArea": { + "SE3": 92.09 + } + }, + { + "deliveryStart": "2025-07-06T09:00:00Z", + "deliveryEnd": "2025-07-06T10:00:00Z", + "entryPerArea": { + "SE3": 104.92 + } + }, + { + "deliveryStart": "2025-07-06T10:00:00Z", + "deliveryEnd": "2025-07-06T11:00:00Z", + "entryPerArea": { + "SE3": 72.5 + } + }, + { + "deliveryStart": "2025-07-06T11:00:00Z", + "deliveryEnd": "2025-07-06T12:00:00Z", + "entryPerArea": { + "SE3": 63.49 + } + }, + { + "deliveryStart": "2025-07-06T12:00:00Z", + "deliveryEnd": "2025-07-06T13:00:00Z", + "entryPerArea": { + "SE3": 91.64 + } + }, + { + "deliveryStart": "2025-07-06T13:00:00Z", + "deliveryEnd": "2025-07-06T14:00:00Z", + "entryPerArea": { + "SE3": 111.79 + } + }, + { + "deliveryStart": "2025-07-06T14:00:00Z", + "deliveryEnd": "2025-07-06T15:00:00Z", + "entryPerArea": { + "SE3": 234.04 + } + }, + { + "deliveryStart": "2025-07-06T15:00:00Z", + "deliveryEnd": "2025-07-06T16:00:00Z", + "entryPerArea": { + "SE3": 435.33 + } + }, + { + "deliveryStart": "2025-07-06T16:00:00Z", + "deliveryEnd": "2025-07-06T17:00:00Z", + "entryPerArea": { + "SE3": 431.84 + } + }, + { + "deliveryStart": "2025-07-06T17:00:00Z", + "deliveryEnd": "2025-07-06T18:00:00Z", + "entryPerArea": { + "SE3": 423.73 + } + }, + { + "deliveryStart": "2025-07-06T18:00:00Z", + "deliveryEnd": "2025-07-06T19:00:00Z", + "entryPerArea": { + "SE3": 437.92 + } + }, + { + "deliveryStart": "2025-07-06T19:00:00Z", + "deliveryEnd": "2025-07-06T20:00:00Z", + "entryPerArea": { + "SE3": 416.42 + } + }, + { + "deliveryStart": "2025-07-06T20:00:00Z", + "deliveryEnd": "2025-07-06T21:00:00Z", + "entryPerArea": { + "SE3": 414.39 + } + }, + { + "deliveryStart": "2025-07-06T21:00:00Z", + "deliveryEnd": "2025-07-06T22:00:00Z", + "entryPerArea": { + "SE3": 396.38 + } + } + ] +} diff --git a/tests/components/nordpool/snapshots/test_services.ambr b/tests/components/nordpool/snapshots/test_services.ambr index b271b433061..5e39082f647 100644 --- a/tests/components/nordpool/snapshots/test_services.ambr +++ b/tests/components/nordpool/snapshots/test_services.ambr @@ -131,3 +131,615 @@ ]), }) # --- +# name: test_service_call_for_price_indices[get_price_indices_for_date_15] + dict({ + 'SE3': list([ + dict({ + 'end': '2025-07-05T22:15:00+00:00', + 'price': 43.57, + 'start': '2025-07-05T22:00:00+00:00', + }), + dict({ + 'end': '2025-07-05T22:30:00+00:00', + 'price': 43.57, + 'start': '2025-07-05T22:15:00+00:00', + }), + dict({ + 'end': '2025-07-05T22:45:00+00:00', + 'price': 43.57, + 'start': '2025-07-05T22:30:00+00:00', + }), + dict({ + 'end': '2025-07-05T23:00:00+00:00', + 'price': 43.57, + 'start': '2025-07-05T22:45:00+00:00', + }), + dict({ + 'end': '2025-07-05T23:15:00+00:00', + 'price': 36.47, + 'start': '2025-07-05T23:00:00+00:00', + }), + dict({ + 'end': '2025-07-05T23:30:00+00:00', + 'price': 36.47, + 'start': '2025-07-05T23:15:00+00:00', + }), + dict({ + 'end': '2025-07-05T23:45:00+00:00', + 'price': 36.47, + 'start': '2025-07-05T23:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T00:00:00+00:00', + 'price': 36.47, + 'start': '2025-07-05T23:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T00:15:00+00:00', + 'price': 35.57, + 'start': '2025-07-06T00:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T00:30:00+00:00', + 'price': 35.57, + 'start': '2025-07-06T00:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T00:45:00+00:00', + 'price': 35.57, + 'start': '2025-07-06T00:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T01:00:00+00:00', + 'price': 35.57, + 'start': '2025-07-06T00:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T01:15:00+00:00', + 'price': 30.73, + 'start': '2025-07-06T01:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T01:30:00+00:00', + 'price': 30.73, + 'start': '2025-07-06T01:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T01:45:00+00:00', + 'price': 30.73, + 'start': '2025-07-06T01:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T02:00:00+00:00', + 'price': 30.73, + 'start': '2025-07-06T01:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T02:15:00+00:00', + 'price': 32.42, + 'start': '2025-07-06T02:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T02:30:00+00:00', + 'price': 32.42, + 'start': '2025-07-06T02:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T02:45:00+00:00', + 'price': 32.42, + 'start': '2025-07-06T02:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T03:00:00+00:00', + 'price': 32.42, + 'start': '2025-07-06T02:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T03:15:00+00:00', + 'price': 38.73, + 'start': '2025-07-06T03:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T03:30:00+00:00', + 'price': 38.73, + 'start': '2025-07-06T03:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T03:45:00+00:00', + 'price': 38.73, + 'start': '2025-07-06T03:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T04:00:00+00:00', + 'price': 38.73, + 'start': '2025-07-06T03:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T04:15:00+00:00', + 'price': 42.78, + 'start': '2025-07-06T04:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T04:30:00+00:00', + 'price': 42.78, + 'start': '2025-07-06T04:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T04:45:00+00:00', + 'price': 42.78, + 'start': '2025-07-06T04:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T05:00:00+00:00', + 'price': 42.78, + 'start': '2025-07-06T04:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T05:15:00+00:00', + 'price': 54.71, + 'start': '2025-07-06T05:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T05:30:00+00:00', + 'price': 54.71, + 'start': '2025-07-06T05:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T05:45:00+00:00', + 'price': 54.71, + 'start': '2025-07-06T05:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T06:00:00+00:00', + 'price': 54.71, + 'start': '2025-07-06T05:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T06:15:00+00:00', + 'price': 83.87, + 'start': '2025-07-06T06:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T06:30:00+00:00', + 'price': 83.87, + 'start': '2025-07-06T06:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T06:45:00+00:00', + 'price': 83.87, + 'start': '2025-07-06T06:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T07:00:00+00:00', + 'price': 83.87, + 'start': '2025-07-06T06:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T07:15:00+00:00', + 'price': 78.8, + 'start': '2025-07-06T07:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T07:30:00+00:00', + 'price': 78.8, + 'start': '2025-07-06T07:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T07:45:00+00:00', + 'price': 78.8, + 'start': '2025-07-06T07:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T08:00:00+00:00', + 'price': 78.8, + 'start': '2025-07-06T07:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T08:15:00+00:00', + 'price': 92.09, + 'start': '2025-07-06T08:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T08:30:00+00:00', + 'price': 92.09, + 'start': '2025-07-06T08:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T08:45:00+00:00', + 'price': 92.09, + 'start': '2025-07-06T08:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T09:00:00+00:00', + 'price': 92.09, + 'start': '2025-07-06T08:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T09:15:00+00:00', + 'price': 104.92, + 'start': '2025-07-06T09:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T09:30:00+00:00', + 'price': 104.92, + 'start': '2025-07-06T09:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T09:45:00+00:00', + 'price': 104.92, + 'start': '2025-07-06T09:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T10:00:00+00:00', + 'price': 104.92, + 'start': '2025-07-06T09:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T10:15:00+00:00', + 'price': 72.5, + 'start': '2025-07-06T10:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T10:30:00+00:00', + 'price': 72.5, + 'start': '2025-07-06T10:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T10:45:00+00:00', + 'price': 72.5, + 'start': '2025-07-06T10:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T11:00:00+00:00', + 'price': 72.5, + 'start': '2025-07-06T10:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T11:15:00+00:00', + 'price': 63.49, + 'start': '2025-07-06T11:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T11:30:00+00:00', + 'price': 63.49, + 'start': '2025-07-06T11:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T11:45:00+00:00', + 'price': 63.49, + 'start': '2025-07-06T11:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T12:00:00+00:00', + 'price': 63.49, + 'start': '2025-07-06T11:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T12:15:00+00:00', + 'price': 91.64, + 'start': '2025-07-06T12:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T12:30:00+00:00', + 'price': 91.64, + 'start': '2025-07-06T12:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T12:45:00+00:00', + 'price': 91.64, + 'start': '2025-07-06T12:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T13:00:00+00:00', + 'price': 91.64, + 'start': '2025-07-06T12:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T13:15:00+00:00', + 'price': 111.79, + 'start': '2025-07-06T13:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T13:30:00+00:00', + 'price': 111.79, + 'start': '2025-07-06T13:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T13:45:00+00:00', + 'price': 111.79, + 'start': '2025-07-06T13:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T14:00:00+00:00', + 'price': 111.79, + 'start': '2025-07-06T13:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T14:15:00+00:00', + 'price': 234.04, + 'start': '2025-07-06T14:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T14:30:00+00:00', + 'price': 234.04, + 'start': '2025-07-06T14:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T14:45:00+00:00', + 'price': 234.04, + 'start': '2025-07-06T14:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T15:00:00+00:00', + 'price': 234.04, + 'start': '2025-07-06T14:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T15:15:00+00:00', + 'price': 435.33, + 'start': '2025-07-06T15:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T15:30:00+00:00', + 'price': 435.33, + 'start': '2025-07-06T15:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T15:45:00+00:00', + 'price': 435.33, + 'start': '2025-07-06T15:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T16:00:00+00:00', + 'price': 435.33, + 'start': '2025-07-06T15:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T16:15:00+00:00', + 'price': 431.84, + 'start': '2025-07-06T16:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T16:30:00+00:00', + 'price': 431.84, + 'start': '2025-07-06T16:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T16:45:00+00:00', + 'price': 431.84, + 'start': '2025-07-06T16:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T17:00:00+00:00', + 'price': 431.84, + 'start': '2025-07-06T16:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T17:15:00+00:00', + 'price': 423.73, + 'start': '2025-07-06T17:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T17:30:00+00:00', + 'price': 423.73, + 'start': '2025-07-06T17:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T17:45:00+00:00', + 'price': 423.73, + 'start': '2025-07-06T17:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T18:00:00+00:00', + 'price': 423.73, + 'start': '2025-07-06T17:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T18:15:00+00:00', + 'price': 437.92, + 'start': '2025-07-06T18:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T18:30:00+00:00', + 'price': 437.92, + 'start': '2025-07-06T18:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T18:45:00+00:00', + 'price': 437.92, + 'start': '2025-07-06T18:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T19:00:00+00:00', + 'price': 437.92, + 'start': '2025-07-06T18:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T19:15:00+00:00', + 'price': 416.42, + 'start': '2025-07-06T19:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T19:30:00+00:00', + 'price': 416.42, + 'start': '2025-07-06T19:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T19:45:00+00:00', + 'price': 416.42, + 'start': '2025-07-06T19:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T20:00:00+00:00', + 'price': 416.42, + 'start': '2025-07-06T19:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T20:15:00+00:00', + 'price': 414.39, + 'start': '2025-07-06T20:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T20:30:00+00:00', + 'price': 414.39, + 'start': '2025-07-06T20:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T20:45:00+00:00', + 'price': 414.39, + 'start': '2025-07-06T20:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T21:00:00+00:00', + 'price': 414.39, + 'start': '2025-07-06T20:45:00+00:00', + }), + dict({ + 'end': '2025-07-06T21:15:00+00:00', + 'price': 396.38, + 'start': '2025-07-06T21:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T21:30:00+00:00', + 'price': 396.38, + 'start': '2025-07-06T21:15:00+00:00', + }), + dict({ + 'end': '2025-07-06T21:45:00+00:00', + 'price': 396.38, + 'start': '2025-07-06T21:30:00+00:00', + }), + dict({ + 'end': '2025-07-06T22:00:00+00:00', + 'price': 396.38, + 'start': '2025-07-06T21:45:00+00:00', + }), + ]), + }) +# --- +# name: test_service_call_for_price_indices[get_price_indices_for_date_60] + dict({ + 'SE3': list([ + dict({ + 'end': '2025-07-05T23:00:00+00:00', + 'price': 43.57, + 'start': '2025-07-05T22:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T00:00:00+00:00', + 'price': 36.47, + 'start': '2025-07-05T23:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T01:00:00+00:00', + 'price': 35.57, + 'start': '2025-07-06T00:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T02:00:00+00:00', + 'price': 30.73, + 'start': '2025-07-06T01:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T03:00:00+00:00', + 'price': 32.42, + 'start': '2025-07-06T02:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T04:00:00+00:00', + 'price': 38.73, + 'start': '2025-07-06T03:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T05:00:00+00:00', + 'price': 42.78, + 'start': '2025-07-06T04:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T06:00:00+00:00', + 'price': 54.71, + 'start': '2025-07-06T05:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T07:00:00+00:00', + 'price': 83.87, + 'start': '2025-07-06T06:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T08:00:00+00:00', + 'price': 78.8, + 'start': '2025-07-06T07:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T09:00:00+00:00', + 'price': 92.09, + 'start': '2025-07-06T08:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T10:00:00+00:00', + 'price': 104.92, + 'start': '2025-07-06T09:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T11:00:00+00:00', + 'price': 72.5, + 'start': '2025-07-06T10:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T12:00:00+00:00', + 'price': 63.49, + 'start': '2025-07-06T11:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T13:00:00+00:00', + 'price': 91.64, + 'start': '2025-07-06T12:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T14:00:00+00:00', + 'price': 111.79, + 'start': '2025-07-06T13:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T15:00:00+00:00', + 'price': 234.04, + 'start': '2025-07-06T14:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T16:00:00+00:00', + 'price': 435.33, + 'start': '2025-07-06T15:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T17:00:00+00:00', + 'price': 431.84, + 'start': '2025-07-06T16:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T18:00:00+00:00', + 'price': 423.73, + 'start': '2025-07-06T17:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T19:00:00+00:00', + 'price': 437.92, + 'start': '2025-07-06T18:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T20:00:00+00:00', + 'price': 416.42, + 'start': '2025-07-06T19:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T21:00:00+00:00', + 'price': 414.39, + 'start': '2025-07-06T20:00:00+00:00', + }), + dict({ + 'end': '2025-07-06T22:00:00+00:00', + 'price': 396.38, + 'start': '2025-07-06T21:00:00+00:00', + }), + ]), + }) +# --- diff --git a/tests/components/nordpool/test_services.py b/tests/components/nordpool/test_services.py index d59ec4712d7..1042783fee8 100644 --- a/tests/components/nordpool/test_services.py +++ b/tests/components/nordpool/test_services.py @@ -1,8 +1,10 @@ """Test services in Nord Pool.""" +import json from unittest.mock import patch from pynordpool import ( + API, NordPoolAuthenticationError, NordPoolEmptyResponseError, NordPoolError, @@ -15,13 +17,16 @@ from homeassistant.components.nordpool.services import ( ATTR_AREAS, ATTR_CONFIG_ENTRY, ATTR_CURRENCY, + ATTR_RESOLUTION, + SERVICE_GET_PRICE_INDICES_FOR_DATE, SERVICE_GET_PRICES_FOR_DATE, ) from homeassistant.const import ATTR_DATE from homeassistant.core import HomeAssistant from homeassistant.exceptions import ServiceValidationError -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, async_load_fixture +from tests.test_util.aiohttp import AiohttpClientMocker TEST_SERVICE_DATA = { ATTR_CONFIG_ENTRY: "to_replace", @@ -33,6 +38,20 @@ TEST_SERVICE_DATA_USE_DEFAULTS = { ATTR_CONFIG_ENTRY: "to_replace", ATTR_DATE: "2024-11-05", } +TEST_SERVICE_INDICES_DATA_60 = { + ATTR_CONFIG_ENTRY: "to_replace", + ATTR_DATE: "2025-07-06", + ATTR_AREAS: "SE3", + ATTR_CURRENCY: "SEK", + ATTR_RESOLUTION: 60, +} +TEST_SERVICE_INDICES_DATA_15 = { + ATTR_CONFIG_ENTRY: "to_replace", + ATTR_DATE: "2025-07-06", + ATTR_AREAS: "SE3", + ATTR_CURRENCY: "SEK", + ATTR_RESOLUTION: 15, +} @pytest.mark.freeze_time("2024-11-05T18:00:00+00:00") @@ -163,3 +182,66 @@ async def test_service_call_config_entry_bad_state( return_response=True, ) assert err.value.translation_key == "entry_not_loaded" + + +@pytest.mark.freeze_time("2024-11-05T18:00:00+00:00") +async def test_service_call_for_price_indices( + hass: HomeAssistant, + load_int: MockConfigEntry, + snapshot: SnapshotAssertion, + aioclient_mock: AiohttpClientMocker, +) -> None: + """Test get_price_indices_for_date service call.""" + + fixture_60 = json.loads(await async_load_fixture(hass, "indices_60.json", DOMAIN)) + fixture_15 = json.loads(await async_load_fixture(hass, "indices_15.json", DOMAIN)) + + aioclient_mock.request( + "GET", + url=API + "/DayAheadPriceIndices", + params={ + "date": "2025-07-06", + "market": "DayAhead", + "indexNames": "SE3", + "currency": "SEK", + "resolutionInMinutes": "60", + }, + json=fixture_60, + ) + + aioclient_mock.request( + "GET", + url=API + "/DayAheadPriceIndices", + params={ + "date": "2025-07-06", + "market": "DayAhead", + "indexNames": "SE3", + "currency": "SEK", + "resolutionInMinutes": "15", + }, + json=fixture_15, + ) + + service_data = TEST_SERVICE_INDICES_DATA_60.copy() + service_data[ATTR_CONFIG_ENTRY] = load_int.entry_id + response = await hass.services.async_call( + DOMAIN, + SERVICE_GET_PRICE_INDICES_FOR_DATE, + service_data, + blocking=True, + return_response=True, + ) + + assert response == snapshot(name="get_price_indices_for_date_60") + + service_data = TEST_SERVICE_INDICES_DATA_15.copy() + service_data[ATTR_CONFIG_ENTRY] = load_int.entry_id + response = await hass.services.async_call( + DOMAIN, + SERVICE_GET_PRICE_INDICES_FOR_DATE, + service_data, + blocking=True, + return_response=True, + ) + + assert response == snapshot(name="get_price_indices_for_date_15") From 3ffcfa42ba34e43f7df32645ad836ba94ca62a8f Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Sat, 5 Jul 2025 23:34:23 +0200 Subject: [PATCH 080/106] Bump pylamarzocco to 2.0.10 (#148233) --- homeassistant/components/lamarzocco/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/lamarzocco/manifest.json b/homeassistant/components/lamarzocco/manifest.json index 7fdafc4dda1..10cb23146ae 100644 --- a/homeassistant/components/lamarzocco/manifest.json +++ b/homeassistant/components/lamarzocco/manifest.json @@ -37,5 +37,5 @@ "iot_class": "cloud_push", "loggers": ["pylamarzocco"], "quality_scale": "platinum", - "requirements": ["pylamarzocco==2.0.9"] + "requirements": ["pylamarzocco==2.0.10"] } diff --git a/requirements_all.txt b/requirements_all.txt index 83e7693822e..01b297fd250 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2100,7 +2100,7 @@ pykwb==0.0.8 pylacrosse==0.4 # homeassistant.components.lamarzocco -pylamarzocco==2.0.9 +pylamarzocco==2.0.10 # homeassistant.components.lastfm pylast==5.1.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index a7fb3353938..29ab2676ee0 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1745,7 +1745,7 @@ pykrakenapi==0.1.8 pykulersky==0.5.8 # homeassistant.components.lamarzocco -pylamarzocco==2.0.9 +pylamarzocco==2.0.10 # homeassistant.components.lastfm pylast==5.1.0 From 26de1ea37b5baf8d351558558c9d1fc247073210 Mon Sep 17 00:00:00 2001 From: Josef Zweck Date: Sun, 6 Jul 2025 00:14:59 +0200 Subject: [PATCH 081/106] Update strings in pihole (#148234) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- homeassistant/components/pi_hole/strings.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/pi_hole/strings.json b/homeassistant/components/pi_hole/strings.json index 069f8a576d4..b3a634f4420 100644 --- a/homeassistant/components/pi_hole/strings.json +++ b/homeassistant/components/pi_hole/strings.json @@ -9,7 +9,7 @@ "location": "[%key:common::config_flow::data::location%]", "ssl": "[%key:common::config_flow::data::ssl%]", "verify_ssl": "[%key:common::config_flow::data::verify_ssl%]", - "api_key": "App Password or API Key" + "api_key": "App password or API key" } }, @@ -49,7 +49,7 @@ }, "ads_blocked": { "name": "Ads blocked", - "unit_of_measurement": "ads" + "unit_of_measurement": "[%key:component::pi_hole::entity::sensor::ads_blocked_today::unit_of_measurement%]" }, "ads_percentage_today": { "name": "Ads percentage blocked today" @@ -68,7 +68,7 @@ }, "dns_queries": { "name": "DNS queries", - "unit_of_measurement": "queries" + "unit_of_measurement": "[%key:component::pi_hole::entity::sensor::dns_queries_today::unit_of_measurement%]" }, "domains_being_blocked": { "name": "Domains blocked", From 70e9c4e2d0a9f72a3dad22248b2230e2f7cf4f18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sun, 6 Jul 2025 07:09:59 +0100 Subject: [PATCH 082/106] Add reauth flow to the Traccar Server integration (#148236) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Josef Zweck --- .../components/traccar_server/config_flow.py | 67 ++++++++++++- .../components/traccar_server/coordinator.py | 6 ++ .../components/traccar_server/strings.json | 13 ++- .../traccar_server/test_config_flow.py | 97 ++++++++++++++++++- 4 files changed, 179 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/traccar_server/config_flow.py b/homeassistant/components/traccar_server/config_flow.py index b186424d32c..ae2f01e698b 100644 --- a/homeassistant/components/traccar_server/config_flow.py +++ b/homeassistant/components/traccar_server/config_flow.py @@ -2,9 +2,15 @@ from __future__ import annotations +from collections.abc import Mapping from typing import Any -from pytraccar import ApiClient, ServerModel, TraccarException +from pytraccar import ( + ApiClient, + ServerModel, + TraccarAuthenticationException, + TraccarException, +) import voluptuous as vol from homeassistant import config_entries @@ -160,6 +166,65 @@ class TraccarServerConfigFlow(ConfigFlow, domain=DOMAIN): errors=errors, ) + async def async_step_reauth( + self, _entry_data: Mapping[str, Any] + ) -> ConfigFlowResult: + """Handle configuration by re-auth.""" + return await self.async_step_reauth_confirm() + + async def async_step_reauth_confirm( + self, + user_input: dict[str, Any] | None = None, + ) -> ConfigFlowResult: + """Handle reauth flow.""" + reauth_entry = self._get_reauth_entry() + errors: dict[str, str] = {} + + if user_input is not None: + test_data = { + **reauth_entry.data, + **user_input, + } + try: + await self._get_server_info(test_data) + except TraccarAuthenticationException: + LOGGER.error("Invalid credentials for Traccar Server") + errors["base"] = "invalid_auth" + except TraccarException as exception: + LOGGER.error("Unable to connect to Traccar Server: %s", exception) + errors["base"] = "cannot_connect" + except Exception: # noqa: BLE001 + LOGGER.exception("Unexpected exception") + errors["base"] = "unknown" + else: + return self.async_update_reload_and_abort( + reauth_entry, + data_updates=user_input, + ) + username = ( + user_input[CONF_USERNAME] + if user_input + else reauth_entry.data[CONF_USERNAME] + ) + return self.async_show_form( + step_id="reauth_confirm", + data_schema=vol.Schema( + { + vol.Required(CONF_USERNAME, default=username): TextSelector( + TextSelectorConfig(type=TextSelectorType.EMAIL) + ), + vol.Required(CONF_PASSWORD): TextSelector( + TextSelectorConfig(type=TextSelectorType.PASSWORD) + ), + } + ), + errors=errors, + description_placeholders={ + CONF_HOST: reauth_entry.data[CONF_HOST], + CONF_PORT: reauth_entry.data[CONF_PORT], + }, + ) + @staticmethod @callback def async_get_options_flow( diff --git a/homeassistant/components/traccar_server/coordinator.py b/homeassistant/components/traccar_server/coordinator.py index 3a0bfe47e5f..9cb0530356f 100644 --- a/homeassistant/components/traccar_server/coordinator.py +++ b/homeassistant/components/traccar_server/coordinator.py @@ -13,11 +13,13 @@ from pytraccar import ( GeofenceModel, PositionModel, SubscriptionData, + TraccarAuthenticationException, TraccarException, ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryAuthFailed from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed from homeassistant.util import dt as dt_util @@ -90,6 +92,8 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat self.client.get_positions(), self.client.get_geofences(), ) + except TraccarAuthenticationException: + raise ConfigEntryAuthFailed from None except TraccarException as ex: raise UpdateFailed(f"Error while updating device data: {ex}") from ex @@ -236,6 +240,8 @@ class TraccarServerCoordinator(DataUpdateCoordinator[TraccarServerCoordinatorDat """Subscribe to events.""" try: await self.client.subscribe(self.handle_subscription_data) + except TraccarAuthenticationException: + raise ConfigEntryAuthFailed from None except TraccarException as ex: if self._should_log_subscription_error: self._should_log_subscription_error = False diff --git a/homeassistant/components/traccar_server/strings.json b/homeassistant/components/traccar_server/strings.json index a4b57562388..89b7b180346 100644 --- a/homeassistant/components/traccar_server/strings.json +++ b/homeassistant/components/traccar_server/strings.json @@ -14,14 +14,23 @@ "host": "The hostname or IP address of your Traccar Server", "username": "The username (email) you use to log in to your Traccar Server" } + }, + "reauth_confirm": { + "description": "The authentication credentials for {host}:{port} need to be updated.", + "data": { + "username": "[%key:common::config_flow::data::username%]", + "password": "[%key:common::config_flow::data::password%]" + } } }, "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", - "unknown": "[%key:common::config_flow::error::unknown%]" + "unknown": "[%key:common::config_flow::error::unknown%]", + "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]" }, "abort": { - "already_configured": "[%key:common::config_flow::abort::already_configured_device%]" + "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", + "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]" } }, "options": { diff --git a/tests/components/traccar_server/test_config_flow.py b/tests/components/traccar_server/test_config_flow.py index 0418e4a5a72..7270a77fef1 100644 --- a/tests/components/traccar_server/test_config_flow.py +++ b/tests/components/traccar_server/test_config_flow.py @@ -4,7 +4,7 @@ from collections.abc import Generator from unittest.mock import AsyncMock import pytest -from pytraccar import TraccarException +from pytraccar import TraccarAuthenticationException, TraccarException from homeassistant import config_entries from homeassistant.components.traccar_server.const import ( @@ -175,3 +175,98 @@ async def test_abort_already_configured( assert result["type"] is FlowResultType.ABORT assert result["reason"] == "already_configured" + + +async def test_reauth_flow( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_traccar_api_client: Generator[AsyncMock], +) -> None: + """Test reauth flow.""" + mock_config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={ + "source": config_entries.SOURCE_REAUTH, + "entry_id": mock_config_entry.entry_id, + }, + data=mock_config_entry.data, + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "reauth_confirm" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_USERNAME: "new-username", + CONF_PASSWORD: "new-password", + }, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reauth_successful" + + # Verify the config entry was updated + assert mock_config_entry.data[CONF_USERNAME] == "new-username" + assert mock_config_entry.data[CONF_PASSWORD] == "new-password" + + +@pytest.mark.parametrize( + ("side_effect", "error"), + [ + (TraccarAuthenticationException, "invalid_auth"), + (TraccarException, "cannot_connect"), + (Exception, "unknown"), + ], +) +async def test_reauth_flow_errors( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_traccar_api_client: Generator[AsyncMock], + side_effect: Exception, + error: str, +) -> None: + """Test reauth flow with errors.""" + mock_config_entry.add_to_hass(hass) + assert await hass.config_entries.async_setup(mock_config_entry.entry_id) + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={ + "source": config_entries.SOURCE_REAUTH, + "entry_id": mock_config_entry.entry_id, + }, + data=mock_config_entry.data, + ) + + mock_traccar_api_client.get_server.side_effect = side_effect + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_USERNAME: "new-username", + CONF_PASSWORD: "new-password", + }, + ) + + assert result["type"] is FlowResultType.FORM + assert result["errors"] == {"base": error} + + # Test recovery after error + mock_traccar_api_client.get_server.side_effect = None + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + { + CONF_USERNAME: "new-username", + CONF_PASSWORD: "new-password", + }, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "reauth_successful" From 8d7e387b46f626e1fd680df7c358e3be2ccb3440 Mon Sep 17 00:00:00 2001 From: Norbert Rittel Date: Sun, 6 Jul 2025 11:23:57 +0200 Subject: [PATCH 083/106] Deduplicate strings in `nordpool` actions (#148258) --- homeassistant/components/nordpool/strings.json | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/nordpool/strings.json b/homeassistant/components/nordpool/strings.json index 06bd74e78a6..3494996af01 100644 --- a/homeassistant/components/nordpool/strings.json +++ b/homeassistant/components/nordpool/strings.json @@ -103,7 +103,7 @@ }, "date": { "name": "Date", - "description": "Only dates two months in the past and one day in the future is allowed." + "description": "Only dates in the range from two months in the past to one day in the future are allowed." }, "areas": { "name": "Areas", @@ -120,20 +120,20 @@ "description": "Retrieves the price indices for a specific date.", "fields": { "config_entry": { - "name": "Config entry", - "description": "The Nord Pool configuration entry for this action." + "name": "[%key:component::nordpool::services::get_prices_for_date::fields::config_entry::name%]", + "description": "[%key:component::nordpool::services::get_prices_for_date::fields::config_entry::description%]" }, "date": { - "name": "Date", - "description": "Only dates two months in the past and one day in the future is allowed." + "name": "[%key:component::nordpool::services::get_prices_for_date::fields::date::name%]", + "description": "[%key:component::nordpool::services::get_prices_for_date::fields::date::description%]" }, "areas": { - "name": "Areas", - "description": "One or multiple areas to get prices for. If left empty it will use the areas already configured." + "name": "[%key:component::nordpool::services::get_prices_for_date::fields::areas::name%]", + "description": "[%key:component::nordpool::services::get_prices_for_date::fields::areas::description%]" }, "currency": { - "name": "Currency", - "description": "Currency to get prices in. If left empty it will use the currency already configured." + "name": "[%key:component::nordpool::services::get_prices_for_date::fields::currency::name%]", + "description": "[%key:component::nordpool::services::get_prices_for_date::fields::currency::description%]" }, "resolution": { "name": "Resolution", From 1b11ac912350628bf52c0cb5bd979fc5f8b8a58a Mon Sep 17 00:00:00 2001 From: Markus Adrario Date: Sun, 6 Jul 2025 12:05:43 +0200 Subject: [PATCH 084/106] Add Homee general tests (#137128) --- .../fixtures/cover_with_position_slats.json | 49 +++++++ .../fixtures/cover_without_position.json | 21 +++ .../homee/snapshots/test_diagnostics.ambr | 49 +++++++ .../components/homee/snapshots/test_init.ambr | 71 ++++++++++ tests/components/homee/test_cover.py | 57 ++++++++ tests/components/homee/test_init.py | 131 ++++++++++++++++++ tests/components/homee/test_sensor.py | 50 ++++++- 7 files changed, 427 insertions(+), 1 deletion(-) create mode 100644 tests/components/homee/snapshots/test_init.ambr create mode 100644 tests/components/homee/test_init.py diff --git a/tests/components/homee/fixtures/cover_with_position_slats.json b/tests/components/homee/fixtures/cover_with_position_slats.json index 8fd0d6f44fe..a61be87ab9f 100644 --- a/tests/components/homee/fixtures/cover_with_position_slats.json +++ b/tests/components/homee/fixtures/cover_with_position_slats.json @@ -96,6 +96,55 @@ "options": { "automations": ["step"] } + }, + { + "id": 4, + "node_id": 3, + "instance": 0, + "minimum": -50, + "maximum": 125, + "current_value": 20.3, + "target_value": 20.3, + "last_value": 20.3, + "unit": "°C", + "step_value": 1.0, + "editable": 0, + "type": 5, + "state": 1, + "last_changed": 1709982925, + "changed_by": 1, + "changed_by_id": 0, + "based_on": 1, + "data": "", + "name": "", + "options": { + "history": { + "day": 1, + "week": 26, + "month": 6 + } + } + }, + { + "id": 5, + "node_id": 3, + "instance": 0, + "minimum": 0, + "maximum": 0, + "current_value": 0.0, + "target_value": 0.0, + "last_value": 0.0, + "unit": "text", + "step_value": 1.0, + "editable": 0, + "type": 44, + "state": 1, + "last_changed": 0, + "changed_by": 1, + "changed_by_id": 0, + "based_on": 1, + "data": "4.54", + "name": "" } ] } diff --git a/tests/components/homee/fixtures/cover_without_position.json b/tests/components/homee/fixtures/cover_without_position.json index e2bc6c7a38d..f6e9ea19c8a 100644 --- a/tests/components/homee/fixtures/cover_without_position.json +++ b/tests/components/homee/fixtures/cover_without_position.json @@ -43,6 +43,27 @@ "observes": [75], "automations": ["toggle"] } + }, + { + "id": 2, + "node_id": 3, + "instance": 0, + "minimum": 0, + "maximum": 0, + "current_value": 0.0, + "target_value": 0.0, + "last_value": 0.0, + "unit": "text", + "step_value": 1.0, + "editable": 0, + "type": 45, + "state": 1, + "last_changed": 0, + "changed_by": 1, + "changed_by_id": 0, + "based_on": 1, + "data": "1.45", + "name": "" } ] } diff --git a/tests/components/homee/snapshots/test_diagnostics.ambr b/tests/components/homee/snapshots/test_diagnostics.ambr index 76d3f426e17..d934c4e225e 100644 --- a/tests/components/homee/snapshots/test_diagnostics.ambr +++ b/tests/components/homee/snapshots/test_diagnostics.ambr @@ -689,6 +689,55 @@ 'type': 113, 'unit': '°', }), + dict({ + 'based_on': 1, + 'changed_by': 1, + 'changed_by_id': 0, + 'current_value': 20.3, + 'data': '', + 'editable': 0, + 'id': 4, + 'instance': 0, + 'last_changed': 1709982925, + 'last_value': 20.3, + 'maximum': 125, + 'minimum': -50, + 'name': '', + 'node_id': 3, + 'options': dict({ + 'history': dict({ + 'day': 1, + 'month': 6, + 'week': 26, + }), + }), + 'state': 1, + 'step_value': 1.0, + 'target_value': 20.3, + 'type': 5, + 'unit': '°C', + }), + dict({ + 'based_on': 1, + 'changed_by': 1, + 'changed_by_id': 0, + 'current_value': 0.0, + 'data': '4.54', + 'editable': 0, + 'id': 5, + 'instance': 0, + 'last_changed': 0, + 'last_value': 0.0, + 'maximum': 0, + 'minimum': 0, + 'name': '', + 'node_id': 3, + 'state': 1, + 'step_value': 1.0, + 'target_value': 0.0, + 'type': 44, + 'unit': 'text', + }), ]), 'cube_type': 14, 'favorite': 0, diff --git a/tests/components/homee/snapshots/test_init.ambr b/tests/components/homee/snapshots/test_init.ambr new file mode 100644 index 00000000000..664740dbeac --- /dev/null +++ b/tests/components/homee/snapshots/test_init.ambr @@ -0,0 +1,71 @@ +# serializer version: 1 +# name: test_general_data + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'config_entries_subentries': , + 'configuration_url': None, + 'connections': set({ + tuple( + 'mac', + '00:05:55:11:ee:cc', + ), + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'homee', + '00055511EECC', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'homee', + 'model': 'homee', + 'model_id': None, + 'name': 'TestHomee', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'suggested_area': None, + 'sw_version': '1.2.3', + 'via_device_id': None, + }) +# --- +# name: test_general_data.1 + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'config_entries_subentries': , + 'configuration_url': None, + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'homee', + '00055511EECC-3', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': None, + 'model': 'shutter_position_switch', + 'model_id': None, + 'name': 'Test Cover', + 'name_by_user': None, + 'primary_config_entry': , + 'serial_number': None, + 'suggested_area': None, + 'sw_version': '4.54', + 'via_device_id': , + }) +# --- diff --git a/tests/components/homee/test_cover.py b/tests/components/homee/test_cover.py index a3e26abc52a..4f215c683a2 100644 --- a/tests/components/homee/test_cover.py +++ b/tests/components/homee/test_cover.py @@ -13,6 +13,10 @@ from homeassistant.components.cover import ( CoverEntityFeature, CoverState, ) +from homeassistant.components.homeassistant import ( + DOMAIN as HA_DOMAIN, + SERVICE_UPDATE_ENTITY, +) from homeassistant.components.homee.const import DOMAIN from homeassistant.const import ( ATTR_ENTITY_ID, @@ -23,9 +27,11 @@ from homeassistant.const import ( SERVICE_SET_COVER_POSITION, SERVICE_SET_COVER_TILT_POSITION, SERVICE_STOP_COVER, + STATE_UNAVAILABLE, ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError +from homeassistant.setup import async_setup_component from . import build_mock_node, setup_integration @@ -39,6 +45,7 @@ async def test_open_close_stop_cover( ) -> None: """Test opening the cover.""" mock_homee.nodes = [build_mock_node("cover_with_position_slats.json")] + mock_homee.get_node_by_id.return_value = mock_homee.nodes[0] await setup_integration(hass, mock_config_entry) @@ -73,6 +80,7 @@ async def test_open_close_reverse_cover( ) -> None: """Test opening the cover.""" mock_homee.nodes = [build_mock_node("cover_with_position_slats.json")] + mock_homee.get_node_by_id.return_value = mock_homee.nodes[0] mock_homee.nodes[0].attributes[0].is_reversed = True await setup_integration(hass, mock_config_entry) @@ -102,6 +110,7 @@ async def test_set_cover_position( ) -> None: """Test setting the cover position.""" mock_homee.nodes = [build_mock_node("cover_with_position_slats.json")] + mock_homee.get_node_by_id.return_value = mock_homee.nodes[0] await setup_integration(hass, mock_config_entry) @@ -246,6 +255,7 @@ async def test_cover_positions( # Cover open, tilt open. # mock_homee.nodes = [cover] mock_homee.nodes = [build_mock_node("cover_with_position_slats.json")] + mock_homee.get_node_by_id.return_value = mock_homee.nodes[0] cover = mock_homee.nodes[0] await setup_integration(hass, mock_config_entry) @@ -348,3 +358,50 @@ async def test_send_error( assert exc_info.value.translation_domain == DOMAIN assert exc_info.value.translation_key == "connection_closed" + + +async def test_node_entity_connection_listener( + hass: HomeAssistant, + mock_homee: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test if loss of connection is sensed correctly.""" + mock_homee.nodes = [build_mock_node("cover_with_position_slats.json")] + mock_homee.get_node_by_id.return_value = mock_homee.nodes[0] + await setup_integration(hass, mock_config_entry) + + states = hass.states.get("cover.test_cover") + assert states.state != STATE_UNAVAILABLE + + await mock_homee.add_connection_listener.call_args_list[1][0][0](False) + await hass.async_block_till_done() + + states = hass.states.get("cover.test_cover") + assert states.state == STATE_UNAVAILABLE + + await mock_homee.add_connection_listener.call_args_list[1][0][0](True) + await hass.async_block_till_done() + + states = hass.states.get("cover.test_cover") + assert states.state != STATE_UNAVAILABLE + + +async def test_node_entity_update_action( + hass: HomeAssistant, + mock_homee: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the update_entity action for a HomeeEntity.""" + mock_homee.nodes = [build_mock_node("cover_with_position_slats.json")] + mock_homee.get_node_by_id.return_value = mock_homee.nodes[0] + await setup_integration(hass, mock_config_entry) + await async_setup_component(hass, HA_DOMAIN, {}) + + await hass.services.async_call( + HA_DOMAIN, + SERVICE_UPDATE_ENTITY, + {ATTR_ENTITY_ID: "cover.test_cover"}, + blocking=True, + ) + + mock_homee.update_node.assert_called_once_with(3) diff --git a/tests/components/homee/test_init.py b/tests/components/homee/test_init.py new file mode 100644 index 00000000000..0b2ae21a8d0 --- /dev/null +++ b/tests/components/homee/test_init.py @@ -0,0 +1,131 @@ +"""Test Homee initialization.""" + +from unittest.mock import MagicMock + +from pyHomee import HomeeAuthFailedException, HomeeConnectionFailedException +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.homee.const import DOMAIN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant +from homeassistant.helpers import device_registry as dr + +from . import build_mock_node, setup_integration +from .conftest import HOMEE_ID + +from tests.common import MockConfigEntry + + +@pytest.mark.parametrize( + "side_eff", + [ + HomeeConnectionFailedException("connection timed out"), + HomeeAuthFailedException("wrong username or password"), + ], +) +async def test_connection_errors( + hass: HomeAssistant, + mock_homee: MagicMock, + mock_config_entry: MockConfigEntry, + side_eff: Exception, +) -> None: + """Test if connection errors on startup are handled correctly.""" + mock_homee.get_access_token.side_effect = side_eff + mock_config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + + assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY + + +async def test_connection_listener( + hass: HomeAssistant, + mock_homee: MagicMock, + mock_config_entry: MockConfigEntry, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test if loss of connection is sensed correctly.""" + mock_homee.nodes = [build_mock_node("homee.json")] + mock_homee.get_node_by_id.return_value = mock_homee.nodes[0] + await setup_integration(hass, mock_config_entry) + + await mock_homee.add_connection_listener.call_args_list[0][0][0](False) + await hass.async_block_till_done() + assert "Disconnected from Homee" in caplog.text + await mock_homee.add_connection_listener.call_args_list[0][0][0](True) + await hass.async_block_till_done() + assert "Reconnected to Homee" in caplog.text + + +async def test_general_data( + hass: HomeAssistant, + mock_homee: MagicMock, + mock_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test if data is set correctly.""" + mock_homee.nodes = [ + build_mock_node("cover_with_position_slats.json"), + build_mock_node("homee.json"), + ] + mock_homee.get_node_by_id = ( + lambda node_id: mock_homee.nodes[0] if node_id == 3 else mock_homee.nodes[1] + ) + await setup_integration(hass, mock_config_entry) + + # Verify hub and device created correctly using snapshots. + hub = device_registry.async_get_device(identifiers={(DOMAIN, f"{HOMEE_ID}")}) + device = device_registry.async_get_device(identifiers={(DOMAIN, f"{HOMEE_ID}-3")}) + + assert hub == snapshot + assert device == snapshot + + +async def test_software_version( + hass: HomeAssistant, + mock_homee: MagicMock, + mock_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test sw_version for device with only AttributeType.SOFTWARE_VERSION.""" + mock_homee.nodes = [build_mock_node("cover_without_position.json")] + await setup_integration(hass, mock_config_entry) + + device = device_registry.async_get_device(identifiers={(DOMAIN, f"{HOMEE_ID}-3")}) + assert device.sw_version == "1.45" + + +async def test_invalid_profile( + hass: HomeAssistant, + mock_homee: MagicMock, + mock_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test unknown value passed to get_name_for_enum.""" + mock_homee.nodes = [build_mock_node("cover_without_position.json")] + # This is a profile, that does not exist in the enum. + mock_homee.nodes[0].profile = 77 + await setup_integration(hass, mock_config_entry) + + device = device_registry.async_get_device(identifiers={(DOMAIN, f"{HOMEE_ID}-3")}) + assert device.model is None + + +async def test_unload_entry( + hass: HomeAssistant, + mock_homee: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test unloading of config entry.""" + mock_homee.nodes = [build_mock_node("cover_with_position_slats.json")] + mock_homee.get_node_by_id.return_value = mock_homee.nodes[0] + await setup_integration(hass, mock_config_entry) + + assert mock_config_entry.state is ConfigEntryState.LOADED + + await hass.config_entries.async_unload(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.NOT_LOADED diff --git a/tests/components/homee/test_sensor.py b/tests/components/homee/test_sensor.py index 1d4ad4b0f66..b51b3a23b75 100644 --- a/tests/components/homee/test_sensor.py +++ b/tests/components/homee/test_sensor.py @@ -5,6 +5,10 @@ from unittest.mock import MagicMock, patch import pytest from syrupy.assertion import SnapshotAssertion +from homeassistant.components.homeassistant import ( + DOMAIN as HA_DOMAIN, + SERVICE_UPDATE_ENTITY, +) from homeassistant.components.homee.const import ( DOMAIN, OPEN_CLOSE_MAP, @@ -13,9 +17,10 @@ from homeassistant.components.homee.const import ( WINDOW_MAP_REVERSED, ) from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN -from homeassistant.const import Platform +from homeassistant.const import ATTR_ENTITY_ID, STATE_UNAVAILABLE, Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er, issue_registry as ir +from homeassistant.setup import async_setup_component from . import async_update_attribute_value, build_mock_node, setup_integration from .conftest import HOMEE_ID @@ -168,6 +173,49 @@ async def test_sensor_deprecation_unused_entity( ) +async def test_entity_connection_listener( + hass: HomeAssistant, + mock_homee: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test if loss of connection is sensed correctly.""" + await setup_sensor(hass, mock_homee, mock_config_entry) + + states = hass.states.get("sensor.test_multisensor_energy_1") + assert states.state is not STATE_UNAVAILABLE + + await mock_homee.add_connection_listener.call_args_list[2][0][0](False) + await hass.async_block_till_done() + + states = hass.states.get("sensor.test_multisensor_energy_1") + assert states.state is STATE_UNAVAILABLE + + await mock_homee.add_connection_listener.call_args_list[2][0][0](True) + await hass.async_block_till_done() + + states = hass.states.get("sensor.test_multisensor_energy_1") + assert states.state is not STATE_UNAVAILABLE + + +async def test_entity_update_action( + hass: HomeAssistant, + mock_homee: MagicMock, + mock_config_entry: MockConfigEntry, +) -> None: + """Test the update_entity action for a HomeeEntity.""" + await setup_sensor(hass, mock_homee, mock_config_entry) + await async_setup_component(hass, HA_DOMAIN, {}) + + await hass.services.async_call( + HA_DOMAIN, + SERVICE_UPDATE_ENTITY, + {ATTR_ENTITY_ID: "sensor.test_multisensor_temperature"}, + blocking=True, + ) + + mock_homee.update_attribute.assert_called_once_with(1, 23) + + async def test_sensor_snapshot( hass: HomeAssistant, mock_homee: MagicMock, From 4ee930507d01b7969bc1e8eb2ba056dcba455cd4 Mon Sep 17 00:00:00 2001 From: Norbert Rittel Date: Sun, 6 Jul 2025 12:11:44 +0200 Subject: [PATCH 085/106] Fix typo in `wrong_hub` abort message of `homee` (#148261) --- homeassistant/components/homee/strings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/homee/strings.json b/homeassistant/components/homee/strings.json index 9523d62c671..267d5553a8c 100644 --- a/homeassistant/components/homee/strings.json +++ b/homeassistant/components/homee/strings.json @@ -5,7 +5,7 @@ "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "reauth_successful": "[%key:common::config_flow::abort::reauth_successful%]", "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]", - "wrong_hub": "IP-Address belongs to a different homee than the configured one." + "wrong_hub": "IP address belongs to a different homee than the configured one." }, "error": { "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", From 0e7a4c91bf585899a2089558154ff923aecb6ba7 Mon Sep 17 00:00:00 2001 From: starkillerOG Date: Sun, 6 Jul 2025 12:38:57 +0200 Subject: [PATCH 086/106] bump motionblinds to 0.6.29 (#148265) --- homeassistant/components/motion_blinds/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/motion_blinds/manifest.json b/homeassistant/components/motion_blinds/manifest.json index a82da20396f..eca520d8946 100644 --- a/homeassistant/components/motion_blinds/manifest.json +++ b/homeassistant/components/motion_blinds/manifest.json @@ -21,5 +21,5 @@ "documentation": "https://www.home-assistant.io/integrations/motion_blinds", "iot_class": "local_push", "loggers": ["motionblinds"], - "requirements": ["motionblinds==0.6.28"] + "requirements": ["motionblinds==0.6.29"] } diff --git a/requirements_all.txt b/requirements_all.txt index 01b297fd250..dd5376baa03 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1452,7 +1452,7 @@ monzopy==1.4.2 mopeka-iot-ble==0.8.0 # homeassistant.components.motion_blinds -motionblinds==0.6.28 +motionblinds==0.6.29 # homeassistant.components.motionblinds_ble motionblindsble==0.1.3 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 29ab2676ee0..58a4f6a221d 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1244,7 +1244,7 @@ monzopy==1.4.2 mopeka-iot-ble==0.8.0 # homeassistant.components.motion_blinds -motionblinds==0.6.28 +motionblinds==0.6.29 # homeassistant.components.motionblinds_ble motionblindsble==0.1.3 From 075efb469a4267ddc2755ef6535012922528de3d Mon Sep 17 00:00:00 2001 From: Robin Thoni Date: Sun, 6 Jul 2025 13:08:27 +0200 Subject: [PATCH 087/106] Bump sfrbox-api to 0.0.12 (#148259) Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --- homeassistant/components/sfr_box/manifest.json | 2 +- homeassistant/components/sfr_box/strings.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/sfr_box/manifest.json b/homeassistant/components/sfr_box/manifest.json index a2d65e9819d..1987453a80d 100644 --- a/homeassistant/components/sfr_box/manifest.json +++ b/homeassistant/components/sfr_box/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/sfr_box", "integration_type": "device", "iot_class": "local_polling", - "requirements": ["sfrbox-api==0.0.11"] + "requirements": ["sfrbox-api==0.0.12"] } diff --git a/homeassistant/components/sfr_box/strings.json b/homeassistant/components/sfr_box/strings.json index 35e9b1869ff..5139ec52bad 100644 --- a/homeassistant/components/sfr_box/strings.json +++ b/homeassistant/components/sfr_box/strings.json @@ -27,7 +27,7 @@ "host": "[%key:common::config_flow::data::host%]" }, "data_description": { - "host": "The hostname or IP address of your SFR device." + "host": "The hostname, IP address, or full URL of your SFR device. e.g.: '192.168.1.1' or 'https://sfrbox.example.com'" }, "description": "Setting the credentials is optional, but enables additional functionality." } diff --git a/requirements_all.txt b/requirements_all.txt index dd5376baa03..3e3c701508c 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2753,7 +2753,7 @@ sensoterra==2.0.1 sentry-sdk==1.45.1 # homeassistant.components.sfr_box -sfrbox-api==0.0.11 +sfrbox-api==0.0.12 # homeassistant.components.sharkiq sharkiq==1.1.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 58a4f6a221d..03f77b69b93 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -2275,7 +2275,7 @@ sensoterra==2.0.1 sentry-sdk==1.45.1 # homeassistant.components.sfr_box -sfrbox-api==0.0.11 +sfrbox-api==0.0.12 # homeassistant.components.sharkiq sharkiq==1.1.0 From 8cb9cadce9b3ceb6ea7054f343ec7a51b5f0467f Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 6 Jul 2025 15:15:38 +0200 Subject: [PATCH 088/106] Extract files_to_prompt from Gemini action (#148203) Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Allen Porter --- .../__init__.py | 57 +++----------- .../entity.py | 74 +++++++++++++++++++ .../snapshots/test_init.ambr | 4 +- .../test_init.py | 11 ++- 4 files changed, 92 insertions(+), 54 deletions(-) diff --git a/homeassistant/components/google_generative_ai_conversation/__init__.py b/homeassistant/components/google_generative_ai_conversation/__init__.py index 99e475a376b..a3b87c05e5a 100644 --- a/homeassistant/components/google_generative_ai_conversation/__init__.py +++ b/homeassistant/components/google_generative_ai_conversation/__init__.py @@ -2,15 +2,12 @@ from __future__ import annotations -import asyncio from functools import partial -import mimetypes from pathlib import Path from types import MappingProxyType from google.genai import Client from google.genai.errors import APIError, ClientError -from google.genai.types import File, FileState from requests.exceptions import Timeout import voluptuous as vol @@ -42,13 +39,13 @@ from .const import ( DEFAULT_TITLE, DEFAULT_TTS_NAME, DOMAIN, - FILE_POLLING_INTERVAL_SECONDS, LOGGER, RECOMMENDED_AI_TASK_OPTIONS, RECOMMENDED_CHAT_MODEL, RECOMMENDED_TTS_OPTIONS, TIMEOUT_MILLIS, ) +from .entity import async_prepare_files_for_prompt SERVICE_GENERATE_CONTENT = "generate_content" CONF_IMAGE_FILENAME = "image_filename" @@ -92,58 +89,22 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: client = config_entry.runtime_data - def append_files_to_prompt(): - image_filenames = call.data[CONF_IMAGE_FILENAME] - filenames = call.data[CONF_FILENAMES] - for filename in set(image_filenames + filenames): + files = call.data[CONF_IMAGE_FILENAME] + call.data[CONF_FILENAMES] + + if files: + for filename in files: if not hass.config.is_allowed_path(filename): raise HomeAssistantError( f"Cannot read `{filename}`, no access to path; " "`allowlist_external_dirs` may need to be adjusted in " "`configuration.yaml`" ) - if not Path(filename).exists(): - raise HomeAssistantError(f"`{filename}` does not exist") - mimetype = mimetypes.guess_type(filename)[0] - with open(filename, "rb") as file: - uploaded_file = client.files.upload( - file=file, config={"mime_type": mimetype} - ) - prompt_parts.append(uploaded_file) - async def wait_for_file_processing(uploaded_file: File) -> None: - """Wait for file processing to complete.""" - while True: - uploaded_file = await client.aio.files.get( - name=uploaded_file.name, - config={"http_options": {"timeout": TIMEOUT_MILLIS}}, + prompt_parts.extend( + await async_prepare_files_for_prompt( + hass, client, [Path(filename) for filename in files] ) - if uploaded_file.state not in ( - FileState.STATE_UNSPECIFIED, - FileState.PROCESSING, - ): - break - LOGGER.debug( - "Waiting for file `%s` to be processed, current state: %s", - uploaded_file.name, - uploaded_file.state, - ) - await asyncio.sleep(FILE_POLLING_INTERVAL_SECONDS) - - if uploaded_file.state == FileState.FAILED: - raise HomeAssistantError( - f"File `{uploaded_file.name}` processing failed, reason: {uploaded_file.error.message}" - ) - - await hass.async_add_executor_job(append_files_to_prompt) - - tasks = [ - asyncio.create_task(wait_for_file_processing(part)) - for part in prompt_parts - if isinstance(part, File) and part.state != FileState.ACTIVE - ] - async with asyncio.timeout(TIMEOUT_MILLIS / 1000): - await asyncio.gather(*tasks) + ) try: response = await client.aio.models.generate_content( diff --git a/homeassistant/components/google_generative_ai_conversation/entity.py b/homeassistant/components/google_generative_ai_conversation/entity.py index d471da36a8c..8f8edea18cb 100644 --- a/homeassistant/components/google_generative_ai_conversation/entity.py +++ b/homeassistant/components/google_generative_ai_conversation/entity.py @@ -2,15 +2,21 @@ from __future__ import annotations +import asyncio import codecs from collections.abc import AsyncGenerator, Callable from dataclasses import replace +import mimetypes +from pathlib import Path from typing import Any, cast +from google.genai import Client from google.genai.errors import APIError, ClientError from google.genai.types import ( AutomaticFunctionCallingConfig, Content, + File, + FileState, FunctionDeclaration, GenerateContentConfig, GenerateContentResponse, @@ -26,6 +32,7 @@ from voluptuous_openapi import convert from homeassistant.components import conversation from homeassistant.config_entries import ConfigEntry, ConfigSubentry +from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import device_registry as dr, llm from homeassistant.helpers.entity import Entity @@ -42,6 +49,7 @@ from .const import ( CONF_TOP_P, CONF_USE_GOOGLE_SEARCH_TOOL, DOMAIN, + FILE_POLLING_INTERVAL_SECONDS, LOGGER, RECOMMENDED_CHAT_MODEL, RECOMMENDED_HARM_BLOCK_THRESHOLD, @@ -49,6 +57,7 @@ from .const import ( RECOMMENDED_TEMPERATURE, RECOMMENDED_TOP_K, RECOMMENDED_TOP_P, + TIMEOUT_MILLIS, ) # Max number of back and forth with the LLM to generate a response @@ -494,3 +503,68 @@ class GoogleGenerativeAILLMBaseEntity(Entity): ), ], ) + + +async def async_prepare_files_for_prompt( + hass: HomeAssistant, client: Client, files: list[Path] +) -> list[File]: + """Append files to a prompt. + + Caller needs to ensure that the files are allowed. + """ + + def upload_files() -> list[File]: + prompt_parts: list[File] = [] + for filename in files: + if not filename.exists(): + raise HomeAssistantError(f"`{filename}` does not exist") + mimetype = mimetypes.guess_type(filename)[0] + prompt_parts.append( + client.files.upload( + file=filename, + config={ + "mime_type": mimetype, + "display_name": filename.name, + }, + ) + ) + return prompt_parts + + async def wait_for_file_processing(uploaded_file: File) -> None: + """Wait for file processing to complete.""" + first = True + while uploaded_file.state in ( + FileState.STATE_UNSPECIFIED, + FileState.PROCESSING, + ): + if first: + first = False + else: + LOGGER.debug( + "Waiting for file `%s` to be processed, current state: %s", + uploaded_file.name, + uploaded_file.state, + ) + await asyncio.sleep(FILE_POLLING_INTERVAL_SECONDS) + + uploaded_file = await client.aio.files.get( + name=uploaded_file.name, + config={"http_options": {"timeout": TIMEOUT_MILLIS}}, + ) + + if uploaded_file.state == FileState.FAILED: + raise HomeAssistantError( + f"File `{uploaded_file.name}` processing failed, reason: {uploaded_file.error.message}" + ) + + prompt_parts = await hass.async_add_executor_job(upload_files) + + tasks = [ + asyncio.create_task(wait_for_file_processing(part)) + for part in prompt_parts + if part.state != FileState.ACTIVE + ] + async with asyncio.timeout(TIMEOUT_MILLIS / 1000): + await asyncio.gather(*tasks) + + return prompt_parts diff --git a/tests/components/google_generative_ai_conversation/snapshots/test_init.ambr b/tests/components/google_generative_ai_conversation/snapshots/test_init.ambr index a2603328959..a0d34f49d37 100644 --- a/tests/components/google_generative_ai_conversation/snapshots/test_init.ambr +++ b/tests/components/google_generative_ai_conversation/snapshots/test_init.ambr @@ -122,8 +122,8 @@ dict({ 'contents': list([ 'Describe this image from my doorbell camera', - b'some file', - b'some file', + File(name='doorbell_snapshot.jpg', display_name=None, mime_type=None, size_bytes=None, create_time=None, expiration_time=None, update_time=None, sha256_hash=None, uri=None, download_uri=None, state=, source=None, video_metadata=None, error=None), + File(name='context.txt', display_name=None, mime_type=None, size_bytes=None, create_time=None, expiration_time=None, update_time=None, sha256_hash=None, uri=None, download_uri=None, state=, source=None, video_metadata=None, error=None), ]), 'model': 'models/gemini-2.5-flash', }), diff --git a/tests/components/google_generative_ai_conversation/test_init.py b/tests/components/google_generative_ai_conversation/test_init.py index c0a610f6a0a..351895c89fb 100644 --- a/tests/components/google_generative_ai_conversation/test_init.py +++ b/tests/components/google_generative_ai_conversation/test_init.py @@ -80,7 +80,10 @@ async def test_generate_content_service_with_image( ) as mock_generate, patch( "google.genai.files.Files.upload", - return_value=b"some file", + side_effect=[ + File(name="doorbell_snapshot.jpg", state=FileState.ACTIVE), + File(name="context.txt", state=FileState.ACTIVE), + ], ), patch("pathlib.Path.exists", return_value=True), patch.object(hass.config, "is_allowed_path", return_value=True), @@ -92,7 +95,7 @@ async def test_generate_content_service_with_image( "generate_content", { "prompt": "Describe this image from my doorbell camera", - "filenames": ["doorbell_snapshot.jpg", "context.txt", "context.txt"], + "filenames": ["doorbell_snapshot.jpg", "context.txt"], }, blocking=True, return_response=True, @@ -146,7 +149,7 @@ async def test_generate_content_file_processing_succeeds( "generate_content", { "prompt": "Describe this image from my doorbell camera", - "filenames": ["doorbell_snapshot.jpg", "context.txt", "context.txt"], + "filenames": ["doorbell_snapshot.jpg", "context.txt"], }, blocking=True, return_response=True, @@ -208,7 +211,7 @@ async def test_generate_content_file_processing_fails( "generate_content", { "prompt": "Describe this image from my doorbell camera", - "filenames": ["doorbell_snapshot.jpg", "context.txt", "context.txt"], + "filenames": ["doorbell_snapshot.jpg", "context.txt"], }, blocking=True, return_response=True, From 4b5c04b2f03e1052f061583c959a07305bf4ba7f Mon Sep 17 00:00:00 2001 From: Allen Porter Date: Sun, 6 Jul 2025 07:56:37 -0700 Subject: [PATCH 089/106] Add AI Task support in Ollama (#148226) Co-authored-by: Paulus Schoutsen --- homeassistant/components/ollama/__init__.py | 29 ++- homeassistant/components/ollama/ai_task.py | 77 ++++++ .../components/ollama/config_flow.py | 80 ++++-- homeassistant/components/ollama/const.py | 7 + homeassistant/components/ollama/entity.py | 14 + homeassistant/components/ollama/strings.json | 38 +++ tests/components/ollama/__init__.py | 5 + tests/components/ollama/conftest.py | 12 +- tests/components/ollama/test_ai_task.py | 245 ++++++++++++++++++ tests/components/ollama/test_config_flow.py | 75 ++++++ tests/components/ollama/test_init.py | 98 ++++++- 11 files changed, 635 insertions(+), 45 deletions(-) create mode 100644 homeassistant/components/ollama/ai_task.py create mode 100644 tests/components/ollama/test_ai_task.py diff --git a/homeassistant/components/ollama/__init__.py b/homeassistant/components/ollama/__init__.py index 6fe4720d13f..e16550c1e94 100644 --- a/homeassistant/components/ollama/__init__.py +++ b/homeassistant/components/ollama/__init__.py @@ -28,6 +28,7 @@ from .const import ( CONF_NUM_CTX, CONF_PROMPT, CONF_THINK, + DEFAULT_AI_TASK_NAME, DEFAULT_NAME, DEFAULT_TIMEOUT, DOMAIN, @@ -47,7 +48,7 @@ __all__ = [ ] CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) -PLATFORMS = (Platform.CONVERSATION,) +PLATFORMS = (Platform.AI_TASK, Platform.CONVERSATION) type OllamaConfigEntry = ConfigEntry[ollama.AsyncClient] @@ -118,6 +119,7 @@ async def async_migrate_integration(hass: HomeAssistant) -> None: parent_entry = api_keys_entries[entry.data[CONF_URL]] hass.config_entries.async_add_subentry(parent_entry, subentry) + conversation_entity = entity_registry.async_get_entity_id( "conversation", DOMAIN, @@ -208,6 +210,31 @@ async def async_migrate_entry(hass: HomeAssistant, entry: OllamaConfigEntry) -> minor_version=1, ) + if entry.version == 3 and entry.minor_version == 1: + # Add AI Task subentry with default options. We can only create a new + # subentry if we can find an existing model in the entry. The model + # was removed in the previous migration step, so we need to + # check the subentries for an existing model. + existing_model = next( + iter( + model + for subentry in entry.subentries.values() + if (model := subentry.data.get(CONF_MODEL)) is not None + ), + None, + ) + if existing_model: + hass.config_entries.async_add_subentry( + entry, + ConfigSubentry( + data=MappingProxyType({CONF_MODEL: existing_model}), + subentry_type="ai_task_data", + title=DEFAULT_AI_TASK_NAME, + unique_id=None, + ), + ) + hass.config_entries.async_update_entry(entry, minor_version=2) + _LOGGER.debug( "Migration to version %s:%s successful", entry.version, entry.minor_version ) diff --git a/homeassistant/components/ollama/ai_task.py b/homeassistant/components/ollama/ai_task.py new file mode 100644 index 00000000000..d796b28aac8 --- /dev/null +++ b/homeassistant/components/ollama/ai_task.py @@ -0,0 +1,77 @@ +"""AI Task integration for Ollama.""" + +from __future__ import annotations + +from json import JSONDecodeError +import logging + +from homeassistant.components import ai_task, conversation +from homeassistant.config_entries import ConfigEntry +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback +from homeassistant.util.json import json_loads + +from .entity import OllamaBaseLLMEntity + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up AI Task entities.""" + for subentry in config_entry.subentries.values(): + if subentry.subentry_type != "ai_task_data": + continue + + async_add_entities( + [OllamaTaskEntity(config_entry, subentry)], + config_subentry_id=subentry.subentry_id, + ) + + +class OllamaTaskEntity( + ai_task.AITaskEntity, + OllamaBaseLLMEntity, +): + """Ollama AI Task entity.""" + + _attr_supported_features = ai_task.AITaskEntityFeature.GENERATE_DATA + + async def _async_generate_data( + self, + task: ai_task.GenDataTask, + chat_log: conversation.ChatLog, + ) -> ai_task.GenDataTaskResult: + """Handle a generate data task.""" + await self._async_handle_chat_log(chat_log, task.structure) + + if not isinstance(chat_log.content[-1], conversation.AssistantContent): + raise HomeAssistantError( + "Last content in chat log is not an AssistantContent" + ) + + text = chat_log.content[-1].content or "" + + if not task.structure: + return ai_task.GenDataTaskResult( + conversation_id=chat_log.conversation_id, + data=text, + ) + try: + data = json_loads(text) + except JSONDecodeError as err: + _LOGGER.error( + "Failed to parse JSON response: %s. Response: %s", + err, + text, + ) + raise HomeAssistantError("Error with Ollama structured response") from err + + return ai_task.GenDataTaskResult( + conversation_id=chat_log.conversation_id, + data=data, + ) diff --git a/homeassistant/components/ollama/config_flow.py b/homeassistant/components/ollama/config_flow.py index 49eb12a5c23..cca917f6c29 100644 --- a/homeassistant/components/ollama/config_flow.py +++ b/homeassistant/components/ollama/config_flow.py @@ -46,6 +46,8 @@ from .const import ( CONF_NUM_CTX, CONF_PROMPT, CONF_THINK, + DEFAULT_AI_TASK_NAME, + DEFAULT_CONVERSATION_NAME, DEFAULT_KEEP_ALIVE, DEFAULT_MAX_HISTORY, DEFAULT_MODEL, @@ -74,7 +76,7 @@ class OllamaConfigFlow(ConfigFlow, domain=DOMAIN): """Handle a config flow for Ollama.""" VERSION = 3 - MINOR_VERSION = 1 + MINOR_VERSION = 2 def __init__(self) -> None: """Initialize config flow.""" @@ -136,11 +138,14 @@ class OllamaConfigFlow(ConfigFlow, domain=DOMAIN): cls, config_entry: ConfigEntry ) -> dict[str, type[ConfigSubentryFlow]]: """Return subentries supported by this integration.""" - return {"conversation": ConversationSubentryFlowHandler} + return { + "conversation": OllamaSubentryFlowHandler, + "ai_task_data": OllamaSubentryFlowHandler, + } -class ConversationSubentryFlowHandler(ConfigSubentryFlow): - """Flow for managing conversation subentries.""" +class OllamaSubentryFlowHandler(ConfigSubentryFlow): + """Flow for managing Ollama subentries.""" def __init__(self) -> None: """Initialize the subentry flow.""" @@ -201,7 +206,11 @@ class ConversationSubentryFlowHandler(ConfigSubentryFlow): step_id="set_options", data_schema=vol.Schema( ollama_config_option_schema( - self.hass, self._is_new, options, models_to_list + self.hass, + self._is_new, + self._subentry_type, + options, + models_to_list, ) ), ) @@ -300,13 +309,19 @@ class ConversationSubentryFlowHandler(ConfigSubentryFlow): def ollama_config_option_schema( hass: HomeAssistant, is_new: bool, + subentry_type: str, options: Mapping[str, Any], models_to_list: list[SelectOptionDict], ) -> dict: """Ollama options schema.""" if is_new: + if subentry_type == "ai_task_data": + default_name = DEFAULT_AI_TASK_NAME + else: + default_name = DEFAULT_CONVERSATION_NAME + schema: dict = { - vol.Required(CONF_NAME, default="Ollama Conversation"): str, + vol.Required(CONF_NAME, default=default_name): str, } else: schema = {} @@ -319,29 +334,38 @@ def ollama_config_option_schema( ): SelectSelector( SelectSelectorConfig(options=models_to_list, custom_value=True) ), - vol.Optional( - CONF_PROMPT, - description={ - "suggested_value": options.get( - CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT - ) - }, - ): TemplateSelector(), - vol.Optional( - CONF_LLM_HASS_API, - description={"suggested_value": options.get(CONF_LLM_HASS_API)}, - ): SelectSelector( - SelectSelectorConfig( - options=[ - SelectOptionDict( - label=api.name, - value=api.id, + } + ) + if subentry_type == "conversation": + schema.update( + { + vol.Optional( + CONF_PROMPT, + description={ + "suggested_value": options.get( + CONF_PROMPT, llm.DEFAULT_INSTRUCTIONS_PROMPT ) - for api in llm.async_get_apis(hass) - ], - multiple=True, - ) - ), + }, + ): TemplateSelector(), + vol.Optional( + CONF_LLM_HASS_API, + description={"suggested_value": options.get(CONF_LLM_HASS_API)}, + ): SelectSelector( + SelectSelectorConfig( + options=[ + SelectOptionDict( + label=api.name, + value=api.id, + ) + for api in llm.async_get_apis(hass) + ], + multiple=True, + ) + ), + } + ) + schema.update( + { vol.Optional( CONF_NUM_CTX, description={ diff --git a/homeassistant/components/ollama/const.py b/homeassistant/components/ollama/const.py index 3175525c70d..7e80570bd5e 100644 --- a/homeassistant/components/ollama/const.py +++ b/homeassistant/components/ollama/const.py @@ -159,3 +159,10 @@ MODEL_NAMES = [ # https://ollama.com/library "zephyr", ] DEFAULT_MODEL = "llama3.2:latest" + +DEFAULT_CONVERSATION_NAME = "Ollama Conversation" +DEFAULT_AI_TASK_NAME = "Ollama AI Task" + +RECOMMENDED_CONVERSATION_OPTIONS = { + CONF_MAX_HISTORY: DEFAULT_MAX_HISTORY, +} diff --git a/homeassistant/components/ollama/entity.py b/homeassistant/components/ollama/entity.py index 7b63b1dff00..4122d0c67d8 100644 --- a/homeassistant/components/ollama/entity.py +++ b/homeassistant/components/ollama/entity.py @@ -8,6 +8,7 @@ import logging from typing import Any import ollama +import voluptuous as vol from voluptuous_openapi import convert from homeassistant.components import conversation @@ -180,6 +181,7 @@ class OllamaBaseLLMEntity(Entity): async def _async_handle_chat_log( self, chat_log: conversation.ChatLog, + structure: vol.Schema | None = None, ) -> None: """Generate an answer for the chat log.""" settings = {**self.entry.data, **self.subentry.data} @@ -200,6 +202,17 @@ class OllamaBaseLLMEntity(Entity): max_messages = int(settings.get(CONF_MAX_HISTORY, DEFAULT_MAX_HISTORY)) self._trim_history(message_history, max_messages) + output_format: dict[str, Any] | None = None + if structure: + output_format = convert( + structure, + custom_serializer=( + chat_log.llm_api.custom_serializer + if chat_log.llm_api + else llm.selector_serializer + ), + ) + # Get response # To prevent infinite loops, we limit the number of iterations for _iteration in range(MAX_TOOL_ITERATIONS): @@ -214,6 +227,7 @@ class OllamaBaseLLMEntity(Entity): keep_alive=f"{settings.get(CONF_KEEP_ALIVE, DEFAULT_KEEP_ALIVE)}s", options={CONF_NUM_CTX: settings.get(CONF_NUM_CTX, DEFAULT_NUM_CTX)}, think=settings.get(CONF_THINK), + format=output_format, ) except (ollama.RequestError, ollama.ResponseError) as err: _LOGGER.error("Unexpected error talking to Ollama server: %s", err) diff --git a/homeassistant/components/ollama/strings.json b/homeassistant/components/ollama/strings.json index bb08e4a4684..4261b2286bf 100644 --- a/homeassistant/components/ollama/strings.json +++ b/homeassistant/components/ollama/strings.json @@ -55,6 +55,44 @@ "progress": { "download": "Please wait while the model is downloaded, which may take a very long time. Check your Ollama server logs for more details." } + }, + "ai_task_data": { + "initiate_flow": { + "user": "Add Generate data with AI service", + "reconfigure": "Reconfigure Generate data with AI service" + }, + "entry_type": "Generate data with AI service", + "step": { + "set_options": { + "data": { + "model": "[%key:component::ollama::config_subentries::conversation::step::set_options::data::model%]", + "name": "[%key:common::config_flow::data::name%]", + "prompt": "[%key:component::ollama::config_subentries::conversation::step::set_options::data::prompt%]", + "max_history": "[%key:component::ollama::config_subentries::conversation::step::set_options::data::max_history%]", + "num_ctx": "[%key:component::ollama::config_subentries::conversation::step::set_options::data::num_ctx%]", + "keep_alive": "[%key:component::ollama::config_subentries::conversation::step::set_options::data::keep_alive%]", + "think": "[%key:component::ollama::config_subentries::conversation::step::set_options::data::think%]" + }, + "data_description": { + "prompt": "[%key:component::ollama::config_subentries::conversation::step::set_options::data_description::prompt%]", + "keep_alive": "[%key:component::ollama::config_subentries::conversation::step::set_options::data_description::keep_alive%]", + "num_ctx": "[%key:component::ollama::config_subentries::conversation::step::set_options::data_description::num_ctx%]", + "think": "[%key:component::ollama::config_subentries::conversation::step::set_options::data_description::think%]" + } + }, + "download": { + "title": "[%key:component::ollama::config_subentries::conversation::step::download::title%]" + } + }, + "abort": { + "reconfigure_successful": "[%key:common::config_flow::abort::reconfigure_successful%]", + "entry_not_loaded": "[%key:component::ollama::config_subentries::conversation::abort::entry_not_loaded%]", + "download_failed": "[%key:component::ollama::config_subentries::conversation::abort::download_failed%]", + "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]" + }, + "progress": { + "download": "[%key:component::ollama::config_subentries::conversation::progress::download%]" + } } } } diff --git a/tests/components/ollama/__init__.py b/tests/components/ollama/__init__.py index 92db3b13304..9e7ae4772d4 100644 --- a/tests/components/ollama/__init__.py +++ b/tests/components/ollama/__init__.py @@ -12,3 +12,8 @@ TEST_OPTIONS = { ollama.CONF_MAX_HISTORY: 2, ollama.CONF_MODEL: "test_model:latest", } + +TEST_AI_TASK_OPTIONS = { + ollama.CONF_MAX_HISTORY: 2, + ollama.CONF_MODEL: "test_model:latest", +} diff --git a/tests/components/ollama/conftest.py b/tests/components/ollama/conftest.py index 552e7dee20a..f3406bf5566 100644 --- a/tests/components/ollama/conftest.py +++ b/tests/components/ollama/conftest.py @@ -11,7 +11,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import llm from homeassistant.setup import async_setup_component -from . import TEST_OPTIONS, TEST_USER_DATA +from . import TEST_AI_TASK_OPTIONS, TEST_OPTIONS, TEST_USER_DATA from tests.common import MockConfigEntry @@ -31,14 +31,20 @@ def mock_config_entry( domain=ollama.DOMAIN, data=TEST_USER_DATA, version=3, - minor_version=1, + minor_version=2, subentries_data=[ { "data": {**TEST_OPTIONS, **mock_config_entry_options}, "subentry_type": "conversation", "title": "Ollama Conversation", "unique_id": None, - } + }, + { + "data": TEST_AI_TASK_OPTIONS, + "subentry_type": "ai_task_data", + "title": "Ollama AI Task", + "unique_id": None, + }, ], ) entry.add_to_hass(hass) diff --git a/tests/components/ollama/test_ai_task.py b/tests/components/ollama/test_ai_task.py new file mode 100644 index 00000000000..ee812e7b316 --- /dev/null +++ b/tests/components/ollama/test_ai_task.py @@ -0,0 +1,245 @@ +"""Test AI Task platform of Ollama integration.""" + +from unittest.mock import patch + +import pytest +import voluptuous as vol + +from homeassistant.components import ai_task +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import entity_registry as er, selector + +from tests.common import MockConfigEntry + + +@pytest.mark.usefixtures("mock_init_component") +async def test_generate_data( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test AI Task data generation.""" + entity_id = "ai_task.ollama_ai_task" + + # Ensure entity is linked to the subentry + entity_entry = entity_registry.async_get(entity_id) + ai_task_entry = next( + iter( + entry + for entry in mock_config_entry.subentries.values() + if entry.subentry_type == "ai_task_data" + ) + ) + assert entity_entry is not None + assert entity_entry.config_entry_id == mock_config_entry.entry_id + assert entity_entry.config_subentry_id == ai_task_entry.subentry_id + + # Mock the Ollama chat response as an async iterator + async def mock_chat_response(): + """Mock streaming response.""" + yield { + "message": {"role": "assistant", "content": "Generated test data"}, + "done": True, + "done_reason": "stop", + } + + with patch( + "ollama.AsyncClient.chat", + return_value=mock_chat_response(), + ): + result = await ai_task.async_generate_data( + hass, + task_name="Test Task", + entity_id=entity_id, + instructions="Generate test data", + ) + + assert result.data == "Generated test data" + + +@pytest.mark.usefixtures("mock_init_component") +async def test_run_task_with_streaming( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test AI Task data generation with streaming response.""" + entity_id = "ai_task.ollama_ai_task" + + async def mock_stream(): + """Mock streaming response.""" + yield {"message": {"role": "assistant", "content": "Stream "}} + yield { + "message": {"role": "assistant", "content": "response"}, + "done": True, + "done_reason": "stop", + } + + with patch( + "ollama.AsyncClient.chat", + return_value=mock_stream(), + ): + result = await ai_task.async_generate_data( + hass, + task_name="Test Streaming Task", + entity_id=entity_id, + instructions="Generate streaming data", + ) + + assert result.data == "Stream response" + + +@pytest.mark.usefixtures("mock_init_component") +async def test_run_task_connection_error( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test AI Task with connection error.""" + entity_id = "ai_task.ollama_ai_task" + + with ( + patch( + "ollama.AsyncClient.chat", + side_effect=Exception("Connection failed"), + ), + pytest.raises(Exception, match="Connection failed"), + ): + await ai_task.async_generate_data( + hass, + task_name="Test Error Task", + entity_id=entity_id, + instructions="Generate data that will fail", + ) + + +@pytest.mark.usefixtures("mock_init_component") +async def test_run_task_empty_response( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test AI Task with empty response.""" + entity_id = "ai_task.ollama_ai_task" + + # Mock response with space (minimally non-empty) + async def mock_minimal_response(): + """Mock minimal streaming response.""" + yield { + "message": {"role": "assistant", "content": " "}, + "done": True, + "done_reason": "stop", + } + + with patch( + "ollama.AsyncClient.chat", + return_value=mock_minimal_response(), + ): + result = await ai_task.async_generate_data( + hass, + task_name="Test Minimal Task", + entity_id=entity_id, + instructions="Generate minimal data", + ) + + assert result.data == " " + + +@pytest.mark.usefixtures("mock_init_component") +async def test_generate_structured_data( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test AI Task data generation.""" + entity_id = "ai_task.ollama_ai_task" + + # Mock the Ollama chat response as an async iterator + async def mock_chat_response(): + """Mock streaming response.""" + yield { + "message": { + "role": "assistant", + "content": '{"characters": ["Mario", "Luigi"]}', + }, + "done": True, + "done_reason": "stop", + } + + with patch( + "ollama.AsyncClient.chat", + return_value=mock_chat_response(), + ) as mock_chat: + result = await ai_task.async_generate_data( + hass, + task_name="Test Task", + entity_id=entity_id, + instructions="Generate test data", + structure=vol.Schema( + { + vol.Required("characters"): selector.selector( + { + "text": { + "multiple": True, + } + } + ) + }, + ), + ) + assert result.data == {"characters": ["Mario", "Luigi"]} + + assert mock_chat.call_count == 1 + assert mock_chat.call_args[1]["format"] == { + "type": "object", + "properties": {"characters": {"items": {"type": "string"}, "type": "array"}}, + "required": ["characters"], + } + + +@pytest.mark.usefixtures("mock_init_component") +async def test_generate_invalid_structured_data( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, +) -> None: + """Test AI Task data generation.""" + entity_id = "ai_task.ollama_ai_task" + + # Mock the Ollama chat response as an async iterator + async def mock_chat_response(): + """Mock streaming response.""" + yield { + "message": { + "role": "assistant", + "content": "INVALID JSON RESPONSE", + }, + "done": True, + "done_reason": "stop", + } + + with ( + patch( + "ollama.AsyncClient.chat", + return_value=mock_chat_response(), + ), + pytest.raises(HomeAssistantError), + ): + await ai_task.async_generate_data( + hass, + task_name="Test Task", + entity_id=entity_id, + instructions="Generate test data", + structure=vol.Schema( + { + vol.Required("characters"): selector.selector( + { + "text": { + "multiple": True, + } + } + ) + }, + ), + ) diff --git a/tests/components/ollama/test_config_flow.py b/tests/components/ollama/test_config_flow.py index 7372a460c95..1a873c2adb7 100644 --- a/tests/components/ollama/test_config_flow.py +++ b/tests/components/ollama/test_config_flow.py @@ -461,3 +461,78 @@ async def test_subentry_reconfigure_with_download( ollama.CONF_NUM_CTX: 8192.0, ollama.CONF_THINK: True, } + + +async def test_creating_ai_task_subentry( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_init_component, +) -> None: + """Test creating an AI task subentry.""" + old_subentries = set(mock_config_entry.subentries) + # Original conversation + original ai_task + assert len(mock_config_entry.subentries) == 2 + + with patch( + "ollama.AsyncClient.list", + return_value={"models": [{"model": "test_model:latest"}]}, + ): + result = await hass.config_entries.subentries.async_init( + (mock_config_entry.entry_id, "ai_task_data"), + context={"source": config_entries.SOURCE_USER}, + ) + + assert result.get("type") is FlowResultType.FORM + assert result.get("step_id") == "set_options" + assert not result.get("errors") + + with patch( + "ollama.AsyncClient.list", + return_value={"models": [{"model": "test_model:latest"}]}, + ): + result2 = await hass.config_entries.subentries.async_configure( + result["flow_id"], + { + "name": "Custom AI Task", + ollama.CONF_MODEL: "test_model:latest", + ollama.CONF_MAX_HISTORY: 5, + ollama.CONF_NUM_CTX: 4096, + ollama.CONF_KEEP_ALIVE: 30, + ollama.CONF_THINK: False, + }, + ) + await hass.async_block_till_done() + + assert result2.get("type") is FlowResultType.CREATE_ENTRY + assert result2.get("title") == "Custom AI Task" + assert result2.get("data") == { + ollama.CONF_MODEL: "test_model:latest", + ollama.CONF_MAX_HISTORY: 5, + ollama.CONF_NUM_CTX: 4096, + ollama.CONF_KEEP_ALIVE: 30, + ollama.CONF_THINK: False, + } + + assert ( + len(mock_config_entry.subentries) == 3 + ) # Original conversation + original ai_task + new ai_task + + new_subentry_id = list(set(mock_config_entry.subentries) - old_subentries)[0] + new_subentry = mock_config_entry.subentries[new_subentry_id] + assert new_subentry.subentry_type == "ai_task_data" + assert new_subentry.title == "Custom AI Task" + + +async def test_ai_task_subentry_not_loaded( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, +) -> None: + """Test creating an AI task subentry when entry is not loaded.""" + # Don't call mock_init_component to simulate not loaded state + result = await hass.config_entries.subentries.async_init( + (mock_config_entry.entry_id, "ai_task_data"), + context={"source": config_entries.SOURCE_USER}, + ) + + assert result.get("type") is FlowResultType.ABORT + assert result.get("reason") == "entry_not_loaded" diff --git a/tests/components/ollama/test_init.py b/tests/components/ollama/test_init.py index c7cd78fca9a..1db57302704 100644 --- a/tests/components/ollama/test_init.py +++ b/tests/components/ollama/test_init.py @@ -93,16 +93,23 @@ async def test_migration_from_v1( return_value=True, ): await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() assert mock_config_entry.version == 3 - assert mock_config_entry.minor_version == 1 + assert mock_config_entry.minor_version == 2 # After migration, parent entry should only have URL assert mock_config_entry.data == {ollama.CONF_URL: "http://localhost:11434"} assert mock_config_entry.options == {} - assert len(mock_config_entry.subentries) == 1 + assert len(mock_config_entry.subentries) == 2 - subentry = next(iter(mock_config_entry.subentries.values())) + subentry = next( + iter( + entry + for entry in mock_config_entry.subentries.values() + if entry.subentry_type == "conversation" + ) + ) assert subentry.unique_id is None assert subentry.title == "llama-3.2-8b" assert subentry.subentry_type == "conversation" @@ -110,6 +117,18 @@ async def test_migration_from_v1( expected_subentry_data = TEST_OPTIONS.copy() assert subentry.data == expected_subentry_data + # Find the AI Task subentry + ai_task_subentry = next( + iter( + entry + for entry in mock_config_entry.subentries.values() + if entry.subentry_type == "ai_task_data" + ) + ) + assert ai_task_subentry.unique_id is None + assert ai_task_subentry.title == "Ollama AI Task" + assert ai_task_subentry.subentry_type == "ai_task_data" + migrated_entity = entity_registry.async_get(entity.entity_id) assert migrated_entity is not None assert migrated_entity.config_entry_id == mock_config_entry.entry_id @@ -204,10 +223,17 @@ async def test_migration_from_v1_with_multiple_urls( for idx, entry in enumerate(entries): assert entry.version == 3 - assert entry.minor_version == 1 + assert entry.minor_version == 2 assert not entry.options - assert len(entry.subentries) == 1 - subentry = list(entry.subentries.values())[0] + assert len(entry.subentries) == 2 + + subentry = next( + iter( + subentry + for subentry in entry.subentries.values() + if subentry.subentry_type == "conversation" + ) + ) assert subentry.subentry_type == "conversation" # Subentry should include the model along with the original options expected_subentry_data = TEST_OPTIONS.copy() @@ -215,6 +241,17 @@ async def test_migration_from_v1_with_multiple_urls( assert subentry.data == expected_subentry_data assert subentry.title == f"Ollama {idx + 1}" + # Find the AI Task subentry + ai_task_subentry = next( + iter( + subentry + for subentry in entry.subentries.values() + if subentry.subentry_type == "ai_task_data" + ) + ) + assert ai_task_subentry.subentry_type == "ai_task_data" + assert ai_task_subentry.title == "Ollama AI Task" + dev = device_registry.async_get_device( identifiers={(DOMAIN, list(entry.subentries.values())[0].subentry_id)} ) @@ -295,9 +332,10 @@ async def test_migration_from_v1_with_same_urls( entry = entries[0] assert entry.version == 3 - assert entry.minor_version == 1 + assert entry.minor_version == 2 assert not entry.options - assert len(entry.subentries) == 2 # Two subentries from the two original entries + # Two conversation subentries from the two original entries and 1 aitask subentry + assert len(entry.subentries) == 3 # Check both subentries exist with correct data subentries = list(entry.subentries.values()) @@ -305,7 +343,11 @@ async def test_migration_from_v1_with_same_urls( assert "Ollama" in titles assert "Ollama 2" in titles - for subentry in subentries: + conversation_subentries = [ + subentry for subentry in subentries if subentry.subentry_type == "conversation" + ] + assert len(conversation_subentries) == 2 + for subentry in conversation_subentries: assert subentry.subentry_type == "conversation" # Subentry should include the model along with the original options expected_subentry_data = TEST_OPTIONS.copy() @@ -415,10 +457,10 @@ async def test_migration_from_v2_1( assert len(entries) == 1 entry = entries[0] assert entry.version == 3 - assert entry.minor_version == 1 + assert entry.minor_version == 2 assert not entry.options assert entry.title == "Ollama" - assert len(entry.subentries) == 2 + assert len(entry.subentries) == 3 conversation_subentries = [ subentry for subentry in entry.subentries.values() @@ -504,14 +546,44 @@ async def test_migration_from_v2_2(hass: HomeAssistant) -> None: # Check migration to v3.1 assert mock_config_entry.version == 3 - assert mock_config_entry.minor_version == 1 + assert mock_config_entry.minor_version == 2 # Check that model was moved from main data to subentry assert mock_config_entry.data == {ollama.CONF_URL: "http://localhost:11434"} - assert len(mock_config_entry.subentries) == 1 + assert len(mock_config_entry.subentries) == 2 subentry = next(iter(mock_config_entry.subentries.values())) assert subentry.data == { **V21_TEST_USER_DATA, ollama.CONF_MODEL: "test_model:latest", } + + +async def test_migration_from_v3_1_without_subentry(hass: HomeAssistant) -> None: + """Test migration from version 3.1 where there is no existing subentry. + + This exercises the code path where the model is not moved to a subentry + because the subentry does not exist, which is a scenario that can happen + if the user created the config entry without adding a subentry, or + if the user manually removed the subentry after the migration to v3.1. + """ + mock_config_entry = MockConfigEntry( + domain=DOMAIN, + data={ + ollama.CONF_MODEL: "test_model:latest", + }, + version=3, + minor_version=1, + ) + mock_config_entry.add_to_hass(hass) + + with patch( + "homeassistant.components.ollama.async_setup_entry", + return_value=True, + ): + await hass.config_entries.async_setup(mock_config_entry.entry_id) + + assert mock_config_entry.version == 3 + assert mock_config_entry.minor_version == 2 + + assert next(iter(mock_config_entry.subentries.values()), None) is None From 404d17efca95e05b7ed5d8e7b65173faf8ea8927 Mon Sep 17 00:00:00 2001 From: karwosts <32912880+karwosts@users.noreply.github.com> Date: Sun, 6 Jul 2025 09:36:38 -0700 Subject: [PATCH 090/106] Translate number selector unit for utility_meter (#148276) --- homeassistant/components/utility_meter/config_flow.py | 1 + homeassistant/components/utility_meter/strings.json | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/homeassistant/components/utility_meter/config_flow.py b/homeassistant/components/utility_meter/config_flow.py index e8acca88cbe..db7cea6ecf2 100644 --- a/homeassistant/components/utility_meter/config_flow.py +++ b/homeassistant/components/utility_meter/config_flow.py @@ -94,6 +94,7 @@ CONFIG_SCHEMA = vol.Schema( max=28, mode=selector.NumberSelectorMode.BOX, unit_of_measurement="days", + translation_key=CONF_METER_OFFSET, ), ), vol.Required(CONF_TARIFFS, default=[]): selector.SelectSelector( diff --git a/homeassistant/components/utility_meter/strings.json b/homeassistant/components/utility_meter/strings.json index aadc0f82412..0ba7ad85050 100644 --- a/homeassistant/components/utility_meter/strings.json +++ b/homeassistant/components/utility_meter/strings.json @@ -58,6 +58,11 @@ "quarterly": "Quarterly", "yearly": "Yearly" } + }, + "offset": { + "unit_of_measurement": { + "days": "days" + } } }, "services": { From 699c60f2937168f857e80469ad9dd7a7d2ae9e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joakim=20S=C3=B8rensen?= Date: Sun, 6 Jul 2025 18:06:27 +0100 Subject: [PATCH 091/106] Add the current version to the starting log to aid troubleshooting (#148271) --- homeassistant/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index c5d4ca79371..469acd5dae8 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -532,7 +532,7 @@ class HomeAssistant: This method is a coroutine. """ - _LOGGER.info("Starting Home Assistant") + _LOGGER.info("Starting Home Assistant %s", __version__) self.set_state(CoreState.starting) self.bus.async_fire_internal(EVENT_CORE_CONFIG_UPDATE) From 008e2a3d10070e7ad5ac93ff679a9836cbc6ab8d Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sun, 6 Jul 2025 19:33:41 +0200 Subject: [PATCH 092/106] Add attachment support to AI task (#148120) --- homeassistant/components/ai_task/__init__.py | 7 ++- homeassistant/components/ai_task/const.py | 4 ++ .../components/ai_task/manifest.json | 2 +- .../components/ai_task/services.yaml | 6 ++ homeassistant/components/ai_task/strings.json | 4 ++ homeassistant/components/ai_task/task.py | 45 +++++++++++++- tests/components/ai_task/conftest.py | 4 +- tests/components/ai_task/test_init.py | 59 +++++++++++++++---- tests/components/ai_task/test_task.py | 37 ++++++++++-- 9 files changed, 147 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/ai_task/__init__.py b/homeassistant/components/ai_task/__init__.py index 95c080cc472..a472b0db131 100644 --- a/homeassistant/components/ai_task/__init__.py +++ b/homeassistant/components/ai_task/__init__.py @@ -20,6 +20,7 @@ from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.typing import UNDEFINED, ConfigType, UndefinedType from .const import ( + ATTR_ATTACHMENTS, ATTR_INSTRUCTIONS, ATTR_REQUIRED, ATTR_STRUCTURE, @@ -32,7 +33,7 @@ from .const import ( ) from .entity import AITaskEntity from .http import async_setup as async_setup_http -from .task import GenDataTask, GenDataTaskResult, async_generate_data +from .task import GenDataTask, GenDataTaskResult, PlayMediaWithId, async_generate_data __all__ = [ "DOMAIN", @@ -40,6 +41,7 @@ __all__ = [ "AITaskEntityFeature", "GenDataTask", "GenDataTaskResult", + "PlayMediaWithId", "async_generate_data", "async_setup", "async_setup_entry", @@ -92,6 +94,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: vol.Schema({str: STRUCTURE_FIELD_SCHEMA}), _validate_structure_fields, ), + vol.Optional(ATTR_ATTACHMENTS): vol.All( + cv.ensure_list, [selector.MediaSelector({"accept": ["*/*"]})] + ), } ), supports_response=SupportsResponse.ONLY, diff --git a/homeassistant/components/ai_task/const.py b/homeassistant/components/ai_task/const.py index fa8702ed69e..09948e9b673 100644 --- a/homeassistant/components/ai_task/const.py +++ b/homeassistant/components/ai_task/const.py @@ -23,6 +23,7 @@ ATTR_INSTRUCTIONS: Final = "instructions" ATTR_TASK_NAME: Final = "task_name" ATTR_STRUCTURE: Final = "structure" ATTR_REQUIRED: Final = "required" +ATTR_ATTACHMENTS: Final = "attachments" DEFAULT_SYSTEM_PROMPT = ( "You are a Home Assistant expert and help users with their tasks." @@ -34,3 +35,6 @@ class AITaskEntityFeature(IntFlag): GENERATE_DATA = 1 """Generate data based on instructions.""" + + SUPPORT_ATTACHMENTS = 2 + """Support attachments with generate data.""" diff --git a/homeassistant/components/ai_task/manifest.json b/homeassistant/components/ai_task/manifest.json index c685410530d..c3e33e7d411 100644 --- a/homeassistant/components/ai_task/manifest.json +++ b/homeassistant/components/ai_task/manifest.json @@ -2,7 +2,7 @@ "domain": "ai_task", "name": "AI Task", "codeowners": ["@home-assistant/core"], - "dependencies": ["conversation"], + "dependencies": ["conversation", "media_source"], "documentation": "https://www.home-assistant.io/integrations/ai_task", "integration_type": "system", "quality_scale": "internal" diff --git a/homeassistant/components/ai_task/services.yaml b/homeassistant/components/ai_task/services.yaml index d55b0e60fac..4298ab62a07 100644 --- a/homeassistant/components/ai_task/services.yaml +++ b/homeassistant/components/ai_task/services.yaml @@ -23,3 +23,9 @@ generate_data: example: '{ "name": { "selector": { "text": }, "description": "Name of the user", "required": "True" } } }, "age": { "selector": { "number": }, "description": "Age of the user" } }' selector: object: + attachments: + required: false + selector: + media: + accept: + - "*" diff --git a/homeassistant/components/ai_task/strings.json b/homeassistant/components/ai_task/strings.json index 92106c3baca..261381b7c31 100644 --- a/homeassistant/components/ai_task/strings.json +++ b/homeassistant/components/ai_task/strings.json @@ -19,6 +19,10 @@ "structure": { "name": "Structured output", "description": "When set, the AI Task will output fields with this in structure. The structure is a dictionary where the keys are the field names and the values contain a 'description', a 'selector', and an optional 'required' field." + }, + "attachments": { + "name": "Attachments", + "description": "List of files to attach for multi-modal AI analysis." } } } diff --git a/homeassistant/components/ai_task/task.py b/homeassistant/components/ai_task/task.py index b6defbfad31..72d1018210c 100644 --- a/homeassistant/components/ai_task/task.py +++ b/homeassistant/components/ai_task/task.py @@ -2,17 +2,30 @@ from __future__ import annotations -from dataclasses import dataclass +from dataclasses import dataclass, fields from typing import Any import voluptuous as vol +from homeassistant.components import media_source from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError from .const import DATA_COMPONENT, DATA_PREFERENCES, AITaskEntityFeature +@dataclass(slots=True) +class PlayMediaWithId(media_source.PlayMedia): + """Play media with a media content ID.""" + + media_content_id: str + """Media source ID to play.""" + + def __str__(self) -> str: + """Return media source ID as a string.""" + return f"" + + async def async_generate_data( hass: HomeAssistant, *, @@ -20,6 +33,7 @@ async def async_generate_data( entity_id: str | None = None, instructions: str, structure: vol.Schema | None = None, + attachments: list[dict] | None = None, ) -> GenDataTaskResult: """Run a task in the AI Task integration.""" if entity_id is None: @@ -37,11 +51,37 @@ async def async_generate_data( f"AI Task entity {entity_id} does not support generating data" ) + # Resolve attachments + resolved_attachments: list[PlayMediaWithId] | None = None + + if attachments: + if AITaskEntityFeature.SUPPORT_ATTACHMENTS not in entity.supported_features: + raise HomeAssistantError( + f"AI Task entity {entity_id} does not support attachments" + ) + + resolved_attachments = [] + + for attachment in attachments: + media = await media_source.async_resolve_media( + hass, attachment["media_content_id"], None + ) + resolved_attachments.append( + PlayMediaWithId( + **{ + field.name: getattr(media, field.name) + for field in fields(media) + }, + media_content_id=attachment["media_content_id"], + ) + ) + return await entity.internal_async_generate_data( GenDataTask( name=task_name, instructions=instructions, structure=structure, + attachments=resolved_attachments, ) ) @@ -59,6 +99,9 @@ class GenDataTask: structure: vol.Schema | None = None """Optional structure for the data to be generated.""" + attachments: list[PlayMediaWithId] | None = None + """List of attachments to go along the instructions.""" + def __str__(self) -> str: """Return task as a string.""" return f"" diff --git a/tests/components/ai_task/conftest.py b/tests/components/ai_task/conftest.py index e80e70ddaed..05d34b15ddc 100644 --- a/tests/components/ai_task/conftest.py +++ b/tests/components/ai_task/conftest.py @@ -35,7 +35,9 @@ class MockAITaskEntity(AITaskEntity): """Mock AI Task entity for testing.""" _attr_name = "Test Task Entity" - _attr_supported_features = AITaskEntityFeature.GENERATE_DATA + _attr_supported_features = ( + AITaskEntityFeature.GENERATE_DATA | AITaskEntityFeature.SUPPORT_ATTACHMENTS + ) def __init__(self) -> None: """Initialize the mock entity.""" diff --git a/tests/components/ai_task/test_init.py b/tests/components/ai_task/test_init.py index d32b09adec5..840285493ac 100644 --- a/tests/components/ai_task/test_init.py +++ b/tests/components/ai_task/test_init.py @@ -1,11 +1,13 @@ """Test initialization of the AI Task component.""" from typing import Any +from unittest.mock import patch from freezegun.api import FrozenDateTimeFactory import pytest import voluptuous as vol +from homeassistant.components import media_source from homeassistant.components.ai_task import AITaskPreferences from homeassistant.components.ai_task.const import DATA_PREFERENCES from homeassistant.core import HomeAssistant @@ -58,7 +60,15 @@ async def test_preferences_storage_load( ), ( {}, - {"entity_id": TEST_ENTITY_ID}, + { + "entity_id": TEST_ENTITY_ID, + "attachments": [ + { + "media_content_id": "media-source://mock/blah_blah_blah.mp4", + "media_content_type": "video/mp4", + } + ], + }, ), ], ) @@ -68,25 +78,50 @@ async def test_generate_data_service( freezer: FrozenDateTimeFactory, set_preferences: dict[str, str | None], msg_extra: dict[str, str], + mock_ai_task_entity: MockAITaskEntity, ) -> None: """Test the generate data service.""" preferences = hass.data[DATA_PREFERENCES] preferences.async_set_preferences(**set_preferences) - result = await hass.services.async_call( - "ai_task", - "generate_data", - { - "task_name": "Test Name", - "instructions": "Test prompt", - } - | msg_extra, - blocking=True, - return_response=True, - ) + with patch( + "homeassistant.components.media_source.async_resolve_media", + return_value=media_source.PlayMedia( + url="http://example.com/media.mp4", + mime_type="video/mp4", + ), + ): + result = await hass.services.async_call( + "ai_task", + "generate_data", + { + "task_name": "Test Name", + "instructions": "Test prompt", + } + | msg_extra, + blocking=True, + return_response=True, + ) assert result["data"] == "Mock result" + assert len(mock_ai_task_entity.mock_generate_data_tasks) == 1 + task = mock_ai_task_entity.mock_generate_data_tasks[0] + + assert len(task.attachments or []) == len( + msg_attachments := msg_extra.get("attachments", []) + ) + + for msg_attachment, attachment in zip( + msg_attachments, task.attachments or [], strict=False + ): + assert attachment.url == "http://example.com/media.mp4" + assert attachment.mime_type == "video/mp4" + assert attachment.media_content_id == msg_attachment["media_content_id"] + assert ( + str(attachment) == f"" + ) + async def test_generate_data_service_structure_fields( hass: HomeAssistant, diff --git a/tests/components/ai_task/test_task.py b/tests/components/ai_task/test_task.py index bed760c8a1d..b11d96823cc 100644 --- a/tests/components/ai_task/test_task.py +++ b/tests/components/ai_task/test_task.py @@ -16,13 +16,13 @@ from .conftest import TEST_ENTITY_ID, MockAITaskEntity from tests.typing import WebSocketGenerator -async def test_run_task_preferred_entity( +async def test_generate_data_preferred_entity( hass: HomeAssistant, init_components: None, mock_ai_task_entity: MockAITaskEntity, hass_ws_client: WebSocketGenerator, ) -> None: - """Test running a task with an unknown entity.""" + """Test generating data with entity via preferences.""" client = await hass_ws_client(hass) with pytest.raises( @@ -90,11 +90,11 @@ async def test_run_task_preferred_entity( ) -async def test_run_data_task_unknown_entity( +async def test_generate_data_unknown_entity( hass: HomeAssistant, init_components: None, ) -> None: - """Test running a data task with an unknown entity.""" + """Test generating data with an unknown entity.""" with pytest.raises( HomeAssistantError, match="AI Task entity ai_task.unknown_entity not found" @@ -113,7 +113,7 @@ async def test_run_data_task_updates_chat_log( init_components: None, snapshot: SnapshotAssertion, ) -> None: - """Test that running a data task updates the chat log.""" + """Test that generating data updates the chat log.""" result = await async_generate_data( hass, task_name="Test Task", @@ -127,3 +127,30 @@ async def test_run_data_task_updates_chat_log( async_get_chat_log(hass, session) as chat_log, ): assert chat_log.content == snapshot + + +async def test_generate_data_attachments_not_supported( + hass: HomeAssistant, + init_components: None, + mock_ai_task_entity: MockAITaskEntity, +) -> None: + """Test generating data with attachments when entity doesn't support them.""" + # Remove attachment support from the entity + mock_ai_task_entity._attr_supported_features = AITaskEntityFeature.GENERATE_DATA + + with pytest.raises( + HomeAssistantError, + match="AI Task entity ai_task.test_task_entity does not support attachments", + ): + await async_generate_data( + hass, + task_name="Test Task", + entity_id=TEST_ENTITY_ID, + instructions="Test prompt", + attachments=[ + { + "media_content_id": "media-source://mock/test.mp4", + "media_content_type": "video/mp4", + } + ], + ) From 2ea20ee2ab892c31ad0f8aecd86b9af5aa9d4784 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 6 Jul 2025 12:40:19 -0500 Subject: [PATCH 093/106] Fix UTF-8 encoding for REST basic authentication (#148225) --- homeassistant/components/rest/data.py | 2 +- tests/components/rest/test_binary_sensor.py | 33 +++++++++++++++++++++ tests/test_util/aiohttp.py | 3 ++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/rest/data.py b/homeassistant/components/rest/data.py index 731d1ffe9c3..c5dcd0a73a5 100644 --- a/homeassistant/components/rest/data.py +++ b/homeassistant/components/rest/data.py @@ -49,7 +49,7 @@ class RestData: # Convert auth tuple to aiohttp.BasicAuth if needed if isinstance(auth, tuple) and len(auth) == 2: self._auth: aiohttp.BasicAuth | aiohttp.DigestAuthMiddleware | None = ( - aiohttp.BasicAuth(auth[0], auth[1]) + aiohttp.BasicAuth(auth[0], auth[1], encoding="utf-8") ) else: self._auth = auth diff --git a/tests/components/rest/test_binary_sensor.py b/tests/components/rest/test_binary_sensor.py index 315f8113309..af7503a7007 100644 --- a/tests/components/rest/test_binary_sensor.py +++ b/tests/components/rest/test_binary_sensor.py @@ -667,3 +667,36 @@ async def test_availability_blocks_value_template( await hass.async_block_till_done() assert error in caplog.text + + +async def test_setup_get_basic_auth_utf8( + hass: HomeAssistant, aioclient_mock: AiohttpClientMocker +) -> None: + """Test setup with basic auth using UTF-8 characters including Unicode char \u2018.""" + # Use a password with the Unicode character \u2018 (left single quotation mark) + aioclient_mock.get("http://localhost", status=HTTPStatus.OK, json={"key": "on"}) + assert await async_setup_component( + hass, + BINARY_SENSOR_DOMAIN, + { + BINARY_SENSOR_DOMAIN: { + "platform": DOMAIN, + "resource": "http://localhost", + "method": "GET", + "value_template": "{{ value_json.key }}", + "name": "foo", + "verify_ssl": "true", + "timeout": 30, + "authentication": "basic", + "username": "test_user", + "password": "test\u2018password", # Password with Unicode char + "headers": {"Accept": CONTENT_TYPE_JSON}, + } + }, + ) + + await hass.async_block_till_done() + assert len(hass.states.async_all(BINARY_SENSOR_DOMAIN)) == 1 + + state = hass.states.get("binary_sensor.foo") + assert state.state == STATE_ON diff --git a/tests/test_util/aiohttp.py b/tests/test_util/aiohttp.py index eea3f4e88b4..fe0de66f44c 100644 --- a/tests/test_util/aiohttp.py +++ b/tests/test_util/aiohttp.py @@ -156,6 +156,9 @@ class AiohttpClientMocker: for response in self._mocks: if response.match_request(method, url, params): + # If auth is provided, try to encode it to trigger any encoding errors + if auth is not None: + auth.encode() self.mock_calls.append((method, url, data, headers)) if response.side_effect: response = await response.side_effect(method, url, data) From 6351c3302e6be434cf9cbf67b538d26cfe4a1483 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20BOU=C3=89?= Date: Sun, 6 Jul 2025 23:40:05 +0200 Subject: [PATCH 094/106] Matter OperationalState CountdownTime (#147705) Co-authored-by: Martin Hjelmare --- homeassistant/components/matter/sensor.py | 19 ++++++- homeassistant/components/matter/strings.json | 3 ++ .../matter/snapshots/test_sensor.ambr | 49 +++++++++++++++++++ tests/components/matter/test_sensor.py | 16 ++++++ 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/matter/sensor.py b/homeassistant/components/matter/sensor.py index 62c70f777e7..f563c246186 100644 --- a/homeassistant/components/matter/sensor.py +++ b/homeassistant/components/matter/sensor.py @@ -3,7 +3,7 @@ from __future__ import annotations from dataclasses import dataclass, field -from datetime import datetime +from datetime import datetime, timedelta from typing import TYPE_CHECKING, cast from chip.clusters import Objects as clusters @@ -44,7 +44,7 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from homeassistant.util import slugify +from homeassistant.util import dt as dt_util, slugify from .entity import MatterEntity, MatterEntityDescription from .helpers import get_matter @@ -942,6 +942,21 @@ DISCOVERY_SCHEMAS = [ # don't discover this entry if the supported state list is empty secondary_value_is_not=[], ), + MatterDiscoverySchema( + platform=Platform.SENSOR, + entity_description=MatterSensorEntityDescription( + key="OperationalStateCountdownTime", + translation_key="estimated_end_time", + device_class=SensorDeviceClass.TIMESTAMP, + state_class=None, + # Add countdown to current datetime to get the estimated end time + device_to_ha=( + lambda x: dt_util.utcnow() + timedelta(seconds=x) if x > 0 else None + ), + ), + entity_class=MatterSensor, + required_attributes=(clusters.OperationalState.Attributes.CountdownTime,), + ), MatterDiscoverySchema( platform=Platform.SENSOR, entity_description=MatterListSensorEntityDescription( diff --git a/homeassistant/components/matter/strings.json b/homeassistant/components/matter/strings.json index 0ac44c006ab..2534dd6aa6e 100644 --- a/homeassistant/components/matter/strings.json +++ b/homeassistant/components/matter/strings.json @@ -318,6 +318,9 @@ "docked": "Docked" } }, + "estimated_end_time": { + "name": "Estimated end time" + }, "switch_current_position": { "name": "Current switch position" }, diff --git a/tests/components/matter/snapshots/test_sensor.ambr b/tests/components/matter/snapshots/test_sensor.ambr index 8e459c0f573..472799b80ae 100644 --- a/tests/components/matter/snapshots/test_sensor.ambr +++ b/tests/components/matter/snapshots/test_sensor.ambr @@ -3443,6 +3443,55 @@ 'state': '1.3', }) # --- +# name: test_sensors[microwave_oven][sensor.microwave_oven_estimated_end_time-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'sensor', + 'entity_category': None, + 'entity_id': 'sensor.microwave_oven_estimated_end_time', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': , + 'original_icon': None, + 'original_name': 'Estimated end time', + 'platform': 'matter', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'estimated_end_time', + 'unique_id': '00000000000004D2-000000000000009D-MatterNodeDevice-1-OperationalStateCountdownTime-96-2', + 'unit_of_measurement': None, + }) +# --- +# name: test_sensors[microwave_oven][sensor.microwave_oven_estimated_end_time-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'timestamp', + 'friendly_name': 'Microwave Oven Estimated end time', + }), + 'context': , + 'entity_id': 'sensor.microwave_oven_estimated_end_time', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '2025-01-01T14:00:30+00:00', + }) +# --- # name: test_sensors[microwave_oven][sensor.microwave_oven_operational_state-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ diff --git a/tests/components/matter/test_sensor.py b/tests/components/matter/test_sensor.py index 3e9af4a6e4b..883a976284e 100644 --- a/tests/components/matter/test_sensor.py +++ b/tests/components/matter/test_sensor.py @@ -17,6 +17,7 @@ from .common import ( ) +@pytest.mark.freeze_time("2025-01-01T14:00:00+00:00") @pytest.mark.usefixtures("entity_registry_enabled_by_default", "matter_devices") async def test_sensors( hass: HomeAssistant, @@ -381,6 +382,21 @@ async def test_draft_electrical_measurement_sensor( assert state.state == "unknown" +@pytest.mark.freeze_time("2025-01-01T14:00:00+00:00") +@pytest.mark.parametrize("node_fixture", ["microwave_oven"]) +async def test_countdown_time_sensor( + hass: HomeAssistant, + matter_client: MagicMock, + matter_node: MatterNode, +) -> None: + """Test CountdownTime sensor.""" + # OperationalState Cluster / CountdownTime (1/96/2) + state = hass.states.get("sensor.microwave_oven_estimated_end_time") + assert state + # 1/96/2 = 30 seconds, so 30 s should be added to the current time. + assert state.state == "2025-01-01T14:00:30+00:00" + + @pytest.mark.parametrize("node_fixture", ["silabs_laundrywasher"]) async def test_list_sensor( hass: HomeAssistant, From 0bce01da0b634ebe1f52ba53f5b584ee0fbc10d0 Mon Sep 17 00:00:00 2001 From: Hessel Date: Mon, 7 Jul 2025 10:09:07 +0200 Subject: [PATCH 095/106] Address some Wallbox quality scale issues (#148200) --- homeassistant/components/wallbox/__init__.py | 14 +- .../components/wallbox/coordinator.py | 12 +- homeassistant/components/wallbox/lock.py | 7 +- homeassistant/components/wallbox/number.py | 7 +- homeassistant/components/wallbox/select.py | 6 +- homeassistant/components/wallbox/sensor.py | 7 +- homeassistant/components/wallbox/strings.json | 8 + homeassistant/components/wallbox/switch.py | 7 +- tests/components/wallbox/conftest.py | 188 +---------------- tests/components/wallbox/const.py | 189 ++++++++++++++++++ tests/components/wallbox/test_config_flow.py | 24 +-- tests/components/wallbox/test_init.py | 88 +++++--- tests/components/wallbox/test_number.py | 11 +- tests/components/wallbox/test_select.py | 26 +-- 14 files changed, 321 insertions(+), 273 deletions(-) diff --git a/homeassistant/components/wallbox/__init__.py b/homeassistant/components/wallbox/__init__.py index 9336ab0e36b..c2983d540df 100644 --- a/homeassistant/components/wallbox/__init__.py +++ b/homeassistant/components/wallbox/__init__.py @@ -9,7 +9,7 @@ from homeassistant.const import CONF_PASSWORD, CONF_USERNAME, Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed -from .const import DOMAIN, UPDATE_INTERVAL +from .const import UPDATE_INTERVAL from .coordinator import InvalidAuth, WallboxCoordinator, async_validate_input PLATFORMS = [ @@ -20,8 +20,10 @@ PLATFORMS = [ Platform.SWITCH, ] +type WallboxConfigEntry = ConfigEntry[WallboxCoordinator] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + +async def async_setup_entry(hass: HomeAssistant, entry: WallboxConfigEntry) -> bool: """Set up Wallbox from a config entry.""" wallbox = Wallbox( entry.data[CONF_USERNAME], @@ -36,7 +38,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: wallbox_coordinator = WallboxCoordinator(hass, entry, wallbox) await wallbox_coordinator.async_config_entry_first_refresh() - hass.data.setdefault(DOMAIN, {})[entry.entry_id] = wallbox_coordinator + entry.runtime_data = wallbox_coordinator await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) @@ -45,8 +47,4 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Unload a config entry.""" - unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) - if unload_ok: - hass.data[DOMAIN].pop(entry.entry_id) - - return unload_ok + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) diff --git a/homeassistant/components/wallbox/coordinator.py b/homeassistant/components/wallbox/coordinator.py index 69bf3a3af1c..ffd235157ac 100644 --- a/homeassistant/components/wallbox/coordinator.py +++ b/homeassistant/components/wallbox/coordinator.py @@ -222,7 +222,9 @@ class WallboxCoordinator(DataUpdateCoordinator[dict[str, Any]]): return data # noqa: TRY300 except requests.exceptions.HTTPError as wallbox_connection_error: if wallbox_connection_error.response.status_code == 403: - raise InvalidAuth from wallbox_connection_error + raise InvalidAuth( + translation_domain=DOMAIN, translation_key="invalid_auth" + ) from wallbox_connection_error if wallbox_connection_error.response.status_code == 429: raise HomeAssistantError( translation_domain=DOMAIN, translation_key="too_many_requests" @@ -248,7 +250,9 @@ class WallboxCoordinator(DataUpdateCoordinator[dict[str, Any]]): return data # noqa: TRY300 except requests.exceptions.HTTPError as wallbox_connection_error: if wallbox_connection_error.response.status_code == 403: - raise InvalidAuth from wallbox_connection_error + raise InvalidAuth( + translation_domain=DOMAIN, translation_key="invalid_auth" + ) from wallbox_connection_error if wallbox_connection_error.response.status_code == 429: raise HomeAssistantError( translation_domain=DOMAIN, translation_key="too_many_requests" @@ -303,7 +307,9 @@ class WallboxCoordinator(DataUpdateCoordinator[dict[str, Any]]): return data # noqa: TRY300 except requests.exceptions.HTTPError as wallbox_connection_error: if wallbox_connection_error.response.status_code == 403: - raise InvalidAuth from wallbox_connection_error + raise InvalidAuth( + translation_domain=DOMAIN, translation_key="invalid_auth" + ) from wallbox_connection_error if wallbox_connection_error.response.status_code == 429: raise HomeAssistantError( translation_domain=DOMAIN, translation_key="too_many_requests" diff --git a/homeassistant/components/wallbox/lock.py b/homeassistant/components/wallbox/lock.py index 7b5c99340f8..6ba9058db96 100644 --- a/homeassistant/components/wallbox/lock.py +++ b/homeassistant/components/wallbox/lock.py @@ -13,7 +13,6 @@ from .const import ( CHARGER_DATA_KEY, CHARGER_LOCKED_UNLOCKED_KEY, CHARGER_SERIAL_NUMBER_KEY, - DOMAIN, ) from .coordinator import WallboxCoordinator from .entity import WallboxEntity @@ -32,7 +31,7 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Create wallbox lock entities in HASS.""" - coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator: WallboxCoordinator = entry.runtime_data async_add_entities( WallboxLock(coordinator, description) for ent in coordinator.data @@ -40,6 +39,10 @@ async def async_setup_entry( ) +# Coordinator is used to centralize the data updates +PARALLEL_UPDATES = 0 + + class WallboxLock(WallboxEntity, LockEntity): """Representation of a wallbox lock.""" diff --git a/homeassistant/components/wallbox/number.py b/homeassistant/components/wallbox/number.py index e1b044bbdb2..af4fbe2c38b 100644 --- a/homeassistant/components/wallbox/number.py +++ b/homeassistant/components/wallbox/number.py @@ -23,7 +23,6 @@ from .const import ( CHARGER_MAX_ICP_CURRENT_KEY, CHARGER_PART_NUMBER_KEY, CHARGER_SERIAL_NUMBER_KEY, - DOMAIN, ) from .coordinator import WallboxCoordinator from .entity import WallboxEntity @@ -84,7 +83,7 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Create wallbox number entities in HASS.""" - coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator: WallboxCoordinator = entry.runtime_data async_add_entities( WallboxNumber(coordinator, entry, description) for ent in coordinator.data @@ -92,6 +91,10 @@ async def async_setup_entry( ) +# Coordinator is used to centralize the data updates +PARALLEL_UPDATES = 0 + + class WallboxNumber(WallboxEntity, NumberEntity): """Representation of the Wallbox portal.""" diff --git a/homeassistant/components/wallbox/select.py b/homeassistant/components/wallbox/select.py index 0048aa35c7c..10ac4e61189 100644 --- a/homeassistant/components/wallbox/select.py +++ b/homeassistant/components/wallbox/select.py @@ -62,7 +62,7 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Create wallbox select entities in HASS.""" - coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator: WallboxCoordinator = entry.runtime_data if coordinator.data[CHARGER_ECO_SMART_KEY] != EcoSmartMode.DISABLED: async_add_entities( WallboxSelect(coordinator, description) @@ -74,6 +74,10 @@ async def async_setup_entry( ) +# Coordinator is used to centralize the data updates +PARALLEL_UPDATES = 0 + + class WallboxSelect(WallboxEntity, SelectEntity): """Representation of the Wallbox portal.""" diff --git a/homeassistant/components/wallbox/sensor.py b/homeassistant/components/wallbox/sensor.py index e19fc2b936a..7d5e5b56309 100644 --- a/homeassistant/components/wallbox/sensor.py +++ b/homeassistant/components/wallbox/sensor.py @@ -43,7 +43,6 @@ from .const import ( CHARGER_SERIAL_NUMBER_KEY, CHARGER_STATE_OF_CHARGE_KEY, CHARGER_STATUS_DESCRIPTION_KEY, - DOMAIN, ) from .coordinator import WallboxCoordinator from .entity import WallboxEntity @@ -174,7 +173,7 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Create wallbox sensor entities in HASS.""" - coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator: WallboxCoordinator = entry.runtime_data async_add_entities( WallboxSensor(coordinator, description) @@ -183,6 +182,10 @@ async def async_setup_entry( ) +# Coordinator is used to centralize the data updates +PARALLEL_UPDATES = 0 + + class WallboxSensor(WallboxEntity, SensorEntity): """Representation of the Wallbox portal.""" diff --git a/homeassistant/components/wallbox/strings.json b/homeassistant/components/wallbox/strings.json index ee98a4855e3..a69251eb832 100644 --- a/homeassistant/components/wallbox/strings.json +++ b/homeassistant/components/wallbox/strings.json @@ -6,6 +6,11 @@ "station": "Station Serial Number", "username": "[%key:common::config_flow::data::username%]", "password": "[%key:common::config_flow::data::password%]" + }, + "data_description": { + "station": "Serial number of the charger, this value can be found in the Wallbox App or in the Wallbox Portal.", + "username": "Username for your Wallbox Account.", + "password": "Password for your Wallbox Account." } }, "reauth_confirm": { @@ -115,6 +120,9 @@ }, "too_many_requests": { "message": "Error communicating with Wallbox API, too many requests" + }, + "invalid_auth": { + "message": "Invalid authentication" } } } diff --git a/homeassistant/components/wallbox/switch.py b/homeassistant/components/wallbox/switch.py index 30275951ab2..7a28f863c4d 100644 --- a/homeassistant/components/wallbox/switch.py +++ b/homeassistant/components/wallbox/switch.py @@ -14,7 +14,6 @@ from .const import ( CHARGER_PAUSE_RESUME_KEY, CHARGER_SERIAL_NUMBER_KEY, CHARGER_STATUS_DESCRIPTION_KEY, - DOMAIN, ChargerStatus, ) from .coordinator import WallboxCoordinator @@ -34,12 +33,16 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Create wallbox sensor entities in HASS.""" - coordinator: WallboxCoordinator = hass.data[DOMAIN][entry.entry_id] + coordinator: WallboxCoordinator = entry.runtime_data async_add_entities( [WallboxSwitch(coordinator, SWITCH_TYPES[CHARGER_PAUSE_RESUME_KEY])] ) +# Coordinator is used to centralize the data updates +PARALLEL_UPDATES = 0 + + class WallboxSwitch(WallboxEntity, SwitchEntity): """Representation of the Wallbox portal.""" diff --git a/tests/components/wallbox/conftest.py b/tests/components/wallbox/conftest.py index ab1032b3816..c20c6e59da1 100644 --- a/tests/components/wallbox/conftest.py +++ b/tests/components/wallbox/conftest.py @@ -7,165 +7,22 @@ import pytest import requests from homeassistant.components.wallbox.const import ( - CHARGER_ADDED_ENERGY_KEY, - CHARGER_ADDED_RANGE_KEY, - CHARGER_CHARGING_POWER_KEY, - CHARGER_CHARGING_SPEED_KEY, - CHARGER_CURRENCY_KEY, - CHARGER_CURRENT_VERSION_KEY, - CHARGER_DATA_KEY, CHARGER_DATA_POST_L1_KEY, CHARGER_DATA_POST_L2_KEY, - CHARGER_ECO_SMART_KEY, - CHARGER_ECO_SMART_MODE_KEY, - CHARGER_ECO_SMART_STATUS_KEY, CHARGER_ENERGY_PRICE_KEY, - CHARGER_FEATURES_KEY, CHARGER_LOCKED_UNLOCKED_KEY, - CHARGER_MAX_AVAILABLE_POWER_KEY, - CHARGER_MAX_CHARGING_CURRENT_KEY, CHARGER_MAX_CHARGING_CURRENT_POST_KEY, CHARGER_MAX_ICP_CURRENT_KEY, - CHARGER_NAME_KEY, - CHARGER_PART_NUMBER_KEY, - CHARGER_PLAN_KEY, - CHARGER_POWER_BOOST_KEY, - CHARGER_SERIAL_NUMBER_KEY, - CHARGER_SOFTWARE_KEY, - CHARGER_STATUS_ID_KEY, CONF_STATION, DOMAIN, ) from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant -from .const import ERROR, REFRESH_TOKEN_TTL, STATUS, TTL, USER_ID +from .const import WALLBOX_AUTHORISATION_RESPONSE, WALLBOX_STATUS_RESPONSE from tests.common import MockConfigEntry -test_response = { - CHARGER_CHARGING_POWER_KEY: 0, - CHARGER_STATUS_ID_KEY: 193, - CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, - CHARGER_CHARGING_SPEED_KEY: 0, - CHARGER_ADDED_RANGE_KEY: 150, - CHARGER_ADDED_ENERGY_KEY: 44.697, - CHARGER_NAME_KEY: "WallboxName", - CHARGER_DATA_KEY: { - CHARGER_MAX_CHARGING_CURRENT_KEY: 24, - CHARGER_ENERGY_PRICE_KEY: 0.4, - CHARGER_LOCKED_UNLOCKED_KEY: False, - CHARGER_SERIAL_NUMBER_KEY: "20000", - CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", - CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, - CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, - CHARGER_MAX_ICP_CURRENT_KEY: 20, - CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, - CHARGER_ECO_SMART_KEY: { - CHARGER_ECO_SMART_STATUS_KEY: False, - CHARGER_ECO_SMART_MODE_KEY: 0, - }, - }, -} - -test_response_bidir = { - CHARGER_CHARGING_POWER_KEY: 0, - CHARGER_STATUS_ID_KEY: 193, - CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, - CHARGER_CHARGING_SPEED_KEY: 0, - CHARGER_ADDED_RANGE_KEY: 150, - CHARGER_ADDED_ENERGY_KEY: 44.697, - CHARGER_NAME_KEY: "WallboxName", - CHARGER_DATA_KEY: { - CHARGER_MAX_CHARGING_CURRENT_KEY: 24, - CHARGER_ENERGY_PRICE_KEY: 0.4, - CHARGER_LOCKED_UNLOCKED_KEY: False, - CHARGER_SERIAL_NUMBER_KEY: "20000", - CHARGER_PART_NUMBER_KEY: "QSP1-0-2-4-9-002-E", - CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, - CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, - CHARGER_MAX_ICP_CURRENT_KEY: 20, - CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, - CHARGER_ECO_SMART_KEY: { - CHARGER_ECO_SMART_STATUS_KEY: False, - CHARGER_ECO_SMART_MODE_KEY: 0, - }, - }, -} - -test_response_eco_mode = { - CHARGER_CHARGING_POWER_KEY: 0, - CHARGER_STATUS_ID_KEY: 193, - CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, - CHARGER_CHARGING_SPEED_KEY: 0, - CHARGER_ADDED_RANGE_KEY: 150, - CHARGER_ADDED_ENERGY_KEY: 44.697, - CHARGER_NAME_KEY: "WallboxName", - CHARGER_DATA_KEY: { - CHARGER_MAX_CHARGING_CURRENT_KEY: 24, - CHARGER_ENERGY_PRICE_KEY: 0.4, - CHARGER_LOCKED_UNLOCKED_KEY: False, - CHARGER_SERIAL_NUMBER_KEY: "20000", - CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", - CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, - CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, - CHARGER_MAX_ICP_CURRENT_KEY: 20, - CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, - CHARGER_ECO_SMART_KEY: { - CHARGER_ECO_SMART_STATUS_KEY: True, - CHARGER_ECO_SMART_MODE_KEY: 0, - }, - }, -} - - -test_response_full_solar = { - CHARGER_CHARGING_POWER_KEY: 0, - CHARGER_STATUS_ID_KEY: 193, - CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, - CHARGER_CHARGING_SPEED_KEY: 0, - CHARGER_ADDED_RANGE_KEY: 150, - CHARGER_ADDED_ENERGY_KEY: 44.697, - CHARGER_NAME_KEY: "WallboxName", - CHARGER_DATA_KEY: { - CHARGER_MAX_CHARGING_CURRENT_KEY: 24, - CHARGER_ENERGY_PRICE_KEY: 0.4, - CHARGER_LOCKED_UNLOCKED_KEY: False, - CHARGER_SERIAL_NUMBER_KEY: "20000", - CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", - CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, - CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, - CHARGER_MAX_ICP_CURRENT_KEY: 20, - CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, - CHARGER_ECO_SMART_KEY: { - CHARGER_ECO_SMART_STATUS_KEY: True, - CHARGER_ECO_SMART_MODE_KEY: 1, - }, - }, -} - -test_response_no_power_boost = { - CHARGER_CHARGING_POWER_KEY: 0, - CHARGER_STATUS_ID_KEY: 193, - CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, - CHARGER_CHARGING_SPEED_KEY: 0, - CHARGER_ADDED_RANGE_KEY: 150, - CHARGER_ADDED_ENERGY_KEY: 44.697, - CHARGER_NAME_KEY: "WallboxName", - CHARGER_DATA_KEY: { - CHARGER_MAX_CHARGING_CURRENT_KEY: 24, - CHARGER_ENERGY_PRICE_KEY: 0.4, - CHARGER_LOCKED_UNLOCKED_KEY: False, - CHARGER_SERIAL_NUMBER_KEY: "20000", - CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", - CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, - CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, - CHARGER_MAX_ICP_CURRENT_KEY: 20, - CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: []}, - }, -} - - http_403_error = requests.exceptions.HTTPError() http_403_error.response = requests.Response() http_403_error.response.status_code = HTTPStatus.FORBIDDEN @@ -176,45 +33,6 @@ http_429_error = requests.exceptions.HTTPError() http_429_error.response = requests.Response() http_429_error.response.status_code = HTTPStatus.TOO_MANY_REQUESTS -authorisation_response = { - "data": { - "attributes": { - "token": "fakekeyhere", - "refresh_token": "refresh_fakekeyhere", - USER_ID: 12345, - TTL: 145656758, - REFRESH_TOKEN_TTL: 145756758, - ERROR: "false", - STATUS: 200, - } - } -} - - -authorisation_response_unauthorised = { - "data": { - "attributes": { - "token": "fakekeyhere", - "refresh_token": "refresh_fakekeyhere", - USER_ID: 12345, - TTL: 145656758, - REFRESH_TOKEN_TTL: 145756758, - ERROR: "false", - STATUS: 404, - } - } -} - -invalid_reauth_response = { - "jwt": "fakekeyhere", - "refresh_token": "refresh_fakekeyhere", - "user_id": 12345, - "ttl": 145656758, - "refresh_token_ttl": 145756758, - "error": False, - "status": 200, -} - @pytest.fixture def entry(hass: HomeAssistant) -> MockConfigEntry: @@ -237,7 +55,7 @@ def mock_wallbox(): """Patch Wallbox class for tests.""" with patch("homeassistant.components.wallbox.Wallbox") as mock: wallbox = MagicMock() - wallbox.authenticate = Mock(return_value=authorisation_response) + wallbox.authenticate = Mock(return_value=WALLBOX_AUTHORISATION_RESPONSE) wallbox.lockCharger = Mock( return_value={ CHARGER_DATA_POST_L1_KEY: { @@ -263,7 +81,7 @@ def mock_wallbox(): } ) wallbox.setIcpMaxCurrent = Mock(return_value={CHARGER_MAX_ICP_CURRENT_KEY: 25}) - wallbox.getChargerStatus = Mock(return_value=test_response) + wallbox.getChargerStatus = Mock(return_value=WALLBOX_STATUS_RESPONSE) mock.return_value = wallbox yield wallbox diff --git a/tests/components/wallbox/const.py b/tests/components/wallbox/const.py index 82c9e5169d5..9650f9d3c61 100644 --- a/tests/components/wallbox/const.py +++ b/tests/components/wallbox/const.py @@ -1,5 +1,31 @@ """Provides constants for Wallbox component tests.""" +from homeassistant.components.wallbox.const import ( + CHARGER_ADDED_ENERGY_KEY, + CHARGER_ADDED_RANGE_KEY, + CHARGER_CHARGING_POWER_KEY, + CHARGER_CHARGING_SPEED_KEY, + CHARGER_CURRENCY_KEY, + CHARGER_CURRENT_VERSION_KEY, + CHARGER_DATA_KEY, + CHARGER_ECO_SMART_KEY, + CHARGER_ECO_SMART_MODE_KEY, + CHARGER_ECO_SMART_STATUS_KEY, + CHARGER_ENERGY_PRICE_KEY, + CHARGER_FEATURES_KEY, + CHARGER_LOCKED_UNLOCKED_KEY, + CHARGER_MAX_AVAILABLE_POWER_KEY, + CHARGER_MAX_CHARGING_CURRENT_KEY, + CHARGER_MAX_ICP_CURRENT_KEY, + CHARGER_NAME_KEY, + CHARGER_PART_NUMBER_KEY, + CHARGER_PLAN_KEY, + CHARGER_POWER_BOOST_KEY, + CHARGER_SERIAL_NUMBER_KEY, + CHARGER_SOFTWARE_KEY, + CHARGER_STATUS_ID_KEY, +) + JWT = "jwt" USER_ID = "user_id" TTL = "ttl" @@ -7,6 +33,169 @@ REFRESH_TOKEN_TTL = "refresh_token_ttl" ERROR = "error" STATUS = "status" +WALLBOX_STATUS_RESPONSE = { + CHARGER_CHARGING_POWER_KEY: 0, + CHARGER_STATUS_ID_KEY: 193, + CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, + CHARGER_CHARGING_SPEED_KEY: 0, + CHARGER_ADDED_RANGE_KEY: 150, + CHARGER_ADDED_ENERGY_KEY: 44.697, + CHARGER_NAME_KEY: "WallboxName", + CHARGER_DATA_KEY: { + CHARGER_MAX_CHARGING_CURRENT_KEY: 24, + CHARGER_ENERGY_PRICE_KEY: 0.4, + CHARGER_LOCKED_UNLOCKED_KEY: False, + CHARGER_SERIAL_NUMBER_KEY: "20000", + CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", + CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, + CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, + CHARGER_ECO_SMART_KEY: { + CHARGER_ECO_SMART_STATUS_KEY: False, + CHARGER_ECO_SMART_MODE_KEY: 0, + }, + }, +} + +WALLBOX_STATUS_RESPONSE_BIDIR = { + CHARGER_CHARGING_POWER_KEY: 0, + CHARGER_STATUS_ID_KEY: 193, + CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, + CHARGER_CHARGING_SPEED_KEY: 0, + CHARGER_ADDED_RANGE_KEY: 150, + CHARGER_ADDED_ENERGY_KEY: 44.697, + CHARGER_NAME_KEY: "WallboxName", + CHARGER_DATA_KEY: { + CHARGER_MAX_CHARGING_CURRENT_KEY: 24, + CHARGER_ENERGY_PRICE_KEY: 0.4, + CHARGER_LOCKED_UNLOCKED_KEY: False, + CHARGER_SERIAL_NUMBER_KEY: "20000", + CHARGER_PART_NUMBER_KEY: "QSP1-0-2-4-9-002-E", + CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, + CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, + CHARGER_ECO_SMART_KEY: { + CHARGER_ECO_SMART_STATUS_KEY: False, + CHARGER_ECO_SMART_MODE_KEY: 0, + }, + }, +} + +WALLBOX_STATUS_RESPONSE_ECO_MODE = { + CHARGER_CHARGING_POWER_KEY: 0, + CHARGER_STATUS_ID_KEY: 193, + CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, + CHARGER_CHARGING_SPEED_KEY: 0, + CHARGER_ADDED_RANGE_KEY: 150, + CHARGER_ADDED_ENERGY_KEY: 44.697, + CHARGER_NAME_KEY: "WallboxName", + CHARGER_DATA_KEY: { + CHARGER_MAX_CHARGING_CURRENT_KEY: 24, + CHARGER_ENERGY_PRICE_KEY: 0.4, + CHARGER_LOCKED_UNLOCKED_KEY: False, + CHARGER_SERIAL_NUMBER_KEY: "20000", + CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", + CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, + CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, + CHARGER_ECO_SMART_KEY: { + CHARGER_ECO_SMART_STATUS_KEY: True, + CHARGER_ECO_SMART_MODE_KEY: 0, + }, + }, +} + + +WALLBOX_STATUS_RESPONSE_FULL_SOLAR = { + CHARGER_CHARGING_POWER_KEY: 0, + CHARGER_STATUS_ID_KEY: 193, + CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, + CHARGER_CHARGING_SPEED_KEY: 0, + CHARGER_ADDED_RANGE_KEY: 150, + CHARGER_ADDED_ENERGY_KEY: 44.697, + CHARGER_NAME_KEY: "WallboxName", + CHARGER_DATA_KEY: { + CHARGER_MAX_CHARGING_CURRENT_KEY: 24, + CHARGER_ENERGY_PRICE_KEY: 0.4, + CHARGER_LOCKED_UNLOCKED_KEY: False, + CHARGER_SERIAL_NUMBER_KEY: "20000", + CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", + CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, + CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: [CHARGER_POWER_BOOST_KEY]}, + CHARGER_ECO_SMART_KEY: { + CHARGER_ECO_SMART_STATUS_KEY: True, + CHARGER_ECO_SMART_MODE_KEY: 1, + }, + }, +} + +WALLBOX_STATUS_RESPONSE_NO_POWER_BOOST = { + CHARGER_CHARGING_POWER_KEY: 0, + CHARGER_STATUS_ID_KEY: 193, + CHARGER_MAX_AVAILABLE_POWER_KEY: 25.0, + CHARGER_CHARGING_SPEED_KEY: 0, + CHARGER_ADDED_RANGE_KEY: 150, + CHARGER_ADDED_ENERGY_KEY: 44.697, + CHARGER_NAME_KEY: "WallboxName", + CHARGER_DATA_KEY: { + CHARGER_MAX_CHARGING_CURRENT_KEY: 24, + CHARGER_ENERGY_PRICE_KEY: 0.4, + CHARGER_LOCKED_UNLOCKED_KEY: False, + CHARGER_SERIAL_NUMBER_KEY: "20000", + CHARGER_PART_NUMBER_KEY: "PLP1-0-2-4-9-002-E", + CHARGER_SOFTWARE_KEY: {CHARGER_CURRENT_VERSION_KEY: "5.5.10"}, + CHARGER_CURRENCY_KEY: {"code": "EUR/kWh"}, + CHARGER_MAX_ICP_CURRENT_KEY: 20, + CHARGER_PLAN_KEY: {CHARGER_FEATURES_KEY: []}, + }, +} + + +WALLBOX_AUTHORISATION_RESPONSE = { + "data": { + "attributes": { + "token": "fakekeyhere", + "refresh_token": "refresh_fakekeyhere", + USER_ID: 12345, + TTL: 145656758, + REFRESH_TOKEN_TTL: 145756758, + ERROR: "false", + STATUS: 200, + } + } +} + + +WALLBOX_AUTHORISATION_RESPONSE_UNAUTHORISED = { + "data": { + "attributes": { + "token": "fakekeyhere", + "refresh_token": "refresh_fakekeyhere", + USER_ID: 12345, + TTL: 145656758, + REFRESH_TOKEN_TTL: 145756758, + ERROR: "false", + STATUS: 404, + } + } +} + +WALLBOX_INVALID_REAUTH_RESPONSE = { + "jwt": "fakekeyhere", + "refresh_token": "refresh_fakekeyhere", + "user_id": 12345, + "ttl": 145656758, + "refresh_token_ttl": 145756758, + "error": False, + "status": 200, +} + + MOCK_NUMBER_ENTITY_ID = "number.wallbox_wallboxname_maximum_charging_current" MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID = "number.wallbox_wallboxname_energy_price" MOCK_NUMBER_ENTITY_ICP_CURRENT_ID = "number.wallbox_wallboxname_maximum_icp_current" diff --git a/tests/components/wallbox/test_config_flow.py b/tests/components/wallbox/test_config_flow.py index d0c34ae0bce..25265aeda4a 100644 --- a/tests/components/wallbox/test_config_flow.py +++ b/tests/components/wallbox/test_config_flow.py @@ -3,7 +3,6 @@ from unittest.mock import Mock, patch from homeassistant import config_entries -from homeassistant.components.wallbox import config_flow from homeassistant.components.wallbox.const import ( CHARGER_ADDED_ENERGY_KEY, CHARGER_ADDED_RANGE_KEY, @@ -18,12 +17,10 @@ from homeassistant.config_entries import ConfigEntryState from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType -from .conftest import ( - authorisation_response, - authorisation_response_unauthorised, - http_403_error, - http_404_error, - setup_integration, +from .conftest import http_403_error, http_404_error, setup_integration +from .const import ( + WALLBOX_AUTHORISATION_RESPONSE, + WALLBOX_AUTHORISATION_RESPONSE_UNAUTHORISED, ) from tests.common import MockConfigEntry @@ -40,10 +37,9 @@ test_response = { async def test_show_set_form(hass: HomeAssistant, mock_wallbox) -> None: """Test that the setup form is served.""" - flow = config_flow.WallboxConfigFlow() - flow.hass = hass - result = await flow.async_step_user(user_input=None) - + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": config_entries.SOURCE_USER} + ) assert result["type"] is FlowResultType.FORM assert result["step_id"] == "user" @@ -112,7 +108,7 @@ async def test_form_validate_input(hass: HomeAssistant) -> None: with ( patch( "homeassistant.components.wallbox.Wallbox.authenticate", - return_value=authorisation_response, + return_value=WALLBOX_AUTHORISATION_RESPONSE, ), patch( "homeassistant.components.wallbox.Wallbox.pauseChargingSession", @@ -143,7 +139,7 @@ async def test_form_reauth( patch.object( mock_wallbox, "authenticate", - return_value=authorisation_response_unauthorised, + return_value=WALLBOX_AUTHORISATION_RESPONSE_UNAUTHORISED, ), patch.object(mock_wallbox, "getChargerStatus", return_value=test_response), ): @@ -176,7 +172,7 @@ async def test_form_reauth_invalid( patch.object( mock_wallbox, "authenticate", - return_value=authorisation_response_unauthorised, + return_value=WALLBOX_AUTHORISATION_RESPONSE_UNAUTHORISED, ), patch.object(mock_wallbox, "getChargerStatus", return_value=test_response), ): diff --git a/tests/components/wallbox/test_init.py b/tests/components/wallbox/test_init.py index ef73decea8f..4d882da7a6e 100644 --- a/tests/components/wallbox/test_init.py +++ b/tests/components/wallbox/test_init.py @@ -1,19 +1,23 @@ """Test Wallbox Init Component.""" +from datetime import datetime, timedelta from unittest.mock import patch -from homeassistant.components.wallbox.const import DOMAIN -from homeassistant.config_entries import ConfigEntryState -from homeassistant.core import HomeAssistant +import pytest -from .conftest import ( - http_403_error, - http_429_error, - setup_integration, - test_response_no_power_boost, +from homeassistant.components.input_number import ATTR_VALUE, SERVICE_SET_VALUE +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import HomeAssistantError + +from .conftest import http_403_error, http_429_error, setup_integration +from .const import ( + MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, + WALLBOX_STATUS_RESPONSE_NO_POWER_BOOST, ) -from tests.common import MockConfigEntry +from tests.common import MockConfigEntry, async_fire_time_changed async def test_wallbox_setup_unload_entry( @@ -40,24 +44,25 @@ async def test_wallbox_unload_entry_connection_error( assert entry.state is ConfigEntryState.NOT_LOADED -async def test_wallbox_refresh_failed_connection_error_auth( +async def test_wallbox_refresh_failed_connection_error_too_many_requests( hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox ) -> None: """Test Wallbox setup with connection error.""" - await setup_integration(hass, entry) - assert entry.state is ConfigEntryState.LOADED + with patch.object(mock_wallbox, "getChargerStatus", side_effect=http_429_error): + await setup_integration(hass, entry) + assert entry.state is ConfigEntryState.SETUP_RETRY - with patch.object(mock_wallbox, "authenticate", side_effect=http_429_error): - wallbox = hass.data[DOMAIN][entry.entry_id] - await wallbox.async_refresh() + await hass.async_block_till_done() assert await hass.config_entries.async_unload(entry.entry_id) assert entry.state is ConfigEntryState.NOT_LOADED -async def test_wallbox_refresh_failed_invalid_auth( - hass: HomeAssistant, entry: MockConfigEntry, mock_wallbox +async def test_wallbox_refresh_failed_error_auth( + hass: HomeAssistant, + entry: MockConfigEntry, + mock_wallbox, ) -> None: """Test Wallbox setup with authentication error.""" @@ -66,11 +71,31 @@ async def test_wallbox_refresh_failed_invalid_auth( with ( patch.object(mock_wallbox, "authenticate", side_effect=http_403_error), - patch.object(mock_wallbox, "pauseChargingSession", side_effect=http_403_error), + pytest.raises(HomeAssistantError), ): - wallbox = hass.data[DOMAIN][entry.entry_id] + await hass.services.async_call( + "number", + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, + ATTR_VALUE: 1.1, + }, + blocking=True, + ) - await wallbox.async_refresh() + with ( + patch.object(mock_wallbox, "authenticate", side_effect=http_429_error), + pytest.raises(HomeAssistantError), + ): + await hass.services.async_call( + "number", + SERVICE_SET_VALUE, + { + ATTR_ENTITY_ID: MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, + ATTR_VALUE: 1.1, + }, + blocking=True, + ) assert await hass.config_entries.async_unload(entry.entry_id) assert entry.state is ConfigEntryState.NOT_LOADED @@ -81,13 +106,10 @@ async def test_wallbox_refresh_failed_http_error( ) -> None: """Test Wallbox setup with authentication error.""" - await setup_integration(hass, entry) - assert entry.state is ConfigEntryState.LOADED - with patch.object(mock_wallbox, "getChargerStatus", side_effect=http_403_error): - wallbox = hass.data[DOMAIN][entry.entry_id] - - await wallbox.async_refresh() + await setup_integration(hass, entry) + assert entry.state is ConfigEntryState.SETUP_RETRY + await hass.async_block_till_done() assert await hass.config_entries.async_unload(entry.entry_id) assert entry.state is ConfigEntryState.NOT_LOADED @@ -102,9 +124,8 @@ async def test_wallbox_refresh_failed_too_many_requests( assert entry.state is ConfigEntryState.LOADED with patch.object(mock_wallbox, "getChargerStatus", side_effect=http_429_error): - wallbox = hass.data[DOMAIN][entry.entry_id] - - await wallbox.async_refresh() + async_fire_time_changed(hass, datetime.now() + timedelta(seconds=120), True) + await hass.async_block_till_done() assert await hass.config_entries.async_unload(entry.entry_id) assert entry.state is ConfigEntryState.NOT_LOADED @@ -119,9 +140,8 @@ async def test_wallbox_refresh_failed_connection_error( assert entry.state is ConfigEntryState.LOADED with patch.object(mock_wallbox, "pauseChargingSession", side_effect=http_403_error): - wallbox = hass.data[DOMAIN][entry.entry_id] - - await wallbox.async_refresh() + async_fire_time_changed(hass, datetime.now() + timedelta(seconds=120), True) + await hass.async_block_till_done() assert await hass.config_entries.async_unload(entry.entry_id) assert entry.state is ConfigEntryState.NOT_LOADED @@ -132,7 +152,9 @@ async def test_wallbox_setup_load_entry_no_eco_mode( ) -> None: """Test Wallbox Unload.""" with patch.object( - mock_wallbox, "getChargerStatus", return_value=test_response_no_power_boost + mock_wallbox, + "getChargerStatus", + return_value=WALLBOX_STATUS_RESPONSE_NO_POWER_BOOST, ): await setup_integration(hass, entry) assert entry.state is ConfigEntryState.LOADED diff --git a/tests/components/wallbox/test_number.py b/tests/components/wallbox/test_number.py index 3aba0792baa..cb332d1cb1e 100644 --- a/tests/components/wallbox/test_number.py +++ b/tests/components/wallbox/test_number.py @@ -11,17 +11,12 @@ from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from .conftest import ( - http_403_error, - http_404_error, - http_429_error, - setup_integration, - test_response_bidir, -) +from .conftest import http_403_error, http_404_error, http_429_error, setup_integration from .const import ( MOCK_NUMBER_ENTITY_ENERGY_PRICE_ID, MOCK_NUMBER_ENTITY_ICP_CURRENT_ID, MOCK_NUMBER_ENTITY_ID, + WALLBOX_STATUS_RESPONSE_BIDIR, ) from tests.common import MockConfigEntry @@ -53,7 +48,7 @@ async def test_wallbox_number_power_class_bidir( ) -> None: """Test wallbox sensor class.""" with patch.object( - mock_wallbox, "getChargerStatus", return_value=test_response_bidir + mock_wallbox, "getChargerStatus", return_value=WALLBOX_STATUS_RESPONSE_BIDIR ): await setup_integration(hass, entry) diff --git a/tests/components/wallbox/test_select.py b/tests/components/wallbox/test_select.py index f194566dbae..c07d0ad5272 100644 --- a/tests/components/wallbox/test_select.py +++ b/tests/components/wallbox/test_select.py @@ -13,23 +13,21 @@ from homeassistant.components.wallbox.const import EcoSmartMode from homeassistant.const import ATTR_ENTITY_ID from homeassistant.core import HomeAssistant, HomeAssistantError -from .conftest import ( - http_404_error, - http_429_error, - setup_integration, - test_response, - test_response_eco_mode, - test_response_full_solar, - test_response_no_power_boost, +from .conftest import http_404_error, http_429_error, setup_integration +from .const import ( + MOCK_SELECT_ENTITY_ID, + WALLBOX_STATUS_RESPONSE, + WALLBOX_STATUS_RESPONSE_ECO_MODE, + WALLBOX_STATUS_RESPONSE_FULL_SOLAR, + WALLBOX_STATUS_RESPONSE_NO_POWER_BOOST, ) -from .const import MOCK_SELECT_ENTITY_ID from tests.common import MockConfigEntry TEST_OPTIONS = [ - (EcoSmartMode.OFF, test_response), - (EcoSmartMode.ECO_MODE, test_response_eco_mode), - (EcoSmartMode.FULL_SOLAR, test_response_full_solar), + (EcoSmartMode.OFF, WALLBOX_STATUS_RESPONSE), + (EcoSmartMode.ECO_MODE, WALLBOX_STATUS_RESPONSE_ECO_MODE), + (EcoSmartMode.FULL_SOLAR, WALLBOX_STATUS_RESPONSE_FULL_SOLAR), ] @@ -61,7 +59,9 @@ async def test_wallbox_select_no_power_boost_class( """Test wallbox select class.""" with patch.object( - mock_wallbox, "getChargerStatus", return_value=test_response_no_power_boost + mock_wallbox, + "getChargerStatus", + return_value=WALLBOX_STATUS_RESPONSE_NO_POWER_BOOST, ): await setup_integration(hass, entry) From 21f6bf39140233e3513f7e7464a5bf0407c09c8e Mon Sep 17 00:00:00 2001 From: Norbert Rittel Date: Mon, 7 Jul 2025 10:26:20 +0200 Subject: [PATCH 096/106] Improve translation_key of `EnergyEvseSupplyStateSensor` in `matter` (#148280) --- homeassistant/components/matter/binary_sensor.py | 2 +- homeassistant/components/matter/strings.json | 4 ++-- .../matter/snapshots/test_binary_sensor.ambr | 14 +++++++------- tests/components/matter/test_binary_sensor.py | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/matter/binary_sensor.py b/homeassistant/components/matter/binary_sensor.py index 09321bd33b2..3ce0cc68012 100644 --- a/homeassistant/components/matter/binary_sensor.py +++ b/homeassistant/components/matter/binary_sensor.py @@ -309,7 +309,7 @@ DISCOVERY_SCHEMAS = [ platform=Platform.BINARY_SENSOR, entity_description=MatterBinarySensorEntityDescription( key="EnergyEvseSupplyStateSensor", - translation_key="evse_supply_charging_state", + translation_key="evse_supply_state", device_class=BinarySensorDeviceClass.RUNNING, device_to_ha={ clusters.EnergyEvse.Enums.SupplyStateEnum.kDisabled: False, diff --git a/homeassistant/components/matter/strings.json b/homeassistant/components/matter/strings.json index 2534dd6aa6e..f7cec270f54 100644 --- a/homeassistant/components/matter/strings.json +++ b/homeassistant/components/matter/strings.json @@ -83,8 +83,8 @@ "evse_plug": { "name": "Plug state" }, - "evse_supply_charging_state": { - "name": "Supply charging state" + "evse_supply_state": { + "name": "Charger supply state" }, "boost_state": { "name": "Boost state" diff --git a/tests/components/matter/snapshots/test_binary_sensor.ambr b/tests/components/matter/snapshots/test_binary_sensor.ambr index f6c7780c517..7e2f1e7618e 100644 --- a/tests/components/matter/snapshots/test_binary_sensor.ambr +++ b/tests/components/matter/snapshots/test_binary_sensor.ambr @@ -685,7 +685,7 @@ 'state': 'on', }) # --- -# name: test_binary_sensors[silabs_evse_charging][binary_sensor.evse_supply_charging_state-entry] +# name: test_binary_sensors[silabs_evse_charging][binary_sensor.evse_charger_supply_state-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -698,7 +698,7 @@ 'disabled_by': None, 'domain': 'binary_sensor', 'entity_category': None, - 'entity_id': 'binary_sensor.evse_supply_charging_state', + 'entity_id': 'binary_sensor.evse_charger_supply_state', 'has_entity_name': True, 'hidden_by': None, 'icon': None, @@ -710,24 +710,24 @@ }), 'original_device_class': , 'original_icon': None, - 'original_name': 'Supply charging state', + 'original_name': 'Charger supply state', 'platform': 'matter', 'previous_unique_id': None, 'suggested_object_id': None, 'supported_features': 0, - 'translation_key': 'evse_supply_charging_state', + 'translation_key': 'evse_supply_state', 'unique_id': '00000000000004D2-0000000000000017-MatterNodeDevice-1-EnergyEvseSupplyStateSensor-153-1', 'unit_of_measurement': None, }) # --- -# name: test_binary_sensors[silabs_evse_charging][binary_sensor.evse_supply_charging_state-state] +# name: test_binary_sensors[silabs_evse_charging][binary_sensor.evse_charger_supply_state-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'device_class': 'running', - 'friendly_name': 'evse Supply charging state', + 'friendly_name': 'evse Charger supply state', }), 'context': , - 'entity_id': 'binary_sensor.evse_supply_charging_state', + 'entity_id': 'binary_sensor.evse_charger_supply_state', 'last_changed': , 'last_reported': , 'last_updated': , diff --git a/tests/components/matter/test_binary_sensor.py b/tests/components/matter/test_binary_sensor.py index 873d6f17528..fcfd4da84c8 100644 --- a/tests/components/matter/test_binary_sensor.py +++ b/tests/components/matter/test_binary_sensor.py @@ -184,8 +184,8 @@ async def test_evse_sensor( assert state assert state.state == "off" - # Test SupplyStateEnum value with binary_sensor.evse_supply_charging - entity_id = "binary_sensor.evse_supply_charging_state" + # Test SupplyStateEnum value with binary_sensor.evse_charger_supply_state + entity_id = "binary_sensor.evse_charger_supply_state" state = hass.states.get(entity_id) assert state assert state.state == "on" From a5d6bfd1b31a27bbdd9a5a89c63caa2397afe1e6 Mon Sep 17 00:00:00 2001 From: Norbert Rittel Date: Mon, 7 Jul 2025 10:30:39 +0200 Subject: [PATCH 097/106] Reword option for 'Main' control in `wled` (#148309) --- homeassistant/components/wled/strings.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/wled/strings.json b/homeassistant/components/wled/strings.json index 50dc0129369..1f15aea979b 100644 --- a/homeassistant/components/wled/strings.json +++ b/homeassistant/components/wled/strings.json @@ -28,7 +28,7 @@ "step": { "init": { "data": { - "keep_master_light": "Keep main light, even with 1 LED segment." + "keep_master_light": "Add 'Main' control even with single LED segment" } } } From f02c1b0d4ee1e3c29a39fc73da311a7d078812a9 Mon Sep 17 00:00:00 2001 From: Shay Levy Date: Mon, 7 Jul 2025 12:37:39 +0300 Subject: [PATCH 098/106] Bump aiowebostv to 0.7.4 (#148273) --- .../components/webostv/config_flow.py | 5 +++- .../components/webostv/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/webostv/test_config_flow.py | 30 ++++++++++++++++++- 5 files changed, 36 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/webostv/config_flow.py b/homeassistant/components/webostv/config_flow.py index 80c8fb7f8f2..2af38cb3d17 100644 --- a/homeassistant/components/webostv/config_flow.py +++ b/homeassistant/components/webostv/config_flow.py @@ -98,7 +98,10 @@ class FlowHandler(ConfigFlow, domain=DOMAIN): data = {CONF_HOST: self._host, CONF_CLIENT_SECRET: client.client_key} if not self._name: - self._name = f"{DEFAULT_NAME} {client.tv_info.system['modelName']}" + if model_name := client.tv_info.system.get("modelName"): + self._name = f"{DEFAULT_NAME} {model_name}" + else: + self._name = DEFAULT_NAME return self.async_create_entry(title=self._name, data=data) return self.async_show_form(step_id="pairing", errors=errors) diff --git a/homeassistant/components/webostv/manifest.json b/homeassistant/components/webostv/manifest.json index 8ac470ae922..c3c3e9a564f 100644 --- a/homeassistant/components/webostv/manifest.json +++ b/homeassistant/components/webostv/manifest.json @@ -6,7 +6,7 @@ "documentation": "https://www.home-assistant.io/integrations/webostv", "iot_class": "local_push", "loggers": ["aiowebostv"], - "requirements": ["aiowebostv==0.7.3"], + "requirements": ["aiowebostv==0.7.4"], "ssdp": [ { "st": "urn:lge-com:service:webos-second-screen:1" diff --git a/requirements_all.txt b/requirements_all.txt index 3e3c701508c..9c893b72175 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -435,7 +435,7 @@ aiowatttime==0.1.1 aiowebdav2==0.4.6 # homeassistant.components.webostv -aiowebostv==0.7.3 +aiowebostv==0.7.4 # homeassistant.components.withings aiowithings==3.1.6 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 03f77b69b93..29be48c7f7d 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -417,7 +417,7 @@ aiowatttime==0.1.1 aiowebdav2==0.4.6 # homeassistant.components.webostv -aiowebostv==0.7.3 +aiowebostv==0.7.4 # homeassistant.components.withings aiowithings==3.1.6 diff --git a/tests/components/webostv/test_config_flow.py b/tests/components/webostv/test_config_flow.py index 564ff9afa9b..2445140aff4 100644 --- a/tests/components/webostv/test_config_flow.py +++ b/tests/components/webostv/test_config_flow.py @@ -4,7 +4,12 @@ from aiowebostv import WebOsTvPairError import pytest from homeassistant import config_entries -from homeassistant.components.webostv.const import CONF_SOURCES, DOMAIN, LIVE_TV_APP_ID +from homeassistant.components.webostv.const import ( + CONF_SOURCES, + DEFAULT_NAME, + DOMAIN, + LIVE_TV_APP_ID, +) from homeassistant.config_entries import SOURCE_SSDP from homeassistant.const import CONF_CLIENT_SECRET, CONF_HOST, CONF_SOURCE from homeassistant.core import HomeAssistant @@ -63,6 +68,29 @@ async def test_form(hass: HomeAssistant, client) -> None: assert config_entry.unique_id == FAKE_UUID +async def test_form_no_model_name(hass: HomeAssistant, client) -> None: + """Test successful user flow without model name.""" + client.tv_info.system = {} + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={CONF_SOURCE: config_entries.SOURCE_USER}, + data=MOCK_USER_CONFIG, + ) + await hass.async_block_till_done() + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "pairing" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={} + ) + + assert result["type"] is FlowResultType.CREATE_ENTRY + assert result["title"] == DEFAULT_NAME + config_entry = result["result"] + assert config_entry.unique_id == FAKE_UUID + + @pytest.mark.parametrize( ("apps", "inputs"), [ From b79e770bcfaf84b6b70e6c81240c5b1df9cc8c70 Mon Sep 17 00:00:00 2001 From: Arie Catsman <120491684+catsmanac@users.noreply.github.com> Date: Mon, 7 Jul 2025 11:40:48 +0200 Subject: [PATCH 099/106] Bump pyenphase to 2.2.1 (#148292) --- homeassistant/components/enphase_envoy/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/enphase_envoy/manifest.json b/homeassistant/components/enphase_envoy/manifest.json index 8387ecc9c9f..278045001fc 100644 --- a/homeassistant/components/enphase_envoy/manifest.json +++ b/homeassistant/components/enphase_envoy/manifest.json @@ -7,7 +7,7 @@ "iot_class": "local_polling", "loggers": ["pyenphase"], "quality_scale": "platinum", - "requirements": ["pyenphase==2.2.0"], + "requirements": ["pyenphase==2.2.1"], "zeroconf": [ { "type": "_enphase-envoy._tcp.local." diff --git a/requirements_all.txt b/requirements_all.txt index 9c893b72175..dcd43de5872 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1962,7 +1962,7 @@ pyeiscp==0.0.7 pyemoncms==0.1.1 # homeassistant.components.enphase_envoy -pyenphase==2.2.0 +pyenphase==2.2.1 # homeassistant.components.envisalink pyenvisalink==4.7 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 29be48c7f7d..8d866c7216e 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1637,7 +1637,7 @@ pyeiscp==0.0.7 pyemoncms==0.1.1 # homeassistant.components.enphase_envoy -pyenphase==2.2.0 +pyenphase==2.2.1 # homeassistant.components.everlights pyeverlights==0.1.0 From 991864a8afbf2bfcfcd831cc28fc98b80b0a87e3 Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Mon, 7 Jul 2025 11:43:39 +0200 Subject: [PATCH 100/106] Bump `gios` to version 6.1.0 (#148274) --- homeassistant/components/gios/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- tests/components/gios/__init__.py | 33 +++++-- tests/components/gios/fixtures/indexes.json | 63 +++++++----- tests/components/gios/fixtures/sensors.json | 56 +++++------ tests/components/gios/fixtures/station.json | 98 ++++++++----------- .../gios/snapshots/test_diagnostics.ambr | 2 + 8 files changed, 134 insertions(+), 124 deletions(-) diff --git a/homeassistant/components/gios/manifest.json b/homeassistant/components/gios/manifest.json index 8deb2eee414..ba87890de03 100644 --- a/homeassistant/components/gios/manifest.json +++ b/homeassistant/components/gios/manifest.json @@ -7,5 +7,5 @@ "integration_type": "service", "iot_class": "cloud_polling", "loggers": ["dacite", "gios"], - "requirements": ["gios==6.0.0"] + "requirements": ["gios==6.1.0"] } diff --git a/requirements_all.txt b/requirements_all.txt index dcd43de5872..95ffd1fcf9e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1020,7 +1020,7 @@ georss-qld-bushfire-alert-client==0.8 getmac==0.9.5 # homeassistant.components.gios -gios==6.0.0 +gios==6.1.0 # homeassistant.components.gitter gitterpy==0.1.7 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 8d866c7216e..2298062fb96 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -890,7 +890,7 @@ georss-qld-bushfire-alert-client==0.8 getmac==0.9.5 # homeassistant.components.gios -gios==6.0.0 +gios==6.1.0 # homeassistant.components.glances glances-api==0.8.0 diff --git a/tests/components/gios/__init__.py b/tests/components/gios/__init__.py index 49388428805..a4dc0a39be6 100644 --- a/tests/components/gios/__init__.py +++ b/tests/components/gios/__init__.py @@ -1,16 +1,29 @@ """Tests for GIOS.""" -import json from unittest.mock import patch from homeassistant.components.gios.const import DOMAIN from homeassistant.core import HomeAssistant -from tests.common import MockConfigEntry, async_load_fixture +from tests.common import ( + MockConfigEntry, + async_load_json_array_fixture, + async_load_json_object_fixture, +) STATIONS = [ - {"id": 123, "stationName": "Test Name 1", "gegrLat": "99.99", "gegrLon": "88.88"}, - {"id": 321, "stationName": "Test Name 2", "gegrLat": "77.77", "gegrLon": "66.66"}, + { + "Identyfikator stacji": 123, + "Nazwa stacji": "Test Name 1", + "WGS84 φ N": "99.99", + "WGS84 λ E": "88.88", + }, + { + "Identyfikator stacji": 321, + "Nazwa stacji": "Test Name 2", + "WGS84 φ N": "77.77", + "WGS84 λ E": "66.66", + }, ] @@ -26,13 +39,13 @@ async def init_integration( entry_id="86129426118ae32020417a53712d6eef", ) - indexes = json.loads(await async_load_fixture(hass, "indexes.json", DOMAIN)) - station = json.loads(await async_load_fixture(hass, "station.json", DOMAIN)) - sensors = json.loads(await async_load_fixture(hass, "sensors.json", DOMAIN)) + indexes = await async_load_json_object_fixture(hass, "indexes.json", DOMAIN) + station = await async_load_json_array_fixture(hass, "station.json", DOMAIN) + sensors = await async_load_json_object_fixture(hass, "sensors.json", DOMAIN) if incomplete_data: - indexes["stIndexLevel"]["indexLevelName"] = "foo" - sensors["pm10"]["values"][0]["value"] = None - sensors["pm10"]["values"][1]["value"] = None + indexes["AqIndex"] = "foo" + sensors["pm10"]["Lista danych pomiarowych"][0]["Wartość"] = None + sensors["pm10"]["Lista danych pomiarowych"][1]["Wartość"] = None if invalid_indexes: indexes = {} diff --git a/tests/components/gios/fixtures/indexes.json b/tests/components/gios/fixtures/indexes.json index c53d1c78f6e..1fb46e9a4d8 100644 --- a/tests/components/gios/fixtures/indexes.json +++ b/tests/components/gios/fixtures/indexes.json @@ -1,29 +1,38 @@ { - "id": 123, - "stCalcDate": "2020-07-31 15:10:17", - "stIndexLevel": { "id": 1, "indexLevelName": "Dobry" }, - "stSourceDataDate": "2020-07-31 14:00:00", - "so2CalcDate": "2020-07-31 15:10:17", - "so2IndexLevel": { "id": 0, "indexLevelName": "Bardzo dobry" }, - "so2SourceDataDate": "2020-07-31 14:00:00", - "no2CalcDate": 1596201017000, - "no2IndexLevel": { "id": 0, "indexLevelName": "Dobry" }, - "no2SourceDataDate": "2020-07-31 14:00:00", - "coCalcDate": "2020-07-31 15:10:17", - "coIndexLevel": { "id": 0, "indexLevelName": "Dobry" }, - "coSourceDataDate": "2020-07-31 14:00:00", - "pm10CalcDate": "2020-07-31 15:10:17", - "pm10IndexLevel": { "id": 0, "indexLevelName": "Dobry" }, - "pm10SourceDataDate": "2020-07-31 14:00:00", - "pm25CalcDate": "2020-07-31 15:10:17", - "pm25IndexLevel": { "id": 0, "indexLevelName": "Dobry" }, - "pm25SourceDataDate": "2020-07-31 14:00:00", - "o3CalcDate": "2020-07-31 15:10:17", - "o3IndexLevel": { "id": 1, "indexLevelName": "Dobry" }, - "o3SourceDataDate": "2020-07-31 14:00:00", - "c6h6CalcDate": "2020-07-31 15:10:17", - "c6h6IndexLevel": { "id": 0, "indexLevelName": "Bardzo dobry" }, - "c6h6SourceDataDate": "2020-07-31 14:00:00", - "stIndexStatus": true, - "stIndexCrParam": "OZON" + "AqIndex": { + "Identyfikator stacji pomiarowej": 123, + "Data wykonania obliczeń indeksu": "2020-07-31 15:10:17", + "Nazwa kategorii indeksu": "Dobry", + "Data danych źródłowych, z których policzono wartość indeksu dla wskaźnika st": "2020-07-31 14:00:00", + "Data wykonania obliczeń indeksu dla wskaźnika SO2": "2020-07-31 15:10:17", + "Wartość indeksu dla wskaźnika SO2": 0, + "Nazwa kategorii indeksu dla wskażnika SO2": "Bardzo dobry", + "Data danych źródłowych, z których policzono wartość indeksu dla wskaźnika SO2": "2020-07-31 14:00:00", + "Data wykonania obliczeń indeksu dla wskaźnika NO2": "2020-07-31 14:00:00", + "Wartość indeksu dla wskaźnika NO2": 0, + "Nazwa kategorii indeksu dla wskażnika NO2": "Dobry", + "Data danych źródłowych, z których policzono wartość indeksu dla wskaźnika NO2": "2020-07-31 14:00:00", + "Data danych źródłowych, z których policzono wartość indeksu dla wskaźnika CO": "2020-07-31 15:10:17", + "Wartość indeksu dla wskaźnika CO": 0, + "Nazwa kategorii indeksu dla wskażnika CO": "Dobry", + "Data wykonania obliczeń indeksu dla wskaźnika CO": "2020-07-31 14:00:00", + "Data danych źródłowych, z których policzono wartość indeksu dla wskaźnika PM10": "2020-07-31 15:10:17", + "Wartość indeksu dla wskaźnika PM10": 0, + "Nazwa kategorii indeksu dla wskażnika PM10": "Dobry", + "Data wykonania obliczeń indeksu dla wskaźnika PM10": "2020-07-31 14:00:00", + "Data danych źródłowych, z których policzono wartość indeksu dla wskaźnika PM2.5": "2020-07-31 15:10:17", + "Wartość indeksu dla wskaźnika PM2.5": 0, + "Nazwa kategorii indeksu dla wskażnika PM2.5": "Dobry", + "Data wykonania obliczeń indeksu dla wskaźnika PM2.5": "2020-07-31 14:00:00", + "Data danych źródłowych, z których policzono wartość indeksu dla wskaźnika O3": "2020-07-31 15:10:17", + "Wartość indeksu dla wskaźnika O3": 1, + "Nazwa kategorii indeksu dla wskażnika O3": "Dobry", + "Data wykonania obliczeń indeksu dla wskaźnika O3": "2020-07-31 14:00:00", + "Data danych źródłowych, z których policzono wartość indeksu dla wskaźnika C6H6": "2020-07-31 15:10:17", + "Wartość indeksu dla wskaźnika C6H6": 0, + "Nazwa kategorii indeksu dla wskażnika C6H6": "Bardzo dobry", + "Data wykonania obliczeń indeksu dla wskaźnika C6H6": "2020-07-31 14:00:00", + "Status indeksu ogólnego dla stacji pomiarowej": true, + "Kod zanieczyszczenia krytycznego": "OZON" + } } diff --git a/tests/components/gios/fixtures/sensors.json b/tests/components/gios/fixtures/sensors.json index db0cf2ff849..0fe387d3126 100644 --- a/tests/components/gios/fixtures/sensors.json +++ b/tests/components/gios/fixtures/sensors.json @@ -1,51 +1,51 @@ { "so2": { - "values": [ - { "date": "2020-07-31 15:00:00", "value": 4.35478 }, - { "date": "2020-07-31 14:00:00", "value": 4.25478 }, - { "date": "2020-07-31 13:00:00", "value": 4.34309 } + "Lista danych pomiarowych": [ + { "Data": "2020-07-31 15:00:00", "Wartość": 4.35478 }, + { "Data": "2020-07-31 14:00:00", "Wartość": 4.25478 }, + { "Data": "2020-07-31 13:00:00", "Wartość": 4.34309 } ] }, "c6h6": { - "values": [ - { "date": "2020-07-31 15:00:00", "value": 0.23789 }, - { "date": "2020-07-31 14:00:00", "value": 0.22789 }, - { "date": "2020-07-31 13:00:00", "value": 0.21315 } + "Lista danych pomiarowych": [ + { "Data": "2020-07-31 15:00:00", "Wartość": 0.23789 }, + { "Data": "2020-07-31 14:00:00", "Wartość": 0.22789 }, + { "Data": "2020-07-31 13:00:00", "Wartość": 0.21315 } ] }, "co": { - "values": [ - { "date": "2020-07-31 15:00:00", "value": 251.874 }, - { "date": "2020-07-31 14:00:00", "value": 250.874 }, - { "date": "2020-07-31 13:00:00", "value": 251.097 } + "Lista danych pomiarowych": [ + { "Data": "2020-07-31 15:00:00", "Wartość": 251.874 }, + { "Data": "2020-07-31 14:00:00", "Wartość": 250.874 }, + { "Data": "2020-07-31 13:00:00", "Wartość": 251.097 } ] }, "no2": { - "values": [ - { "date": "2020-07-31 15:00:00", "value": 7.13411 }, - { "date": "2020-07-31 14:00:00", "value": 7.33411 }, - { "date": "2020-07-31 13:00:00", "value": 9.32578 } + "Lista danych pomiarowych": [ + { "Data": "2020-07-31 15:00:00", "Wartość": 7.13411 }, + { "Data": "2020-07-31 14:00:00", "Wartość": 7.33411 }, + { "Data": "2020-07-31 13:00:00", "Wartość": 9.32578 } ] }, "o3": { - "values": [ - { "date": "2020-07-31 15:00:00", "value": 95.7768 }, - { "date": "2020-07-31 14:00:00", "value": 93.7768 }, - { "date": "2020-07-31 13:00:00", "value": 89.4232 } + "Lista danych pomiarowych": [ + { "Data": "2020-07-31 15:00:00", "Wartość": 95.7768 }, + { "Data": "2020-07-31 14:00:00", "Wartość": 93.7768 }, + { "Data": "2020-07-31 13:00:00", "Wartość": 89.4232 } ] }, "pm2.5": { - "values": [ - { "date": "2020-07-31 15:00:00", "value": 4 }, - { "date": "2020-07-31 14:00:00", "value": 4 }, - { "date": "2020-07-31 13:00:00", "value": 5 } + "Lista danych pomiarowych": [ + { "Data": "2020-07-31 15:00:00", "Wartość": 4 }, + { "Data": "2020-07-31 14:00:00", "Wartość": 4 }, + { "Data": "2020-07-31 13:00:00", "Wartość": 5 } ] }, "pm10": { - "values": [ - { "date": "2020-07-31 15:00:00", "value": 16.8344 }, - { "date": "2020-07-31 14:00:00", "value": 17.8344 }, - { "date": "2020-07-31 13:00:00", "value": 20.8094 } + "Lista danych pomiarowych": [ + { "Data": "2020-07-31 15:00:00", "Wartość": 16.8344 }, + { "Data": "2020-07-31 14:00:00", "Wartość": 17.8344 }, + { "Data": "2020-07-31 13:00:00", "Wartość": 20.8094 } ] } } diff --git a/tests/components/gios/fixtures/station.json b/tests/components/gios/fixtures/station.json index 16cd824a489..167e4db3aee 100644 --- a/tests/components/gios/fixtures/station.json +++ b/tests/components/gios/fixtures/station.json @@ -1,72 +1,58 @@ [ { - "id": 672, - "stationId": 117, - "param": { - "paramName": "dwutlenek siarki", - "paramFormula": "SO2", - "paramCode": "SO2", - "idParam": 1 - } + "Identyfikator stanowiska": 672, + "Identyfikator stacji": 117, + "Wskaźnik": "dwutlenek siarki", + "Wskaźnik - wzór": "SO2", + "Wskaźnik - kod": "SO2", + "Id wskaźnika": 1 }, { - "id": 658, - "stationId": 117, - "param": { - "paramName": "benzen", - "paramFormula": "C6H6", - "paramCode": "C6H6", - "idParam": 10 - } + "Identyfikator stanowiska": 658, + "Identyfikator stacji": 117, + "Wskaźnik": "benzen", + "Wskaźnik - wzór": "C6H6", + "Wskaźnik - kod": "C6H6", + "Id wskaźnika": 10 }, { - "id": 660, - "stationId": 117, - "param": { - "paramName": "tlenek węgla", - "paramFormula": "CO", - "paramCode": "CO", - "idParam": 8 - } + "Identyfikator stanowiska": 660, + "Identyfikator stacji": 117, + "Wskaźnik": "tlenek węgla", + "Wskaźnik - wzór": "CO", + "Wskaźnik - kod": "CO", + "Id wskaźnika": 8 }, { - "id": 665, - "stationId": 117, - "param": { - "paramName": "dwutlenek azotu", - "paramFormula": "NO2", - "paramCode": "NO2", - "idParam": 6 - } + "Identyfikator stanowiska": 665, + "Identyfikator stacji": 117, + "Wskaźnik": "dwutlenek azotu", + "Wskaźnik - wzór": "NO2", + "Wskaźnik - kod": "NO2", + "Id wskaźnika": 6 }, { - "id": 667, - "stationId": 117, - "param": { - "paramName": "ozon", - "paramFormula": "O3", - "paramCode": "O3", - "idParam": 5 - } + "Identyfikator stanowiska": 667, + "Identyfikator stacji": 117, + "Wskaźnik": "ozon", + "Wskaźnik - wzór": "O3", + "Wskaźnik - kod": "O3", + "Id wskaźnika": 5 }, { - "id": 670, - "stationId": 117, - "param": { - "paramName": "pył zawieszony PM2.5", - "paramFormula": "PM2.5", - "paramCode": "PM2.5", - "idParam": 69 - } + "Identyfikator stanowiska": 670, + "Identyfikator stacji": 117, + "Wskaźnik": "pył zawieszony PM2.5", + "Wskaźnik - wzór": "PM2.5", + "Wskaźnik - kod": "PM2.5", + "Id wskaźnika": 69 }, { - "id": 14395, - "stationId": 117, - "param": { - "paramName": "pył zawieszony PM10", - "paramFormula": "PM10", - "paramCode": "PM10", - "idParam": 3 - } + "Identyfikator stanowiska": 14395, + "Identyfikator stacji": 117, + "Wskaźnik": "pył zawieszony PM10", + "Wskaźnik - wzór": "PM10", + "Wskaźnik - kod": "PM10", + "Id wskaźnika": 3 } ] diff --git a/tests/components/gios/snapshots/test_diagnostics.ambr b/tests/components/gios/snapshots/test_diagnostics.ambr index 890edc00482..4095bf8bf53 100644 --- a/tests/components/gios/snapshots/test_diagnostics.ambr +++ b/tests/components/gios/snapshots/test_diagnostics.ambr @@ -42,12 +42,14 @@ 'name': 'carbon monoxide', 'value': 251.874, }), + 'no': None, 'no2': dict({ 'id': 665, 'index': 'good', 'name': 'nitrogen dioxide', 'value': 7.13411, }), + 'nox': None, 'o3': dict({ 'id': 667, 'index': 'good', From 42b50c71ec064297fb6623ae284305183be35f4e Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Mon, 7 Jul 2025 11:54:36 +0200 Subject: [PATCH 101/106] Revert "Add tests for Sonos Alarms" (#148319) --- tests/components/sonos/conftest.py | 28 +------------- tests/components/sonos/test_init.py | 5 --- tests/components/sonos/test_switch.py | 54 +-------------------------- 3 files changed, 3 insertions(+), 84 deletions(-) diff --git a/tests/components/sonos/conftest.py b/tests/components/sonos/conftest.py index a2a4e53cae4..d3de2a889d5 100644 --- a/tests/components/sonos/conftest.py +++ b/tests/components/sonos/conftest.py @@ -214,25 +214,12 @@ class MockSoCo(MagicMock): surround_level = 3 music_surround_level = 4 soundbar_audio_input_format = "Dolby 5.1" - factory: SoCoMockFactory | None = None @property def visible_zones(self): """Return visible zones and allow property to be overridden by device classes.""" return {self} - @property - def all_zones(self) -> set[MockSoCo]: - """Return a set of all mock zones, or just self if no factory or zones.""" - if self.factory is not None: - if zones := self.factory.mock_all_zones: - return zones - return {self} - - def set_factory(self, factory: SoCoMockFactory) -> None: - """Set the factory for this mock.""" - self.factory = factory - class SoCoMockFactory: """Factory for creating SoCo Mocks.""" @@ -257,19 +244,11 @@ class SoCoMockFactory: self.sonos_playlists = sonos_playlists self.sonos_queue = sonos_queue - @property - def mock_all_zones(self) -> set[MockSoCo]: - """Return a set of all mock zones.""" - return { - mock for mock in self.mock_list.values() if mock.mock_include_in_all_zones - } - def cache_mock( self, mock_soco: MockSoCo, ip_address: str, name: str = "Zone A" ) -> MockSoCo: """Put a user created mock into the cache.""" mock_soco.mock_add_spec(SoCo) - mock_soco.set_factory(self) mock_soco.ip_address = ip_address if ip_address != "192.168.42.2": mock_soco.uid += f"_{ip_address}" @@ -281,11 +260,6 @@ class SoCoMockFactory: my_speaker_info = self.speaker_info.copy() my_speaker_info["zone_name"] = name my_speaker_info["uid"] = mock_soco.uid - # Generate a different MAC for the non-default speakers. - # otherwise new devices will not be created. - if ip_address != "192.168.42.2": - last_octet = ip_address.split(".")[-1] - my_speaker_info["mac_address"] = f"00-00-00-00-00-{last_octet.zfill(2)}" mock_soco.get_speaker_info = Mock(return_value=my_speaker_info) mock_soco.add_to_queue = Mock(return_value=10) mock_soco.add_uri_to_queue = Mock(return_value=10) @@ -304,7 +278,7 @@ class SoCoMockFactory: mock_soco.alarmClock = self.alarm_clock mock_soco.get_battery_info.return_value = self.battery_info - mock_soco.mock_include_in_all_zones = True + mock_soco.all_zones = {mock_soco} mock_soco.group.coordinator = mock_soco mock_soco.household_id = "test_household_id" self.mock_list[ip_address] = mock_soco diff --git a/tests/components/sonos/test_init.py b/tests/components/sonos/test_init.py index 901ae359917..c1b98b2ec60 100644 --- a/tests/components/sonos/test_init.py +++ b/tests/components/sonos/test_init.py @@ -324,15 +324,10 @@ async def test_async_poll_manual_hosts_5( soco_1 = soco_factory.cache_mock(MockSoCo(), "10.10.10.1", "Living Room") soco_1.renderingControl = Mock() soco_1.renderingControl.GetVolume = Mock() - # Unavailable speakers should not be included in all zones - soco_1.mock_include_in_all_zones = False - speaker_1_activity = SpeakerActivity(hass, soco_1) soco_2 = soco_factory.cache_mock(MockSoCo(), "10.10.10.2", "Bedroom") soco_2.renderingControl = Mock() soco_2.renderingControl.GetVolume = Mock() - soco_2.mock_include_in_all_zones = False - speaker_2_activity = SpeakerActivity(hass, soco_2) with caplog.at_level(logging.DEBUG): diff --git a/tests/components/sonos/test_switch.py b/tests/components/sonos/test_switch.py index 56dd96b0caf..04457ee95c7 100644 --- a/tests/components/sonos/test_switch.py +++ b/tests/components/sonos/test_switch.py @@ -26,10 +26,10 @@ from homeassistant.const import ( STATE_ON, ) from homeassistant.core import HomeAssistant -from homeassistant.helpers import device_registry as dr, entity_registry as er +from homeassistant.helpers import entity_registry as er from homeassistant.util import dt as dt_util -from .conftest import MockSoCo, SonosMockEvent, SonosMockService +from .conftest import MockSoCo, SonosMockEvent from tests.common import async_fire_time_changed @@ -211,53 +211,3 @@ async def test_alarm_create_delete( assert "switch.sonos_alarm_14" in entity_registry.entities assert "switch.sonos_alarm_15" not in entity_registry.entities - - -async def test_alarm_change_device( - hass: HomeAssistant, - async_setup_sonos, - soco: MockSoCo, - alarm_clock: SonosMockService, - alarm_clock_extended: SonosMockService, - alarm_event: SonosMockEvent, - entity_registry: er.EntityRegistry, - device_registry: dr.DeviceRegistry, - sonos_setup_two_speakers: list[MockSoCo], -) -> None: - """Test Sonos Alarm being moved to a different speaker. - - This test simulates a scenario where an alarm is created on one speaker - and then moved to another speaker. It checks that the entity is correctly - created on the new speaker and removed from the old one. - """ - entity_id = "switch.sonos_alarm_14" - soco_lr = sonos_setup_two_speakers[0] - - await async_setup_sonos() - - # Initially, the alarm is created on the soco mock - assert entity_id in entity_registry.entities - entity = entity_registry.async_get(entity_id) - device = device_registry.async_get(entity.device_id) - assert device.name == soco.get_speaker_info()["zone_name"] - - # Simulate the alarm being moved to the soco_lr speaker - alarm_update = copy(alarm_clock_extended.ListAlarms.return_value) - alarm_update["CurrentAlarmList"] = alarm_update["CurrentAlarmList"].replace( - "RINCON_test", f"{soco_lr.uid}" - ) - alarm_clock.ListAlarms.return_value = alarm_update - - # Update the alarm_list_version so it gets processed. - alarm_event.variables["alarm_list_version"] = f"{soco_lr.uid}:1000" - alarm_update["CurrentAlarmListVersion"] = alarm_event.increment_variable( - "alarm_list_version" - ) - - alarm_clock.subscribe.return_value.callback(event=alarm_event) - await hass.async_block_till_done(wait_background_tasks=True) - - assert entity_id in entity_registry.entities - alarm_14 = entity_registry.async_get(entity_id) - device = device_registry.async_get(alarm_14.device_id) - assert device.name == soco_lr.get_speaker_info()["zone_name"] From 0c783e87d1f5a4d5e669d6cfe888d45fb98138b4 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Mon, 7 Jul 2025 11:59:35 +0200 Subject: [PATCH 102/106] Fix homee test (#148322) --- tests/components/homee/test_init.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/components/homee/test_init.py b/tests/components/homee/test_init.py index 0b2ae21a8d0..c24cb39295d 100644 --- a/tests/components/homee/test_init.py +++ b/tests/components/homee/test_init.py @@ -18,10 +18,18 @@ from tests.common import MockConfigEntry @pytest.mark.parametrize( - "side_eff", + ("side_eff", "config_entry_state", "active_flows"), [ - HomeeConnectionFailedException("connection timed out"), - HomeeAuthFailedException("wrong username or password"), + ( + HomeeConnectionFailedException("connection timed out"), + ConfigEntryState.SETUP_RETRY, + [], + ), + ( + HomeeAuthFailedException("wrong username or password"), + ConfigEntryState.SETUP_ERROR, + ["reauth"], + ), ], ) async def test_connection_errors( @@ -29,6 +37,8 @@ async def test_connection_errors( mock_homee: MagicMock, mock_config_entry: MockConfigEntry, side_eff: Exception, + config_entry_state: ConfigEntryState, + active_flows: list[str], ) -> None: """Test if connection errors on startup are handled correctly.""" mock_homee.get_access_token.side_effect = side_eff @@ -36,7 +46,11 @@ async def test_connection_errors( await hass.config_entries.async_setup(mock_config_entry.entry_id) - assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY + assert mock_config_entry.state is config_entry_state + + assert [ + flow["context"]["source"] for flow in hass.config_entries.flow.async_progress() + ] == active_flows async def test_connection_listener( From 15c9ddea78365cb9c058b40bc523221db73ae1a6 Mon Sep 17 00:00:00 2001 From: tronikos Date: Mon, 7 Jul 2025 04:10:50 -0700 Subject: [PATCH 103/106] Bump gassist-text to 0.0.14 (#148312) --- homeassistant/components/google_assistant_sdk/helpers.py | 4 ++-- homeassistant/components/google_assistant_sdk/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/google_assistant_sdk/helpers.py b/homeassistant/components/google_assistant_sdk/helpers.py index b319e1e432c..c40c848ff3f 100644 --- a/homeassistant/components/google_assistant_sdk/helpers.py +++ b/homeassistant/components/google_assistant_sdk/helpers.py @@ -80,10 +80,10 @@ async def async_send_text_commands( credentials = Credentials(session.token[CONF_ACCESS_TOKEN]) # type: ignore[no-untyped-call] language_code = entry.options.get(CONF_LANGUAGE_CODE, default_language_code(hass)) + command_response_list = [] with TextAssistant( credentials, language_code, audio_out=bool(media_players) ) as assistant: - command_response_list = [] for command in commands: try: resp = await hass.async_add_executor_job(assistant.assist, command) @@ -117,7 +117,7 @@ async def async_send_text_commands( blocking=True, ) command_response_list.append(CommandResponse(text_response)) - return command_response_list + return command_response_list def default_language_code(hass: HomeAssistant) -> str: diff --git a/homeassistant/components/google_assistant_sdk/manifest.json b/homeassistant/components/google_assistant_sdk/manifest.json index 70e93f39f42..5a6a42c394c 100644 --- a/homeassistant/components/google_assistant_sdk/manifest.json +++ b/homeassistant/components/google_assistant_sdk/manifest.json @@ -7,6 +7,6 @@ "documentation": "https://www.home-assistant.io/integrations/google_assistant_sdk", "integration_type": "service", "iot_class": "cloud_polling", - "requirements": ["gassist-text==0.0.12"], + "requirements": ["gassist-text==0.0.14"], "single_config_entry": true } diff --git a/requirements_all.txt b/requirements_all.txt index 95ffd1fcf9e..73f7819e6f3 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -989,7 +989,7 @@ gTTS==2.5.3 gardena-bluetooth==1.6.0 # homeassistant.components.google_assistant_sdk -gassist-text==0.0.12 +gassist-text==0.0.14 # homeassistant.components.google gcal-sync==7.1.0 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 2298062fb96..a17bf245623 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -859,7 +859,7 @@ gTTS==2.5.3 gardena-bluetooth==1.6.0 # homeassistant.components.google_assistant_sdk -gassist-text==0.0.12 +gassist-text==0.0.14 # homeassistant.components.google gcal-sync==7.1.0 From 448d6041e5e85221701456d1181b156d4f158954 Mon Sep 17 00:00:00 2001 From: Norbert Rittel Date: Mon, 7 Jul 2025 14:06:13 +0200 Subject: [PATCH 104/106] Fix missing sentence-casing in `wallbox` (#148332) --- homeassistant/components/wallbox/strings.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/wallbox/strings.json b/homeassistant/components/wallbox/strings.json index a69251eb832..13f038d14b6 100644 --- a/homeassistant/components/wallbox/strings.json +++ b/homeassistant/components/wallbox/strings.json @@ -3,14 +3,14 @@ "step": { "user": { "data": { - "station": "Station Serial Number", + "station": "Station serial number", "username": "[%key:common::config_flow::data::username%]", "password": "[%key:common::config_flow::data::password%]" }, "data_description": { - "station": "Serial number of the charger, this value can be found in the Wallbox App or in the Wallbox Portal.", - "username": "Username for your Wallbox Account.", - "password": "Password for your Wallbox Account." + "station": "Serial number of the charger. Can be found in the Wallbox app or in the Wallbox portal.", + "username": "Username for your Wallbox account.", + "password": "Password for your Wallbox account." } }, "reauth_confirm": { @@ -24,7 +24,7 @@ "cannot_connect": "[%key:common::config_flow::error::cannot_connect%]", "invalid_auth": "[%key:common::config_flow::error::invalid_auth%]", "unknown": "[%key:common::config_flow::error::unknown%]", - "reauth_invalid": "Re-authentication failed; Serial Number does not match original" + "reauth_invalid": "Re-authentication failed; serial number does not match original" }, "abort": { "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", From c60e06d32f30d59abe06b90cfdad925e3a8d7364 Mon Sep 17 00:00:00 2001 From: Norbert Rittel Date: Mon, 7 Jul 2025 14:06:27 +0200 Subject: [PATCH 105/106] Fix missing sentence-casing and spelling of "REST" in `iskra` (#148330) --- homeassistant/components/iskra/strings.json | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/iskra/strings.json b/homeassistant/components/iskra/strings.json index 5818cdfa1db..da7817cc78b 100644 --- a/homeassistant/components/iskra/strings.json +++ b/homeassistant/components/iskra/strings.json @@ -2,8 +2,8 @@ "config": { "step": { "user": { - "title": "Configure Iskra Device", - "description": "Enter the IP address of your Iskra Device and select protocol.", + "title": "Configure Iskra device", + "description": "Enter the IP address of your Iskra device and select protocol.", "data": { "host": "[%key:common::config_flow::data::host%]" }, @@ -12,7 +12,7 @@ } }, "authentication": { - "title": "Configure Rest API Credentials", + "title": "Configure REST API credentials", "description": "Enter username and password", "data": { "username": "[%key:common::config_flow::data::username%]", @@ -44,7 +44,7 @@ "selector": { "protocol": { "options": { - "rest_api": "Rest API", + "rest_api": "REST API", "modbus_tcp": "Modbus TCP" } } From b71bcb002b75f211c0fbfd1d03da2fd5f06011ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ab=C3=ADlio=20Costa?= Date: Mon, 7 Jul 2025 13:48:48 +0100 Subject: [PATCH 106/106] Move target selector extractor method to common module (#148087) --- .../components/homeassistant/__init__.py | 9 +- homeassistant/components/homekit/__init__.py | 13 +- homeassistant/components/lifx/manager.py | 10 +- .../components/unifiprotect/services.py | 15 +- homeassistant/helpers/service.py | 259 ++-------- homeassistant/helpers/target.py | 240 +++++++++ tests/helpers/test_service.py | 78 +++ tests/helpers/test_target.py | 459 ++++++++++++++++++ 8 files changed, 855 insertions(+), 228 deletions(-) create mode 100644 homeassistant/helpers/target.py create mode 100644 tests/helpers/test_target.py diff --git a/homeassistant/components/homeassistant/__init__.py b/homeassistant/components/homeassistant/__init__.py index d5dabfa2e08..32fe690f0f1 100644 --- a/homeassistant/components/homeassistant/__init__.py +++ b/homeassistant/components/homeassistant/__init__.py @@ -44,11 +44,14 @@ from homeassistant.helpers.entity_component import async_update_entity from homeassistant.helpers.issue_registry import IssueSeverity from homeassistant.helpers.service import ( async_extract_config_entry_ids, - async_extract_referenced_entity_ids, async_register_admin_service, ) from homeassistant.helpers.signal import KEY_HA_STOP from homeassistant.helpers.system_info import async_get_system_info +from homeassistant.helpers.target import ( + TargetSelectorData, + async_extract_referenced_entity_ids, +) from homeassistant.helpers.template import async_load_custom_templates from homeassistant.helpers.typing import ConfigType @@ -111,7 +114,9 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # noqa: async def async_handle_turn_service(service: ServiceCall) -> None: """Handle calls to homeassistant.turn_on/off.""" - referenced = async_extract_referenced_entity_ids(hass, service) + referenced = async_extract_referenced_entity_ids( + hass, TargetSelectorData(service.data) + ) all_referenced = referenced.referenced | referenced.indirectly_referenced # Generic turn on/off method requires entity id diff --git a/homeassistant/components/homekit/__init__.py b/homeassistant/components/homekit/__init__.py index 8b526b62302..50b11265cf4 100644 --- a/homeassistant/components/homekit/__init__.py +++ b/homeassistant/components/homekit/__init__.py @@ -75,11 +75,12 @@ from homeassistant.helpers.entityfilter import ( EntityFilter, ) from homeassistant.helpers.reload import async_integration_yaml_config -from homeassistant.helpers.service import ( - async_extract_referenced_entity_ids, - async_register_admin_service, -) +from homeassistant.helpers.service import async_register_admin_service from homeassistant.helpers.start import async_at_started +from homeassistant.helpers.target import ( + TargetSelectorData, + async_extract_referenced_entity_ids, +) from homeassistant.helpers.typing import ConfigType from homeassistant.loader import IntegrationNotFound, async_get_integration from homeassistant.util.async_ import create_eager_task @@ -482,7 +483,9 @@ def _async_register_events_and_services(hass: HomeAssistant) -> None: async def async_handle_homekit_unpair(service: ServiceCall) -> None: """Handle unpair HomeKit service call.""" - referenced = async_extract_referenced_entity_ids(hass, service) + referenced = async_extract_referenced_entity_ids( + hass, TargetSelectorData(service.data) + ) dev_reg = dr.async_get(hass) for device_id in referenced.referenced_devices: if not (dev_reg_ent := dev_reg.async_get(device_id)): diff --git a/homeassistant/components/lifx/manager.py b/homeassistant/components/lifx/manager.py index 33712441157..f2e37426736 100644 --- a/homeassistant/components/lifx/manager.py +++ b/homeassistant/components/lifx/manager.py @@ -28,7 +28,10 @@ from homeassistant.components.light import ( from homeassistant.const import ATTR_MODE from homeassistant.core import HomeAssistant, ServiceCall, callback from homeassistant.helpers import config_validation as cv -from homeassistant.helpers.service import async_extract_referenced_entity_ids +from homeassistant.helpers.target import ( + TargetSelectorData, + async_extract_referenced_entity_ids, +) from .const import _ATTR_COLOR_TEMP, ATTR_THEME, DOMAIN from .coordinator import LIFXConfigEntry, LIFXUpdateCoordinator @@ -268,7 +271,9 @@ class LIFXManager: async def service_handler(service: ServiceCall) -> None: """Apply a service, i.e. start an effect.""" - referenced = async_extract_referenced_entity_ids(self.hass, service) + referenced = async_extract_referenced_entity_ids( + self.hass, TargetSelectorData(service.data) + ) all_referenced = referenced.referenced | referenced.indirectly_referenced if all_referenced: await self.start_effect(all_referenced, service.service, **service.data) @@ -499,6 +504,5 @@ class LIFXManager: if self.entry_id_to_entity_id[entry.entry_id] in entity_ids: coordinators.append(entry.runtime_data) bulbs.append(entry.runtime_data.device) - if start_effect_func := self._effect_dispatch.get(service): await start_effect_func(self, bulbs, coordinators, **kwargs) diff --git a/homeassistant/components/unifiprotect/services.py b/homeassistant/components/unifiprotect/services.py index 40fe0a991f2..708a4883ddd 100644 --- a/homeassistant/components/unifiprotect/services.py +++ b/homeassistant/components/unifiprotect/services.py @@ -26,7 +26,10 @@ from homeassistant.helpers import ( device_registry as dr, entity_registry as er, ) -from homeassistant.helpers.service import async_extract_referenced_entity_ids +from homeassistant.helpers.target import ( + TargetSelectorData, + async_extract_referenced_entity_ids, +) from homeassistant.util.json import JsonValueType from homeassistant.util.read_only_dict import ReadOnlyDict @@ -115,7 +118,7 @@ def _async_get_ufp_instance(hass: HomeAssistant, device_id: str) -> ProtectApiCl @callback def _async_get_ufp_camera(call: ServiceCall) -> Camera: - ref = async_extract_referenced_entity_ids(call.hass, call) + ref = async_extract_referenced_entity_ids(call.hass, TargetSelectorData(call.data)) entity_registry = er.async_get(call.hass) entity_id = ref.indirectly_referenced.pop() @@ -133,7 +136,7 @@ def _async_get_protect_from_call(call: ServiceCall) -> set[ProtectApiClient]: return { _async_get_ufp_instance(call.hass, device_id) for device_id in async_extract_referenced_entity_ids( - call.hass, call + call.hass, TargetSelectorData(call.data) ).referenced_devices } @@ -196,7 +199,7 @@ def _async_unique_id_to_mac(unique_id: str) -> str: async def set_chime_paired_doorbells(call: ServiceCall) -> None: """Set paired doorbells on chime.""" - ref = async_extract_referenced_entity_ids(call.hass, call) + ref = async_extract_referenced_entity_ids(call.hass, TargetSelectorData(call.data)) entity_registry = er.async_get(call.hass) entity_id = ref.indirectly_referenced.pop() @@ -211,7 +214,9 @@ async def set_chime_paired_doorbells(call: ServiceCall) -> None: assert chime is not None call.data = ReadOnlyDict(call.data.get("doorbells") or {}) - doorbell_refs = async_extract_referenced_entity_ids(call.hass, call) + doorbell_refs = async_extract_referenced_entity_ids( + call.hass, TargetSelectorData(call.data) + ) doorbell_ids: set[str] = set() for camera_id in doorbell_refs.referenced | doorbell_refs.indirectly_referenced: doorbell_sensor = entity_registry.async_get(camera_id) diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index c7d4a26c86e..1d4dac10c27 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -9,17 +9,13 @@ from enum import Enum from functools import cache, partial import logging from types import ModuleType -from typing import TYPE_CHECKING, Any, TypedDict, TypeGuard, cast +from typing import TYPE_CHECKING, Any, TypedDict, cast, override import voluptuous as vol from homeassistant.auth.permissions.const import CAT_ENTITIES, POLICY_CONTROL from homeassistant.const import ( - ATTR_AREA_ID, - ATTR_DEVICE_ID, ATTR_ENTITY_ID, - ATTR_FLOOR_ID, - ATTR_LABEL_ID, CONF_ACTION, CONF_ENTITY_ID, CONF_SERVICE_DATA, @@ -54,16 +50,14 @@ from homeassistant.util.yaml import load_yaml_dict from homeassistant.util.yaml.loader import JSON_TYPE from . import ( - area_registry, config_validation as cv, device_registry, entity_registry, - floor_registry, - label_registry, + target as target_helpers, template, translation, ) -from .group import expand_entity_ids +from .deprecation import deprecated_class, deprecated_function from .selector import TargetSelector from .typing import ConfigType, TemplateVarsType, VolDictType, VolSchemaType @@ -225,87 +219,31 @@ class ServiceParams(TypedDict): target: dict | None -class ServiceTargetSelector: +@deprecated_class( + "homeassistant.helpers.target.TargetSelectorData", + breaks_in_ha_version="2026.8", +) +class ServiceTargetSelector(target_helpers.TargetSelectorData): """Class to hold a target selector for a service.""" - __slots__ = ("area_ids", "device_ids", "entity_ids", "floor_ids", "label_ids") - def __init__(self, service_call: ServiceCall) -> None: """Extract ids from service call data.""" - service_call_data = service_call.data - entity_ids: str | list | None = service_call_data.get(ATTR_ENTITY_ID) - device_ids: str | list | None = service_call_data.get(ATTR_DEVICE_ID) - area_ids: str | list | None = service_call_data.get(ATTR_AREA_ID) - floor_ids: str | list | None = service_call_data.get(ATTR_FLOOR_ID) - label_ids: str | list | None = service_call_data.get(ATTR_LABEL_ID) - - self.entity_ids = ( - set(cv.ensure_list(entity_ids)) if _has_match(entity_ids) else set() - ) - self.device_ids = ( - set(cv.ensure_list(device_ids)) if _has_match(device_ids) else set() - ) - self.area_ids = set(cv.ensure_list(area_ids)) if _has_match(area_ids) else set() - self.floor_ids = ( - set(cv.ensure_list(floor_ids)) if _has_match(floor_ids) else set() - ) - self.label_ids = ( - set(cv.ensure_list(label_ids)) if _has_match(label_ids) else set() - ) - - @property - def has_any_selector(self) -> bool: - """Determine if any selectors are present.""" - return bool( - self.entity_ids - or self.device_ids - or self.area_ids - or self.floor_ids - or self.label_ids - ) + super().__init__(service_call.data) -@dataclasses.dataclass(slots=True) -class SelectedEntities: +@deprecated_class( + "homeassistant.helpers.target.SelectedEntities", + breaks_in_ha_version="2026.8", +) +class SelectedEntities(target_helpers.SelectedEntities): """Class to hold the selected entities.""" - # Entities that were explicitly mentioned. - referenced: set[str] = dataclasses.field(default_factory=set) - - # Entities that were referenced via device/area/floor/label ID. - # Should not trigger a warning when they don't exist. - indirectly_referenced: set[str] = dataclasses.field(default_factory=set) - - # Referenced items that could not be found. - missing_devices: set[str] = dataclasses.field(default_factory=set) - missing_areas: set[str] = dataclasses.field(default_factory=set) - missing_floors: set[str] = dataclasses.field(default_factory=set) - missing_labels: set[str] = dataclasses.field(default_factory=set) - - # Referenced devices - referenced_devices: set[str] = dataclasses.field(default_factory=set) - referenced_areas: set[str] = dataclasses.field(default_factory=set) - - def log_missing(self, missing_entities: set[str]) -> None: + @override + def log_missing( + self, missing_entities: set[str], logger: logging.Logger | None = None + ) -> None: """Log about missing items.""" - parts = [] - for label, items in ( - ("floors", self.missing_floors), - ("areas", self.missing_areas), - ("devices", self.missing_devices), - ("entities", missing_entities), - ("labels", self.missing_labels), - ): - if items: - parts.append(f"{label} {', '.join(sorted(items))}") - - if not parts: - return - - _LOGGER.warning( - "Referenced %s are missing or not currently available", - ", ".join(parts), - ) + super().log_missing(missing_entities, logger or _LOGGER) @bind_hass @@ -466,7 +404,10 @@ async def async_extract_entities[_EntityT: Entity]( if data_ent_id == ENTITY_MATCH_ALL: return [entity for entity in entities if entity.available] - referenced = async_extract_referenced_entity_ids(hass, service_call, expand_group) + selector_data = target_helpers.TargetSelectorData(service_call.data) + referenced = target_helpers.async_extract_referenced_entity_ids( + hass, selector_data, expand_group + ) combined = referenced.referenced | referenced.indirectly_referenced found = [] @@ -482,7 +423,7 @@ async def async_extract_entities[_EntityT: Entity]( found.append(entity) - referenced.log_missing(referenced.referenced & combined) + referenced.log_missing(referenced.referenced & combined, _LOGGER) return found @@ -495,141 +436,27 @@ async def async_extract_entity_ids( Will convert group entity ids to the entity ids it represents. """ - referenced = async_extract_referenced_entity_ids(hass, service_call, expand_group) + selector_data = target_helpers.TargetSelectorData(service_call.data) + referenced = target_helpers.async_extract_referenced_entity_ids( + hass, selector_data, expand_group + ) return referenced.referenced | referenced.indirectly_referenced -def _has_match(ids: str | list[str] | None) -> TypeGuard[str | list[str]]: - """Check if ids can match anything.""" - return ids not in (None, ENTITY_MATCH_NONE) - - +@deprecated_function( + "homeassistant.helpers.target.async_extract_referenced_entity_ids", + breaks_in_ha_version="2026.8", +) @bind_hass def async_extract_referenced_entity_ids( hass: HomeAssistant, service_call: ServiceCall, expand_group: bool = True ) -> SelectedEntities: """Extract referenced entity IDs from a service call.""" - selector = ServiceTargetSelector(service_call) - selected = SelectedEntities() - - if not selector.has_any_selector: - return selected - - entity_ids: set[str] | list[str] = selector.entity_ids - if expand_group: - entity_ids = expand_entity_ids(hass, entity_ids) - - selected.referenced.update(entity_ids) - - if ( - not selector.device_ids - and not selector.area_ids - and not selector.floor_ids - and not selector.label_ids - ): - return selected - - entities = entity_registry.async_get(hass).entities - dev_reg = device_registry.async_get(hass) - area_reg = area_registry.async_get(hass) - - if selector.floor_ids: - floor_reg = floor_registry.async_get(hass) - for floor_id in selector.floor_ids: - if floor_id not in floor_reg.floors: - selected.missing_floors.add(floor_id) - - for area_id in selector.area_ids: - if area_id not in area_reg.areas: - selected.missing_areas.add(area_id) - - for device_id in selector.device_ids: - if device_id not in dev_reg.devices: - selected.missing_devices.add(device_id) - - if selector.label_ids: - label_reg = label_registry.async_get(hass) - for label_id in selector.label_ids: - if label_id not in label_reg.labels: - selected.missing_labels.add(label_id) - - for entity_entry in entities.get_entries_for_label(label_id): - if ( - entity_entry.entity_category is None - and entity_entry.hidden_by is None - ): - selected.indirectly_referenced.add(entity_entry.entity_id) - - for device_entry in dev_reg.devices.get_devices_for_label(label_id): - selected.referenced_devices.add(device_entry.id) - - for area_entry in area_reg.areas.get_areas_for_label(label_id): - selected.referenced_areas.add(area_entry.id) - - # Find areas for targeted floors - if selector.floor_ids: - selected.referenced_areas.update( - area_entry.id - for floor_id in selector.floor_ids - for area_entry in area_reg.areas.get_areas_for_floor(floor_id) - ) - - selected.referenced_areas.update(selector.area_ids) - selected.referenced_devices.update(selector.device_ids) - - if not selected.referenced_areas and not selected.referenced_devices: - return selected - - # Add indirectly referenced by device - selected.indirectly_referenced.update( - entry.entity_id - for device_id in selected.referenced_devices - for entry in entities.get_entries_for_device_id(device_id) - # Do not add entities which are hidden or which are config - # or diagnostic entities. - if (entry.entity_category is None and entry.hidden_by is None) + selector_data = target_helpers.TargetSelectorData(service_call.data) + selected = target_helpers.async_extract_referenced_entity_ids( + hass, selector_data, expand_group ) - - # Find devices for targeted areas - referenced_devices_by_area: set[str] = set() - if selected.referenced_areas: - for area_id in selected.referenced_areas: - referenced_devices_by_area.update( - device_entry.id - for device_entry in dev_reg.devices.get_devices_for_area_id(area_id) - ) - selected.referenced_devices.update(referenced_devices_by_area) - - # Add indirectly referenced by area - selected.indirectly_referenced.update( - entry.entity_id - for area_id in selected.referenced_areas - # The entity's area matches a targeted area - for entry in entities.get_entries_for_area_id(area_id) - # Do not add entities which are hidden or which are config - # or diagnostic entities. - if entry.entity_category is None and entry.hidden_by is None - ) - # Add indirectly referenced by area through device - selected.indirectly_referenced.update( - entry.entity_id - for device_id in referenced_devices_by_area - for entry in entities.get_entries_for_device_id(device_id) - # Do not add entities which are hidden or which are config - # or diagnostic entities. - if ( - entry.entity_category is None - and entry.hidden_by is None - and ( - # The entity's device matches a device referenced - # by an area and the entity - # has no explicitly set area - not entry.area_id - ) - ) - ) - - return selected + return SelectedEntities(**dataclasses.asdict(selected)) @bind_hass @@ -637,7 +464,10 @@ async def async_extract_config_entry_ids( hass: HomeAssistant, service_call: ServiceCall, expand_group: bool = True ) -> set[str]: """Extract referenced config entry ids from a service call.""" - referenced = async_extract_referenced_entity_ids(hass, service_call, expand_group) + selector_data = target_helpers.TargetSelectorData(service_call.data) + referenced = target_helpers.async_extract_referenced_entity_ids( + hass, selector_data, expand_group + ) ent_reg = entity_registry.async_get(hass) dev_reg = device_registry.async_get(hass) config_entry_ids: set[str] = set() @@ -948,11 +778,14 @@ async def entity_service_call( target_all_entities = call.data.get(ATTR_ENTITY_ID) == ENTITY_MATCH_ALL if target_all_entities: - referenced: SelectedEntities | None = None + referenced: target_helpers.SelectedEntities | None = None all_referenced: set[str] | None = None else: # A set of entities we're trying to target. - referenced = async_extract_referenced_entity_ids(hass, call, True) + selector_data = target_helpers.TargetSelectorData(call.data) + referenced = target_helpers.async_extract_referenced_entity_ids( + hass, selector_data, True + ) all_referenced = referenced.referenced | referenced.indirectly_referenced # If the service function is a string, we'll pass it the service call data @@ -977,7 +810,7 @@ async def entity_service_call( missing = referenced.referenced.copy() for entity in entity_candidates: missing.discard(entity.entity_id) - referenced.log_missing(missing) + referenced.log_missing(missing, _LOGGER) entities: list[Entity] = [] for entity in entity_candidates: diff --git a/homeassistant/helpers/target.py b/homeassistant/helpers/target.py new file mode 100644 index 00000000000..c16819235b9 --- /dev/null +++ b/homeassistant/helpers/target.py @@ -0,0 +1,240 @@ +"""Helpers for dealing with entity targets.""" + +from __future__ import annotations + +import dataclasses +from logging import Logger +from typing import TypeGuard + +from homeassistant.const import ( + ATTR_AREA_ID, + ATTR_DEVICE_ID, + ATTR_ENTITY_ID, + ATTR_FLOOR_ID, + ATTR_LABEL_ID, + ENTITY_MATCH_NONE, +) +from homeassistant.core import HomeAssistant + +from . import ( + area_registry as ar, + config_validation as cv, + device_registry as dr, + entity_registry as er, + floor_registry as fr, + group, + label_registry as lr, +) +from .typing import ConfigType + + +def _has_match(ids: str | list[str] | None) -> TypeGuard[str | list[str]]: + """Check if ids can match anything.""" + return ids not in (None, ENTITY_MATCH_NONE) + + +class TargetSelectorData: + """Class to hold data of target selector.""" + + __slots__ = ("area_ids", "device_ids", "entity_ids", "floor_ids", "label_ids") + + def __init__(self, config: ConfigType) -> None: + """Extract ids from the config.""" + entity_ids: str | list | None = config.get(ATTR_ENTITY_ID) + device_ids: str | list | None = config.get(ATTR_DEVICE_ID) + area_ids: str | list | None = config.get(ATTR_AREA_ID) + floor_ids: str | list | None = config.get(ATTR_FLOOR_ID) + label_ids: str | list | None = config.get(ATTR_LABEL_ID) + + self.entity_ids = ( + set(cv.ensure_list(entity_ids)) if _has_match(entity_ids) else set() + ) + self.device_ids = ( + set(cv.ensure_list(device_ids)) if _has_match(device_ids) else set() + ) + self.area_ids = set(cv.ensure_list(area_ids)) if _has_match(area_ids) else set() + self.floor_ids = ( + set(cv.ensure_list(floor_ids)) if _has_match(floor_ids) else set() + ) + self.label_ids = ( + set(cv.ensure_list(label_ids)) if _has_match(label_ids) else set() + ) + + @property + def has_any_selector(self) -> bool: + """Determine if any selectors are present.""" + return bool( + self.entity_ids + or self.device_ids + or self.area_ids + or self.floor_ids + or self.label_ids + ) + + +@dataclasses.dataclass(slots=True) +class SelectedEntities: + """Class to hold the selected entities.""" + + # Entities that were explicitly mentioned. + referenced: set[str] = dataclasses.field(default_factory=set) + + # Entities that were referenced via device/area/floor/label ID. + # Should not trigger a warning when they don't exist. + indirectly_referenced: set[str] = dataclasses.field(default_factory=set) + + # Referenced items that could not be found. + missing_devices: set[str] = dataclasses.field(default_factory=set) + missing_areas: set[str] = dataclasses.field(default_factory=set) + missing_floors: set[str] = dataclasses.field(default_factory=set) + missing_labels: set[str] = dataclasses.field(default_factory=set) + + referenced_devices: set[str] = dataclasses.field(default_factory=set) + referenced_areas: set[str] = dataclasses.field(default_factory=set) + + def log_missing(self, missing_entities: set[str], logger: Logger) -> None: + """Log about missing items.""" + parts = [] + for label, items in ( + ("floors", self.missing_floors), + ("areas", self.missing_areas), + ("devices", self.missing_devices), + ("entities", missing_entities), + ("labels", self.missing_labels), + ): + if items: + parts.append(f"{label} {', '.join(sorted(items))}") + + if not parts: + return + + logger.warning( + "Referenced %s are missing or not currently available", + ", ".join(parts), + ) + + +def async_extract_referenced_entity_ids( + hass: HomeAssistant, selector_data: TargetSelectorData, expand_group: bool = True +) -> SelectedEntities: + """Extract referenced entity IDs from a target selector.""" + selected = SelectedEntities() + + if not selector_data.has_any_selector: + return selected + + entity_ids: set[str] | list[str] = selector_data.entity_ids + if expand_group: + entity_ids = group.expand_entity_ids(hass, entity_ids) + + selected.referenced.update(entity_ids) + + if ( + not selector_data.device_ids + and not selector_data.area_ids + and not selector_data.floor_ids + and not selector_data.label_ids + ): + return selected + + entities = er.async_get(hass).entities + dev_reg = dr.async_get(hass) + area_reg = ar.async_get(hass) + + if selector_data.floor_ids: + floor_reg = fr.async_get(hass) + for floor_id in selector_data.floor_ids: + if floor_id not in floor_reg.floors: + selected.missing_floors.add(floor_id) + + for area_id in selector_data.area_ids: + if area_id not in area_reg.areas: + selected.missing_areas.add(area_id) + + for device_id in selector_data.device_ids: + if device_id not in dev_reg.devices: + selected.missing_devices.add(device_id) + + if selector_data.label_ids: + label_reg = lr.async_get(hass) + for label_id in selector_data.label_ids: + if label_id not in label_reg.labels: + selected.missing_labels.add(label_id) + + for entity_entry in entities.get_entries_for_label(label_id): + if ( + entity_entry.entity_category is None + and entity_entry.hidden_by is None + ): + selected.indirectly_referenced.add(entity_entry.entity_id) + + for device_entry in dev_reg.devices.get_devices_for_label(label_id): + selected.referenced_devices.add(device_entry.id) + + for area_entry in area_reg.areas.get_areas_for_label(label_id): + selected.referenced_areas.add(area_entry.id) + + # Find areas for targeted floors + if selector_data.floor_ids: + selected.referenced_areas.update( + area_entry.id + for floor_id in selector_data.floor_ids + for area_entry in area_reg.areas.get_areas_for_floor(floor_id) + ) + + selected.referenced_areas.update(selector_data.area_ids) + selected.referenced_devices.update(selector_data.device_ids) + + if not selected.referenced_areas and not selected.referenced_devices: + return selected + + # Add indirectly referenced by device + selected.indirectly_referenced.update( + entry.entity_id + for device_id in selected.referenced_devices + for entry in entities.get_entries_for_device_id(device_id) + # Do not add entities which are hidden or which are config + # or diagnostic entities. + if (entry.entity_category is None and entry.hidden_by is None) + ) + + # Find devices for targeted areas + referenced_devices_by_area: set[str] = set() + if selected.referenced_areas: + for area_id in selected.referenced_areas: + referenced_devices_by_area.update( + device_entry.id + for device_entry in dev_reg.devices.get_devices_for_area_id(area_id) + ) + selected.referenced_devices.update(referenced_devices_by_area) + + # Add indirectly referenced by area + selected.indirectly_referenced.update( + entry.entity_id + for area_id in selected.referenced_areas + # The entity's area matches a targeted area + for entry in entities.get_entries_for_area_id(area_id) + # Do not add entities which are hidden or which are config + # or diagnostic entities. + if entry.entity_category is None and entry.hidden_by is None + ) + # Add indirectly referenced by area through device + selected.indirectly_referenced.update( + entry.entity_id + for device_id in referenced_devices_by_area + for entry in entities.get_entries_for_device_id(device_id) + # Do not add entities which are hidden or which are config + # or diagnostic entities. + if ( + entry.entity_category is None + and entry.hidden_by is None + and ( + # The entity's device matches a device referenced + # by an area and the entity + # has no explicitly set area + not entry.area_id + ) + ) + ) + + return selected diff --git a/tests/helpers/test_service.py b/tests/helpers/test_service.py index 5d018f5f3ee..0191827cd58 100644 --- a/tests/helpers/test_service.py +++ b/tests/helpers/test_service.py @@ -3,6 +3,7 @@ import asyncio from collections.abc import Iterable from copy import deepcopy +import dataclasses import io from typing import Any from unittest.mock import AsyncMock, Mock, patch @@ -2322,3 +2323,80 @@ async def test_reload_service_helper(hass: HomeAssistant) -> None: ] await asyncio.gather(*tasks) assert reloaded == unordered(["all", "target1", "target2", "target3", "target4"]) + + +async def test_deprecated_service_target_selector_class(hass: HomeAssistant) -> None: + """Test that the deprecated ServiceTargetSelector class forwards correctly.""" + call = ServiceCall( + hass, + "test", + "test", + { + "entity_id": ["light.test", "switch.test"], + "area_id": "kitchen", + "device_id": ["device1", "device2"], + "floor_id": "first_floor", + "label_id": ["label1", "label2"], + }, + ) + selector = service.ServiceTargetSelector(call) + + assert selector.entity_ids == {"light.test", "switch.test"} + assert selector.area_ids == {"kitchen"} + assert selector.device_ids == {"device1", "device2"} + assert selector.floor_ids == {"first_floor"} + assert selector.label_ids == {"label1", "label2"} + assert selector.has_any_selector is True + + +async def test_deprecated_selected_entities_class( + hass: HomeAssistant, caplog: pytest.LogCaptureFixture +) -> None: + """Test that the deprecated SelectedEntities class forwards correctly.""" + selected = service.SelectedEntities( + referenced={"entity.test"}, + indirectly_referenced=set(), + referenced_devices=set(), + referenced_areas=set(), + missing_devices={"missing_device"}, + missing_areas={"missing_area"}, + missing_floors={"missing_floor"}, + missing_labels={"missing_label"}, + ) + + missing_entities = {"entity.missing"} + selected.log_missing(missing_entities) + assert ( + "Referenced floors missing_floor, areas missing_area, " + "devices missing_device, entities entity.missing, " + "labels missing_label are missing or not currently available" in caplog.text + ) + + +async def test_deprecated_async_extract_referenced_entity_ids( + hass: HomeAssistant, +) -> None: + """Test that the deprecated async_extract_referenced_entity_ids function forwards correctly.""" + from homeassistant.helpers import target # noqa: PLC0415 + + mock_selected = target.SelectedEntities( + referenced={"entity.test"}, + indirectly_referenced={"entity.indirect"}, + ) + with patch( + "homeassistant.helpers.target.async_extract_referenced_entity_ids", + return_value=mock_selected, + ) as mock_target_func: + call = ServiceCall(hass, "test", "test", {"entity_id": "light.test"}) + result = service.async_extract_referenced_entity_ids( + hass, call, expand_group=False + ) + + # Verify target helper was called with correct parameters + mock_target_func.assert_called_once() + args = mock_target_func.call_args + assert args[0][0] is hass + assert args[0][1].entity_ids == {"light.test"} + assert args[0][2] is False + + assert dataclasses.asdict(result) == dataclasses.asdict(mock_selected) diff --git a/tests/helpers/test_target.py b/tests/helpers/test_target.py new file mode 100644 index 00000000000..ca38f316d89 --- /dev/null +++ b/tests/helpers/test_target.py @@ -0,0 +1,459 @@ +"""Test service helpers.""" + +import pytest + +# TODO(abmantis): is this import needed? +# To prevent circular import when running just this file +import homeassistant.components # noqa: F401 +from homeassistant.components.group import Group +from homeassistant.const import ( + ATTR_AREA_ID, + ATTR_DEVICE_ID, + ATTR_ENTITY_ID, + ATTR_FLOOR_ID, + ATTR_LABEL_ID, + ENTITY_MATCH_NONE, + STATE_OFF, + STATE_ON, + EntityCategory, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers import ( + area_registry as ar, + device_registry as dr, + entity_registry as er, + target, +) +from homeassistant.helpers.typing import ConfigType +from homeassistant.setup import async_setup_component + +from tests.common import ( + RegistryEntryWithDefaults, + mock_area_registry, + mock_device_registry, + mock_registry, +) + + +@pytest.fixture +def registries_mock(hass: HomeAssistant) -> None: + """Mock including floor and area info.""" + hass.states.async_set("light.Bowl", STATE_ON) + hass.states.async_set("light.Ceiling", STATE_OFF) + hass.states.async_set("light.Kitchen", STATE_OFF) + + area_in_floor = ar.AreaEntry( + id="test-area", + name="Test area", + aliases={}, + floor_id="test-floor", + icon=None, + picture=None, + temperature_entity_id=None, + humidity_entity_id=None, + ) + area_in_floor_a = ar.AreaEntry( + id="area-a", + name="Area A", + aliases={}, + floor_id="floor-a", + icon=None, + picture=None, + temperature_entity_id=None, + humidity_entity_id=None, + ) + area_with_labels = ar.AreaEntry( + id="area-with-labels", + name="Area with labels", + aliases={}, + floor_id=None, + icon=None, + labels={"label_area"}, + picture=None, + temperature_entity_id=None, + humidity_entity_id=None, + ) + mock_area_registry( + hass, + { + area_in_floor.id: area_in_floor, + area_in_floor_a.id: area_in_floor_a, + area_with_labels.id: area_with_labels, + }, + ) + + device_in_area = dr.DeviceEntry(id="device-test-area", area_id="test-area") + device_no_area = dr.DeviceEntry(id="device-no-area-id") + device_diff_area = dr.DeviceEntry(id="device-diff-area", area_id="diff-area") + device_area_a = dr.DeviceEntry(id="device-area-a-id", area_id="area-a") + device_has_label1 = dr.DeviceEntry(id="device-has-label1-id", labels={"label1"}) + device_has_label2 = dr.DeviceEntry(id="device-has-label2-id", labels={"label2"}) + device_has_labels = dr.DeviceEntry( + id="device-has-labels-id", + labels={"label1", "label2"}, + area_id=area_with_labels.id, + ) + + mock_device_registry( + hass, + { + device_in_area.id: device_in_area, + device_no_area.id: device_no_area, + device_diff_area.id: device_diff_area, + device_area_a.id: device_area_a, + device_has_label1.id: device_has_label1, + device_has_label2.id: device_has_label2, + device_has_labels.id: device_has_labels, + }, + ) + + entity_in_own_area = RegistryEntryWithDefaults( + entity_id="light.in_own_area", + unique_id="in-own-area-id", + platform="test", + area_id="own-area", + ) + config_entity_in_own_area = RegistryEntryWithDefaults( + entity_id="light.config_in_own_area", + unique_id="config-in-own-area-id", + platform="test", + area_id="own-area", + entity_category=EntityCategory.CONFIG, + ) + hidden_entity_in_own_area = RegistryEntryWithDefaults( + entity_id="light.hidden_in_own_area", + unique_id="hidden-in-own-area-id", + platform="test", + area_id="own-area", + hidden_by=er.RegistryEntryHider.USER, + ) + entity_in_area = RegistryEntryWithDefaults( + entity_id="light.in_area", + unique_id="in-area-id", + platform="test", + device_id=device_in_area.id, + ) + config_entity_in_area = RegistryEntryWithDefaults( + entity_id="light.config_in_area", + unique_id="config-in-area-id", + platform="test", + device_id=device_in_area.id, + entity_category=EntityCategory.CONFIG, + ) + hidden_entity_in_area = RegistryEntryWithDefaults( + entity_id="light.hidden_in_area", + unique_id="hidden-in-area-id", + platform="test", + device_id=device_in_area.id, + hidden_by=er.RegistryEntryHider.USER, + ) + entity_in_other_area = RegistryEntryWithDefaults( + entity_id="light.in_other_area", + unique_id="in-area-a-id", + platform="test", + device_id=device_in_area.id, + area_id="other-area", + ) + entity_assigned_to_area = RegistryEntryWithDefaults( + entity_id="light.assigned_to_area", + unique_id="assigned-area-id", + platform="test", + device_id=device_in_area.id, + area_id="test-area", + ) + entity_no_area = RegistryEntryWithDefaults( + entity_id="light.no_area", + unique_id="no-area-id", + platform="test", + device_id=device_no_area.id, + ) + config_entity_no_area = RegistryEntryWithDefaults( + entity_id="light.config_no_area", + unique_id="config-no-area-id", + platform="test", + device_id=device_no_area.id, + entity_category=EntityCategory.CONFIG, + ) + hidden_entity_no_area = RegistryEntryWithDefaults( + entity_id="light.hidden_no_area", + unique_id="hidden-no-area-id", + platform="test", + device_id=device_no_area.id, + hidden_by=er.RegistryEntryHider.USER, + ) + entity_diff_area = RegistryEntryWithDefaults( + entity_id="light.diff_area", + unique_id="diff-area-id", + platform="test", + device_id=device_diff_area.id, + ) + entity_in_area_a = RegistryEntryWithDefaults( + entity_id="light.in_area_a", + unique_id="in-area-a-id", + platform="test", + device_id=device_area_a.id, + area_id="area-a", + ) + entity_in_area_b = RegistryEntryWithDefaults( + entity_id="light.in_area_b", + unique_id="in-area-b-id", + platform="test", + device_id=device_area_a.id, + area_id="area-b", + ) + entity_with_my_label = RegistryEntryWithDefaults( + entity_id="light.with_my_label", + unique_id="with_my_label", + platform="test", + labels={"my-label"}, + ) + hidden_entity_with_my_label = RegistryEntryWithDefaults( + entity_id="light.hidden_with_my_label", + unique_id="hidden_with_my_label", + platform="test", + labels={"my-label"}, + hidden_by=er.RegistryEntryHider.USER, + ) + config_entity_with_my_label = RegistryEntryWithDefaults( + entity_id="light.config_with_my_label", + unique_id="config_with_my_label", + platform="test", + labels={"my-label"}, + entity_category=EntityCategory.CONFIG, + ) + entity_with_label1_from_device = RegistryEntryWithDefaults( + entity_id="light.with_label1_from_device", + unique_id="with_label1_from_device", + platform="test", + device_id=device_has_label1.id, + ) + entity_with_label1_from_device_and_different_area = RegistryEntryWithDefaults( + entity_id="light.with_label1_from_device_diff_area", + unique_id="with_label1_from_device_diff_area", + platform="test", + device_id=device_has_label1.id, + area_id=area_in_floor_a.id, + ) + entity_with_label1_and_label2_from_device = RegistryEntryWithDefaults( + entity_id="light.with_label1_and_label2_from_device", + unique_id="with_label1_and_label2_from_device", + platform="test", + labels={"label1"}, + device_id=device_has_label2.id, + ) + entity_with_labels_from_device = RegistryEntryWithDefaults( + entity_id="light.with_labels_from_device", + unique_id="with_labels_from_device", + platform="test", + device_id=device_has_labels.id, + ) + mock_registry( + hass, + { + entity_in_own_area.entity_id: entity_in_own_area, + config_entity_in_own_area.entity_id: config_entity_in_own_area, + hidden_entity_in_own_area.entity_id: hidden_entity_in_own_area, + entity_in_area.entity_id: entity_in_area, + config_entity_in_area.entity_id: config_entity_in_area, + hidden_entity_in_area.entity_id: hidden_entity_in_area, + entity_in_other_area.entity_id: entity_in_other_area, + entity_assigned_to_area.entity_id: entity_assigned_to_area, + entity_no_area.entity_id: entity_no_area, + config_entity_no_area.entity_id: config_entity_no_area, + hidden_entity_no_area.entity_id: hidden_entity_no_area, + entity_diff_area.entity_id: entity_diff_area, + entity_in_area_a.entity_id: entity_in_area_a, + entity_in_area_b.entity_id: entity_in_area_b, + config_entity_with_my_label.entity_id: config_entity_with_my_label, + entity_with_label1_and_label2_from_device.entity_id: entity_with_label1_and_label2_from_device, + entity_with_label1_from_device.entity_id: entity_with_label1_from_device, + entity_with_label1_from_device_and_different_area.entity_id: entity_with_label1_from_device_and_different_area, + entity_with_labels_from_device.entity_id: entity_with_labels_from_device, + entity_with_my_label.entity_id: entity_with_my_label, + hidden_entity_with_my_label.entity_id: hidden_entity_with_my_label, + }, + ) + + +@pytest.mark.parametrize( + ("selector_config", "expand_group", "expected_selected"), + [ + ( + { + ATTR_ENTITY_ID: ENTITY_MATCH_NONE, + ATTR_AREA_ID: ENTITY_MATCH_NONE, + ATTR_FLOOR_ID: ENTITY_MATCH_NONE, + ATTR_LABEL_ID: ENTITY_MATCH_NONE, + }, + False, + target.SelectedEntities(), + ), + ( + {ATTR_ENTITY_ID: "light.bowl"}, + False, + target.SelectedEntities(referenced={"light.bowl"}), + ), + ( + {ATTR_ENTITY_ID: "group.test"}, + True, + target.SelectedEntities(referenced={"light.ceiling", "light.kitchen"}), + ), + ( + {ATTR_ENTITY_ID: "group.test"}, + False, + target.SelectedEntities(referenced={"group.test"}), + ), + ( + {ATTR_AREA_ID: "own-area"}, + False, + target.SelectedEntities( + indirectly_referenced={"light.in_own_area"}, + referenced_areas={"own-area"}, + missing_areas={"own-area"}, + ), + ), + ( + {ATTR_AREA_ID: "test-area"}, + False, + target.SelectedEntities( + indirectly_referenced={ + "light.in_area", + "light.assigned_to_area", + }, + referenced_areas={"test-area"}, + referenced_devices={"device-test-area"}, + ), + ), + ( + {ATTR_AREA_ID: ["test-area", "diff-area"]}, + False, + target.SelectedEntities( + indirectly_referenced={ + "light.in_area", + "light.diff_area", + "light.assigned_to_area", + }, + referenced_areas={"test-area", "diff-area"}, + referenced_devices={"device-diff-area", "device-test-area"}, + missing_areas={"diff-area"}, + ), + ), + ( + {ATTR_DEVICE_ID: "device-no-area-id"}, + False, + target.SelectedEntities( + indirectly_referenced={"light.no_area"}, + referenced_devices={"device-no-area-id"}, + ), + ), + ( + {ATTR_DEVICE_ID: "device-area-a-id"}, + False, + target.SelectedEntities( + indirectly_referenced={"light.in_area_a", "light.in_area_b"}, + referenced_devices={"device-area-a-id"}, + ), + ), + ( + {ATTR_FLOOR_ID: "test-floor"}, + False, + target.SelectedEntities( + indirectly_referenced={"light.in_area", "light.assigned_to_area"}, + referenced_devices={"device-test-area"}, + referenced_areas={"test-area"}, + missing_floors={"test-floor"}, + ), + ), + ( + {ATTR_FLOOR_ID: ["test-floor", "floor-a"]}, + False, + target.SelectedEntities( + indirectly_referenced={ + "light.in_area", + "light.assigned_to_area", + "light.in_area_a", + "light.with_label1_from_device_diff_area", + }, + referenced_devices={"device-area-a-id", "device-test-area"}, + referenced_areas={"area-a", "test-area"}, + missing_floors={"floor-a", "test-floor"}, + ), + ), + ( + {ATTR_LABEL_ID: "my-label"}, + False, + target.SelectedEntities( + indirectly_referenced={"light.with_my_label"}, + missing_labels={"my-label"}, + ), + ), + ( + {ATTR_LABEL_ID: "label1"}, + False, + target.SelectedEntities( + indirectly_referenced={ + "light.with_label1_from_device", + "light.with_label1_from_device_diff_area", + "light.with_labels_from_device", + "light.with_label1_and_label2_from_device", + }, + referenced_devices={"device-has-label1-id", "device-has-labels-id"}, + missing_labels={"label1"}, + ), + ), + ( + {ATTR_LABEL_ID: ["label2"]}, + False, + target.SelectedEntities( + indirectly_referenced={ + "light.with_labels_from_device", + "light.with_label1_and_label2_from_device", + }, + referenced_devices={"device-has-label2-id", "device-has-labels-id"}, + missing_labels={"label2"}, + ), + ), + ( + {ATTR_LABEL_ID: ["label_area"]}, + False, + target.SelectedEntities( + indirectly_referenced={"light.with_labels_from_device"}, + referenced_devices={"device-has-labels-id"}, + referenced_areas={"area-with-labels"}, + missing_labels={"label_area"}, + ), + ), + ], +) +@pytest.mark.usefixtures("registries_mock") +async def test_extract_referenced_entity_ids( + hass: HomeAssistant, + selector_config: ConfigType, + expand_group: bool, + expected_selected: target.SelectedEntities, +) -> None: + """Test extract_entity_ids method.""" + hass.states.async_set("light.Bowl", STATE_ON) + hass.states.async_set("light.Ceiling", STATE_OFF) + hass.states.async_set("light.Kitchen", STATE_OFF) + + assert await async_setup_component(hass, "group", {}) + await hass.async_block_till_done() + await Group.async_create_group( + hass, + "test", + created_by_service=False, + entity_ids=["light.Ceiling", "light.Kitchen"], + icon=None, + mode=None, + object_id=None, + order=None, + ) + + target_data = target.TargetSelectorData(selector_config) + assert ( + target.async_extract_referenced_entity_ids( + hass, target_data, expand_group=expand_group + ) + == expected_selected + )