From 1ab2bbe3b03e0434c30d0c1fbdb9710509a979c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2E=20Diego=20Rodr=C3=ADguez=20Royo?= Date: Thu, 21 Nov 2024 20:45:43 +0100 Subject: [PATCH] Don't save Home Assistant device ID at Home Connect device (#131013) --- .../components/home_connect/__init__.py | 33 ++++++++++--------- .../home_connect/test_diagnostics.py | 5 +-- tests/components/home_connect/test_init.py | 2 +- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/homeassistant/components/home_connect/__init__.py b/homeassistant/components/home_connect/__init__.py index 175545c9665..c05b04a2c24 100644 --- a/homeassistant/components/home_connect/__init__.py +++ b/homeassistant/components/home_connect/__init__.py @@ -6,7 +6,6 @@ from datetime import timedelta import logging from typing import Any -from homeconnect.api import HomeConnectAppliance from requests import HTTPError import voluptuous as vol @@ -92,12 +91,27 @@ PLATFORMS = [ def _get_appliance_by_device_id( hass: HomeAssistant, device_id: str -) -> HomeConnectAppliance: +) -> api.HomeConnectAppliance: """Return a Home Connect appliance instance given an device_id.""" + device_registry = dr.async_get(hass) + device_entry = device_registry.async_get(device_id) + assert device_entry + + ha_id = next( + ( + identifier[1] + for identifier in device_entry.identifiers + if identifier[0] == DOMAIN + ), + None, + ) + assert ha_id + for hc_api in hass.data[DOMAIN].values(): for device in hc_api.devices: - if device.device_id == device_id: - return device.appliance + appliance = device.appliance + if appliance.haId == ha_id: + return appliance raise ValueError(f"Appliance for device id {device_id} not found") @@ -259,20 +273,9 @@ async def update_all_devices(hass: HomeAssistant, entry: ConfigEntry) -> None: data = hass.data[DOMAIN] hc_api = data[entry.entry_id] - device_registry = dr.async_get(hass) try: await hass.async_add_executor_job(hc_api.get_devices) for device in hc_api.devices: - device_entry = device_registry.async_get_or_create( - config_entry_id=entry.entry_id, - identifiers={(DOMAIN, device.appliance.haId)}, - name=device.appliance.name, - manufacturer=device.appliance.brand, - model=device.appliance.vib, - ) - - device.device_id = device_entry.id - await hass.async_add_executor_job(device.initialize) except HTTPError as err: _LOGGER.warning("Cannot update devices: %s", err.response.status_code) diff --git a/tests/components/home_connect/test_diagnostics.py b/tests/components/home_connect/test_diagnostics.py index a56ccef360f..e3915804599 100644 --- a/tests/components/home_connect/test_diagnostics.py +++ b/tests/components/home_connect/test_diagnostics.py @@ -54,8 +54,9 @@ async def test_async_get_device_diagnostics( assert await integration_setup() assert config_entry.state == ConfigEntryState.LOADED - device = device_registry.async_get_device( - identifiers={(DOMAIN, "SIEMENS-HCS02DWH1-6BE58C26DCC1")} + device = device_registry.async_get_or_create( + config_entry_id=config_entry.entry_id, + identifiers={(DOMAIN, "SIEMENS-HCS02DWH1-6BE58C26DCC1")}, ) assert await async_get_device_diagnostics(hass, config_entry, device) == snapshot diff --git a/tests/components/home_connect/test_init.py b/tests/components/home_connect/test_init.py index 52550d705a9..849e93e33d2 100644 --- a/tests/components/home_connect/test_init.py +++ b/tests/components/home_connect/test_init.py @@ -305,7 +305,7 @@ async def test_services_exception( service_call["service_data"]["device_id"] = "DOES_NOT_EXISTS" - with pytest.raises(ValueError): + with pytest.raises(AssertionError): await hass.services.async_call(**service_call)