From c862aed863360bb010a83d79b24a84fc9d76f03b Mon Sep 17 00:00:00 2001 From: Rob Hofmann Date: Sun, 15 Nov 2026 00:14:49 +0100 Subject: [PATCH] vNext: Find the config entry of a device with config_entry_id DeviceEntry.config_entries is deprecated, because a device belongs to one config entry, and it stops working in Home Assistant 2027.10. The services now read DeviceEntry.config_entry_id, which exists in 2026.10 and later. New tests call both services on a loaded entry, on an unloaded entry and with an unknown device. --- custom_components/gree_custom/services.py | 29 +++----- tests/config_flow/test_services.py | 91 +++++++++++++++++++++++ 2 files changed, 102 insertions(+), 18 deletions(-) create mode 100644 tests/config_flow/test_services.py diff --git a/custom_components/gree_custom/services.py b/custom_components/gree_custom/services.py index 8fbc3df7..0baf7cad 100755 --- a/custom_components/gree_custom/services.py +++ b/custom_components/gree_custom/services.py @@ -76,28 +76,21 @@ def async_get_device_from_service_call( translation_key="invalid_device_id", ) - config_entry: GreeConfigEntry | None = None - for entry_id in device_entry.config_entries: - entry = call.hass.config_entries.async_get_entry(entry_id) - - if TYPE_CHECKING: - assert entry - - if entry.domain != DOMAIN: - continue - if entry.state is not ConfigEntryState.LOADED: - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="entry_not_loaded", - ) - - config_entry = entry - - if not config_entry: + # A device belongs to one config entry. DeviceEntry.config_entries is + # deprecated and stops working in Home Assistant 2027.10. + config_entry: GreeConfigEntry | None = call.hass.config_entries.async_get_entry( + device_entry.config_entry_id + ) + if config_entry is None or config_entry.domain != DOMAIN: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="config_entry_not_found", ) + if config_entry.state is not ConfigEntryState.LOADED: + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="entry_not_loaded", + ) runtime_data: GreeCoordinator | None = config_entry.runtime_data.get(mac, None) diff --git a/tests/config_flow/test_services.py b/tests/config_flow/test_services.py new file mode 100644 index 00000000..48513109 --- /dev/null +++ b/tests/config_flow/test_services.py @@ -0,0 +1,91 @@ +"""The get_prop_values services, inside a real Home Assistant. + +A service call names a device. The service finds the config entry of that +device through DeviceEntry.config_entry_id, because DeviceEntry.config_entries +is deprecated and stops working in Home Assistant 2027.10. +""" + +import logging + +import pytest + +from custom_components.gree_custom.const import ATTR_SVC_PROPS, DOMAIN +from custom_components.gree_custom.services import SVC_GET_PROPS, SVC_GET_PROPS_ALL +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import ATTR_DEVICE_ID +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ServiceValidationError +from tests.config_flow.conftest import MAC_A, GreeEnv, add_entry, registry_device + + +async def test_get_prop_values_reads_the_named_device( + hass: HomeAssistant, gree: GreeEnv, caplog: pytest.LogCaptureFixture +) -> None: + """Both services answer for a device of a loaded entry, without a deprecation.""" + host = await gree.start_udp(gree.devices[MAC_A]) + entry = await add_entry(hass, ["local"], [MAC_A], [host]) + device = registry_device(hass, entry, MAC_A) + assert device is not None + caplog.set_level(logging.WARNING) + + response = await hass.services.async_call( + DOMAIN, + SVC_GET_PROPS, + {ATTR_DEVICE_ID: device.id, ATTR_SVC_PROPS: ["Pow"]}, + blocking=True, + return_response=True, + ) + assert response is not None + assert "Pow" in response["states"] + + response = await hass.services.async_call( + DOMAIN, + SVC_GET_PROPS_ALL, + {ATTR_DEVICE_ID: device.id}, + blocking=True, + return_response=True, + ) + assert response is not None + assert "Pow" in response["states"] + + assert "config_entries" not in caplog.text + + +async def test_a_device_of_an_unloaded_entry_is_refused( + hass: HomeAssistant, gree: GreeEnv +) -> None: + """The entry is there, but not loaded, so the service cannot reach the unit.""" + host = await gree.start_udp(gree.devices[MAC_A]) + entry = await add_entry(hass, ["local"], [MAC_A], [host]) + device = registry_device(hass, entry, MAC_A) + assert device is not None + + await hass.config_entries.async_unload(entry.entry_id) + await hass.async_block_till_done() + assert entry.state is ConfigEntryState.NOT_LOADED + + with pytest.raises(ServiceValidationError) as error: + await hass.services.async_call( + DOMAIN, + SVC_GET_PROPS_ALL, + {ATTR_DEVICE_ID: device.id}, + blocking=True, + return_response=True, + ) + assert error.value.translation_key == "entry_not_loaded" + + +async def test_an_unknown_device_is_refused(hass: HomeAssistant, gree: GreeEnv) -> None: + """A device ID that is not in the registry gives a clear error.""" + host = await gree.start_udp(gree.devices[MAC_A]) + await add_entry(hass, ["local"], [MAC_A], [host]) + + with pytest.raises(ServiceValidationError) as error: + await hass.services.async_call( + DOMAIN, + SVC_GET_PROPS_ALL, + {ATTR_DEVICE_ID: "not-a-device"}, + blocking=True, + return_response=True, + ) + assert error.value.translation_key == "invalid_device_id"