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
|
|
|
"""
|
2017-03-01 15:38:49 +00:00
|
|
|
import asyncio
|
2017-03-03 21:11:40 +00:00
|
|
|
import json
|
2017-03-01 15:38:49 +00:00
|
|
|
from datetime import timedelta
|
2015-01-09 08:07:58 +00:00
|
|
|
import logging
|
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2017-04-09 08:05:34 +00:00
|
|
|
from homeassistant.core import callback
|
2016-06-12 00:43:13 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
2017-03-01 15:38:49 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.event import async_track_point_in_utc_time
|
|
|
|
from homeassistant.helpers.discovery import async_load_platform, async_discover
|
|
|
|
import homeassistant.util.dt as dt_util
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2017-04-19 03:58:25 +00:00
|
|
|
REQUIREMENTS = ['netdisco==1.0.0rc3']
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2016-08-22 12:19:19 +00:00
|
|
|
DOMAIN = 'discovery'
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=300)
|
2015-08-24 00:20:09 +00:00
|
|
|
SERVICE_NETGEAR = 'netgear_router'
|
2016-08-22 12:19:19 +00:00
|
|
|
SERVICE_WEMO = 'belkin_wemo'
|
2016-10-22 06:20:15 +00:00
|
|
|
SERVICE_HASS_IOS_APP = 'hass_ios'
|
2017-04-16 21:37:39 +00:00
|
|
|
SERVICE_IKEA_TRADFRI = 'ikea_tradfri'
|
2015-07-20 07:07:01 +00:00
|
|
|
|
2015-01-09 08:07:58 +00:00
|
|
|
SERVICE_HANDLERS = {
|
2016-10-22 06:20:15 +00:00
|
|
|
SERVICE_HASS_IOS_APP: ('ios', None),
|
2016-06-12 00:43:13 +00:00
|
|
|
SERVICE_NETGEAR: ('device_tracker', None),
|
|
|
|
SERVICE_WEMO: ('wemo', None),
|
2017-04-16 21:37:39 +00:00
|
|
|
SERVICE_IKEA_TRADFRI: ('tradfri', None),
|
2016-06-12 00:43:13 +00:00
|
|
|
'philips_hue': ('light', 'hue'),
|
|
|
|
'google_cast': ('media_player', 'cast'),
|
|
|
|
'panasonic_viera': ('media_player', 'panasonic_viera'),
|
|
|
|
'plex_mediaserver': ('media_player', 'plex'),
|
|
|
|
'roku': ('media_player', 'roku'),
|
|
|
|
'sonos': ('media_player', 'sonos'),
|
2016-10-27 06:46:44 +00:00
|
|
|
'yamaha': ('media_player', 'yamaha'),
|
2016-06-12 00:43:13 +00:00
|
|
|
'logitech_mediaserver': ('media_player', 'squeezebox'),
|
2016-07-26 06:20:56 +00:00
|
|
|
'directv': ('media_player', 'directv'),
|
2016-12-14 04:04:40 +00:00
|
|
|
'denonavr': ('media_player', 'denonavr'),
|
2016-12-16 06:20:00 +00:00
|
|
|
'samsung_tv': ('media_player', 'samsungtv'),
|
2017-01-05 22:39:28 +00:00
|
|
|
'yeelight': ('light', 'yeelight'),
|
2017-02-08 11:17:23 +00:00
|
|
|
'apple_tv': ('media_player', 'apple_tv'),
|
2017-02-28 14:23:07 +00:00
|
|
|
'frontier_silicon': ('media_player', 'frontier_silicon'),
|
2017-02-22 22:01:06 +00:00
|
|
|
'openhome': ('media_player', 'openhome'),
|
2015-01-09 08:07:58 +00:00
|
|
|
}
|
|
|
|
|
2017-02-23 10:54:35 +00:00
|
|
|
CONF_IGNORE = 'ignore'
|
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
2017-03-01 15:38:49 +00:00
|
|
|
vol.Required(DOMAIN): vol.Schema({
|
2017-02-23 10:54:35 +00:00
|
|
|
vol.Optional(CONF_IGNORE, default=[]):
|
|
|
|
vol.All(cv.ensure_list, [vol.In(SERVICE_HANDLERS)])
|
|
|
|
}),
|
2016-09-30 02:02:22 +00:00
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_setup(hass, config):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Start a discovery service."""
|
2017-03-01 15:38:49 +00:00
|
|
|
from netdisco.discovery import NetworkDiscovery
|
2015-03-01 05:06:59 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
netdisco = NetworkDiscovery()
|
2017-03-03 21:11:40 +00:00
|
|
|
already_discovered = set()
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
# Disable zeroconf logging, it spams
|
|
|
|
logging.getLogger('zeroconf').setLevel(logging.CRITICAL)
|
|
|
|
|
2017-02-23 10:54:35 +00:00
|
|
|
# Platforms ignore by config
|
|
|
|
ignored_platforms = config[DOMAIN][CONF_IGNORE]
|
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def new_service_found(service, info):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Called when a new service is found."""
|
2017-02-23 10:54:35 +00:00
|
|
|
if service in ignored_platforms:
|
|
|
|
logger.info("Ignoring service: %s %s", service, info)
|
|
|
|
return
|
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
comp_plat = SERVICE_HANDLERS.get(service)
|
2015-03-22 05:02:47 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
# We do not know how to handle this service.
|
|
|
|
if not comp_plat:
|
|
|
|
return
|
|
|
|
|
2017-03-03 21:11:40 +00:00
|
|
|
discovery_hash = json.dumps([service, info], sort_keys=True)
|
|
|
|
if discovery_hash in already_discovered:
|
|
|
|
return
|
|
|
|
|
|
|
|
already_discovered.add(discovery_hash)
|
|
|
|
|
|
|
|
logger.info("Found new service: %s %s", service, info)
|
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
component, platform = comp_plat
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
if platform is None:
|
|
|
|
yield from async_discover(hass, service, info, component, config)
|
|
|
|
else:
|
|
|
|
yield from async_load_platform(
|
|
|
|
hass, component, platform, info, config)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
@asyncio.coroutine
|
2017-04-09 08:05:34 +00:00
|
|
|
def scan_devices(now):
|
2017-03-01 15:38:49 +00:00
|
|
|
"""Scan for devices."""
|
|
|
|
results = yield from hass.loop.run_in_executor(
|
|
|
|
None, _discover, netdisco)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
for result in results:
|
|
|
|
hass.async_add_job(new_service_found(*result))
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2017-03-01 15:38:49 +00:00
|
|
|
async_track_point_in_utc_time(hass, scan_devices,
|
|
|
|
dt_util.utcnow() + SCAN_INTERVAL)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2017-04-09 08:05:34 +00:00
|
|
|
@callback
|
|
|
|
def schedule_first(event):
|
|
|
|
"""Schedule the first discovery when Home Assistant starts up."""
|
|
|
|
async_track_point_in_utc_time(hass, scan_devices, dt_util.utcnow())
|
|
|
|
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_START, schedule_first)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
return True
|
2017-03-01 15:38:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _discover(netdisco):
|
|
|
|
"""Discover devices."""
|
|
|
|
results = []
|
|
|
|
try:
|
|
|
|
netdisco.scan()
|
|
|
|
|
|
|
|
for disc in netdisco.discover():
|
|
|
|
for service in netdisco.get_info(disc):
|
|
|
|
results.append((disc, service))
|
|
|
|
finally:
|
|
|
|
netdisco.stop()
|
|
|
|
|
|
|
|
return results
|