mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
UniFi - Try to discover local controller (#31326)
* Its working * Use "unifi" as default host if a controller can be found * Fix tests * Make a fixture of patching the discovery function
This commit is contained in:
parent
56657fa859
commit
a8374cf423
@ -1,4 +1,6 @@
|
|||||||
"""Config flow for UniFi."""
|
"""Config flow for UniFi."""
|
||||||
|
import socket
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
@ -104,11 +106,15 @@ class UnifiFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
)
|
)
|
||||||
return self.async_abort(reason="unknown")
|
return self.async_abort(reason="unknown")
|
||||||
|
|
||||||
|
host = ""
|
||||||
|
if await async_discover_unifi(self.hass):
|
||||||
|
host = "unifi"
|
||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="user",
|
step_id="user",
|
||||||
data_schema=vol.Schema(
|
data_schema=vol.Schema(
|
||||||
{
|
{
|
||||||
vol.Required(CONF_HOST): str,
|
vol.Required(CONF_HOST, default=host): str,
|
||||||
vol.Required(CONF_USERNAME): str,
|
vol.Required(CONF_USERNAME): str,
|
||||||
vol.Required(CONF_PASSWORD): str,
|
vol.Required(CONF_PASSWORD): str,
|
||||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): int,
|
vol.Optional(CONF_PORT, default=DEFAULT_PORT): int,
|
||||||
@ -235,3 +241,11 @@ class UnifiOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
async def _update_options(self):
|
async def _update_options(self):
|
||||||
"""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):
|
||||||
|
"""Discover UniFi address."""
|
||||||
|
try:
|
||||||
|
return await hass.async_add_executor_job(socket.gethostbyname, "unifi")
|
||||||
|
except socket.gaierror:
|
||||||
|
return None
|
||||||
|
13
tests/components/unifi/conftest.py
Normal file
13
tests/components/unifi/conftest.py
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
"""Fixtures for UniFi methods."""
|
||||||
|
from asynctest import patch
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def mock_discovery():
|
||||||
|
"""No real network traffic allowed."""
|
||||||
|
with patch(
|
||||||
|
"homeassistant.components.unifi.config_flow.async_discover_unifi",
|
||||||
|
return_value=None,
|
||||||
|
) as mock:
|
||||||
|
yield mock
|
@ -16,14 +16,22 @@ from homeassistant.const import (
|
|||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_works(hass, aioclient_mock):
|
async def test_flow_works(hass, aioclient_mock, mock_discovery):
|
||||||
"""Test config flow."""
|
"""Test config flow."""
|
||||||
|
mock_discovery.return_value = "1"
|
||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
config_flow.DOMAIN, context={"source": "user"}
|
config_flow.DOMAIN, context={"source": "user"}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == "form"
|
assert result["type"] == "form"
|
||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
|
assert result["data_schema"]({CONF_USERNAME: "", CONF_PASSWORD: ""}) == {
|
||||||
|
CONF_HOST: "unifi",
|
||||||
|
CONF_USERNAME: "",
|
||||||
|
CONF_PASSWORD: "",
|
||||||
|
CONF_PORT: 8443,
|
||||||
|
CONF_VERIFY_SSL: False,
|
||||||
|
}
|
||||||
|
|
||||||
aioclient_mock.post(
|
aioclient_mock.post(
|
||||||
"https://1.2.3.4:1234/api/login",
|
"https://1.2.3.4:1234/api/login",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user