Improve config flow type hints in screenlogic (#125199)

This commit is contained in:
epenet 2024-09-04 19:09:41 +02:00 committed by GitHub
parent 349ea35dc3
commit 416a2de179
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 8 deletions

View File

@ -32,9 +32,9 @@ GATEWAY_MANUAL_ENTRY = "manual"
PENTAIR_OUI = "00-C0-33"
async def async_discover_gateways_by_unique_id(hass):
async def async_discover_gateways_by_unique_id() -> dict[str, dict[str, Any]]:
"""Discover gateways and return a dict of them by unique id."""
discovered_gateways = {}
discovered_gateways: dict[str, dict[str, Any]] = {}
try:
hosts = await discovery.async_discover()
_LOGGER.debug("Discovered hosts: %s", hosts)
@ -51,16 +51,16 @@ async def async_discover_gateways_by_unique_id(hass):
return discovered_gateways
def _extract_mac_from_name(name):
def _extract_mac_from_name(name: str) -> str:
return format_mac(f"{PENTAIR_OUI}-{name.split(':')[1].strip()}")
def short_mac(mac):
def short_mac(mac: str) -> str:
"""Short version of the mac as seen in the app."""
return "-".join(mac.split(":")[3:]).upper()
def name_for_mac(mac):
def name_for_mac(mac: str) -> str:
"""Derive the gateway name from the mac."""
return f"Pentair: {short_mac(mac)}"
@ -83,9 +83,11 @@ class ScreenlogicConfigFlow(ConfigFlow, domain=DOMAIN):
"""Get the options flow for ScreenLogic."""
return ScreenLogicOptionsFlowHandler(config_entry)
async def async_step_user(self, user_input=None) -> ConfigFlowResult:
async def async_step_user(
self, user_input: dict[str, Any] | None = None
) -> ConfigFlowResult:
"""Handle the start of the config flow."""
self.discovered_gateways = await async_discover_gateways_by_unique_id(self.hass)
self.discovered_gateways = await async_discover_gateways_by_unique_id()
return await self.async_step_gateway_select()
async def async_step_dhcp(

View File

@ -2,6 +2,7 @@
from datetime import timedelta
import logging
from typing import TYPE_CHECKING
from screenlogicpy import ScreenLogicGateway
from screenlogicpy.const.common import (
@ -33,11 +34,13 @@ async def async_get_connect_info(
"""Construct connect_info from configuration entry and returns it to caller."""
mac = entry.unique_id
# Attempt to rediscover gateway to follow IP changes
discovered_gateways = await async_discover_gateways_by_unique_id(hass)
discovered_gateways = await async_discover_gateways_by_unique_id()
if mac in discovered_gateways:
return discovered_gateways[mac]
_LOGGER.debug("Gateway rediscovery failed for %s", entry.title)
if TYPE_CHECKING:
assert mac is not None
# Static connection defined or fallback from discovery
return {
SL_GATEWAY_NAME: name_for_mac(mac),