Skip to content

Commit

Permalink
Use new ServiceInfo location in components (part 3) (#135687)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Jan 15, 2025
1 parent 19a89eb commit 406c009
Show file tree
Hide file tree
Showing 31 changed files with 143 additions and 100 deletions.
23 changes: 14 additions & 9 deletions homeassistant/components/samsungtv/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from samsungtvws.encrypted.authenticator import SamsungTVEncryptedWSAsyncAuthenticator
import voluptuous as vol

from homeassistant.components import dhcp, ssdp, zeroconf
from homeassistant.config_entries import (
ConfigEntry,
ConfigEntryState,
Expand All @@ -32,6 +31,14 @@
from homeassistant.data_entry_flow import AbortFlow
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from homeassistant.helpers.service_info.ssdp import (
ATTR_UPNP_MANUFACTURER,
ATTR_UPNP_MODEL_NAME,
ATTR_UPNP_UDN,
SsdpServiceInfo,
)
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo

from .bridge import SamsungTVBridge, async_get_device_info, mac_from_device_info
from .const import (
Expand Down Expand Up @@ -439,11 +446,11 @@ def _abort_if_manufacturer_is_not_samsung(self) -> None:
raise AbortFlow(RESULT_NOT_SUPPORTED)

async def async_step_ssdp(
self, discovery_info: ssdp.SsdpServiceInfo
self, discovery_info: SsdpServiceInfo
) -> ConfigFlowResult:
"""Handle a flow initialized by ssdp discovery."""
LOGGER.debug("Samsung device found via SSDP: %s", discovery_info)
model_name: str = discovery_info.upnp.get(ssdp.ATTR_UPNP_MODEL_NAME) or ""
model_name: str = discovery_info.upnp.get(ATTR_UPNP_MODEL_NAME) or ""
if discovery_info.ssdp_st == UPNP_SVC_RENDERING_CONTROL:
self._ssdp_rendering_control_location = discovery_info.ssdp_location
LOGGER.debug(
Expand All @@ -456,12 +463,10 @@ async def async_step_ssdp(
"Set SSDP MainTvAgent location to: %s",
self._ssdp_main_tv_agent_location,
)
self._udn = self._upnp_udn = _strip_uuid(
discovery_info.upnp[ssdp.ATTR_UPNP_UDN]
)
self._udn = self._upnp_udn = _strip_uuid(discovery_info.upnp[ATTR_UPNP_UDN])
if hostname := urlparse(discovery_info.ssdp_location or "").hostname:
self._host = hostname
self._manufacturer = discovery_info.upnp.get(ssdp.ATTR_UPNP_MANUFACTURER)
self._manufacturer = discovery_info.upnp.get(ATTR_UPNP_MANUFACTURER)
self._abort_if_manufacturer_is_not_samsung()

# Set defaults, in case they cannot be extracted from device_info
Expand All @@ -486,7 +491,7 @@ async def async_step_ssdp(
return await self.async_step_confirm()

async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
self, discovery_info: DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle a flow initialized by dhcp discovery."""
LOGGER.debug("Samsung device found via DHCP: %s", discovery_info)
Expand All @@ -498,7 +503,7 @@ async def async_step_dhcp(
return await self.async_step_confirm()

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
self, discovery_info: ZeroconfServiceInfo
) -> ConfigFlowResult:
"""Handle a flow initialized by zeroconf discovery."""
LOGGER.debug("Samsung device found via ZEROCONF: %s", discovery_info)
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/screenlogic/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from screenlogicpy.requests import login
import voluptuous as vol

from homeassistant.components import dhcp
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
Expand All @@ -21,6 +20,7 @@
from homeassistant.core import callback
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo

from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, MIN_SCAN_INTERVAL

Expand Down Expand Up @@ -91,7 +91,7 @@ async def async_step_user(
return await self.async_step_gateway_select()

async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
self, discovery_info: DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle dhcp discovery."""
mac = format_mac(discovery_info.macaddress)
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/smappee/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
from pysmappee import helper, mqtt
import voluptuous as vol

from homeassistant.components import zeroconf
from homeassistant.config_entries import ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_IP_ADDRESS
from homeassistant.helpers import config_entry_oauth2_flow
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo

from . import api
from .const import (
Expand Down Expand Up @@ -43,7 +43,7 @@ def logger(self) -> logging.Logger:
return logging.getLogger(__name__)

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
self, discovery_info: ZeroconfServiceInfo
) -> ConfigFlowResult:
"""Handle zeroconf discovery."""

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/smlight/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError
import voluptuous as vol

from homeassistant.components import zeroconf
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo

from .const import DOMAIN

Expand Down Expand Up @@ -82,7 +82,7 @@ async def async_step_auth(
)

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
self, discovery_info: ZeroconfServiceInfo
) -> ConfigFlowResult:
"""Handle a discovered Lan coordinator."""
local_name = discovery_info.hostname[:-1]
Expand Down
12 changes: 8 additions & 4 deletions homeassistant/components/songpal/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
from songpal import Device, SongpalException
import voluptuous as vol

from homeassistant.components import ssdp
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.helpers.service_info.ssdp import (
ATTR_UPNP_FRIENDLY_NAME,
ATTR_UPNP_UDN,
SsdpServiceInfo,
)

from .const import CONF_ENDPOINT, DOMAIN

Expand Down Expand Up @@ -99,15 +103,15 @@ async def async_step_init(
)

async def async_step_ssdp(
self, discovery_info: ssdp.SsdpServiceInfo
self, discovery_info: SsdpServiceInfo
) -> ConfigFlowResult:
"""Handle a discovered Songpal device."""
await self.async_set_unique_id(discovery_info.upnp[ssdp.ATTR_UPNP_UDN])
await self.async_set_unique_id(discovery_info.upnp[ATTR_UPNP_UDN])
self._abort_if_unique_id_configured()

_LOGGER.debug("Discovered: %s", discovery_info)

friendly_name = discovery_info.upnp[ssdp.ATTR_UPNP_FRIENDLY_NAME]
friendly_name = discovery_info.upnp[ATTR_UPNP_FRIENDLY_NAME]
hostname = urlparse(discovery_info.ssdp_location).hostname
scalarweb_info = discovery_info.upnp["X_ScalarWebAPI_DeviceInfo"]
endpoint = scalarweb_info["X_ScalarWebAPI_BaseURL"]
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/squeezebox/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
from pysqueezebox import Server, async_discover
import voluptuous as vol

from homeassistant.components import dhcp
from homeassistant.components.media_player import DOMAIN as MP_DOMAIN
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT, CONF_USERNAME
from homeassistant.data_entry_flow import AbortFlow
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo

from .const import CONF_HTTPS, DEFAULT_PORT, DOMAIN

Expand Down Expand Up @@ -200,7 +200,7 @@ async def async_step_integration_discovery(
return await self.async_step_edit()

async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
self, discovery_info: DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle dhcp discovery of a Squeezebox player."""
_LOGGER.debug(
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/steamist/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from discovery30303 import Device30303, normalize_mac
import voluptuous as vol

from homeassistant.components import dhcp
from homeassistant.config_entries import ConfigEntryState, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_DEVICE, CONF_HOST, CONF_MODEL, CONF_NAME
from homeassistant.core import callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
from homeassistant.helpers.typing import DiscoveryInfoType

from .const import CONNECTION_EXCEPTIONS, DISCOVER_SCAN_TIMEOUT, DOMAIN
Expand All @@ -41,7 +41,7 @@ def __init__(self) -> None:
self._discovered_device: Device30303 | None = None

async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
self, discovery_info: DhcpServiceInfo
) -> ConfigFlowResult:
"""Handle discovery via dhcp."""
self._discovered_device = Device30303(
Expand Down
17 changes: 11 additions & 6 deletions homeassistant/components/syncthru/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@
from url_normalize import url_normalize
import voluptuous as vol

from homeassistant.components import ssdp
from homeassistant.config_entries import ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_NAME, CONF_URL
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.service_info.ssdp import (
ATTR_UPNP_FRIENDLY_NAME,
ATTR_UPNP_PRESENTATION_URL,
ATTR_UPNP_UDN,
SsdpServiceInfo,
)

from .const import DEFAULT_MODEL, DEFAULT_NAME_TEMPLATE, DOMAIN

Expand All @@ -33,15 +38,15 @@ async def async_step_user(
return await self._async_check_and_create("user", user_input)

async def async_step_ssdp(
self, discovery_info: ssdp.SsdpServiceInfo
self, discovery_info: SsdpServiceInfo
) -> ConfigFlowResult:
"""Handle SSDP initiated flow."""
await self.async_set_unique_id(discovery_info.upnp[ssdp.ATTR_UPNP_UDN])
await self.async_set_unique_id(discovery_info.upnp[ATTR_UPNP_UDN])
self._abort_if_unique_id_configured()

self.url = url_normalize(
discovery_info.upnp.get(
ssdp.ATTR_UPNP_PRESENTATION_URL,
ATTR_UPNP_PRESENTATION_URL,
f"http://{urlparse(discovery_info.ssdp_location or '').hostname}/",
)
)
Expand All @@ -52,11 +57,11 @@ async def async_step_ssdp(
# Update unique id of entry with the same URL
if not existing_entry.unique_id:
self.hass.config_entries.async_update_entry(
existing_entry, unique_id=discovery_info.upnp[ssdp.ATTR_UPNP_UDN]
existing_entry, unique_id=discovery_info.upnp[ATTR_UPNP_UDN]
)
return self.async_abort(reason="already_configured")

self.name = discovery_info.upnp.get(ssdp.ATTR_UPNP_FRIENDLY_NAME, "")
self.name = discovery_info.upnp.get(ATTR_UPNP_FRIENDLY_NAME, "")
if self.name:
# Remove trailing " (ip)" if present for consistency with user driven config
self.name = re.sub(r"\s+\([\d.]+\)\s*$", "", self.name)
Expand Down
15 changes: 10 additions & 5 deletions homeassistant/components/synology_dsm/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
)
import voluptuous as vol

from homeassistant.components import ssdp, zeroconf
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
Expand All @@ -41,6 +40,12 @@
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.aiohttp_client import async_get_clientsession
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.service_info.ssdp import (
ATTR_UPNP_FRIENDLY_NAME,
ATTR_UPNP_SERIAL,
SsdpServiceInfo,
)
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
from homeassistant.helpers.typing import DiscoveryInfoType, VolDictType
from homeassistant.util.network import is_ip_address as is_ip

Expand Down Expand Up @@ -243,7 +248,7 @@ async def async_step_user(
return await self.async_validate_input_create_entry(user_input, step_id=step)

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
self, discovery_info: ZeroconfServiceInfo
) -> ConfigFlowResult:
"""Handle a discovered synology_dsm via zeroconf."""
discovered_macs = [
Expand All @@ -258,13 +263,13 @@ async def async_step_zeroconf(
return await self._async_from_discovery(host, friendly_name, discovered_macs)

async def async_step_ssdp(
self, discovery_info: ssdp.SsdpServiceInfo
self, discovery_info: SsdpServiceInfo
) -> ConfigFlowResult:
"""Handle a discovered synology_dsm via ssdp."""
parsed_url = urlparse(discovery_info.ssdp_location)
upnp_friendly_name: str = discovery_info.upnp[ssdp.ATTR_UPNP_FRIENDLY_NAME]
upnp_friendly_name: str = discovery_info.upnp[ATTR_UPNP_FRIENDLY_NAME]
friendly_name = upnp_friendly_name.split("(", 1)[0].strip()
mac_address = discovery_info.upnp[ssdp.ATTR_UPNP_SERIAL]
mac_address = discovery_info.upnp[ATTR_UPNP_SERIAL]
discovered_macs = [format_synology_mac(mac_address)]
# Synology NAS can broadcast on multiple IP addresses, since they can be connected to multiple ethernets.
# The serial of the NAS is actually its MAC address.
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/system_bridge/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
from systembridgemodels.modules import GetData, Module
import voluptuous as vol

from homeassistant.components import zeroconf
from homeassistant.config_entries import SOURCE_REAUTH, ConfigFlow, ConfigFlowResult
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_TOKEN
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo

from .const import DATA_WAIT_TIMEOUT, DOMAIN

Expand Down Expand Up @@ -179,7 +179,7 @@ async def async_step_authenticate(
)

async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
self, discovery_info: ZeroconfServiceInfo
) -> ConfigFlowResult:
"""Handle zeroconf discovery."""
properties = discovery_info.properties
Expand Down
9 changes: 6 additions & 3 deletions homeassistant/components/tado/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import requests.exceptions
import voluptuous as vol

from homeassistant.components import zeroconf
from homeassistant.config_entries import (
ConfigEntry,
ConfigFlow,
Expand All @@ -20,6 +19,10 @@
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.service_info.zeroconf import (
ATTR_PROPERTIES_ID,
ZeroconfServiceInfo,
)

from .const import (
CONF_FALLBACK,
Expand Down Expand Up @@ -104,14 +107,14 @@ async def async_step_user(
)

async def async_step_homekit(
self, discovery_info: zeroconf.ZeroconfServiceInfo
self, discovery_info: ZeroconfServiceInfo
) -> ConfigFlowResult:
"""Handle HomeKit discovery."""
self._async_abort_entries_match()
properties = {
key.lower(): value for (key, value) in discovery_info.properties.items()
}
await self.async_set_unique_id(properties[zeroconf.ATTR_PROPERTIES_ID])
await self.async_set_unique_id(properties[ATTR_PROPERTIES_ID])
self._abort_if_unique_id_configured()
return await self.async_step_user()

Expand Down
Loading

0 comments on commit 406c009

Please sign in to comment.