From 1526d953bfedf1fc9e79b4003e8b365da6342d41 Mon Sep 17 00:00:00 2001 From: Luke Lashley Date: Sun, 21 Sep 2025 01:52:26 -0400 Subject: [PATCH 1/2] Make Roborock A01 initilization threadsafe (#152699) --- homeassistant/components/roborock/__init__.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/roborock/__init__.py b/homeassistant/components/roborock/__init__.py index bc10ab7309c1e..2e354a1e48730 100644 --- a/homeassistant/components/roborock/__init__.py +++ b/homeassistant/components/roborock/__init__.py @@ -319,8 +319,11 @@ async def setup_device_a01( product_info: HomeDataProduct, ) -> RoborockDataUpdateCoordinatorA01 | None: """Set up a A01 protocol device.""" - mqtt_client = RoborockMqttClientA01( - user_data, DeviceData(device, product_info.name), product_info.category + mqtt_client = await hass.async_add_executor_job( + RoborockMqttClientA01, + user_data, + DeviceData(device, product_info.model), + product_info.category, ) coord = RoborockDataUpdateCoordinatorA01( hass, entry, device, product_info, mqtt_client From befc93bc73efcc8e5a31ecdc130b4fe1c7ee122f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Diego=20Rodr=C3=ADguez=20Royo?= Date: Sun, 21 Sep 2025 11:23:51 +0200 Subject: [PATCH 2/2] Delete Home Connect alarm clock entity from time platform (#152188) --- .../components/home_connect/__init__.py | 1 - .../components/home_connect/icons.json | 16 +- .../components/home_connect/strings.json | 5 - homeassistant/components/home_connect/time.py | 172 ------- tests/components/home_connect/test_time.py | 474 ------------------ 5 files changed, 8 insertions(+), 660 deletions(-) delete mode 100644 homeassistant/components/home_connect/time.py delete mode 100644 tests/components/home_connect/test_time.py diff --git a/homeassistant/components/home_connect/__init__.py b/homeassistant/components/home_connect/__init__.py index 4a48d1f1ad77a..f0d5e7dbf02fc 100644 --- a/homeassistant/components/home_connect/__init__.py +++ b/homeassistant/components/home_connect/__init__.py @@ -37,7 +37,6 @@ Platform.SELECT, Platform.SENSOR, Platform.SWITCH, - Platform.TIME, ] diff --git a/homeassistant/components/home_connect/icons.json b/homeassistant/components/home_connect/icons.json index 9b4c9276998b8..0e8f1c4f98897 100644 --- a/homeassistant/components/home_connect/icons.json +++ b/homeassistant/components/home_connect/icons.json @@ -66,6 +66,14 @@ "default": "mdi:stop" } }, + "number": { + "start_in_relative": { + "default": "mdi:progress-clock" + }, + "finish_in_relative": { + "default": "mdi:progress-clock" + } + }, "sensor": { "operation_state": { "default": "mdi:state-machine", @@ -251,14 +259,6 @@ "i_dos_2_active": { "default": "mdi:numeric-2-circle" } - }, - "time": { - "start_in_relative": { - "default": "mdi:progress-clock" - }, - "finish_in_relative": { - "default": "mdi:progress-clock" - } } } } diff --git a/homeassistant/components/home_connect/strings.json b/homeassistant/components/home_connect/strings.json index 1d3bffb78477a..2ef931ec52a83 100644 --- a/homeassistant/components/home_connect/strings.json +++ b/homeassistant/components/home_connect/strings.json @@ -1852,11 +1852,6 @@ "i_dos2_active": { "name": "[%key:component::home_connect::services::set_program_and_options::fields::laundry_care_washer_option_i_dos2_active::name%]" } - }, - "time": { - "alarm_clock": { - "name": "Alarm clock" - } } } } diff --git a/homeassistant/components/home_connect/time.py b/homeassistant/components/home_connect/time.py deleted file mode 100644 index 6a6e57c4dd343..0000000000000 --- a/homeassistant/components/home_connect/time.py +++ /dev/null @@ -1,172 +0,0 @@ -"""Provides time entities for Home Connect.""" - -from datetime import time -from typing import cast - -from aiohomeconnect.model import SettingKey -from aiohomeconnect.model.error import HomeConnectError - -from homeassistant.components.automation import automations_with_entity -from homeassistant.components.script import scripts_with_entity -from homeassistant.components.time import TimeEntity, TimeEntityDescription -from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import entity_registry as er -from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from homeassistant.helpers.issue_registry import ( - IssueSeverity, - async_create_issue, - async_delete_issue, -) - -from .common import setup_home_connect_entry -from .const import DOMAIN -from .coordinator import HomeConnectApplianceData, HomeConnectConfigEntry -from .entity import HomeConnectEntity -from .utils import get_dict_from_home_connect_error - -PARALLEL_UPDATES = 1 - -TIME_ENTITIES = ( - TimeEntityDescription( - key=SettingKey.BSH_COMMON_ALARM_CLOCK, - translation_key="alarm_clock", - entity_registry_enabled_default=False, - ), -) - - -def _get_entities_for_appliance( - entry: HomeConnectConfigEntry, - appliance: HomeConnectApplianceData, -) -> list[HomeConnectEntity]: - """Get a list of entities.""" - return [ - HomeConnectTimeEntity(entry.runtime_data, appliance, description) - for description in TIME_ENTITIES - if description.key in appliance.settings - ] - - -async def async_setup_entry( - hass: HomeAssistant, - entry: HomeConnectConfigEntry, - async_add_entities: AddConfigEntryEntitiesCallback, -) -> None: - """Set up the Home Connect switch.""" - setup_home_connect_entry( - entry, - _get_entities_for_appliance, - async_add_entities, - ) - - -def seconds_to_time(seconds: int) -> time: - """Convert seconds to a time object.""" - minutes, sec = divmod(seconds, 60) - hours, minutes = divmod(minutes, 60) - return time(hour=hours, minute=minutes, second=sec) - - -def time_to_seconds(t: time) -> int: - """Convert a time object to seconds.""" - return t.hour * 3600 + t.minute * 60 + t.second - - -class HomeConnectTimeEntity(HomeConnectEntity, TimeEntity): - """Time setting class for Home Connect.""" - - async def async_added_to_hass(self) -> None: - """Call when entity is added to hass.""" - await super().async_added_to_hass() - if self.bsh_key is SettingKey.BSH_COMMON_ALARM_CLOCK: - automations = automations_with_entity(self.hass, self.entity_id) - scripts = scripts_with_entity(self.hass, self.entity_id) - items = automations + scripts - if not items: - return - - entity_reg: er.EntityRegistry = er.async_get(self.hass) - entity_automations = [ - automation_entity - for automation_id in automations - if (automation_entity := entity_reg.async_get(automation_id)) - ] - entity_scripts = [ - script_entity - for script_id in scripts - if (script_entity := entity_reg.async_get(script_id)) - ] - - items_list = [ - f"- [{item.original_name}](/config/automation/edit/{item.unique_id})" - for item in entity_automations - ] + [ - f"- [{item.original_name}](/config/script/edit/{item.unique_id})" - for item in entity_scripts - ] - - async_create_issue( - self.hass, - DOMAIN, - f"deprecated_time_alarm_clock_in_automations_scripts_{self.entity_id}", - breaks_in_ha_version="2025.10.0", - is_fixable=True, - is_persistent=True, - severity=IssueSeverity.WARNING, - translation_key="deprecated_time_alarm_clock", - translation_placeholders={ - "entity_id": self.entity_id, - "items": "\n".join(items_list), - }, - ) - - async def async_will_remove_from_hass(self) -> None: - """Call when entity will be removed from hass.""" - if self.bsh_key is SettingKey.BSH_COMMON_ALARM_CLOCK: - async_delete_issue( - self.hass, - DOMAIN, - f"deprecated_time_alarm_clock_in_automations_scripts_{self.entity_id}", - ) - async_delete_issue( - self.hass, DOMAIN, f"deprecated_time_alarm_clock_{self.entity_id}" - ) - - async def async_set_value(self, value: time) -> None: - """Set the native value of the entity.""" - async_create_issue( - self.hass, - DOMAIN, - f"deprecated_time_alarm_clock_{self.entity_id}", - breaks_in_ha_version="2025.10.0", - is_fixable=True, - is_persistent=True, - severity=IssueSeverity.WARNING, - translation_key="deprecated_time_alarm_clock", - translation_placeholders={ - "entity_id": self.entity_id, - }, - ) - try: - await self.coordinator.client.set_setting( - self.appliance.info.ha_id, - setting_key=SettingKey(self.bsh_key), - value=time_to_seconds(value), - ) - except HomeConnectError as err: - raise HomeAssistantError( - translation_domain=DOMAIN, - translation_key="set_setting_entity", - translation_placeholders={ - **get_dict_from_home_connect_error(err), - "entity_id": self.entity_id, - "key": self.bsh_key, - "value": str(value), - }, - ) from err - - def update_native_value(self) -> None: - """Set the value of the entity.""" - data = self.appliance.settings[cast(SettingKey, self.bsh_key)] - self._attr_native_value = seconds_to_time(data.value) diff --git a/tests/components/home_connect/test_time.py b/tests/components/home_connect/test_time.py deleted file mode 100644 index 9e114768b6f29..0000000000000 --- a/tests/components/home_connect/test_time.py +++ /dev/null @@ -1,474 +0,0 @@ -"""Tests for home_connect time entities.""" - -from collections.abc import Awaitable, Callable -from datetime import time -from http import HTTPStatus -from unittest.mock import AsyncMock, MagicMock - -from aiohomeconnect.model import ( - ArrayOfEvents, - ArrayOfSettings, - EventMessage, - EventType, - GetSetting, - HomeAppliance, - SettingKey, -) -from aiohomeconnect.model.error import HomeConnectApiError, HomeConnectError -import pytest - -from homeassistant.components.automation import ( - DOMAIN as AUTOMATION_DOMAIN, - automations_with_entity, -) -from homeassistant.components.home_connect.const import DOMAIN -from homeassistant.components.script import DOMAIN as SCRIPT_DOMAIN, scripts_with_entity -from homeassistant.components.time import DOMAIN as TIME_DOMAIN, SERVICE_SET_VALUE -from homeassistant.config_entries import ConfigEntryState -from homeassistant.const import ATTR_ENTITY_ID, ATTR_TIME, STATE_UNAVAILABLE, Platform -from homeassistant.core import HomeAssistant -from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import ( - device_registry as dr, - entity_registry as er, - issue_registry as ir, -) -from homeassistant.setup import async_setup_component - -from tests.common import MockConfigEntry -from tests.typing import ClientSessionGenerator - - -@pytest.fixture -def platforms() -> list[str]: - """Fixture to specify platforms to test.""" - return [Platform.TIME] - - -@pytest.mark.usefixtures("entity_registry_enabled_by_default") -@pytest.mark.parametrize("appliance", ["Oven"], indirect=True) -async def test_paired_depaired_devices_flow( - hass: HomeAssistant, - device_registry: dr.DeviceRegistry, - entity_registry: er.EntityRegistry, - client: MagicMock, - config_entry: MockConfigEntry, - integration_setup: Callable[[MagicMock], Awaitable[bool]], - appliance: HomeAppliance, -) -> None: - """Test that removed devices are correctly removed from and added to hass on API events.""" - assert await integration_setup(client) - assert config_entry.state is ConfigEntryState.LOADED - - device = device_registry.async_get_device(identifiers={(DOMAIN, appliance.ha_id)}) - assert device - entity_entries = entity_registry.entities.get_entries_for_device_id(device.id) - assert entity_entries - - await client.add_events( - [ - EventMessage( - appliance.ha_id, - EventType.DEPAIRED, - data=ArrayOfEvents([]), - ) - ] - ) - await hass.async_block_till_done() - - device = device_registry.async_get_device(identifiers={(DOMAIN, appliance.ha_id)}) - assert not device - for entity_entry in entity_entries: - assert not entity_registry.async_get(entity_entry.entity_id) - - # Now that all everything related to the device is removed, pair it again - await client.add_events( - [ - EventMessage( - appliance.ha_id, - EventType.PAIRED, - data=ArrayOfEvents([]), - ) - ] - ) - await hass.async_block_till_done() - - assert device_registry.async_get_device(identifiers={(DOMAIN, appliance.ha_id)}) - for entity_entry in entity_entries: - assert entity_registry.async_get(entity_entry.entity_id) - - -@pytest.mark.usefixtures("entity_registry_enabled_by_default") -@pytest.mark.parametrize( - ("appliance", "keys_to_check"), - [ - ( - "Oven", - (SettingKey.BSH_COMMON_ALARM_CLOCK,), - ) - ], - indirect=["appliance"], -) -async def test_connected_devices( - hass: HomeAssistant, - device_registry: dr.DeviceRegistry, - entity_registry: er.EntityRegistry, - client: MagicMock, - config_entry: MockConfigEntry, - integration_setup: Callable[[MagicMock], Awaitable[bool]], - appliance: HomeAppliance, - keys_to_check: tuple, -) -> None: - """Test that devices reconnected. - - Specifically those devices whose settings, status, etc. could - not be obtained while disconnected and once connected, the entities are added. - """ - get_settings_original_mock = client.get_settings - - async def get_settings_side_effect(ha_id: str): - if ha_id == appliance.ha_id: - raise HomeConnectApiError( - "SDK.Error.HomeAppliance.Connection.Initialization.Failed" - ) - return await get_settings_original_mock.side_effect(ha_id) - - client.get_settings = AsyncMock(side_effect=get_settings_side_effect) - assert await integration_setup(client) - assert config_entry.state is ConfigEntryState.LOADED - client.get_settings = get_settings_original_mock - - device = device_registry.async_get_device(identifiers={(DOMAIN, appliance.ha_id)}) - assert device - for key in keys_to_check: - assert not entity_registry.async_get_entity_id( - Platform.TIME, - DOMAIN, - f"{appliance.ha_id}-{key}", - ) - - await client.add_events( - [ - EventMessage( - appliance.ha_id, - EventType.CONNECTED, - data=ArrayOfEvents([]), - ) - ] - ) - await hass.async_block_till_done() - - for key in keys_to_check: - assert entity_registry.async_get_entity_id( - Platform.TIME, - DOMAIN, - f"{appliance.ha_id}-{key}", - ) - - -@pytest.mark.usefixtures("entity_registry_enabled_by_default") -@pytest.mark.parametrize("appliance", ["Oven"], indirect=True) -async def test_time_entity_availability( - hass: HomeAssistant, - client: MagicMock, - config_entry: MockConfigEntry, - integration_setup: Callable[[MagicMock], Awaitable[bool]], - appliance: HomeAppliance, -) -> None: - """Test if time entities availability are based on the appliance connection state.""" - entity_ids = [ - "time.oven_alarm_clock", - ] - assert await integration_setup(client) - assert config_entry.state is ConfigEntryState.LOADED - - for entity_id in entity_ids: - state = hass.states.get(entity_id) - assert state - assert state.state != STATE_UNAVAILABLE - - await client.add_events( - [ - EventMessage( - appliance.ha_id, - EventType.DISCONNECTED, - ArrayOfEvents([]), - ) - ] - ) - await hass.async_block_till_done() - - for entity_id in entity_ids: - assert hass.states.is_state(entity_id, STATE_UNAVAILABLE) - - await client.add_events( - [ - EventMessage( - appliance.ha_id, - EventType.CONNECTED, - ArrayOfEvents([]), - ) - ] - ) - await hass.async_block_till_done() - - for entity_id in entity_ids: - state = hass.states.get(entity_id) - assert state - assert state.state != STATE_UNAVAILABLE - - -@pytest.mark.usefixtures("entity_registry_enabled_by_default") -@pytest.mark.parametrize("appliance", ["Oven"], indirect=True) -@pytest.mark.parametrize( - ("entity_id", "setting_key"), - [ - ( - f"{TIME_DOMAIN}.oven_alarm_clock", - SettingKey.BSH_COMMON_ALARM_CLOCK, - ), - ], -) -async def test_time_entity_functionality( - hass: HomeAssistant, - client: MagicMock, - config_entry: MockConfigEntry, - integration_setup: Callable[[MagicMock], Awaitable[bool]], - appliance: HomeAppliance, - entity_id: str, - setting_key: SettingKey, -) -> None: - """Test time entity functionality.""" - assert await integration_setup(client) - assert config_entry.state is ConfigEntryState.LOADED - - value = 30 - entity_state = hass.states.get(entity_id) - assert entity_state is not None - assert entity_state.state != value - await hass.services.async_call( - TIME_DOMAIN, - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: entity_id, - ATTR_TIME: time(second=value), - }, - ) - await hass.async_block_till_done() - client.set_setting.assert_awaited_once_with( - appliance.ha_id, setting_key=setting_key, value=value - ) - assert hass.states.is_state(entity_id, str(time(second=value))) - - -@pytest.mark.usefixtures("entity_registry_enabled_by_default") -@pytest.mark.parametrize( - ("entity_id", "setting_key", "mock_attr"), - [ - ( - f"{TIME_DOMAIN}.oven_alarm_clock", - SettingKey.BSH_COMMON_ALARM_CLOCK, - "set_setting", - ), - ], -) -async def test_time_entity_error( - hass: HomeAssistant, - client_with_exception: MagicMock, - config_entry: MockConfigEntry, - integration_setup: Callable[[MagicMock], Awaitable[bool]], - entity_id: str, - setting_key: SettingKey, - mock_attr: str, -) -> None: - """Test time entity error.""" - client_with_exception.get_settings.side_effect = None - client_with_exception.get_settings.return_value = ArrayOfSettings( - [ - GetSetting( - key=setting_key, - raw_key=setting_key.value, - value=30, - ) - ] - ) - assert await integration_setup(client_with_exception) - assert config_entry.state is ConfigEntryState.LOADED - - with pytest.raises(HomeConnectError): - await getattr(client_with_exception, mock_attr)() - - with pytest.raises( - HomeAssistantError, match=r"Error.*assign.*value.*to.*setting.*" - ): - await hass.services.async_call( - TIME_DOMAIN, - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: entity_id, - ATTR_TIME: time(minute=1), - }, - blocking=True, - ) - assert getattr(client_with_exception, mock_attr).call_count == 2 - - -@pytest.mark.usefixtures("entity_registry_enabled_by_default") -@pytest.mark.parametrize("appliance", ["Oven"], indirect=True) -async def test_create_alarm_clock_deprecation_issue( - hass: HomeAssistant, - issue_registry: ir.IssueRegistry, - client: MagicMock, - config_entry: MockConfigEntry, - integration_setup: Callable[[MagicMock], Awaitable[bool]], -) -> None: - """Test that we create an issue when an automation or script is using a alarm clock time entity or the entity is used by the user.""" - entity_id = f"{TIME_DOMAIN}.oven_alarm_clock" - automation_script_issue_id = ( - f"deprecated_time_alarm_clock_in_automations_scripts_{entity_id}" - ) - action_handler_issue_id = f"deprecated_time_alarm_clock_{entity_id}" - - assert await async_setup_component( - hass, - AUTOMATION_DOMAIN, - { - AUTOMATION_DOMAIN: { - "alias": "test", - "trigger": {"platform": "state", "entity_id": entity_id}, - "action": { - "action": "automation.turn_on", - "target": { - "entity_id": "automation.test", - }, - }, - } - }, - ) - assert await async_setup_component( - hass, - SCRIPT_DOMAIN, - { - SCRIPT_DOMAIN: { - "test": { - "sequence": [ - { - "action": "switch.turn_on", - "entity_id": entity_id, - }, - ], - } - } - }, - ) - - assert await integration_setup(client) - assert config_entry.state is ConfigEntryState.LOADED - - await hass.services.async_call( - TIME_DOMAIN, - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: entity_id, - ATTR_TIME: time(minute=1), - }, - blocking=True, - ) - - assert automations_with_entity(hass, entity_id)[0] == "automation.test" - assert scripts_with_entity(hass, entity_id)[0] == "script.test" - - assert len(issue_registry.issues) == 2 - assert issue_registry.async_get_issue(DOMAIN, automation_script_issue_id) - assert issue_registry.async_get_issue(DOMAIN, action_handler_issue_id) - - await hass.config_entries.async_unload(config_entry.entry_id) - await hass.async_block_till_done() - - # Assert the issue is no longer present - assert not issue_registry.async_get_issue(DOMAIN, automation_script_issue_id) - assert not issue_registry.async_get_issue(DOMAIN, action_handler_issue_id) - assert len(issue_registry.issues) == 0 - - -@pytest.mark.usefixtures("entity_registry_enabled_by_default") -@pytest.mark.parametrize("appliance", ["Oven"], indirect=True) -async def test_alarm_clock_deprecation_issue_fix( - hass: HomeAssistant, - hass_client: ClientSessionGenerator, - issue_registry: ir.IssueRegistry, - client: MagicMock, - config_entry: MockConfigEntry, - integration_setup: Callable[[MagicMock], Awaitable[bool]], -) -> None: - """Test we can fix the issues created when a alarm clock time entity is in an automation or in a script or when is used.""" - entity_id = f"{TIME_DOMAIN}.oven_alarm_clock" - automation_script_issue_id = ( - f"deprecated_time_alarm_clock_in_automations_scripts_{entity_id}" - ) - action_handler_issue_id = f"deprecated_time_alarm_clock_{entity_id}" - - assert await async_setup_component( - hass, - AUTOMATION_DOMAIN, - { - AUTOMATION_DOMAIN: { - "alias": "test", - "trigger": {"platform": "state", "entity_id": entity_id}, - "action": { - "action": "automation.turn_on", - "target": { - "entity_id": "automation.test", - }, - }, - } - }, - ) - assert await async_setup_component( - hass, - SCRIPT_DOMAIN, - { - SCRIPT_DOMAIN: { - "test": { - "sequence": [ - { - "action": "switch.turn_on", - "entity_id": entity_id, - }, - ], - } - } - }, - ) - - assert await integration_setup(client) - assert config_entry.state is ConfigEntryState.LOADED - - await hass.services.async_call( - TIME_DOMAIN, - SERVICE_SET_VALUE, - { - ATTR_ENTITY_ID: entity_id, - ATTR_TIME: time(minute=1), - }, - blocking=True, - ) - - assert len(issue_registry.issues) == 2 - assert issue_registry.async_get_issue(DOMAIN, automation_script_issue_id) - assert issue_registry.async_get_issue(DOMAIN, action_handler_issue_id) - - for issue in issue_registry.issues.copy().values(): - _client = await hass_client() - resp = await _client.post( - "/api/repairs/issues/fix", - json={"handler": DOMAIN, "issue_id": issue.issue_id}, - ) - assert resp.status == HTTPStatus.OK - flow_id = (await resp.json())["flow_id"] - resp = await _client.post(f"/api/repairs/issues/fix/{flow_id}") - - # Assert the issue is no longer present - assert not issue_registry.async_get_issue(DOMAIN, automation_script_issue_id) - assert not issue_registry.async_get_issue(DOMAIN, action_handler_issue_id) - assert len(issue_registry.issues) == 0