Add zeroconf ATTR constants (#58671)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-10-31 16:13:26 +01:00 committed by GitHub
parent 13386fc41b
commit 2ae86124c7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 19 deletions

View File

@ -11,11 +11,12 @@ import async_timeout
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries, core from homeassistant import config_entries, core
from homeassistant.components import ssdp from homeassistant.components import ssdp, zeroconf
from homeassistant.const import CONF_HOST, CONF_USERNAME from homeassistant.const import CONF_HOST, CONF_USERNAME
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import aiohttp_client from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.typing import DiscoveryInfoType
from .bridge import authenticate_bridge from .bridge import authenticate_bridge
from .const import ( from .const import (
@ -207,14 +208,17 @@ class HueFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
self.bridge = bridge self.bridge = bridge
return await self.async_step_link() return await self.async_step_link()
async def async_step_zeroconf(self, discovery_info): async def async_step_zeroconf(
self, discovery_info: DiscoveryInfoType
) -> FlowResult:
"""Handle a discovered Hue bridge. """Handle a discovered Hue bridge.
This flow is triggered by the Zeroconf component. It will check if the This flow is triggered by the Zeroconf component. It will check if the
host is already configured and delegate to the import step if not. host is already configured and delegate to the import step if not.
""" """
bridge = self._async_get_bridge( bridge = self._async_get_bridge(
discovery_info["host"], discovery_info["properties"]["bridgeid"] discovery_info[zeroconf.ATTR_HOST],
discovery_info[zeroconf.ATTR_PROPERTIES]["bridgeid"],
) )
await self.async_set_unique_id(bridge.id) await self.async_set_unique_id(bridge.id)

View File

@ -8,7 +8,7 @@ from ipaddress import IPv4Address, IPv6Address, ip_address
import logging import logging
import socket import socket
import sys import sys
from typing import Any, TypedDict, cast from typing import Any, Final, TypedDict, cast
import voluptuous as vol import voluptuous as vol
from zeroconf import InterfaceChoice, IPVersion, ServiceStateChange from zeroconf import InterfaceChoice, IPVersion, ServiceStateChange
@ -61,6 +61,12 @@ MAX_PROPERTY_VALUE_LEN = 230
# Dns label max length # Dns label max length
MAX_NAME_LEN = 63 MAX_NAME_LEN = 63
# Attributes for HaServiceInfo
ATTR_HOST: Final = "host"
ATTR_NAME: Final = "name"
ATTR_PROPERTIES: Final = "properties"
CONFIG_SCHEMA = vol.Schema( CONFIG_SCHEMA = vol.Schema(
{ {
DOMAIN: vol.All( DOMAIN: vol.All(
@ -349,7 +355,7 @@ class ZeroconfDiscovery:
# If we can handle it as a HomeKit discovery, we do that here. # If we can handle it as a HomeKit discovery, we do that here.
if service_type in HOMEKIT_TYPES: if service_type in HOMEKIT_TYPES:
props = info["properties"] props = info[ATTR_PROPERTIES]
if domain := async_get_homekit_discovery_domain(self.homekit_models, props): if domain := async_get_homekit_discovery_domain(self.homekit_models, props):
discovery_flow.async_create_flow( discovery_flow.async_create_flow(
self.hass, domain, {"source": config_entries.SOURCE_HOMEKIT}, info self.hass, domain, {"source": config_entries.SOURCE_HOMEKIT}, info
@ -371,18 +377,18 @@ class ZeroconfDiscovery:
# likely bad homekit data # likely bad homekit data
return return
if "name" in info: if ATTR_NAME in info:
lowercase_name: str | None = info["name"].lower() lowercase_name: str | None = info[ATTR_NAME].lower()
else: else:
lowercase_name = None lowercase_name = None
if "macaddress" in info["properties"]: if "macaddress" in info[ATTR_PROPERTIES]:
uppercase_mac: str | None = info["properties"]["macaddress"].upper() uppercase_mac: str | None = info[ATTR_PROPERTIES]["macaddress"].upper()
else: else:
uppercase_mac = None uppercase_mac = None
if "manufacturer" in info["properties"]: if "manufacturer" in info[ATTR_PROPERTIES]:
lowercase_manufacturer: str | None = info["properties"][ lowercase_manufacturer: str | None = info[ATTR_PROPERTIES][
"manufacturer" "manufacturer"
].lower() ].lower()
else: else:
@ -474,14 +480,14 @@ def info_from_service(service: AsyncServiceInfo) -> HaServiceInfo | None:
if (host := _first_non_link_local_or_v6_address(addresses)) is None: if (host := _first_non_link_local_or_v6_address(addresses)) is None:
return None return None
return { return HaServiceInfo(
"host": str(host), host=str(host),
"port": service.port, port=service.port,
"hostname": service.server, hostname=service.server,
"type": service.type, type=service.type,
"name": service.name, name=service.name,
"properties": properties, properties=properties,
} )
def _first_non_link_local_or_v6_address(addresses: list[bytes]) -> str | None: def _first_non_link_local_or_v6_address(addresses: list[bytes]) -> str | None: