Move deconz function to util.py (#136414)

This commit is contained in:
epenet 2025-01-25 08:41:54 +01:00 committed by GitHub
parent 829fab5371
commit ddf071c80e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 12 deletions

View File

@ -9,12 +9,12 @@ from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from .config_flow import get_master_hub
from .const import CONF_MASTER_GATEWAY, DOMAIN, PLATFORMS from .const import CONF_MASTER_GATEWAY, DOMAIN, PLATFORMS
from .deconz_event import async_setup_events, async_unload_events from .deconz_event import async_setup_events, async_unload_events
from .errors import AuthenticationRequired, CannotConnect from .errors import AuthenticationRequired, CannotConnect
from .hub import DeconzHub, get_deconz_api from .hub import DeconzHub, get_deconz_api
from .services import async_setup_services from .services import async_setup_services
from .util import get_master_hub
CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)

View File

@ -27,7 +27,7 @@ from homeassistant.config_entries import (
OptionsFlow, OptionsFlow,
) )
from homeassistant.const import CONF_API_KEY, CONF_HOST, CONF_PORT 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 import aiohttp_client
from homeassistant.helpers.service_info.hassio import HassioServiceInfo from homeassistant.helpers.service_info.hassio import HassioServiceInfo
from homeassistant.helpers.service_info.ssdp import ATTR_UPNP_SERIAL, SsdpServiceInfo from homeassistant.helpers.service_info.ssdp import ATTR_UPNP_SERIAL, SsdpServiceInfo
@ -51,15 +51,6 @@ CONF_SERIAL = "serial"
CONF_MANUAL_INPUT = "Manually define gateway" 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): class DeconzFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle a deCONZ config flow.""" """Handle a deCONZ config flow."""

View File

@ -12,9 +12,9 @@ from homeassistant.helpers import (
from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC from homeassistant.helpers.device_registry import CONNECTION_NETWORK_MAC
from homeassistant.util.read_only_dict import ReadOnlyDict from homeassistant.util.read_only_dict import ReadOnlyDict
from .config_flow import get_master_hub
from .const import CONF_BRIDGE_ID, DOMAIN, LOGGER from .const import CONF_BRIDGE_ID, DOMAIN, LOGGER
from .hub import DeconzHub from .hub import DeconzHub
from .util import get_master_hub
DECONZ_SERVICES = "deconz_services" DECONZ_SERVICES = "deconz_services"

View File

@ -2,9 +2,24 @@
from __future__ import annotations 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: def serial_from_unique_id(unique_id: str | None) -> str | None:
"""Get a device serial number from a unique ID, if possible.""" """Get a device serial number from a unique ID, if possible."""
if not unique_id or unique_id.count(":") != 7: if not unique_id or unique_id.count(":") != 7:
return None return None
return unique_id.partition("-")[0] 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