From 4f7e405a2c692f1f4a0397b0ec53d58b6c26cdab Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Mon, 15 Nov 2021 20:26:50 +0100 Subject: [PATCH] Use zeroconf attributes in esphome (#58963) Co-authored-by: epenet --- .../components/esphome/config_flow.py | 17 +++-- tests/components/esphome/test_config_flow.py | 73 ++++++++++--------- 2 files changed, 47 insertions(+), 43 deletions(-) diff --git a/homeassistant/components/esphome/config_flow.py b/homeassistant/components/esphome/config_flow.py index fa72cbd995e..fab4b045d0c 100644 --- a/homeassistant/components/esphome/config_flow.py +++ b/homeassistant/components/esphome/config_flow.py @@ -142,14 +142,14 @@ class EsphomeFlowHandler(ConfigFlow, domain=DOMAIN): ) -> FlowResult: """Handle zeroconf discovery.""" # Hostname is format: livingroom.local. - local_name = discovery_info["hostname"][:-1] + local_name = discovery_info[zeroconf.ATTR_HOSTNAME][:-1] node_name = local_name[: -len(".local")] - address = discovery_info["properties"].get("address", local_name) + address = discovery_info[zeroconf.ATTR_PROPERTIES].get("address", local_name) # Check if already configured await self.async_set_unique_id(node_name) self._abort_if_unique_id_configured( - updates={CONF_HOST: discovery_info[CONF_HOST]} + updates={CONF_HOST: discovery_info[zeroconf.ATTR_HOST]} ) for entry in self._async_current_entries(): @@ -157,7 +157,7 @@ class EsphomeFlowHandler(ConfigFlow, domain=DOMAIN): if CONF_HOST in entry.data and entry.data[CONF_HOST] in ( address, - discovery_info[CONF_HOST], + discovery_info[zeroconf.ATTR_HOST], ): # Is this address or IP address already configured? already_configured = True @@ -174,14 +174,17 @@ class EsphomeFlowHandler(ConfigFlow, domain=DOMAIN): if not entry.unique_id: self.hass.config_entries.async_update_entry( entry, - data={**entry.data, CONF_HOST: discovery_info[CONF_HOST]}, + data={ + **entry.data, + CONF_HOST: discovery_info[zeroconf.ATTR_HOST], + }, unique_id=node_name, ) return self.async_abort(reason="already_configured") - self._host = discovery_info[CONF_HOST] - self._port = discovery_info[CONF_PORT] + self._host = discovery_info[zeroconf.ATTR_HOST] + self._port = discovery_info[zeroconf.ATTR_PORT] self._name = node_name return await self.async_step_discovery_confirm() diff --git a/tests/components/esphome/test_config_flow.py b/tests/components/esphome/test_config_flow.py index 6a96e88cab0..1ae332142a0 100644 --- a/tests/components/esphome/test_config_flow.py +++ b/tests/components/esphome/test_config_flow.py @@ -12,6 +12,7 @@ from aioesphomeapi import ( import pytest from homeassistant import config_entries +from homeassistant.components import zeroconf from homeassistant.components.esphome import CONF_NOISE_PSK, DOMAIN, DomainData from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_PORT from homeassistant.data_entry_flow import ( @@ -215,12 +216,12 @@ async def test_discovery_initiation(hass, mock_client, mock_zeroconf): """Test discovery importing works.""" mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266")) - service_info = { - "host": "192.168.43.183", - "port": 6053, - "hostname": "test8266.local.", - "properties": {}, - } + service_info = zeroconf.ZeroconfServiceInfo( + host="192.168.43.183", + port=6053, + hostname="test8266.local.", + properties={}, + ) flow = await hass.config_entries.flow.async_init( "esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info ) @@ -247,12 +248,12 @@ async def test_discovery_already_configured_hostname(hass, mock_client): entry.add_to_hass(hass) - service_info = { - "host": "192.168.43.183", - "port": 6053, - "hostname": "test8266.local.", - "properties": {}, - } + service_info = zeroconf.ZeroconfServiceInfo( + host="192.168.43.183", + port=6053, + hostname="test8266.local.", + properties={}, + ) result = await hass.config_entries.flow.async_init( "esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info ) @@ -272,12 +273,12 @@ async def test_discovery_already_configured_ip(hass, mock_client): entry.add_to_hass(hass) - service_info = { - "host": "192.168.43.183", - "port": 6053, - "hostname": "test8266.local.", - "properties": {"address": "192.168.43.183"}, - } + service_info = zeroconf.ZeroconfServiceInfo( + host="192.168.43.183", + port=6053, + hostname="test8266.local.", + properties={"address": "192.168.43.183"}, + ) result = await hass.config_entries.flow.async_init( "esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info ) @@ -301,12 +302,12 @@ async def test_discovery_already_configured_name(hass, mock_client): domain_data = DomainData.get(hass) domain_data.set_entry_data(entry, mock_entry_data) - service_info = { - "host": "192.168.43.184", - "port": 6053, - "hostname": "test8266.local.", - "properties": {"address": "test8266.local"}, - } + service_info = zeroconf.ZeroconfServiceInfo( + host="192.168.43.184", + port=6053, + hostname="test8266.local.", + properties={"address": "test8266.local"}, + ) result = await hass.config_entries.flow.async_init( "esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info ) @@ -320,12 +321,12 @@ async def test_discovery_already_configured_name(hass, mock_client): async def test_discovery_duplicate_data(hass, mock_client): """Test discovery aborts if same mDNS packet arrives.""" - service_info = { - "host": "192.168.43.183", - "port": 6053, - "hostname": "test8266.local.", - "properties": {"address": "test8266.local"}, - } + service_info = zeroconf.ZeroconfServiceInfo( + host="192.168.43.183", + port=6053, + hostname="test8266.local.", + properties={"address": "test8266.local"}, + ) mock_client.device_info = AsyncMock(return_value=MockDeviceInfo(False, "test8266")) @@ -351,12 +352,12 @@ async def test_discovery_updates_unique_id(hass, mock_client): entry.add_to_hass(hass) - service_info = { - "host": "192.168.43.183", - "port": 6053, - "hostname": "test8266.local.", - "properties": {"address": "test8266.local"}, - } + service_info = zeroconf.ZeroconfServiceInfo( + host="192.168.43.183", + port=6053, + hostname="test8266.local.", + properties={"address": "test8266.local"}, + ) result = await hass.config_entries.flow.async_init( "esphome", context={"source": config_entries.SOURCE_ZEROCONF}, data=service_info )