From d90801f6dd732c074a95f3268954b07af86d49e6 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Wed, 13 Sep 2017 06:44:42 +0300 Subject: [PATCH] components/xiaomi: Add initial discovery using NetDisco. (#9283) There's a kind of duplication of functionality between NetDisco and "xiaomi" component, the latter features its own "discovery" in addition to general HomeAssistant discovery service, based on NetDisco. As such, this patch is pretty simple: the only purpose of NetDisco discovery is "plug and play", "zero configuration" discovery that Xiaomi Gateway device is present on the local network, and triggering of "xiaomi" component loading, which then "rediscovers" the gateway using its own method. --- homeassistant/components/discovery.py | 2 ++ homeassistant/components/xiaomi.py | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/discovery.py b/homeassistant/components/discovery.py index 232230a2241..1f8b12eef6b 100644 --- a/homeassistant/components/discovery.py +++ b/homeassistant/components/discovery.py @@ -34,6 +34,7 @@ SERVICE_HASSIO = 'hassio' SERVICE_AXIS = 'axis' SERVICE_APPLE_TV = 'apple_tv' SERVICE_WINK = 'wink' +SERVICE_XIAOMI_GW = 'xiaomi_gw' SERVICE_HANDLERS = { SERVICE_HASS_IOS_APP: ('ios', None), @@ -44,6 +45,7 @@ SERVICE_HANDLERS = { SERVICE_AXIS: ('axis', None), SERVICE_APPLE_TV: ('apple_tv', None), SERVICE_WINK: ('wink', None), + SERVICE_XIAOMI_GW: ('xiaomi', None), 'philips_hue': ('light', 'hue'), 'google_cast': ('media_player', 'cast'), 'panasonic_viera': ('media_player', 'panasonic_viera'), diff --git a/homeassistant/components/xiaomi.py b/homeassistant/components/xiaomi.py index 1d14a76d251..ac197d2d942 100644 --- a/homeassistant/components/xiaomi.py +++ b/homeassistant/components/xiaomi.py @@ -4,10 +4,10 @@ import voluptuous as vol import homeassistant.helpers.config_validation as cv from homeassistant.helpers import discovery from homeassistant.helpers.entity import Entity +from homeassistant.components.discovery import SERVICE_XIAOMI_GW from homeassistant.const import (ATTR_BATTERY_LEVEL, EVENT_HOMEASSISTANT_STOP, CONF_MAC) - REQUIREMENTS = ['https://github.com/Danielhiversen/PyXiaomiGateway/archive/' '0.3.2.zip#PyXiaomiGateway==0.3.2'] @@ -57,9 +57,22 @@ _LOGGER = logging.getLogger(__name__) def setup(hass, config): """Set up the Xiaomi component.""" - gateways = config[DOMAIN][CONF_GATEWAYS] - interface = config[DOMAIN][CONF_INTERFACE] - discovery_retry = config[DOMAIN][CONF_DISCOVERY_RETRY] + gateways = [] + interface = 'any' + discovery_retry = 3 + if DOMAIN in config: + gateways = config[DOMAIN][CONF_GATEWAYS] + interface = config[DOMAIN][CONF_INTERFACE] + discovery_retry = config[DOMAIN][CONF_DISCOVERY_RETRY] + + def xiaomi_gw_discovered(service, discovery_info): + """Called when Xiaomi Gateway device(s) has been found.""" + # We don't need to do anything here, the purpose of HA's + # discovery service is to just trigger loading of this + # component, and then its own discovery process kicks in. + _LOGGER.info("Discovered: %s", discovery_info) + + discovery.listen(hass, SERVICE_XIAOMI_GW, xiaomi_gw_discovered) from PyXiaomiGateway import PyXiaomiGateway hass.data[PY_XIAOMI_GATEWAY] = PyXiaomiGateway(hass.add_job, gateways,