Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions custom_components/gree_custom/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
91 changes: 91 additions & 0 deletions tests/config_flow/test_services.py
Original file line number Diff line number Diff line change
@@ -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"
Loading