2015-01-09 08:07:58 +00:00
|
|
|
"""
|
|
|
|
Starts a service to scan in intervals for new devices.
|
|
|
|
|
2015-01-12 16:21:50 +00:00
|
|
|
Will emit EVENT_PLATFORM_DISCOVERED whenever a new service has been discovered.
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
Knows which components handle certain types, will make sure they are
|
2015-01-12 16:21:50 +00:00
|
|
|
loaded before the EVENT_PLATFORM_DISCOVERED is fired.
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import threading
|
|
|
|
|
|
|
|
# pylint: disable=no-name-in-module, import-error
|
|
|
|
from homeassistant.external.netdisco.netdisco import DiscoveryService
|
|
|
|
import homeassistant.external.netdisco.netdisco.const as services
|
|
|
|
|
|
|
|
from homeassistant import bootstrap
|
2015-01-11 07:47:23 +00:00
|
|
|
from homeassistant.const import (
|
2015-01-12 16:21:50 +00:00
|
|
|
EVENT_HOMEASSISTANT_START, EVENT_PLATFORM_DISCOVERED,
|
2015-01-11 07:47:23 +00:00
|
|
|
ATTR_SERVICE, ATTR_DISCOVERED)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
DOMAIN = "discovery"
|
|
|
|
DEPENDENCIES = []
|
|
|
|
|
|
|
|
SCAN_INTERVAL = 300 # seconds
|
|
|
|
|
|
|
|
SERVICE_HANDLERS = {
|
|
|
|
services.BELKIN_WEMO: "switch",
|
|
|
|
services.GOOGLE_CAST: "chromecast",
|
|
|
|
services.PHILIPS_HUE: "light",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def listen(hass, service, callback):
|
|
|
|
"""
|
|
|
|
Setup listener for discovery of specific service.
|
|
|
|
Service can be a string or a list/tuple.
|
|
|
|
"""
|
|
|
|
|
2015-01-11 22:21:44 +00:00
|
|
|
if isinstance(service, str):
|
2015-01-09 08:07:58 +00:00
|
|
|
service = (service,)
|
2015-01-11 22:21:44 +00:00
|
|
|
else:
|
|
|
|
service = tuple(service)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
def discovery_event_listener(event):
|
|
|
|
""" Listens for discovery events. """
|
|
|
|
if event.data[ATTR_SERVICE] in service:
|
|
|
|
callback(event.data[ATTR_SERVICE], event.data[ATTR_DISCOVERED])
|
|
|
|
|
2015-01-12 16:21:50 +00:00
|
|
|
hass.bus.listen(EVENT_PLATFORM_DISCOVERED, discovery_event_listener)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
|
|
|
""" Starts a discovery service. """
|
|
|
|
|
|
|
|
# Disable zeroconf logging, it spams
|
|
|
|
logging.getLogger('zeroconf').setLevel(logging.CRITICAL)
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
lock = threading.Lock()
|
|
|
|
|
|
|
|
def new_service_listener(service, info):
|
|
|
|
""" Called when a new service is found. """
|
|
|
|
with lock:
|
|
|
|
component = SERVICE_HANDLERS.get(service)
|
|
|
|
|
|
|
|
logger.info("Found new service: %s %s", service, info)
|
|
|
|
|
|
|
|
if component and component not in hass.components:
|
2015-01-15 07:18:44 +00:00
|
|
|
bootstrap.setup_component(hass, component, config)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2015-01-12 16:21:50 +00:00
|
|
|
hass.bus.fire(EVENT_PLATFORM_DISCOVERED, {
|
2015-01-09 08:07:58 +00:00
|
|
|
ATTR_SERVICE: service,
|
|
|
|
ATTR_DISCOVERED: info
|
|
|
|
})
|
|
|
|
|
|
|
|
def start_discovery(event):
|
|
|
|
""" Start discovering. """
|
|
|
|
netdisco = DiscoveryService(SCAN_INTERVAL)
|
|
|
|
netdisco.add_listener(new_service_listener)
|
|
|
|
netdisco.start()
|
|
|
|
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_discovery)
|
|
|
|
|
|
|
|
return True
|