From f33feabcbd759df4819a62149e5ec30ca7078887 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 25 Aug 2026 09:11:06 +0200 Subject: [PATCH] Use get_device_and_config_entry_for_domain in google_air_quality (#180094) --- .../components/google_air_quality/services.py | 38 ++++++++----------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/google_air_quality/services.py b/homeassistant/components/google_air_quality/services.py index 6fff8844b7fd..04a813f7a049 100644 --- a/homeassistant/components/google_air_quality/services.py +++ b/homeassistant/components/google_air_quality/services.py @@ -6,7 +6,7 @@ from typing import Final, cast from google_air_quality_api.exceptions import GoogleAirQualityApiError import voluptuous as vol -from homeassistant.config_entries import ConfigEntry +from homeassistant.config_entries import ConfigEntryState from homeassistant.const import ATTR_DEVICE_ID from homeassistant.core import ( HomeAssistant, @@ -41,28 +41,22 @@ def _get_config_entry_and_subentry_id( hass: HomeAssistant, device_id: str ) -> tuple[GoogleAirQualityConfigEntry, str]: """Get the config entry and subentry from a selected location device.""" - device = dr.async_get(hass).async_get(device_id) - if device is not None: - for entry_id, subentry_ids in device.config_entries_subentries.items(): - config_entry: ConfigEntry | None = hass.config_entries.async_get_entry( - entry_id - ) - if config_entry is None or config_entry.domain != DOMAIN: - continue - - gaq_config_entry = cast(GoogleAirQualityConfigEntry, config_entry) - for subentry_id in subentry_ids: - if ( - subentry_id is not None - and subentry_id - in gaq_config_entry.runtime_data.subentries_runtime_data - ): - return gaq_config_entry, subentry_id - - raise ServiceValidationError( - translation_domain=DOMAIN, - translation_key="device_not_found", + config_entry: GoogleAirQualityConfigEntry | None + device, config_entry = dr.async_get_device_and_config_entry_for_domain( + hass, device_id, domain=DOMAIN ) + if ( + device is None + or config_entry is None + or config_entry.state is not ConfigEntryState.LOADED + or (subentry_id := device.config_subentry_id) is None + or subentry_id not in config_entry.runtime_data.subentries_runtime_data + ): + raise ServiceValidationError( + translation_domain=DOMAIN, + translation_key="device_not_found", + ) + return config_entry, subentry_id async def _async_get_forecast(call: ServiceCall) -> ServiceResponse: