Xiaomi Miio zeroconf discovery (#35352)

This commit is contained in:
starkillerOG authored and GitHub committed 2020-05-14 15:59:40 +02:00
1 parent c67d035366
commit 256370afa8
8 files changed
+151 -24

No files matched your search

@@ -38,7 +38,11 @@ async def async_setup_gateway_entry(
host = entry.data[CONF_HOST]
token = entry.data[CONF_TOKEN]
name = entry.title
gateway_id = entry.data["gateway_id"]
gateway_id = entry.unique_id
# For backwards compat
if entry.unique_id.endswith("-gateway"):
hass.config_entries.async_update_entry(entry, unique_id=entry.data["mac"])
# Connect to gateway
gateway = ConnectXiaomiGateway(hass)
@@ -33,10 +33,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
f"{config_entry.title} Alarm",
config_entry.data["model"],
config_entry.data["mac"],
config_entry.data["gateway_id"],
config_entry.unique_id,
)
entities.append(entity)
async_add_entities(entities)
async_add_entities(entities, update_before_add=True)
class XiaomiGatewayAlarm(AlarmControlPanelEntity):
@@ -5,6 +5,7 @@ import voluptuous as vol
from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_TOKEN
from homeassistant.helpers.device_registry import format_mac
# pylint: disable=unused-import
from .const import DOMAIN
@@ -15,14 +16,13 @@ _LOGGER = logging.getLogger(__name__)
CONF_FLOW_TYPE = "config_flow_device"
CONF_GATEWAY = "gateway"
DEFAULT_GATEWAY_NAME = "Xiaomi Gateway"
ZEROCONF_GATEWAY = "lumi-gateway"
GATEWAY_CONFIG = vol.Schema(
{
vol.Required(CONF_HOST): str,
vol.Required(CONF_TOKEN): vol.All(str, vol.Length(min=32, max=32)),
vol.Optional(CONF_NAME, default=DEFAULT_GATEWAY_NAME): str,
}
)
GATEWAY_SETTINGS = {
vol.Required(CONF_TOKEN): vol.All(str, vol.Length(min=32, max=32)),
vol.Optional(CONF_NAME, default=DEFAULT_GATEWAY_NAME): str,
}
GATEWAY_CONFIG = vol.Schema({vol.Required(CONF_HOST): str}).extend(GATEWAY_SETTINGS)
CONFIG_SCHEMA = vol.Schema({vol.Optional(CONF_GATEWAY, default=False): bool})
@@ -33,6 +33,10 @@ class XiaomiMiioFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
def __init__(self):
"""Initialize."""
self.host = None
async def async_step_user(self, user_input=None):
"""Handle a flow initialized by the user."""
errors = {}
@@ -47,36 +51,67 @@ class XiaomiMiioFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
step_id="user", data_schema=CONFIG_SCHEMA, errors=errors
)
async def async_step_zeroconf(self, discovery_info):
"""Handle zeroconf discovery."""
name = discovery_info.get("name")
self.host = discovery_info.get("host")
mac_address = discovery_info.get("properties", {}).get("mac")
if not name or not self.host or not mac_address:
return self.async_abort(reason="not_xiaomi_miio")
# Check which device is discovered.
if name.startswith(ZEROCONF_GATEWAY):
unique_id = format_mac(mac_address)
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured({CONF_HOST: self.host})
return await self.async_step_gateway()
# Discovered device is not yet supported
_LOGGER.debug(
"Not yet supported Xiaomi Miio device '%s' discovered with host %s",
name,
self.host,
)
return self.async_abort(reason="not_xiaomi_miio")
async def async_step_gateway(self, user_input=None):
"""Handle a flow initialized by the user to configure a gateway."""
errors = {}
if user_input is not None:
host = user_input[CONF_HOST]
token = user_input[CONF_TOKEN]
if user_input.get(CONF_HOST):
self.host = user_input[CONF_HOST]
# Try to connect to a Xiaomi Gateway.
connect_gateway_class = ConnectXiaomiGateway(self.hass)
await connect_gateway_class.async_connect_gateway(host, token)
await connect_gateway_class.async_connect_gateway(self.host, token)
gateway_info = connect_gateway_class.gateway_info
if gateway_info is not None:
unique_id = f"{gateway_info.model}-{gateway_info.mac_address}-gateway"
mac = format_mac(gateway_info.mac_address)
unique_id = mac
await self.async_set_unique_id(unique_id)
self._abort_if_unique_id_configured()
return self.async_create_entry(
title=user_input[CONF_NAME],
data={
CONF_FLOW_TYPE: CONF_GATEWAY,
CONF_HOST: host,
CONF_HOST: self.host,
CONF_TOKEN: token,
"gateway_id": unique_id,
"model": gateway_info.model,
"mac": gateway_info.mac_address,
"mac": mac,
},
)
errors["base"] = "connect_error"
if self.host:
schema = vol.Schema(GATEWAY_SETTINGS)
else:
schema = GATEWAY_CONFIG
return self.async_show_form(
step_id="gateway", data_schema=GATEWAY_CONFIG, errors=errors
step_id="gateway", data_schema=schema, errors=errors
)
@@ -4,5 +4,6 @@
"config_flow": true,
"documentation": "https://www.home-assistant.io/integrations/xiaomi_miio",
"requirements": ["construct==2.9.45", "python-miio==0.5.0.1"],
"codeowners": ["@rytilahti", "@syssi"]
"codeowners": ["@rytilahti", "@syssi"],
"zeroconf": ["_miio._udp.local."]
}
@@ -23,7 +23,8 @@
"no_device_selected": "No device selected, please select one device."
},
"abort": {
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]"
"already_configured": "[%key:common::config_flow::abort::already_configured_device%]",
"already_in_progress": "Config flow for this Xiaomi Miio device is already in progress."
}
}
}
@@ -1,7 +1,8 @@
{
"config": {
"abort": {
"already_configured": "Device is already configured"
"already_configured": "Device is already configured",
"already_in_progress": "Config flow for this Xiaomi Miio device is already in progress."
},
"error": {
"connect_error": "Failed to connect",
@@ -26,4 +27,4 @@
}
}
}
}
}