Use ZeroconfServiceInfo in xiaomi_miio (#60132)

Co-authored-by: epenet <epenet@users.noreply.github.com>
pull/60145/head
epenet 2021-11-22 15:18:22 +01:00 committed by GitHub
parent 8b26b69366
commit 2cd241ff0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 22 deletions

View File

@ -7,9 +7,11 @@ from micloud.micloudexception import MiCloudAccessDenied
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components import zeroconf
from homeassistant.config_entries import SOURCE_REAUTH
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN
from homeassistant.core import callback
from homeassistant.data_entry_flow import FlowResult
from homeassistant.helpers.device_registry import format_mac
from .const import (
@ -154,13 +156,15 @@ class XiaomiMiioFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle a flow initialized by the user."""
return await self.async_step_cloud()
async def async_step_zeroconf(self, discovery_info):
async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
"""Handle zeroconf discovery."""
name = discovery_info.get("name")
self.host = discovery_info.get("host")
self.mac = discovery_info.get("properties", {}).get("mac")
name = discovery_info[zeroconf.ATTR_NAME]
self.host = discovery_info[zeroconf.ATTR_HOST]
self.mac = discovery_info[zeroconf.ATTR_PROPERTIES].get("mac")
if self.mac is None:
poch = discovery_info.get("properties", {}).get("poch", "")
poch = discovery_info[zeroconf.ATTR_PROPERTIES].get("poch", "")
if (result := search(r"mac=\w+", poch)) is not None:
self.mac = result.group(0).split("=")[1]

View File

@ -7,6 +7,7 @@ from miio import DeviceException
import pytest
from homeassistant import config_entries, data_entry_flow
from homeassistant.components import zeroconf
from homeassistant.components.xiaomi_miio import const
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN
@ -391,11 +392,11 @@ async def test_zeroconf_gateway_success(hass):
result = await hass.config_entries.flow.async_init(
const.DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data={
CONF_HOST: TEST_HOST,
ZEROCONF_NAME: TEST_ZEROCONF_NAME,
ZEROCONF_PROP: {ZEROCONF_MAC: TEST_MAC},
},
data=zeroconf.ZeroconfServiceInfo(
host=TEST_HOST,
name=TEST_ZEROCONF_NAME,
properties={ZEROCONF_MAC: TEST_MAC},
),
)
assert result["type"] == "form"
@ -430,11 +431,11 @@ async def test_zeroconf_unknown_device(hass):
result = await hass.config_entries.flow.async_init(
const.DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data={
CONF_HOST: TEST_HOST,
ZEROCONF_NAME: "not-a-xiaomi-miio-device",
ZEROCONF_PROP: {ZEROCONF_MAC: TEST_MAC},
},
data=zeroconf.ZeroconfServiceInfo(
host=TEST_HOST,
name="not-a-xiaomi-miio-device",
properties={ZEROCONF_MAC: TEST_MAC},
),
)
assert result["type"] == "abort"
@ -444,7 +445,9 @@ async def test_zeroconf_unknown_device(hass):
async def test_zeroconf_no_data(hass):
"""Test a failed zeroconf discovery because of no data."""
result = await hass.config_entries.flow.async_init(
const.DOMAIN, context={"source": config_entries.SOURCE_ZEROCONF}, data={}
const.DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo(host=None, name=None, properties={}),
)
assert result["type"] == "abort"
@ -456,7 +459,9 @@ async def test_zeroconf_missing_data(hass):
result = await hass.config_entries.flow.async_init(
const.DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data={CONF_HOST: TEST_HOST, ZEROCONF_NAME: TEST_ZEROCONF_NAME},
data=zeroconf.ZeroconfServiceInfo(
host=TEST_HOST, name=TEST_ZEROCONF_NAME, properties={}
),
)
assert result["type"] == "abort"
@ -746,11 +751,11 @@ async def zeroconf_device_success(hass, zeroconf_name_to_test, model_to_test):
result = await hass.config_entries.flow.async_init(
const.DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data={
CONF_HOST: TEST_HOST,
ZEROCONF_NAME: zeroconf_name_to_test,
ZEROCONF_PROP: {"poch": f"0:mac={TEST_MAC_DEVICE}\x00"},
},
data=zeroconf.ZeroconfServiceInfo(
host=TEST_HOST,
name=zeroconf_name_to_test,
properties={"poch": f"0:mac={TEST_MAC_DEVICE}\x00"},
),
)
assert result["type"] == "form"