Use dataclass properties in upnp (#60893)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
e1b4e40ac6
commit
4207d5a85f
2 changed files with 21 additions and 28 deletions
|
@ -15,7 +15,6 @@ from homeassistant import config_entries
|
||||||
from homeassistant.components import ssdp
|
from homeassistant.components import ssdp
|
||||||
from homeassistant.components.binary_sensor import BinarySensorEntityDescription
|
from homeassistant.components.binary_sensor import BinarySensorEntityDescription
|
||||||
from homeassistant.components.sensor import SensorEntityDescription
|
from homeassistant.components.sensor import SensorEntityDescription
|
||||||
from homeassistant.components.ssdp import SsdpChange
|
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.exceptions import ConfigEntryNotReady
|
from homeassistant.exceptions import ConfigEntryNotReady
|
||||||
|
@ -91,16 +90,16 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
|
|
||||||
# Register device discovered-callback.
|
# Register device discovered-callback.
|
||||||
device_discovered_event = asyncio.Event()
|
device_discovered_event = asyncio.Event()
|
||||||
discovery_info: Mapping[str, Any] | None = None
|
discovery_info: ssdp.SsdpServiceInfo | None = None
|
||||||
|
|
||||||
async def device_discovered(headers: Mapping[str, Any], change: SsdpChange) -> None:
|
async def device_discovered(
|
||||||
if change == SsdpChange.BYEBYE:
|
headers: ssdp.SsdpServiceInfo, change: ssdp.SsdpChange
|
||||||
|
) -> None:
|
||||||
|
if change == ssdp.SsdpChange.BYEBYE:
|
||||||
return
|
return
|
||||||
|
|
||||||
nonlocal discovery_info
|
nonlocal discovery_info
|
||||||
LOGGER.debug(
|
LOGGER.debug("Device discovered: %s, at: %s", usn, headers.ssdp_location)
|
||||||
"Device discovered: %s, at: %s", usn, headers[ssdp.ATTR_SSDP_LOCATION]
|
|
||||||
)
|
|
||||||
discovery_info = headers
|
discovery_info = headers
|
||||||
device_discovered_event.set()
|
device_discovered_event.set()
|
||||||
|
|
||||||
|
@ -121,9 +120,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
cancel_discovered_callback()
|
cancel_discovered_callback()
|
||||||
|
|
||||||
# Create device.
|
# Create device.
|
||||||
location = discovery_info[ # pylint: disable=unsubscriptable-object
|
location = discovery_info.ssdp_location
|
||||||
ssdp.ATTR_SSDP_LOCATION
|
|
||||||
]
|
|
||||||
try:
|
try:
|
||||||
device = await Device.async_create_device(hass, location)
|
device = await Device.async_create_device(hass, location)
|
||||||
except UpnpConnectionError as err:
|
except UpnpConnectionError as err:
|
||||||
|
|
|
@ -10,7 +10,7 @@ import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components import ssdp
|
from homeassistant.components import ssdp
|
||||||
from homeassistant.components.ssdp import SsdpChange
|
from homeassistant.components.ssdp import SsdpChange, SsdpServiceInfo
|
||||||
from homeassistant.const import CONF_SCAN_INTERVAL
|
from homeassistant.const import CONF_SCAN_INTERVAL
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.data_entry_flow import FlowResult
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
|
@ -52,14 +52,14 @@ async def _async_wait_for_discoveries(hass: HomeAssistant) -> bool:
|
||||||
"""Wait for a device to be discovered."""
|
"""Wait for a device to be discovered."""
|
||||||
device_discovered_event = asyncio.Event()
|
device_discovered_event = asyncio.Event()
|
||||||
|
|
||||||
async def device_discovered(info: Mapping[str, Any], change: SsdpChange) -> None:
|
async def device_discovered(info: SsdpServiceInfo, change: SsdpChange) -> None:
|
||||||
if change == SsdpChange.BYEBYE:
|
if change == SsdpChange.BYEBYE:
|
||||||
return
|
return
|
||||||
|
|
||||||
LOGGER.info(
|
LOGGER.info(
|
||||||
"Device discovered: %s, at: %s",
|
"Device discovered: %s, at: %s",
|
||||||
info[ssdp.ATTR_SSDP_USN],
|
info.ssdp_usn,
|
||||||
info[ssdp.ATTR_SSDP_LOCATION],
|
info.ssdp_location,
|
||||||
)
|
)
|
||||||
device_discovered_event.set()
|
device_discovered_event.set()
|
||||||
|
|
||||||
|
@ -112,7 +112,7 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
"""Initialize the UPnP/IGD config flow."""
|
"""Initialize the UPnP/IGD config flow."""
|
||||||
self._discoveries: Mapping = None
|
self._discoveries: list[SsdpServiceInfo] | None = None
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
self, user_input: Mapping | None = None
|
self, user_input: Mapping | None = None
|
||||||
|
@ -125,15 +125,13 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
matching_discoveries = [
|
matching_discoveries = [
|
||||||
discovery
|
discovery
|
||||||
for discovery in self._discoveries
|
for discovery in self._discoveries
|
||||||
if discovery[ssdp.ATTR_SSDP_USN] == user_input["unique_id"]
|
if discovery.ssdp_usn == user_input["unique_id"]
|
||||||
]
|
]
|
||||||
if not matching_discoveries:
|
if not matching_discoveries:
|
||||||
return self.async_abort(reason="no_devices_found")
|
return self.async_abort(reason="no_devices_found")
|
||||||
|
|
||||||
discovery = matching_discoveries[0]
|
discovery = matching_discoveries[0]
|
||||||
await self.async_set_unique_id(
|
await self.async_set_unique_id(discovery.ssdp_usn, raise_on_progress=False)
|
||||||
discovery[ssdp.ATTR_SSDP_USN], raise_on_progress=False
|
|
||||||
)
|
|
||||||
return await self._async_create_entry_from_discovery(discovery)
|
return await self._async_create_entry_from_discovery(discovery)
|
||||||
|
|
||||||
# Discover devices.
|
# Discover devices.
|
||||||
|
@ -148,7 +146,7 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
for discovery in discoveries
|
for discovery in discoveries
|
||||||
if (
|
if (
|
||||||
_is_complete_discovery(discovery)
|
_is_complete_discovery(discovery)
|
||||||
and discovery[ssdp.ATTR_SSDP_USN] not in current_unique_ids
|
and discovery.ssdp_usn not in current_unique_ids
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -160,9 +158,7 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
{
|
{
|
||||||
vol.Required("unique_id"): vol.In(
|
vol.Required("unique_id"): vol.In(
|
||||||
{
|
{
|
||||||
discovery[ssdp.ATTR_SSDP_USN]: _friendly_name_from_discovery(
|
discovery.ssdp_usn: _friendly_name_from_discovery(discovery)
|
||||||
discovery
|
|
||||||
)
|
|
||||||
for discovery in self._discoveries
|
for discovery in self._discoveries
|
||||||
}
|
}
|
||||||
),
|
),
|
||||||
|
@ -204,7 +200,7 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
return self.async_abort(reason="incomplete_discovery")
|
return self.async_abort(reason="incomplete_discovery")
|
||||||
|
|
||||||
# Ensure not already configuring/configured.
|
# Ensure not already configuring/configured.
|
||||||
unique_id = discovery[ssdp.ATTR_SSDP_USN]
|
unique_id = discovery.ssdp_usn
|
||||||
await self.async_set_unique_id(unique_id)
|
await self.async_set_unique_id(unique_id)
|
||||||
|
|
||||||
return await self._async_create_entry_from_discovery(discovery)
|
return await self._async_create_entry_from_discovery(discovery)
|
||||||
|
@ -269,7 +265,7 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
async def _async_create_entry_from_discovery(
|
async def _async_create_entry_from_discovery(
|
||||||
self,
|
self,
|
||||||
discovery: Mapping,
|
discovery: SsdpServiceInfo,
|
||||||
) -> Mapping[str, Any]:
|
) -> Mapping[str, Any]:
|
||||||
"""Create an entry from discovery."""
|
"""Create an entry from discovery."""
|
||||||
LOGGER.debug(
|
LOGGER.debug(
|
||||||
|
@ -279,9 +275,9 @@ class UpnpFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
||||||
|
|
||||||
title = _friendly_name_from_discovery(discovery)
|
title = _friendly_name_from_discovery(discovery)
|
||||||
data = {
|
data = {
|
||||||
CONFIG_ENTRY_UDN: discovery[ssdp.ATTR_UPNP_UDN],
|
CONFIG_ENTRY_UDN: discovery.upnp[ssdp.ATTR_UPNP_UDN],
|
||||||
CONFIG_ENTRY_ST: discovery[ssdp.ATTR_SSDP_ST],
|
CONFIG_ENTRY_ST: discovery.ssdp_st,
|
||||||
CONFIG_ENTRY_HOSTNAME: discovery["_host"],
|
CONFIG_ENTRY_HOSTNAME: discovery.ssdp_headers["_host"],
|
||||||
}
|
}
|
||||||
return self.async_create_entry(title=title, data=data)
|
return self.async_create_entry(title=title, data=data)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue