mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 13:17:32 +00:00
Use ZeroconfServiceInfo in xiaomi_aqara (#60131)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
2cd241ff0a
commit
c8451001a0
@ -6,8 +6,10 @@ import voluptuous as vol
|
|||||||
from xiaomi_gateway import MULTICAST_PORT, XiaomiGateway, XiaomiGatewayDiscovery
|
from xiaomi_gateway import MULTICAST_PORT, XiaomiGateway, XiaomiGatewayDiscovery
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
|
from homeassistant.components import zeroconf
|
||||||
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME, CONF_PORT, CONF_PROTOCOL
|
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME, CONF_PORT, CONF_PROTOCOL
|
||||||
from homeassistant.core import callback
|
from homeassistant.core import callback
|
||||||
|
from homeassistant.data_entry_flow import FlowResult
|
||||||
from homeassistant.helpers.device_registry import format_mac
|
from homeassistant.helpers.device_registry import format_mac
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
@ -144,11 +146,13 @@ class XiaomiAqaraFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
step_id="select", data_schema=select_schema, errors=errors
|
step_id="select", data_schema=select_schema, errors=errors
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_zeroconf(self, discovery_info):
|
async def async_step_zeroconf(
|
||||||
|
self, discovery_info: zeroconf.ZeroconfServiceInfo
|
||||||
|
) -> FlowResult:
|
||||||
"""Handle zeroconf discovery."""
|
"""Handle zeroconf discovery."""
|
||||||
name = discovery_info.get("name")
|
name = discovery_info[zeroconf.ATTR_NAME]
|
||||||
self.host = discovery_info.get("host")
|
self.host = discovery_info[zeroconf.ATTR_HOST]
|
||||||
mac_address = discovery_info.get("properties", {}).get("mac")
|
mac_address = discovery_info[zeroconf.ATTR_PROPERTIES].get("mac")
|
||||||
|
|
||||||
if not name or not self.host or not mac_address:
|
if not name or not self.host or not mac_address:
|
||||||
return self.async_abort(reason="not_xiaomi_aqara")
|
return self.async_abort(reason="not_xiaomi_aqara")
|
||||||
|
@ -5,6 +5,7 @@ from unittest.mock import Mock, patch
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
|
from homeassistant.components import zeroconf
|
||||||
from homeassistant.components.xiaomi_aqara import config_flow, const
|
from homeassistant.components.xiaomi_aqara import config_flow, const
|
||||||
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME, CONF_PORT, CONF_PROTOCOL
|
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME, CONF_PORT, CONF_PROTOCOL
|
||||||
|
|
||||||
@ -400,11 +401,11 @@ async def test_zeroconf_success(hass):
|
|||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
const.DOMAIN,
|
const.DOMAIN,
|
||||||
context={"source": config_entries.SOURCE_ZEROCONF},
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||||
data={
|
data=zeroconf.ZeroconfServiceInfo(
|
||||||
CONF_HOST: TEST_HOST,
|
host=TEST_HOST,
|
||||||
ZEROCONF_NAME: TEST_ZEROCONF_NAME,
|
name=TEST_ZEROCONF_NAME,
|
||||||
ZEROCONF_PROP: {ZEROCONF_MAC: TEST_MAC},
|
properties={ZEROCONF_MAC: TEST_MAC},
|
||||||
},
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == "form"
|
assert result["type"] == "form"
|
||||||
@ -443,7 +444,9 @@ async def test_zeroconf_missing_data(hass):
|
|||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
const.DOMAIN,
|
const.DOMAIN,
|
||||||
context={"source": config_entries.SOURCE_ZEROCONF},
|
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"
|
assert result["type"] == "abort"
|
||||||
@ -455,11 +458,11 @@ async def test_zeroconf_unknown_device(hass):
|
|||||||
result = await hass.config_entries.flow.async_init(
|
result = await hass.config_entries.flow.async_init(
|
||||||
const.DOMAIN,
|
const.DOMAIN,
|
||||||
context={"source": config_entries.SOURCE_ZEROCONF},
|
context={"source": config_entries.SOURCE_ZEROCONF},
|
||||||
data={
|
data=zeroconf.ZeroconfServiceInfo(
|
||||||
CONF_HOST: TEST_HOST,
|
host=TEST_HOST,
|
||||||
ZEROCONF_NAME: "not-a-xiaomi-aqara-gateway",
|
name="not-a-xiaomi-aqara-gateway",
|
||||||
ZEROCONF_PROP: {ZEROCONF_MAC: TEST_MAC},
|
properties={ZEROCONF_MAC: TEST_MAC},
|
||||||
},
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == "abort"
|
assert result["type"] == "abort"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user