From 8cc2f3b7a481340fa2328b36904966dd619c88a4 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 5 Nov 2021 06:34:10 +0100 Subject: [PATCH] Use zeroconf attributes in enphase-envoy (#58961) --- .../components/enphase_envoy/config_flow.py | 10 +++++--- .../enphase_envoy/test_config_flow.py | 25 ++++++++++--------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/enphase_envoy/config_flow.py b/homeassistant/components/enphase_envoy/config_flow.py index 8a4b4b19e58..fa253ca442c 100644 --- a/homeassistant/components/enphase_envoy/config_flow.py +++ b/homeassistant/components/enphase_envoy/config_flow.py @@ -9,6 +9,7 @@ import httpx import voluptuous as vol from homeassistant import config_entries +from homeassistant.components import zeroconf from homeassistant.const import ( CONF_HOST, CONF_IP_ADDRESS, @@ -20,6 +21,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.data_entry_flow import FlowResult from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers.httpx_client import get_async_client +from homeassistant.helpers.typing import DiscoveryInfoType from .const import DOMAIN @@ -99,11 +101,13 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): if CONF_HOST in entry.data } - async def async_step_zeroconf(self, discovery_info): + async def async_step_zeroconf( + self, discovery_info: DiscoveryInfoType + ) -> FlowResult: """Handle a flow initialized by zeroconf discovery.""" - self.serial = discovery_info["properties"]["serialnum"] + self.serial = discovery_info[zeroconf.ATTR_PROPERTIES]["serialnum"] await self.async_set_unique_id(self.serial) - self.ip_address = discovery_info[CONF_HOST] + self.ip_address = discovery_info[zeroconf.ATTR_HOST] self._abort_if_unique_id_configured({CONF_HOST: self.ip_address}) for entry in self._async_current_entries(include_ignore=False): if ( diff --git a/tests/components/enphase_envoy/test_config_flow.py b/tests/components/enphase_envoy/test_config_flow.py index 6e32cf88975..bbe8b014d13 100644 --- a/tests/components/enphase_envoy/test_config_flow.py +++ b/tests/components/enphase_envoy/test_config_flow.py @@ -4,6 +4,7 @@ from unittest.mock import MagicMock, patch import httpx from homeassistant import config_entries +from homeassistant.components import zeroconf from homeassistant.components.enphase_envoy.const import DOMAIN from homeassistant.core import HomeAssistant @@ -157,10 +158,10 @@ async def test_zeroconf(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_ZEROCONF}, - data={ - "properties": {"serialnum": "1234"}, - "host": "1.1.1.1", - }, + data=zeroconf.HaServiceInfo( + properties={"serialnum": "1234"}, + host="1.1.1.1", + ), ) await hass.async_block_till_done() @@ -253,10 +254,10 @@ async def test_zeroconf_serial_already_exists(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_ZEROCONF}, - data={ - "properties": {"serialnum": "1234"}, - "host": "1.1.1.1", - }, + data=zeroconf.HaServiceInfo( + properties={"serialnum": "1234"}, + host="1.1.1.1", + ), ) assert result["type"] == "abort" @@ -288,10 +289,10 @@ async def test_zeroconf_host_already_exists(hass: HomeAssistant) -> None: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_ZEROCONF}, - data={ - "properties": {"serialnum": "1234"}, - "host": "1.1.1.1", - }, + data=zeroconf.HaServiceInfo( + properties={"serialnum": "1234"}, + host="1.1.1.1", + ), ) await hass.async_block_till_done()