From f17d58a049979d1f7f2b4345deca160b0d5f206d Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 19 Nov 2021 12:48:51 +0100 Subject: [PATCH] Use DhcpServiceInfo in emonitor (#59965) Co-authored-by: epenet --- .../components/emonitor/config_flow.py | 11 ++++--- tests/components/emonitor/test_config_flow.py | 32 +++++++++---------- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/emonitor/config_flow.py b/homeassistant/components/emonitor/config_flow.py index cc63e707013..a745f652be4 100644 --- a/homeassistant/components/emonitor/config_flow.py +++ b/homeassistant/components/emonitor/config_flow.py @@ -6,8 +6,9 @@ import aiohttp import voluptuous as vol from homeassistant import config_entries, core -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 import aiohttp_client from homeassistant.helpers.device_registry import format_mac @@ -62,12 +63,12 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): errors=errors, ) - async def async_step_dhcp(self, discovery_info): + async def async_step_dhcp(self, discovery_info: dhcp.DhcpServiceInfo) -> FlowResult: """Handle dhcp discovery.""" - self.discovered_ip = discovery_info[IP_ADDRESS] - await self.async_set_unique_id(format_mac(discovery_info[MAC_ADDRESS])) + self.discovered_ip = discovery_info[dhcp.IP_ADDRESS] + await self.async_set_unique_id(format_mac(discovery_info[dhcp.MAC_ADDRESS])) self._abort_if_unique_id_configured(updates={CONF_HOST: self.discovered_ip}) - name = name_short_mac(short_mac(discovery_info[MAC_ADDRESS])) + name = name_short_mac(short_mac(discovery_info[dhcp.MAC_ADDRESS])) self.context["title_placeholders"] = {"name": name} try: self.discovered_info = await fetch_mac_and_title( diff --git a/tests/components/emonitor/test_config_flow.py b/tests/components/emonitor/test_config_flow.py index a030f242d8d..0314ce7420e 100644 --- a/tests/components/emonitor/test_config_flow.py +++ b/tests/components/emonitor/test_config_flow.py @@ -5,7 +5,7 @@ from aioemonitor.monitor import EmonitorNetwork, EmonitorStatus import aiohttp from homeassistant import config_entries -from homeassistant.components.dhcp import HOSTNAME, IP_ADDRESS, MAC_ADDRESS +from homeassistant.components import dhcp from homeassistant.components.emonitor.const import DOMAIN from homeassistant.const import CONF_HOST @@ -102,11 +102,11 @@ async def test_dhcp_can_confirm(hass): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_DHCP}, - data={ - HOSTNAME: "emonitor", - IP_ADDRESS: "1.2.3.4", - MAC_ADDRESS: "aa:bb:cc:dd:ee:ff", - }, + data=dhcp.DhcpServiceInfo( + hostname="emonitor", + ip="1.2.3.4", + macaddress="aa:bb:cc:dd:ee:ff", + ), ) await hass.async_block_till_done() @@ -145,11 +145,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: "emonitor", - IP_ADDRESS: "1.2.3.4", - MAC_ADDRESS: "aa:bb:cc:dd:ee:ff", - }, + data=dhcp.DhcpServiceInfo( + hostname="emonitor", + ip="1.2.3.4", + macaddress="aa:bb:cc:dd:ee:ff", + ), ) await hass.async_block_till_done() @@ -174,11 +174,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: "emonitor", - IP_ADDRESS: "1.2.3.4", - MAC_ADDRESS: "aa:bb:cc:dd:ee:ff", - }, + data=dhcp.DhcpServiceInfo( + hostname="emonitor", + ip="1.2.3.4", + macaddress="aa:bb:cc:dd:ee:ff", + ), ) await hass.async_block_till_done()