2019-05-13 08:16:55 +00:00
|
|
|
"""Config flow for UPNP."""
|
2021-03-18 13:43:52 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-04-20 15:40:41 +00:00
|
|
|
from collections.abc import Mapping
|
2022-03-08 06:51:23 +00:00
|
|
|
from typing import Any, cast
|
2020-05-03 01:03:54 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-05-13 08:16:55 +00:00
|
|
|
from homeassistant import config_entries
|
2020-05-03 01:03:54 +00:00
|
|
|
from homeassistant.components import ssdp
|
2022-05-30 15:07:18 +00:00
|
|
|
from homeassistant.components.ssdp import SsdpServiceInfo
|
2022-04-12 21:10:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-01 19:23:38 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2019-05-13 08:16:55 +00:00
|
|
|
|
2021-01-29 09:23:34 +00:00
|
|
|
from .const import (
|
2022-04-20 21:01:43 +00:00
|
|
|
CONFIG_ENTRY_LOCATION,
|
|
|
|
CONFIG_ENTRY_MAC_ADDRESS,
|
|
|
|
CONFIG_ENTRY_ORIGINAL_UDN,
|
2020-05-03 01:03:54 +00:00
|
|
|
CONFIG_ENTRY_ST,
|
|
|
|
CONFIG_ENTRY_UDN,
|
|
|
|
DOMAIN,
|
2021-08-17 18:23:41 +00:00
|
|
|
LOGGER,
|
2021-08-13 16:13:25 +00:00
|
|
|
ST_IGD_V1,
|
|
|
|
ST_IGD_V2,
|
2020-05-03 01:03:54 +00:00
|
|
|
)
|
2022-04-20 21:01:43 +00:00
|
|
|
from .device import async_get_mac_address_from_host
|
2021-08-13 16:13:25 +00:00
|
|
|
|
|
|
|
|
2021-12-01 19:23:38 +00:00
|
|
|
def _friendly_name_from_discovery(discovery_info: ssdp.SsdpServiceInfo) -> str:
|
2021-08-13 16:13:25 +00:00
|
|
|
"""Extract user-friendly name from discovery."""
|
2022-03-08 06:51:23 +00:00
|
|
|
return cast(
|
|
|
|
str,
|
2021-12-01 19:23:38 +00:00
|
|
|
discovery_info.upnp.get(ssdp.ATTR_UPNP_FRIENDLY_NAME)
|
|
|
|
or discovery_info.upnp.get(ssdp.ATTR_UPNP_MODEL_NAME)
|
2022-03-08 06:51:23 +00:00
|
|
|
or discovery_info.ssdp_headers.get("_host", ""),
|
2021-08-13 16:13:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-01 19:23:38 +00:00
|
|
|
def _is_complete_discovery(discovery_info: ssdp.SsdpServiceInfo) -> bool:
|
2021-10-19 06:57:48 +00:00
|
|
|
"""Test if discovery is complete and usable."""
|
2022-03-08 06:51:23 +00:00
|
|
|
return bool(
|
2021-12-01 19:23:38 +00:00
|
|
|
ssdp.ATTR_UPNP_UDN in discovery_info.upnp
|
|
|
|
and discovery_info.ssdp_st
|
|
|
|
and discovery_info.ssdp_location
|
|
|
|
and discovery_info.ssdp_usn
|
2021-10-19 06:57:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-12-01 19:23:38 +00:00
|
|
|
async def _async_discover_igd_devices(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
) -> list[ssdp.SsdpServiceInfo]:
|
2021-08-13 16:13:25 +00:00
|
|
|
"""Discovery IGD devices."""
|
2021-09-11 23:38:16 +00:00
|
|
|
return await ssdp.async_get_discovery_info_by_st(
|
2021-08-13 16:13:25 +00:00
|
|
|
hass, ST_IGD_V1
|
2021-09-11 23:38:16 +00:00
|
|
|
) + await ssdp.async_get_discovery_info_by_st(hass, ST_IGD_V2)
|
2021-01-29 09:23:34 +00:00
|
|
|
|
|
|
|
|
2022-04-20 21:01:43 +00:00
|
|
|
async def _async_mac_address_from_discovery(
|
|
|
|
hass: HomeAssistant, discovery: SsdpServiceInfo
|
|
|
|
) -> str | None:
|
|
|
|
"""Get the mac address from a discovery."""
|
|
|
|
host = discovery.ssdp_headers["_host"]
|
|
|
|
return await async_get_mac_address_from_host(hass, host)
|
|
|
|
|
|
|
|
|
2022-06-17 16:26:45 +00:00
|
|
|
def _is_igd_device(discovery_info: ssdp.SsdpServiceInfo) -> bool:
|
|
|
|
"""Test if discovery is a complete IGD device."""
|
|
|
|
root_device_info = discovery_info.upnp
|
|
|
|
return root_device_info.get(ssdp.ATTR_UPNP_DEVICE_TYPE) in {ST_IGD_V1, ST_IGD_V2}
|
|
|
|
|
|
|
|
|
2020-05-03 01:03:54 +00:00
|
|
|
class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a UPnP/IGD config flow."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
|
|
|
# Paths:
|
|
|
|
# - ssdp(discovery_info) --> ssdp_confirm(None) --> ssdp_confirm({}) --> create_entry()
|
|
|
|
# - user(None): scan --> user({...}) --> create_entry()
|
|
|
|
# - import(None) --> create_entry()
|
|
|
|
|
2021-01-29 09:23:34 +00:00
|
|
|
def __init__(self) -> None:
|
2020-05-03 01:03:54 +00:00
|
|
|
"""Initialize the UPnP/IGD config flow."""
|
2021-12-03 07:32:42 +00:00
|
|
|
self._discoveries: list[SsdpServiceInfo] | None = None
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2022-05-30 15:07:18 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: Mapping[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2020-05-03 01:03:54 +00:00
|
|
|
"""Handle a flow start."""
|
2021-08-17 18:23:41 +00:00
|
|
|
LOGGER.debug("async_step_user: user_input: %s", user_input)
|
2020-05-03 01:03:54 +00:00
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
# Ensure wanted device was discovered.
|
2022-03-08 06:51:23 +00:00
|
|
|
assert self._discoveries
|
2022-04-20 21:01:43 +00:00
|
|
|
discovery = next(
|
|
|
|
iter(
|
|
|
|
discovery
|
|
|
|
for discovery in self._discoveries
|
|
|
|
if discovery.ssdp_usn == user_input["unique_id"]
|
|
|
|
)
|
|
|
|
)
|
2021-12-03 07:32:42 +00:00
|
|
|
await self.async_set_unique_id(discovery.ssdp_usn, raise_on_progress=False)
|
2020-05-11 18:03:12 +00:00
|
|
|
return await self._async_create_entry_from_discovery(discovery)
|
2020-05-03 01:03:54 +00:00
|
|
|
|
|
|
|
# Discover devices.
|
2021-09-11 23:38:16 +00:00
|
|
|
discoveries = await _async_discover_igd_devices(self.hass)
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2021-01-29 09:23:34 +00:00
|
|
|
# Store discoveries which have not been configured.
|
|
|
|
current_unique_ids = {
|
|
|
|
entry.unique_id for entry in self._async_current_entries()
|
|
|
|
}
|
2020-05-03 01:03:54 +00:00
|
|
|
self._discoveries = [
|
2021-01-29 09:23:34 +00:00
|
|
|
discovery
|
2020-05-03 01:03:54 +00:00
|
|
|
for discovery in discoveries
|
2021-10-19 06:57:48 +00:00
|
|
|
if (
|
|
|
|
_is_complete_discovery(discovery)
|
2022-06-17 16:26:45 +00:00
|
|
|
and _is_igd_device(discovery)
|
2021-12-03 07:32:42 +00:00
|
|
|
and discovery.ssdp_usn not in current_unique_ids
|
2021-10-19 06:57:48 +00:00
|
|
|
)
|
2020-05-03 01:03:54 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# Ensure anything to add.
|
|
|
|
if not self._discoveries:
|
|
|
|
return self.async_abort(reason="no_devices_found")
|
|
|
|
|
|
|
|
data_schema = vol.Schema(
|
|
|
|
{
|
2021-01-29 09:23:34 +00:00
|
|
|
vol.Required("unique_id"): vol.In(
|
2020-05-03 01:03:54 +00:00
|
|
|
{
|
2021-12-03 07:32:42 +00:00
|
|
|
discovery.ssdp_usn: _friendly_name_from_discovery(discovery)
|
2020-05-03 01:03:54 +00:00
|
|
|
for discovery in self._discoveries
|
|
|
|
}
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2020-08-27 11:56:20 +00:00
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=data_schema,
|
|
|
|
)
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2021-12-01 19:23:38 +00:00
|
|
|
async def async_step_ssdp(self, discovery_info: ssdp.SsdpServiceInfo) -> FlowResult:
|
2020-05-03 01:03:54 +00:00
|
|
|
"""Handle a discovered UPnP/IGD device.
|
|
|
|
|
|
|
|
This flow is triggered by the SSDP component. It will check if the
|
|
|
|
host is already configured and delegate to the import step if not.
|
|
|
|
"""
|
2021-08-17 18:23:41 +00:00
|
|
|
LOGGER.debug("async_step_ssdp: discovery_info: %s", discovery_info)
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2020-05-14 20:58:41 +00:00
|
|
|
# Ensure complete discovery.
|
2021-10-19 06:57:48 +00:00
|
|
|
if not _is_complete_discovery(discovery_info):
|
2021-08-17 18:23:41 +00:00
|
|
|
LOGGER.debug("Incomplete discovery, ignoring")
|
2020-05-14 20:58:41 +00:00
|
|
|
return self.async_abort(reason="incomplete_discovery")
|
|
|
|
|
2022-06-17 16:26:45 +00:00
|
|
|
# Ensure device is usable. Ideally we would use IgdDevice.is_profile_device,
|
|
|
|
# but that requires constructing the device completely.
|
|
|
|
if not _is_igd_device(discovery_info):
|
|
|
|
LOGGER.debug("Non IGD device, ignoring")
|
|
|
|
return self.async_abort(reason="non_igd_device")
|
|
|
|
|
2020-05-03 01:03:54 +00:00
|
|
|
# Ensure not already configuring/configured.
|
2021-12-01 19:23:38 +00:00
|
|
|
unique_id = discovery_info.ssdp_usn
|
2021-01-29 09:23:34 +00:00
|
|
|
await self.async_set_unique_id(unique_id)
|
2022-04-20 21:01:43 +00:00
|
|
|
mac_address = await _async_mac_address_from_discovery(self.hass, discovery_info)
|
2022-02-27 18:29:29 +00:00
|
|
|
self._abort_if_unique_id_configured(
|
2022-04-20 21:01:43 +00:00
|
|
|
# Store mac address for older entries.
|
|
|
|
# The location is stored in the config entry such that when the location changes, the entry is reloaded.
|
|
|
|
updates={
|
|
|
|
CONFIG_ENTRY_MAC_ADDRESS: mac_address,
|
|
|
|
CONFIG_ENTRY_LOCATION: discovery_info.ssdp_location,
|
|
|
|
},
|
2022-02-27 18:29:29 +00:00
|
|
|
)
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2021-08-13 16:13:25 +00:00
|
|
|
# Handle devices changing their UDN, only allow a single host.
|
2022-04-20 21:01:43 +00:00
|
|
|
for entry in self._async_current_entries(include_ignore=True):
|
|
|
|
entry_mac_address = entry.data.get(CONFIG_ENTRY_MAC_ADDRESS)
|
|
|
|
entry_st = entry.data.get(CONFIG_ENTRY_ST)
|
|
|
|
if entry_mac_address != mac_address:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if discovery_info.ssdp_st != entry_st:
|
|
|
|
# Check ssdp_st to prevent swapping between IGDv1 and IGDv2.
|
|
|
|
continue
|
|
|
|
|
|
|
|
if entry.source == config_entries.SOURCE_IGNORE:
|
|
|
|
# Host was already ignored. Don't update ignored entries.
|
2021-02-21 02:26:17 +00:00
|
|
|
return self.async_abort(reason="discovery_ignored")
|
|
|
|
|
2022-04-20 21:01:43 +00:00
|
|
|
LOGGER.debug("Updating entry: %s", entry.entry_id)
|
|
|
|
self.hass.config_entries.async_update_entry(
|
|
|
|
entry,
|
|
|
|
unique_id=unique_id,
|
|
|
|
data={**entry.data, CONFIG_ENTRY_UDN: discovery_info.ssdp_udn},
|
|
|
|
)
|
|
|
|
if entry.state == config_entries.ConfigEntryState.LOADED:
|
|
|
|
# Only reload when entry has state LOADED; when entry has state SETUP_RETRY,
|
|
|
|
# another load is started, causing the entry to be loaded twice.
|
|
|
|
LOGGER.debug("Reloading entry: %s", entry.entry_id)
|
|
|
|
self.hass.async_create_task(
|
|
|
|
self.hass.config_entries.async_reload(entry.entry_id)
|
|
|
|
)
|
|
|
|
return self.async_abort(reason="config_entry_updated")
|
|
|
|
|
2020-05-03 01:03:54 +00:00
|
|
|
# Store discovery.
|
2021-08-13 16:13:25 +00:00
|
|
|
self._discoveries = [discovery_info]
|
2020-05-03 01:03:54 +00:00
|
|
|
|
|
|
|
# Ensure user recognizable.
|
|
|
|
self.context["title_placeholders"] = {
|
2021-08-13 16:13:25 +00:00
|
|
|
"name": _friendly_name_from_discovery(discovery_info),
|
2020-05-03 01:03:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return await self.async_step_ssdp_confirm()
|
|
|
|
|
2021-01-29 09:23:34 +00:00
|
|
|
async def async_step_ssdp_confirm(
|
2022-05-30 15:07:18 +00:00
|
|
|
self, user_input: Mapping[str, Any] | None = None
|
2022-03-08 06:51:23 +00:00
|
|
|
) -> FlowResult:
|
2020-05-03 01:03:54 +00:00
|
|
|
"""Confirm integration via SSDP."""
|
2021-08-17 18:23:41 +00:00
|
|
|
LOGGER.debug("async_step_ssdp_confirm: user_input: %s", user_input)
|
2020-05-03 01:03:54 +00:00
|
|
|
if user_input is None:
|
|
|
|
return self.async_show_form(step_id="ssdp_confirm")
|
|
|
|
|
2022-03-08 06:51:23 +00:00
|
|
|
assert self._discoveries
|
2020-05-03 01:03:54 +00:00
|
|
|
discovery = self._discoveries[0]
|
2020-05-11 18:03:12 +00:00
|
|
|
return await self._async_create_entry_from_discovery(discovery)
|
|
|
|
|
2020-05-10 02:52:08 +00:00
|
|
|
async def _async_create_entry_from_discovery(
|
2020-08-27 11:56:20 +00:00
|
|
|
self,
|
2021-12-03 07:32:42 +00:00
|
|
|
discovery: SsdpServiceInfo,
|
2022-03-08 06:51:23 +00:00
|
|
|
) -> FlowResult:
|
2020-05-10 02:52:08 +00:00
|
|
|
"""Create an entry from discovery."""
|
2021-08-17 18:23:41 +00:00
|
|
|
LOGGER.debug(
|
2020-10-06 11:57:36 +00:00
|
|
|
"_async_create_entry_from_discovery: discovery: %s",
|
2020-08-27 11:56:20 +00:00
|
|
|
discovery,
|
2020-05-10 02:52:08 +00:00
|
|
|
)
|
2020-05-03 01:03:54 +00:00
|
|
|
|
2021-08-13 16:13:25 +00:00
|
|
|
title = _friendly_name_from_discovery(discovery)
|
2022-04-20 21:01:43 +00:00
|
|
|
mac_address = await _async_mac_address_from_discovery(self.hass, discovery)
|
2020-05-03 01:03:54 +00:00
|
|
|
data = {
|
2021-12-03 07:32:42 +00:00
|
|
|
CONFIG_ENTRY_UDN: discovery.upnp[ssdp.ATTR_UPNP_UDN],
|
|
|
|
CONFIG_ENTRY_ST: discovery.ssdp_st,
|
2022-04-20 21:01:43 +00:00
|
|
|
CONFIG_ENTRY_ORIGINAL_UDN: discovery.upnp[ssdp.ATTR_UPNP_UDN],
|
|
|
|
CONFIG_ENTRY_LOCATION: discovery.ssdp_location,
|
|
|
|
CONFIG_ENTRY_MAC_ADDRESS: mac_address,
|
2020-05-03 01:03:54 +00:00
|
|
|
}
|
|
|
|
return self.async_create_entry(title=title, data=data)
|