mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Adjust config-flow type hints in unifi (#72411)
* Adjust config-flow type hints in unifi * Use mapping * Use mapping * Fix tests * Fix tests * Revert "Use mapping" This reverts commit 126fedc84828dfa2badc1b6f673ab8a4e702d230.
This commit is contained in:
parent
2e36a79357
commit
f33151ff8b
@ -5,7 +5,11 @@ Discovery of UniFi Network instances hosted on UDM and UDM Pro devices
|
|||||||
through SSDP. Reauthentication when issue with credentials are reported.
|
through SSDP. Reauthentication when issue with credentials are reported.
|
||||||
Configuration of options through options flow.
|
Configuration of options through options flow.
|
||||||
"""
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections.abc import Mapping
|
||||||
import socket
|
import socket
|
||||||
|
from typing import Any
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -19,7 +23,7 @@ from homeassistant.const import (
|
|||||||
CONF_USERNAME,
|
CONF_USERNAME,
|
||||||
CONF_VERIFY_SSL,
|
CONF_VERIFY_SSL,
|
||||||
)
|
)
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.helpers.device_registry import format_mac
|
from homeassistant.helpers.device_registry import format_mac
|
||||||
@ -63,11 +67,13 @@ class UnifiFlowHandler(config_entries.ConfigFlow, domain=UNIFI_DOMAIN):
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
@callback
|
@callback
|
||||||
def async_get_options_flow(config_entry):
|
def async_get_options_flow(
|
||||||
|
config_entry: config_entries.ConfigEntry,
|
||||||
|
) -> UnifiOptionsFlowHandler:
|
||||||
"""Get the options flow for this handler."""
|
"""Get the options flow for this handler."""
|
||||||
return UnifiOptionsFlowHandler(config_entry)
|
return UnifiOptionsFlowHandler(config_entry)
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self) -> None:
|
||||||
"""Initialize the UniFi Network flow."""
|
"""Initialize the UniFi Network flow."""
|
||||||
self.config = {}
|
self.config = {}
|
||||||
self.site_ids = {}
|
self.site_ids = {}
|
||||||
@ -75,7 +81,9 @@ class UnifiFlowHandler(config_entries.ConfigFlow, domain=UNIFI_DOMAIN):
|
|||||||
self.reauth_config_entry = None
|
self.reauth_config_entry = None
|
||||||
self.reauth_schema = {}
|
self.reauth_schema = {}
|
||||||
|
|
||||||
async def async_step_user(self, user_input=None):
|
async def async_step_user(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Handle a flow initialized by the user."""
|
"""Handle a flow initialized by the user."""
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
@ -123,7 +131,7 @@ class UnifiFlowHandler(config_entries.ConfigFlow, domain=UNIFI_DOMAIN):
|
|||||||
|
|
||||||
return await self.async_step_site()
|
return await self.async_step_site()
|
||||||
|
|
||||||
if not (host := self.config.get(CONF_HOST, "")) and await async_discover_unifi(
|
if not (host := self.config.get(CONF_HOST, "")) and await _async_discover_unifi(
|
||||||
self.hass
|
self.hass
|
||||||
):
|
):
|
||||||
host = "unifi"
|
host = "unifi"
|
||||||
@ -144,7 +152,9 @@ class UnifiFlowHandler(config_entries.ConfigFlow, domain=UNIFI_DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_site(self, user_input=None):
|
async def async_step_site(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Select site to control."""
|
"""Select site to control."""
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
@ -192,7 +202,7 @@ class UnifiFlowHandler(config_entries.ConfigFlow, domain=UNIFI_DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_reauth(self, data: dict):
|
async def async_step_reauth(self, data: Mapping[str, Any]) -> FlowResult:
|
||||||
"""Trigger a reauthentication flow."""
|
"""Trigger a reauthentication flow."""
|
||||||
config_entry = self.hass.config_entries.async_get_entry(
|
config_entry = self.hass.config_entries.async_get_entry(
|
||||||
self.context["entry_id"]
|
self.context["entry_id"]
|
||||||
@ -248,13 +258,15 @@ class UnifiFlowHandler(config_entries.ConfigFlow, domain=UNIFI_DOMAIN):
|
|||||||
class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
|
class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
|
||||||
"""Handle Unifi Network options."""
|
"""Handle Unifi Network options."""
|
||||||
|
|
||||||
def __init__(self, config_entry):
|
def __init__(self, config_entry: config_entries.ConfigEntry) -> None:
|
||||||
"""Initialize UniFi Network options flow."""
|
"""Initialize UniFi Network options flow."""
|
||||||
self.config_entry = config_entry
|
self.config_entry = config_entry
|
||||||
self.options = dict(config_entry.options)
|
self.options = dict(config_entry.options)
|
||||||
self.controller = None
|
self.controller = None
|
||||||
|
|
||||||
async def async_step_init(self, user_input=None):
|
async def async_step_init(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Manage the UniFi Network options."""
|
"""Manage the UniFi Network options."""
|
||||||
if self.config_entry.entry_id not in self.hass.data[UNIFI_DOMAIN]:
|
if self.config_entry.entry_id not in self.hass.data[UNIFI_DOMAIN]:
|
||||||
return self.async_abort(reason="integration_not_setup")
|
return self.async_abort(reason="integration_not_setup")
|
||||||
@ -266,7 +278,9 @@ class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
|
|
||||||
return await self.async_step_simple_options()
|
return await self.async_step_simple_options()
|
||||||
|
|
||||||
async def async_step_simple_options(self, user_input=None):
|
async def async_step_simple_options(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""For users without advanced settings enabled."""
|
"""For users without advanced settings enabled."""
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
self.options.update(user_input)
|
self.options.update(user_input)
|
||||||
@ -299,7 +313,9 @@ class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
last_step=True,
|
last_step=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_device_tracker(self, user_input=None):
|
async def async_step_device_tracker(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Manage the device tracker options."""
|
"""Manage the device tracker options."""
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
self.options.update(user_input)
|
self.options.update(user_input)
|
||||||
@ -359,7 +375,9 @@ class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
last_step=False,
|
last_step=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_client_control(self, user_input=None):
|
async def async_step_client_control(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Manage configuration of network access controlled clients."""
|
"""Manage configuration of network access controlled clients."""
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
@ -403,7 +421,9 @@ class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
last_step=False,
|
last_step=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_statistics_sensors(self, user_input=None):
|
async def async_step_statistics_sensors(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Manage the statistics sensors options."""
|
"""Manage the statistics sensors options."""
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
self.options.update(user_input)
|
self.options.update(user_input)
|
||||||
@ -426,12 +446,12 @@ class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
last_step=True,
|
last_step=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def _update_options(self):
|
async def _update_options(self) -> FlowResult:
|
||||||
"""Update config entry options."""
|
"""Update config entry options."""
|
||||||
return self.async_create_entry(title="", data=self.options)
|
return self.async_create_entry(title="", data=self.options)
|
||||||
|
|
||||||
|
|
||||||
async def async_discover_unifi(hass):
|
async def _async_discover_unifi(hass: HomeAssistant) -> str | None:
|
||||||
"""Discover UniFi Network address."""
|
"""Discover UniFi Network address."""
|
||||||
try:
|
try:
|
||||||
return await hass.async_add_executor_job(socket.gethostbyname, "unifi")
|
return await hass.async_add_executor_job(socket.gethostbyname, "unifi")
|
||||||
|
@ -34,7 +34,7 @@ def mock_unifi_websocket():
|
|||||||
def mock_discovery():
|
def mock_discovery():
|
||||||
"""No real network traffic allowed."""
|
"""No real network traffic allowed."""
|
||||||
with patch(
|
with patch(
|
||||||
"homeassistant.components.unifi.config_flow.async_discover_unifi",
|
"homeassistant.components.unifi.config_flow._async_discover_unifi",
|
||||||
return_value=None,
|
return_value=None,
|
||||||
) as mock:
|
) as mock:
|
||||||
yield mock
|
yield mock
|
||||||
|
@ -7,7 +7,7 @@ import aiounifi
|
|||||||
|
|
||||||
from homeassistant import config_entries, data_entry_flow
|
from homeassistant import config_entries, data_entry_flow
|
||||||
from homeassistant.components import ssdp
|
from homeassistant.components import ssdp
|
||||||
from homeassistant.components.unifi.config_flow import async_discover_unifi
|
from homeassistant.components.unifi.config_flow import _async_discover_unifi
|
||||||
from homeassistant.components.unifi.const import (
|
from homeassistant.components.unifi.const import (
|
||||||
CONF_ALLOW_BANDWIDTH_SENSORS,
|
CONF_ALLOW_BANDWIDTH_SENSORS,
|
||||||
CONF_ALLOW_UPTIME_SENSORS,
|
CONF_ALLOW_UPTIME_SENSORS,
|
||||||
@ -686,10 +686,10 @@ async def test_form_ssdp_gets_form_with_ignored_entry(hass):
|
|||||||
async def test_discover_unifi_positive(hass):
|
async def test_discover_unifi_positive(hass):
|
||||||
"""Verify positive run of UniFi discovery."""
|
"""Verify positive run of UniFi discovery."""
|
||||||
with patch("socket.gethostbyname", return_value=True):
|
with patch("socket.gethostbyname", return_value=True):
|
||||||
assert await async_discover_unifi(hass)
|
assert await _async_discover_unifi(hass)
|
||||||
|
|
||||||
|
|
||||||
async def test_discover_unifi_negative(hass):
|
async def test_discover_unifi_negative(hass):
|
||||||
"""Verify negative run of UniFi discovery."""
|
"""Verify negative run of UniFi discovery."""
|
||||||
with patch("socket.gethostbyname", side_effect=socket.gaierror):
|
with patch("socket.gethostbyname", side_effect=socket.gaierror):
|
||||||
assert await async_discover_unifi(hass) is None
|
assert await _async_discover_unifi(hass) is None
|
||||||
|
Loading…
x
Reference in New Issue
Block a user