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
|
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
from homeassistant.const import EVENT_HOMEASSISTANT_START
|
|
|
|
from homeassistant.helpers.discovery import load_platform, discover
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2016-10-27 06:46:44 +00:00
|
|
|
REQUIREMENTS = ['netdisco==0.7.5']
|
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
|
|
|
|
2016-08-22 12:19:19 +00:00
|
|
|
SCAN_INTERVAL = 300 # seconds
|
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'
|
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),
|
|
|
|
'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'),
|
2015-01-09 08:07:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 02:02:22 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
def setup(hass, config):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Start a discovery service."""
|
2015-03-01 05:06:59 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2015-07-20 07:07:01 +00:00
|
|
|
from netdisco.service import DiscoveryService
|
2015-01-09 08:07:58 +00:00
|
|
|
|
|
|
|
# Disable zeroconf logging, it spams
|
|
|
|
logging.getLogger('zeroconf').setLevel(logging.CRITICAL)
|
|
|
|
|
|
|
|
lock = threading.Lock()
|
|
|
|
|
|
|
|
def new_service_listener(service, info):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Called when a new service is found."""
|
2015-01-09 08:07:58 +00:00
|
|
|
with lock:
|
2015-03-22 05:02:47 +00:00
|
|
|
logger.info("Found new service: %s %s", service, info)
|
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
comp_plat = SERVICE_HANDLERS.get(service)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2016-03-07 17:49:31 +00:00
|
|
|
# We do not know how to handle this service.
|
2016-06-12 00:43:13 +00:00
|
|
|
if not comp_plat:
|
2015-03-22 05:02:47 +00:00
|
|
|
return
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
component, platform = comp_plat
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2016-06-12 00:43:13 +00:00
|
|
|
if platform is None:
|
|
|
|
discover(hass, service, info, component, config)
|
|
|
|
else:
|
|
|
|
load_platform(hass, component, platform, info, config)
|
2015-01-09 08:07:58 +00:00
|
|
|
|
2015-10-25 17:00:54 +00:00
|
|
|
# pylint: disable=unused-argument
|
2015-01-09 08:07:58 +00:00
|
|
|
def start_discovery(event):
|
2016-03-07 17:49:31 +00:00
|
|
|
"""Start discovering."""
|
2015-01-09 08:07:58 +00:00
|
|
|
netdisco = DiscoveryService(SCAN_INTERVAL)
|
|
|
|
netdisco.add_listener(new_service_listener)
|
|
|
|
netdisco.start()
|
|
|
|
|
|
|
|
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, start_discovery)
|
|
|
|
|
|
|
|
return True
|