Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions homeassistant/components/hue/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import voluptuous as vol

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.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.typing import DiscoveryInfoType

from .bridge import authenticate_bridge
from .const import (
Expand Down Expand Up @@ -207,14 +208,17 @@ async def async_step_ssdp(self, discovery_info):
self.bridge = bridge
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.

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.
"""
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)
Expand Down
38 changes: 22 additions & 16 deletions homeassistant/components/zeroconf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
import socket
import sys
from typing import Any, TypedDict, cast
from typing import Any, Final, TypedDict, cast

import voluptuous as vol
from zeroconf import InterfaceChoice, IPVersion, ServiceStateChange
Expand Down Expand Up @@ -61,6 +61,12 @@
# Dns label max length
MAX_NAME_LEN = 63

# Attributes for HaServiceInfo
ATTR_HOST: Final = "host"
ATTR_NAME: Final = "name"
ATTR_PROPERTIES: Final = "properties"


CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.All(
Expand Down Expand Up @@ -349,7 +355,7 @@ async def _process_service_update(

# If we can handle it as a HomeKit discovery, we do that here.
if service_type in HOMEKIT_TYPES:
props = info["properties"]
props = info[ATTR_PROPERTIES]
if domain := async_get_homekit_discovery_domain(self.homekit_models, props):
discovery_flow.async_create_flow(
self.hass, domain, {"source": config_entries.SOURCE_HOMEKIT}, info
Expand All @@ -371,18 +377,18 @@ async def _process_service_update(
# likely bad homekit data
return

if "name" in info:
lowercase_name: str | None = info["name"].lower()
if ATTR_NAME in info:
lowercase_name: str | None = info[ATTR_NAME].lower()
else:
lowercase_name = None

if "macaddress" in info["properties"]:
uppercase_mac: str | None = info["properties"]["macaddress"].upper()
if "macaddress" in info[ATTR_PROPERTIES]:
uppercase_mac: str | None = info[ATTR_PROPERTIES]["macaddress"].upper()
else:
uppercase_mac = None

if "manufacturer" in info["properties"]:
lowercase_manufacturer: str | None = info["properties"][
if "manufacturer" in info[ATTR_PROPERTIES]:
lowercase_manufacturer: str | None = info[ATTR_PROPERTIES][
"manufacturer"
].lower()
else:
Expand Down Expand Up @@ -476,14 +482,14 @@ def info_from_service(service: AsyncServiceInfo) -> HaServiceInfo | None:
if (host := _first_non_link_local_or_v6_address(addresses)) is None:
return None

return {
"host": str(host),
"port": service.port,
"hostname": service.server,
"type": service.type,
"name": service.name,
"properties": properties,
}
return HaServiceInfo(
host=str(host),
port=service.port,
hostname=service.server,
type=service.type,
name=service.name,
properties=properties,
)


def _first_non_link_local_or_v6_address(addresses: list[bytes]) -> str | None:
Expand Down