From ddf071c80e898e8931efe5e38f29c2a40b210abd Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 25 Jan 2025 08:41:54 +0100 Subject: [PATCH] Move deconz function to util.py (#136414) --- homeassistant/components/deconz/__init__.py | 2 +- homeassistant/components/deconz/config_flow.py | 11 +---------- homeassistant/components/deconz/services.py | 2 +- homeassistant/components/deconz/util.py | 15 +++++++++++++++ 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/deconz/__init__.py b/homeassistant/components/deconz/__init__.py index 4d48e6c9892..42c81e69740 100644 --- a/homeassistant/components/deconz/__init__.py +++ b/homeassistant/components/deconz/__init__.py @@ -9,12 +9,12 @@ from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import config_validation as cv from homeassistant.helpers.typing import ConfigType -from .config_flow import get_master_hub from .const import CONF_MASTER_GATEWAY, DOMAIN, PLATFORMS from .deconz_event import async_setup_events, async_unload_events from .errors import AuthenticationRequired, CannotConnect from .hub import DeconzHub, get_deconz_api from .services import async_setup_services +from .util import get_master_hub CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) diff --git a/homeassistant/components/deconz/config_flow.py b/homeassistant/components/deconz/config_flow.py index 7f5fc96c022..41e45d53c76 100644 --- a/homeassistant/components/deconz/config_flow.py +++ b/homeassistant/components/deconz/config_flow.py @@ -27,7 +27,7 @@ from homeassistant.config_entries import ( OptionsFlow, ) from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT -from homeassistant.core import HomeAssistant, callback +from homeassistant.core import callback from homeassistant.helpers import aiohttp_client from homeassistant.helpers.service_info.hassio import HassioServiceInfo from homeassistant.helpers.service_info.ssdp import ATTR_UPNP_SERIAL, SsdpServiceInfo @@ -51,15 +51,6 @@ CONF_SERIAL = "serial" CONF_MANUAL_INPUT = "Manually define gateway" -@callback -def get_master_hub(hass: HomeAssistant) -> DeconzHub: - """Return the gateway which is marked as master.""" - for hub in hass.data[DOMAIN].values(): - if hub.master: - return cast(DeconzHub, hub) - raise ValueError - - class DeconzFlowHandler(ConfigFlow, domain=DOMAIN): """Handle a deCONZ config flow.""" diff --git a/homeassistant/components/deconz/services.py b/homeassistant/components/deconz/services.py index e10195d86bc..6127fe44308 100644 --- a/homeassistant/components/deconz/services.py +++ b/homeassistant/components/deconz/services.py @@ -12,9 +12,9 @@ from homeassistant.helpers import ( from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.util.read_only_dict import ReadOnlyDict -from .config_flow import get_master_hub from .const import CONF_BRIDGE_ID, DOMAIN, LOGGER from .hub import DeconzHub +from .util import get_master_hub DECONZ_SERVICES = "deconz_services" diff --git a/homeassistant/components/deconz/util.py b/homeassistant/components/deconz/util.py index 7c44280200d..bcf338b2d6d 100644 --- a/homeassistant/components/deconz/util.py +++ b/homeassistant/components/deconz/util.py @@ -2,9 +2,24 @@ from __future__ import annotations +from homeassistant.core import HomeAssistant, callback + +from .const import DOMAIN +from .hub import DeconzHub + def serial_from_unique_id(unique_id: str | None) -> str | None: """Get a device serial number from a unique ID, if possible.""" if not unique_id or unique_id.count(":") != 7: return None return unique_id.partition("-")[0] + + +@callback +def get_master_hub(hass: HomeAssistant) -> DeconzHub: + """Return the gateway which is marked as master.""" + hub: DeconzHub + for hub in hass.data[DOMAIN].values(): + if hub.master: + return hub + raise ValueError