Allow Guardian config flow to be ignored (#36207)
* Allow Guardian config flow to be ignored * Tests
This commit is contained in:
1 parent
4e74fae615
commit
7571fdc957
2 files changed
+49
-13
No files matched your search
@@ -5,6 +5,7 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries, core
|
||||
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT
|
||||
from homeassistant.core import callback
|
||||
|
||||
from .const import CONF_UID, DOMAIN, LOGGER # pylint:disable=unused-import
|
||||
|
||||
@@ -12,6 +13,20 @@ DATA_SCHEMA = vol.Schema(
|
||||
{vol.Required(CONF_IP_ADDRESS): str, vol.Required(CONF_PORT, default=7777): int}
|
||||
)
|
||||
|
||||
UNIQUE_ID = "guardian_{0}"
|
||||
|
||||
|
||||
@callback
|
||||
def async_get_pin_from_discovery_hostname(hostname):
|
||||
"""Get the device's 4-digit PIN from its zeroconf-discovered hostname."""
|
||||
return hostname.split(".")[0].split("-")[1]
|
||||
|
||||
|
||||
@callback
|
||||
def async_get_pin_from_uid(uid):
|
||||
"""Get the device's 4-digit PIN from its UID."""
|
||||
return uid[-4:]
|
||||
|
||||
|
||||
async def validate_input(hass: core.HomeAssistant, data):
|
||||
"""Validate the user input allows us to connect.
|
||||
@@ -22,7 +37,6 @@ async def validate_input(hass: core.HomeAssistant, data):
|
||||
ping_data = await client.device.ping()
|
||||
|
||||
return {
|
||||
"title": f"Elexa Guardian ({data[CONF_IP_ADDRESS]})",
|
||||
CONF_UID: ping_data["data"]["uid"],
|
||||
}
|
||||
|
||||
@@ -37,6 +51,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Initialize."""
|
||||
self.discovery_info = {}
|
||||
|
||||
async def _async_set_unique_id(self, pin):
|
||||
"""Set the config entry's unique ID (based on the device's 4-digit PIN)."""
|
||||
await self.async_set_unique_id(UNIQUE_ID.format(pin))
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Handle configuration via the UI."""
|
||||
if user_input is None:
|
||||
@@ -44,9 +63,6 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
step_id="user", data_schema=DATA_SCHEMA, errors={}
|
||||
)
|
||||
|
||||
await self.async_set_unique_id(user_input[CONF_IP_ADDRESS])
|
||||
self._abort_if_unique_id_configured()
|
||||
|
||||
try:
|
||||
info = await validate_input(self.hass, user_input)
|
||||
except GuardianError as err:
|
||||
@@ -57,8 +73,11 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
errors={CONF_IP_ADDRESS: "cannot_connect"},
|
||||
)
|
||||
|
||||
pin = async_get_pin_from_uid(info[CONF_UID])
|
||||
await self._async_set_unique_id(pin)
|
||||
|
||||
return self.async_create_entry(
|
||||
title=info["title"], data={CONF_UID: info["uid"], **user_input}
|
||||
title=info[CONF_UID], data={CONF_UID: info["uid"], **user_input}
|
||||
)
|
||||
|
||||
async def async_step_zeroconf(self, discovery_info=None):
|
||||
@@ -66,19 +85,20 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
if discovery_info is None:
|
||||
return self.async_abort(reason="connection_error")
|
||||
|
||||
ip_address = discovery_info["host"]
|
||||
pin = async_get_pin_from_discovery_hostname(discovery_info["hostname"])
|
||||
await self._async_set_unique_id(pin)
|
||||
|
||||
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
||||
self.context[CONF_IP_ADDRESS] = ip_address
|
||||
self.context[CONF_IP_ADDRESS] = discovery_info["host"]
|
||||
|
||||
if any(
|
||||
ip_address == flow["context"][CONF_IP_ADDRESS]
|
||||
discovery_info["host"] == flow["context"][CONF_IP_ADDRESS]
|
||||
for flow in self._async_in_progress()
|
||||
):
|
||||
return self.async_abort(reason="already_in_progress")
|
||||
|
||||
self.discovery_info = {
|
||||
CONF_IP_ADDRESS: ip_address,
|
||||
CONF_IP_ADDRESS: discovery_info["host"],
|
||||
CONF_PORT: discovery_info["port"],
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user