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
11 changes: 5 additions & 6 deletions homeassistant/components/axis/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS, MAC_ADDRESS
from homeassistant.components import dhcp, zeroconf
from homeassistant.config_entries import SOURCE_IGNORE
from homeassistant.const import (
CONF_HOST,
Expand Down Expand Up @@ -153,13 +152,13 @@ async def async_step_reauth(self, device_config: dict):

return await self.async_step_user()

async def async_step_dhcp(self, discovery_info: dict):
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Prepare configuration for a DHCP discovered Axis device."""
return await self._process_discovered_device(
{
CONF_HOST: discovery_info[IP_ADDRESS],
CONF_MAC: format_mac(discovery_info.get(MAC_ADDRESS, "")),
CONF_NAME: discovery_info.get(HOSTNAME),
CONF_HOST: discovery_info[dhcp.IP_ADDRESS],
CONF_MAC: format_mac(discovery_info[dhcp.MAC_ADDRESS]),
CONF_NAME: discovery_info[dhcp.HOSTNAME],
CONF_PORT: DEFAULT_PORT,
}
)
Expand Down
25 changes: 17 additions & 8 deletions homeassistant/components/dhcp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
import os
import threading
from typing import Final, TypedDict

from aiodiscover import DiscoverHosts
from aiodiscover.discovery import (
Expand Down Expand Up @@ -45,15 +46,23 @@
FILTER = "udp and (port 67 or 68)"
REQUESTED_ADDR = "requested_addr"
MESSAGE_TYPE = "message-type"
HOSTNAME = "hostname"
MAC_ADDRESS = "macaddress"
IP_ADDRESS = "ip"
HOSTNAME: Final = "hostname"
MAC_ADDRESS: Final = "macaddress"
IP_ADDRESS: Final = "ip"
DHCP_REQUEST = 3
SCAN_INTERVAL = timedelta(minutes=60)

_LOGGER = logging.getLogger(__name__)


class DhcpServiceInfo(TypedDict):
"""Prepared info from dhcp entries."""

ip: str
hostname: str
macaddress: str


async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up the dhcp component."""

Expand Down Expand Up @@ -150,11 +159,11 @@ def async_process_client(self, ip_address, hostname, mac_address):
self.hass,
entry["domain"],
{"source": config_entries.SOURCE_DHCP},
{
IP_ADDRESS: ip_address,
HOSTNAME: lowercase_hostname,
MAC_ADDRESS: data[MAC_ADDRESS],
},
DhcpServiceInfo(
ip=ip_address,
hostname=lowercase_hostname,
macaddress=data[MAC_ADDRESS],
),
)


Expand Down
10 changes: 5 additions & 5 deletions homeassistant/components/flux_led/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS, MAC_ADDRESS
from homeassistant.components import dhcp
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_MODE, CONF_NAME, CONF_PROTOCOL
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
Expand Down Expand Up @@ -82,12 +82,12 @@ async def async_step_import(self, user_input: dict[str, Any]) -> FlowResult:
},
)

async def async_step_dhcp(self, discovery_info: DiscoveryInfoType) -> FlowResult:
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle discovery via dhcp."""
self._discovered_device = {
FLUX_HOST: discovery_info[IP_ADDRESS],
FLUX_MODEL: discovery_info[HOSTNAME],
FLUX_MAC: discovery_info[MAC_ADDRESS].replace(":", ""),
FLUX_HOST: discovery_info[dhcp.IP_ADDRESS],
FLUX_MODEL: discovery_info[dhcp.HOSTNAME],
FLUX_MAC: discovery_info[dhcp.MAC_ADDRESS].replace(":", ""),
}
return await self._async_handle_discovery()

Expand Down
11 changes: 5 additions & 6 deletions homeassistant/components/goalzero/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components.dhcp import IP_ADDRESS, MAC_ADDRESS
from homeassistant.components import dhcp
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.device_registry import format_mac
from homeassistant.helpers.typing import DiscoveryInfoType

from .const import DEFAULT_NAME, DOMAIN

Expand All @@ -27,13 +26,13 @@ class GoalZeroFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):

def __init__(self) -> None:
"""Initialize a Goal Zero Yeti flow."""
self.ip_address = None
self.ip_address: str | None = None

async def async_step_dhcp(self, discovery_info: DiscoveryInfoType) -> FlowResult:
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle dhcp discovery."""
self.ip_address = discovery_info[IP_ADDRESS]
self.ip_address = discovery_info[dhcp.IP_ADDRESS]

await self.async_set_unique_id(discovery_info[MAC_ADDRESS])
await self.async_set_unique_id(discovery_info[dhcp.MAC_ADDRESS])
self._abort_if_unique_id_configured(updates={CONF_HOST: self.ip_address})
self._async_abort_entries_match({CONF_HOST: self.ip_address})

Expand Down
8 changes: 3 additions & 5 deletions homeassistant/components/guardian/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.components.dhcp import IP_ADDRESS
from homeassistant.components import dhcp, zeroconf
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT
from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.typing import DiscoveryInfoType

from .const import CONF_UID, DOMAIN, LOGGER

Expand Down Expand Up @@ -99,10 +97,10 @@ async def async_step_user(
title=info[CONF_UID], data={CONF_UID: info["uid"], **user_input}
)

async def async_step_dhcp(self, discovery_info: DiscoveryInfoType) -> FlowResult:
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle the configuration via dhcp."""
self.discovery_info = {
CONF_IP_ADDRESS: discovery_info[IP_ADDRESS],
CONF_IP_ADDRESS: discovery_info[dhcp.IP_ADDRESS],
CONF_PORT: DEFAULT_PORT,
}
return await self._async_handle_discovery()
Expand Down
9 changes: 4 additions & 5 deletions homeassistant/components/samsungtv/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import voluptuous as vol

from homeassistant import config_entries, data_entry_flow
from homeassistant.components import zeroconf
from homeassistant.components.dhcp import IP_ADDRESS, MAC_ADDRESS
from homeassistant.components import dhcp, zeroconf
from homeassistant.components.ssdp import (
ATTR_SSDP_LOCATION,
ATTR_UPNP_MANUFACTURER,
Expand Down Expand Up @@ -285,12 +284,12 @@ async def async_step_ssdp(
return await self.async_step_confirm()

async def async_step_dhcp(
self, discovery_info: DiscoveryInfoType
self, discovery_info: dhcp.DhcpServiceInfo
) -> data_entry_flow.FlowResult:
"""Handle a flow initialized by dhcp discovery."""
LOGGER.debug("Samsung device found via DHCP: %s", discovery_info)
self._mac = discovery_info[MAC_ADDRESS]
self._host = discovery_info[IP_ADDRESS]
self._mac = discovery_info[dhcp.MAC_ADDRESS]
self._host = discovery_info[dhcp.IP_ADDRESS]
await self._async_start_discovery_with_mac_address()
await self._async_set_device_unique_id()
self.context["title_placeholders"] = {"device": self._title}
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/tplink/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.components.dhcp import IP_ADDRESS, MAC_ADDRESS
from homeassistant.components import dhcp
from homeassistant.const import CONF_DEVICE, CONF_HOST, CONF_MAC, CONF_NAME
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
Expand All @@ -32,10 +32,10 @@ def __init__(self) -> None:
self._discovered_devices: dict[str, SmartDevice] = {}
self._discovered_device: SmartDevice | None = None

async def async_step_dhcp(self, discovery_info: DiscoveryInfoType) -> FlowResult:
async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle discovery via dhcp."""
return await self._async_handle_discovery(
discovery_info[IP_ADDRESS], discovery_info[MAC_ADDRESS]
discovery_info[dhcp.IP_ADDRESS], discovery_info[dhcp.MAC_ADDRESS]
)

async def async_step_discovery(
Expand Down
5 changes: 3 additions & 2 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import homeassistant.util.uuid as uuid_util

if TYPE_CHECKING:
from homeassistant.components.dhcp import DhcpServiceInfo
from homeassistant.components.zeroconf import ZeroconfServiceInfo

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -1378,10 +1379,10 @@ async def async_step_zeroconf(
return await self.async_step_discovery(cast(dict, discovery_info))

async def async_step_dhcp(
self, discovery_info: DiscoveryInfoType
self, discovery_info: DhcpServiceInfo
) -> data_entry_flow.FlowResult:
"""Handle a flow initialized by DHCP discovery."""
return await self.async_step_discovery(discovery_info)
return await self.async_step_discovery(cast(dict, discovery_info))

async def async_step_usb(
self, discovery_info: DiscoveryInfoType
Expand Down
12 changes: 10 additions & 2 deletions homeassistant/helpers/config_entry_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Any, Awaitable, Callable, Union

from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.components import dhcp, zeroconf
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.typing import UNDEFINED, DiscoveryInfoType, UndefinedType
Expand Down Expand Up @@ -82,6 +82,15 @@ async def async_step_discovery(

return await self.async_step_confirm()

async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult:
"""Handle a flow initialized by dhcp discovery."""
if self._async_in_progress() or self._async_current_entries():
return self.async_abort(reason="single_instance_allowed")

await self.async_set_unique_id(self._domain)

return await self.async_step_confirm()

async def async_step_homekit(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
Expand All @@ -106,7 +115,6 @@ async def async_step_zeroconf(

async_step_ssdp = async_step_discovery
async_step_mqtt = async_step_discovery
async_step_dhcp = async_step_discovery

async def async_step_import(self, _: dict[str, Any] | None) -> FlowResult:
"""Handle a flow initialized by import."""
Expand Down