From 1731278d86cc9ebe5dd7e6038fc826ae9a35cee7 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Tue, 25 Aug 2026 09:56:55 +0200 Subject: [PATCH] Use get_device_and_config_entry_for_domain in switchbot (#180111) --- .../components/switchbot/services.py | 29 +++++-------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/switchbot/services.py b/homeassistant/components/switchbot/services.py index 7e8c9a921521..a7a979d1765a 100644 --- a/homeassistant/components/switchbot/services.py +++ b/homeassistant/components/switchbot/services.py @@ -31,45 +31,32 @@ def _async_get_switchbot_entry_for_device_id( hass: HomeAssistant, device_id: str ) -> SwitchbotConfigEntry: """Return the loaded SwitchBot config entry for a device id.""" - device_registry = dr.async_get(hass) - if not (device_entry := device_registry.async_get(device_id)): + config_entry: SwitchbotConfigEntry | None + device, config_entry = dr.async_get_device_and_config_entry_for_domain( + hass, device_id, domain=DOMAIN + ) + if device is None: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="invalid_device_id", translation_placeholders={"device_id": device_id}, ) - entries = [ - hass.config_entries.async_get_entry(entry_id) - for entry_id in device_entry.config_entries - ] - switchbot_entries = [ - entry for entry in entries if entry is not None and entry.domain == DOMAIN - ] - if not switchbot_entries: + if config_entry is None: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="device_not_belonging", translation_placeholders={"device_id": device_id}, ) - if not ( - loaded_entry := next( - ( - entry - for entry in switchbot_entries - if entry.state is ConfigEntryState.LOADED - ), - None, - ) - ): + if config_entry.state is not ConfigEntryState.LOADED: raise ServiceValidationError( translation_domain=DOMAIN, translation_key="device_entry_not_loaded", translation_placeholders={"device_id": device_id}, ) - return loaded_entry + return config_entry def _is_supported_keypad(entry: SwitchbotConfigEntry) -> bool: