Use DhcpServiceInfo in broadlink (#59961)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-11-19 12:29:20 +01:00 committed by GitHub
parent 2aa8c2cf74
commit 59547289b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 40 deletions

View file

@ -13,7 +13,7 @@ from broadlink.exceptions import (
import voluptuous as vol
from homeassistant import config_entries, data_entry_flow
from homeassistant.components.dhcp import IP_ADDRESS, MAC_ADDRESS
from homeassistant.components import dhcp
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME, CONF_TIMEOUT, CONF_TYPE
from homeassistant.helpers import config_validation as cv
@ -53,10 +53,12 @@ class BroadlinkFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"host": device.host[0],
}
async def async_step_dhcp(self, discovery_info):
async def async_step_dhcp(
self, discovery_info: dhcp.DhcpServiceInfo
) -> data_entry_flow.FlowResult:
"""Handle dhcp discovery."""
host = discovery_info[IP_ADDRESS]
unique_id = discovery_info[MAC_ADDRESS].lower().replace(":", "")
host = discovery_info[dhcp.IP_ADDRESS]
unique_id = discovery_info[dhcp.MAC_ADDRESS].lower().replace(":", "")
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured(updates={CONF_HOST: host})

View file

@ -7,8 +7,8 @@ import broadlink.exceptions as blke
import pytest
from homeassistant import config_entries
from homeassistant.components import dhcp
from homeassistant.components.broadlink.const import DOMAIN
from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS, MAC_ADDRESS
from homeassistant.helpers import device_registry
from . import get_device
@ -834,11 +834,11 @@ async def test_dhcp_can_finish(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data={
HOSTNAME: "broadlink",
IP_ADDRESS: "1.2.3.4",
MAC_ADDRESS: device_registry.format_mac(device.mac),
},
data=dhcp.DhcpServiceInfo(
hostname="broadlink",
ip="1.2.3.4",
macaddress=device_registry.format_mac(device.mac),
),
)
await hass.async_block_till_done()
@ -868,11 +868,11 @@ async def test_dhcp_fails_to_connect(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data={
HOSTNAME: "broadlink",
IP_ADDRESS: "1.2.3.4",
MAC_ADDRESS: "34:ea:34:b4:3b:5a",
},
data=dhcp.DhcpServiceInfo(
hostname="broadlink",
ip="1.2.3.4",
macaddress="34:ea:34:b4:3b:5a",
),
)
await hass.async_block_till_done()
@ -887,11 +887,11 @@ async def test_dhcp_unreachable(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data={
HOSTNAME: "broadlink",
IP_ADDRESS: "1.2.3.4",
MAC_ADDRESS: "34:ea:34:b4:3b:5a",
},
data=dhcp.DhcpServiceInfo(
hostname="broadlink",
ip="1.2.3.4",
macaddress="34:ea:34:b4:3b:5a",
),
)
await hass.async_block_till_done()
@ -906,11 +906,11 @@ async def test_dhcp_connect_unknown_error(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data={
HOSTNAME: "broadlink",
IP_ADDRESS: "1.2.3.4",
MAC_ADDRESS: "34:ea:34:b4:3b:5a",
},
data=dhcp.DhcpServiceInfo(
hostname="broadlink",
ip="1.2.3.4",
macaddress="34:ea:34:b4:3b:5a",
),
)
await hass.async_block_till_done()
@ -928,11 +928,11 @@ async def test_dhcp_device_not_supported(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data={
HOSTNAME: "broadlink",
IP_ADDRESS: device.host,
MAC_ADDRESS: device_registry.format_mac(device.mac),
},
data=dhcp.DhcpServiceInfo(
hostname="broadlink",
ip=device.host,
macaddress=device_registry.format_mac(device.mac),
),
)
assert result["type"] == "abort"
@ -952,11 +952,11 @@ async def test_dhcp_already_exists(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data={
HOSTNAME: "broadlink",
IP_ADDRESS: "1.2.3.4",
MAC_ADDRESS: "34:ea:34:b4:3b:5a",
},
data=dhcp.DhcpServiceInfo(
hostname="broadlink",
ip="1.2.3.4",
macaddress="34:ea:34:b4:3b:5a",
),
)
await hass.async_block_till_done()
@ -977,11 +977,11 @@ async def test_dhcp_updates_host(hass):
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_DHCP},
data={
HOSTNAME: "broadlink",
IP_ADDRESS: "4.5.6.7",
MAC_ADDRESS: "34:ea:34:b4:3b:5a",
},
data=dhcp.DhcpServiceInfo(
hostname="broadlink",
ip="4.5.6.7",
macaddress="34:ea:34:b4:3b:5a",
),
)
await hass.async_block_till_done()