Use ZeroconfServiceInfo in lutron_caseta (#59988)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-11-19 16:36:18 +01:00 committed by GitHub
parent 2b7bcd6aeb
commit 9aa41be8b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 20 deletions

View File

@ -10,8 +10,10 @@ from pylutron_caseta.smartbridge import Smartbridge
import voluptuous as vol import voluptuous as vol
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.const import CONF_HOST, CONF_NAME from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from .const import ( from .const import (
ABORT_REASON_CANNOT_CONNECT, 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) 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.""" """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-"): if hostname is None or not hostname.startswith("lutron-"):
return self.async_abort(reason="not_lutron_device") return self.async_abort(reason="not_lutron_device")
self.lutron_id = hostname.split("-")[1].replace(".local.", "") self.lutron_id = hostname.split("-")[1].replace(".local.", "")
await self.async_set_unique_id(self.lutron_id) 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._abort_if_unique_id_configured({CONF_HOST: host})
self.data[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() 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.""" """Handle a flow initialized by homekit discovery."""
return await self.async_step_zeroconf(discovery_info) return await self.async_step_zeroconf(discovery_info)

View File

@ -8,6 +8,7 @@ from pylutron_caseta.smartbridge import Smartbridge
import pytest import pytest
from homeassistant import config_entries, data_entry_flow from homeassistant import config_entries, data_entry_flow
from homeassistant.components import zeroconf
from homeassistant.components.lutron_caseta import DOMAIN from homeassistant.components.lutron_caseta import DOMAIN
import homeassistant.components.lutron_caseta.config_flow as CasetaConfigFlow import homeassistant.components.lutron_caseta.config_flow as CasetaConfigFlow
from homeassistant.components.lutron_caseta.const import ( 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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF}, context={"source": config_entries.SOURCE_ZEROCONF},
data={ data=zeroconf.ZeroconfServiceInfo(
CONF_HOST: "1.1.1.1", host="1.1.1.1",
ATTR_HOSTNAME: "lutron-abc.local.", hostname="lutron-abc.local.",
}, ),
) )
await hass.async_block_till_done() 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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF}, context={"source": config_entries.SOURCE_ZEROCONF},
data={ data=zeroconf.ZeroconfServiceInfo(
CONF_HOST: "1.1.1.1", host="1.1.1.1",
ATTR_HOSTNAME: "lutron-abc.local.", hostname="lutron-abc.local.",
}, ),
) )
await hass.async_block_till_done() 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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF}, context={"source": config_entries.SOURCE_ZEROCONF},
data={ data=zeroconf.ZeroconfServiceInfo(
CONF_HOST: "1.1.1.1", host="1.1.1.1",
ATTR_HOSTNAME: "notlutron-abc.local.", hostname="notlutron-abc.local.",
}, ),
) )
await hass.async_block_till_done() 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( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": source}, context={"source": source},
data={ data=zeroconf.ZeroconfServiceInfo(
CONF_HOST: "1.1.1.1", host="1.1.1.1",
ATTR_HOSTNAME: "lutron-abc.local.", hostname="lutron-abc.local.",
}, ),
) )
await hass.async_block_till_done() await hass.async_block_till_done()