Support Huawei LTE SSDP discovery (#28214)
* Support Huawei LTE SSDP discovery * Avoid KeyError on simultaneous user initiated flow Co-Authored-By: Paulus Schoutsen <paulus@home-assistant.io> * Format code * Add already configured check * Initialize context in test flows * Move deviceType match to manifest * Update generated.ssdp * Add SSDP config flow test case * Remove stale debug print from tests
This commit is contained in:
1 parent
f3ea44cd92
commit
6a7b5657ac
6 files changed
+116
-27
No files matched your search
@@ -1,7 +1,9 @@
|
||||
{
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "This device is already configured"
|
||||
"already_configured": "This device has already been configured",
|
||||
"already_in_progress": "This device is already being configured",
|
||||
"not_huawei_lte": "Not a Huawei LTE device"
|
||||
},
|
||||
"error": {
|
||||
"connection_failed": "Connection failed",
|
||||
|
||||
@@ -19,6 +19,7 @@ from url_normalize import url_normalize
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.ssdp import ATTR_HOST, ATTR_NAME, ATTR_PRESENTATIONURL
|
||||
from homeassistant.const import CONF_PASSWORD, CONF_RECIPIENT, CONF_URL, CONF_USERNAME
|
||||
from homeassistant.core import callback
|
||||
from .const import CONNECTION_TIMEOUT, DEFAULT_DEVICE_NAME
|
||||
@@ -52,7 +53,14 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
(
|
||||
(
|
||||
vol.Required(
|
||||
CONF_URL, default=user_input.get(CONF_URL, "")
|
||||
CONF_URL,
|
||||
default=user_input.get(
|
||||
CONF_URL,
|
||||
# https://github.com/PyCQA/pylint/issues/3167
|
||||
self.context.get( # pylint: disable=no-member
|
||||
CONF_URL, ""
|
||||
),
|
||||
),
|
||||
),
|
||||
str,
|
||||
),
|
||||
@@ -78,6 +86,14 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle import initiated config flow."""
|
||||
return await self.async_step_user(user_input)
|
||||
|
||||
def _already_configured(self, user_input):
|
||||
"""See if we already have a router matching user input configured."""
|
||||
existing_urls = {
|
||||
url_normalize(entry.data[CONF_URL], default_scheme="http")
|
||||
for entry in self._async_current_entries()
|
||||
}
|
||||
return user_input[CONF_URL] in existing_urls
|
||||
|
||||
async def async_step_user(self, user_input=None):
|
||||
"""Handle user initiated config flow."""
|
||||
if user_input is None:
|
||||
@@ -95,12 +111,7 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
user_input=user_input, errors=errors
|
||||
)
|
||||
|
||||
# See if we already have a router configured with this URL
|
||||
existing_urls = { # existing entries
|
||||
url_normalize(entry.data[CONF_URL], default_scheme="http")
|
||||
for entry in self._async_current_entries()
|
||||
}
|
||||
if user_input[CONF_URL] in existing_urls:
|
||||
if self._already_configured(user_input):
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
conn = None
|
||||
@@ -194,6 +205,31 @@ class ConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
|
||||
return self.async_create_entry(title=title, data=user_input)
|
||||
|
||||
async def async_step_ssdp(self, discovery_info):
|
||||
"""Handle SSDP initiated config flow."""
|
||||
# Attempt to distinguish from other non-LTE Huawei router devices, at least
|
||||
# some ones we are interested in have "Mobile Wi-Fi" friendlyName.
|
||||
if "mobile" not in discovery_info.get(ATTR_NAME, "").lower():
|
||||
return self.async_abort(reason="not_huawei_lte")
|
||||
|
||||
# https://github.com/PyCQA/pylint/issues/3167
|
||||
url = self.context[CONF_URL] = url_normalize( # pylint: disable=no-member
|
||||
discovery_info.get(
|
||||
ATTR_PRESENTATIONURL, f"http://{discovery_info[ATTR_HOST]}/"
|
||||
)
|
||||
)
|
||||
|
||||
if any(
|
||||
url == flow["context"].get(CONF_URL) for flow in self._async_in_progress()
|
||||
):
|
||||
return self.async_abort(reason="already_in_progress")
|
||||
|
||||
user_input = {CONF_URL: url}
|
||||
if self._already_configured(user_input):
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
return await self._async_show_user_form(user_input)
|
||||
|
||||
|
||||
class OptionsFlowHandler(config_entries.OptionsFlow):
|
||||
"""Huawei LTE options flow."""
|
||||
|
||||
@@ -9,6 +9,12 @@
|
||||
"stringcase==1.2.0",
|
||||
"url-normalize==1.4.1"
|
||||
],
|
||||
"ssdp": [
|
||||
{
|
||||
"deviceType": "urn:schemas-upnp-org:device:InternetGatewayDevice:1",
|
||||
"manufacturer": "Huawei"
|
||||
}
|
||||
],
|
||||
"dependencies": [],
|
||||
"codeowners": [
|
||||
"@scop"
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
{
|
||||
"config": {
|
||||
"abort": {
|
||||
"already_configured": "This device is already configured"
|
||||
"already_configured": "This device has already been configured",
|
||||
"already_in_progress": "This device is already being configured",
|
||||
"not_huawei_lte": "Not a Huawei LTE device"
|
||||
},
|
||||
"error": {
|
||||
"connection_failed": "Connection failed",
|
||||
|
||||
Reference in new issue
Block a user