From 9aa41be8b78dc56e82646ac6277047fde64ff011 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 19 Nov 2021 16:36:18 +0100 Subject: [PATCH] Use ZeroconfServiceInfo in lutron_caseta (#59988) Co-authored-by: epenet --- .../components/lutron_caseta/config_flow.py | 14 +++++--- .../lutron_caseta/test_config_flow.py | 33 ++++++++++--------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/lutron_caseta/config_flow.py b/homeassistant/components/lutron_caseta/config_flow.py index 9d028e97c87..dac0d6c5f94 100644 --- a/homeassistant/components/lutron_caseta/config_flow.py +++ b/homeassistant/components/lutron_caseta/config_flow.py @@ -10,8 +10,10 @@ from pylutron_caseta.smartbridge import Smartbridge import voluptuous as vol from homeassistant import config_entries +from homeassistant.components import zeroconf from homeassistant.const import CONF_HOST, CONF_NAME from homeassistant.core import callback +from homeassistant.data_entry_flow import FlowResult from .const import ( ABORT_REASON_CANNOT_CONNECT, @@ -61,16 +63,18 @@ class LutronCasetaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): return self.async_show_form(step_id="user", data_schema=DATA_SCHEMA_USER) - async def async_step_zeroconf(self, discovery_info): + async def async_step_zeroconf( + self, discovery_info: zeroconf.ZeroconfServiceInfo + ) -> FlowResult: """Handle a flow initialized by zeroconf discovery.""" - hostname = discovery_info["hostname"] + hostname = discovery_info[zeroconf.ATTR_HOSTNAME] if hostname is None or not hostname.startswith("lutron-"): return self.async_abort(reason="not_lutron_device") self.lutron_id = hostname.split("-")[1].replace(".local.", "") await self.async_set_unique_id(self.lutron_id) - host = discovery_info[CONF_HOST] + host = discovery_info[zeroconf.ATTR_HOST] self._abort_if_unique_id_configured({CONF_HOST: host}) self.data[CONF_HOST] = host @@ -80,7 +84,9 @@ class LutronCasetaFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): } return await self.async_step_link() - async def async_step_homekit(self, discovery_info): + async def async_step_homekit( + self, discovery_info: zeroconf.ZeroconfServiceInfo + ) -> FlowResult: """Handle a flow initialized by homekit discovery.""" return await self.async_step_zeroconf(discovery_info) diff --git a/tests/components/lutron_caseta/test_config_flow.py b/tests/components/lutron_caseta/test_config_flow.py index 2697bb363f6..ee9403bad50 100644 --- a/tests/components/lutron_caseta/test_config_flow.py +++ b/tests/components/lutron_caseta/test_config_flow.py @@ -8,6 +8,7 @@ from pylutron_caseta.smartbridge import Smartbridge import pytest from homeassistant import config_entries, data_entry_flow +from homeassistant.components import zeroconf from homeassistant.components.lutron_caseta import DOMAIN import homeassistant.components.lutron_caseta.config_flow as CasetaConfigFlow from homeassistant.components.lutron_caseta.const import ( @@ -424,10 +425,10 @@ async def test_zeroconf_host_already_configured(hass, tmpdir): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_ZEROCONF}, - data={ - CONF_HOST: "1.1.1.1", - ATTR_HOSTNAME: "lutron-abc.local.", - }, + data=zeroconf.ZeroconfServiceInfo( + host="1.1.1.1", + hostname="lutron-abc.local.", + ), ) await hass.async_block_till_done() @@ -447,10 +448,10 @@ async def test_zeroconf_lutron_id_already_configured(hass): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_ZEROCONF}, - data={ - CONF_HOST: "1.1.1.1", - ATTR_HOSTNAME: "lutron-abc.local.", - }, + data=zeroconf.ZeroconfServiceInfo( + host="1.1.1.1", + hostname="lutron-abc.local.", + ), ) await hass.async_block_till_done() @@ -465,10 +466,10 @@ async def test_zeroconf_not_lutron_device(hass): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_ZEROCONF}, - data={ - CONF_HOST: "1.1.1.1", - ATTR_HOSTNAME: "notlutron-abc.local.", - }, + data=zeroconf.ZeroconfServiceInfo( + host="1.1.1.1", + hostname="notlutron-abc.local.", + ), ) await hass.async_block_till_done() @@ -489,10 +490,10 @@ async def test_zeroconf(hass, source, tmpdir): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": source}, - data={ - CONF_HOST: "1.1.1.1", - ATTR_HOSTNAME: "lutron-abc.local.", - }, + data=zeroconf.ZeroconfServiceInfo( + host="1.1.1.1", + hostname="lutron-abc.local.", + ), ) await hass.async_block_till_done()