From 3757ddf9dfa094c14cd25f88a005a364756c3049 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 21 Apr 2014 17:58:58 -0700 Subject: [PATCH] Add option to set fixed hosts for chromecasts instead of discovery --- home-assistant.conf.default | 2 ++ homeassistant/bootstrap.py | 11 ++++++++++- homeassistant/components/chromecast.py | 8 +++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/home-assistant.conf.default b/home-assistant.conf.default index ce6f7d9c14b7..159a483d7e60 100644 --- a/home-assistant.conf.default +++ b/home-assistant.conf.default @@ -20,6 +20,8 @@ username=admin password=PASSWORD [chromecast] +# Optional: hard code the hosts to find chromecasts instead of scanning the network +# hosts=192.168.1.9,192.168.1.12 [wemo] diff --git a/homeassistant/bootstrap.py b/homeassistant/bootstrap.py index 48933f8c87c5..f09b9d97ac2d 100644 --- a/homeassistant/bootstrap.py +++ b/homeassistant/bootstrap.py @@ -54,6 +54,13 @@ def from_config_file(config_path): except (configparser.NoSectionError, configparser.NoOptionError): return default + def get_hosts(section): + """ Helper method to retrieve hosts from config. """ + if has_opt(section, "hosts"): + return get_opt(section, "hosts").split(",") + else: + return None + # Device scanner dev_scan = None @@ -122,7 +129,9 @@ def from_config_file(config_path): if has_section("chromecast"): chromecast = load_module('chromecast') - chromecast_started = chromecast.setup(bus, statemachine) + hosts = get_hosts("chromecast") + + chromecast_started = chromecast.setup(bus, statemachine, hosts) add_status("Chromecast", chromecast_started) else: diff --git a/homeassistant/components/chromecast.py b/homeassistant/components/chromecast.py index b6ae5be8a7c7..2150a639f6d6 100644 --- a/homeassistant/components/chromecast.py +++ b/homeassistant/components/chromecast.py @@ -88,7 +88,7 @@ def media_prev_track(bus, entity_id=None): # pylint: disable=too-many-locals, too-many-branches -def setup(bus, statemachine): +def setup(bus, statemachine, hosts=None): """ Listen for chromecast events. """ logger = logging.getLogger(__name__) @@ -101,8 +101,10 @@ def setup(bus, statemachine): return False - logger.info("Scanning for Chromecasts") - hosts = pychromecast.discover_chromecasts() + # If no hosts given, scan for chromecasts + if not hosts: + logger.info("Scanning for Chromecasts") + hosts = pychromecast.discover_chromecasts() casts = {}