mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 21:57:51 +00:00
Fix Xiaomi Miio discovery (#47134)
This commit is contained in:
parent
72b82449d8
commit
1c9a9be197
@ -1,5 +1,6 @@
|
|||||||
"""Config flow to configure Xiaomi Miio."""
|
"""Config flow to configure Xiaomi Miio."""
|
||||||
import logging
|
import logging
|
||||||
|
from re import search
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -24,7 +25,6 @@ from .device import ConnectXiaomiDevice
|
|||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_GATEWAY_NAME = "Xiaomi Gateway"
|
DEFAULT_GATEWAY_NAME = "Xiaomi Gateway"
|
||||||
DEFAULT_DEVICE_NAME = "Xiaomi Device"
|
|
||||||
|
|
||||||
DEVICE_SETTINGS = {
|
DEVICE_SETTINGS = {
|
||||||
vol.Required(CONF_TOKEN): vol.All(str, vol.Length(min=32, max=32)),
|
vol.Required(CONF_TOKEN): vol.All(str, vol.Length(min=32, max=32)),
|
||||||
@ -57,14 +57,21 @@ class XiaomiMiioFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
name = discovery_info.get("name")
|
name = discovery_info.get("name")
|
||||||
self.host = discovery_info.get("host")
|
self.host = discovery_info.get("host")
|
||||||
self.mac = discovery_info.get("properties", {}).get("mac")
|
self.mac = discovery_info.get("properties", {}).get("mac")
|
||||||
|
if self.mac is None:
|
||||||
|
poch = discovery_info.get("properties", {}).get("poch", "")
|
||||||
|
result = search(r"mac=\w+", poch)
|
||||||
|
if result is not None:
|
||||||
|
self.mac = result.group(0).split("=")[1]
|
||||||
|
|
||||||
if not name or not self.host or not self.mac:
|
if not name or not self.host or not self.mac:
|
||||||
return self.async_abort(reason="not_xiaomi_miio")
|
return self.async_abort(reason="not_xiaomi_miio")
|
||||||
|
|
||||||
|
self.mac = format_mac(self.mac)
|
||||||
|
|
||||||
# Check which device is discovered.
|
# Check which device is discovered.
|
||||||
for gateway_model in MODELS_GATEWAY:
|
for gateway_model in MODELS_GATEWAY:
|
||||||
if name.startswith(gateway_model.replace(".", "-")):
|
if name.startswith(gateway_model.replace(".", "-")):
|
||||||
unique_id = format_mac(self.mac)
|
unique_id = self.mac
|
||||||
await self.async_set_unique_id(unique_id)
|
await self.async_set_unique_id(unique_id)
|
||||||
self._abort_if_unique_id_configured({CONF_HOST: self.host})
|
self._abort_if_unique_id_configured({CONF_HOST: self.host})
|
||||||
|
|
||||||
@ -76,12 +83,12 @@ class XiaomiMiioFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
for device_model in MODELS_ALL_DEVICES:
|
for device_model in MODELS_ALL_DEVICES:
|
||||||
if name.startswith(device_model.replace(".", "-")):
|
if name.startswith(device_model.replace(".", "-")):
|
||||||
unique_id = format_mac(self.mac)
|
unique_id = self.mac
|
||||||
await self.async_set_unique_id(unique_id)
|
await self.async_set_unique_id(unique_id)
|
||||||
self._abort_if_unique_id_configured({CONF_HOST: self.host})
|
self._abort_if_unique_id_configured({CONF_HOST: self.host})
|
||||||
|
|
||||||
self.context.update(
|
self.context.update(
|
||||||
{"title_placeholders": {"name": f"Miio Device {self.host}"}}
|
{"title_placeholders": {"name": f"{device_model} {self.host}"}}
|
||||||
)
|
)
|
||||||
|
|
||||||
return await self.async_step_device()
|
return await self.async_step_device()
|
||||||
@ -133,7 +140,7 @@ class XiaomiMiioFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Setup all other Miio Devices
|
# Setup all other Miio Devices
|
||||||
name = user_input.get(CONF_NAME, DEFAULT_DEVICE_NAME)
|
name = user_input.get(CONF_NAME, model)
|
||||||
|
|
||||||
for device_model in MODELS_ALL_DEVICES:
|
for device_model in MODELS_ALL_DEVICES:
|
||||||
if model.startswith(device_model):
|
if model.startswith(device_model):
|
||||||
|
@ -1,24 +1,24 @@
|
|||||||
{
|
{
|
||||||
"config": {
|
"config": {
|
||||||
"flow_title": "Xiaomi Miio: {name}",
|
"abort": {
|
||||||
"step": {
|
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
||||||
"device": {
|
"already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]"
|
||||||
"title": "Connect to a Xiaomi Miio Device or Xiaomi Gateway",
|
|
||||||
"description": "You will need the 32 character [%key:common::config_flow::data::api_token%], see https://www.home-assistant.io/integrations/vacuum.xiaomi_miio/#retrieving-the-access-token for instructions. Please note, that this [%key:common::config_flow::data::api_token%] is different from the key used by the Xiaomi Aqara integration.",
|
|
||||||
"data": {
|
|
||||||
"host": "[%key:common::config_flow::data::ip%]",
|
|
||||||
"token": "[%key:common::config_flow::data::api_token%]",
|
|
||||||
"model": "Device model (Optional)"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
"error": {
|
"error": {
|
||||||
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
"cannot_connect": "[%key:common::config_flow::error::cannot_connect%]",
|
||||||
"unknown_device": "The device model is not known, not able to setup the device using config flow."
|
"unknown_device": "The device model is not known, not able to setup the device using config flow."
|
||||||
},
|
},
|
||||||
"abort": {
|
"flow_title": "Xiaomi Miio: {name}",
|
||||||
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
|
"step": {
|
||||||
"already_in_progress": "[%key:common::config_flow::abort::already_in_progress%]"
|
"device": {
|
||||||
|
"data": {
|
||||||
|
"host": "[%key:common::config_flow::data::ip%]",
|
||||||
|
"model": "Device model (Optional)",
|
||||||
|
"token": "[%key:common::config_flow::data::api_token%]"
|
||||||
|
},
|
||||||
|
"description": "You will need the 32 character [%key:common::config_flow::data::api_token%], see https://www.home-assistant.io/integrations/xiaomi_miio#retrieving-the-access-token for instructions. Please note, that this [%key:common::config_flow::data::api_token%] is different from the key used by the Xiaomi Aqara integration.",
|
||||||
|
"title": "Connect to a Xiaomi Miio Device or Xiaomi Gateway"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,10 +6,7 @@ from miio import DeviceException
|
|||||||
from homeassistant import config_entries
|
from homeassistant import config_entries
|
||||||
from homeassistant.components import zeroconf
|
from homeassistant.components import zeroconf
|
||||||
from homeassistant.components.xiaomi_miio import const
|
from homeassistant.components.xiaomi_miio import const
|
||||||
from homeassistant.components.xiaomi_miio.config_flow import (
|
from homeassistant.components.xiaomi_miio.config_flow import DEFAULT_GATEWAY_NAME
|
||||||
DEFAULT_DEVICE_NAME,
|
|
||||||
DEFAULT_GATEWAY_NAME,
|
|
||||||
)
|
|
||||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN
|
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN
|
||||||
|
|
||||||
ZEROCONF_NAME = "name"
|
ZEROCONF_NAME = "name"
|
||||||
@ -21,6 +18,7 @@ TEST_TOKEN = "12345678901234567890123456789012"
|
|||||||
TEST_NAME = "Test_Gateway"
|
TEST_NAME = "Test_Gateway"
|
||||||
TEST_MODEL = const.MODELS_GATEWAY[0]
|
TEST_MODEL = const.MODELS_GATEWAY[0]
|
||||||
TEST_MAC = "ab:cd:ef:gh:ij:kl"
|
TEST_MAC = "ab:cd:ef:gh:ij:kl"
|
||||||
|
TEST_MAC_DEVICE = "abcdefghijkl"
|
||||||
TEST_GATEWAY_ID = TEST_MAC
|
TEST_GATEWAY_ID = TEST_MAC
|
||||||
TEST_HARDWARE_VERSION = "AB123"
|
TEST_HARDWARE_VERSION = "AB123"
|
||||||
TEST_FIRMWARE_VERSION = "1.2.3_456"
|
TEST_FIRMWARE_VERSION = "1.2.3_456"
|
||||||
@ -294,7 +292,7 @@ async def test_config_flow_step_device_manual_model_succes(hass):
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == "create_entry"
|
assert result["type"] == "create_entry"
|
||||||
assert result["title"] == DEFAULT_DEVICE_NAME
|
assert result["title"] == overwrite_model
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
const.CONF_FLOW_TYPE: const.CONF_DEVICE,
|
const.CONF_FLOW_TYPE: const.CONF_DEVICE,
|
||||||
CONF_HOST: TEST_HOST,
|
CONF_HOST: TEST_HOST,
|
||||||
@ -328,7 +326,7 @@ async def config_flow_device_success(hass, model_to_test):
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == "create_entry"
|
assert result["type"] == "create_entry"
|
||||||
assert result["title"] == DEFAULT_DEVICE_NAME
|
assert result["title"] == model_to_test
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
const.CONF_FLOW_TYPE: const.CONF_DEVICE,
|
const.CONF_FLOW_TYPE: const.CONF_DEVICE,
|
||||||
CONF_HOST: TEST_HOST,
|
CONF_HOST: TEST_HOST,
|
||||||
@ -346,7 +344,7 @@ async def zeroconf_device_success(hass, zeroconf_name_to_test, model_to_test):
|
|||||||
data={
|
data={
|
||||||
zeroconf.ATTR_HOST: TEST_HOST,
|
zeroconf.ATTR_HOST: TEST_HOST,
|
||||||
ZEROCONF_NAME: zeroconf_name_to_test,
|
ZEROCONF_NAME: zeroconf_name_to_test,
|
||||||
ZEROCONF_PROP: {ZEROCONF_MAC: TEST_MAC},
|
ZEROCONF_PROP: {"poch": f"0:mac={TEST_MAC_DEVICE}\x00"},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -368,7 +366,7 @@ async def zeroconf_device_success(hass, zeroconf_name_to_test, model_to_test):
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert result["type"] == "create_entry"
|
assert result["type"] == "create_entry"
|
||||||
assert result["title"] == DEFAULT_DEVICE_NAME
|
assert result["title"] == model_to_test
|
||||||
assert result["data"] == {
|
assert result["data"] == {
|
||||||
const.CONF_FLOW_TYPE: const.CONF_DEVICE,
|
const.CONF_FLOW_TYPE: const.CONF_DEVICE,
|
||||||
CONF_HOST: TEST_HOST,
|
CONF_HOST: TEST_HOST,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user