Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve config flow type hints in screenlogic #125199

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions homeassistant/components/screenlogic/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
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)
Expand All @@ -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)}"

Expand All @@ -83,9 +83,11 @@ def async_get_options_flow(
"""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(
Expand Down
5 changes: 4 additions & 1 deletion homeassistant/components/screenlogic/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from datetime import timedelta
import logging
from typing import TYPE_CHECKING

from screenlogicpy import ScreenLogicGateway
from screenlogicpy.const.common import (
Expand Down Expand Up @@ -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),
Expand Down