Created
October 25, 2016 10:56
-
-
Save sdague/f6b35adc33032d9da0232fc670970a71 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import logging | |
# The domain of your component. Should be equal to the name of your component. | |
DOMAIN = 'hue' | |
_LOGGER = logging.getLogger(__name__) | |
_CONFIGURED_BRIDGE = None | |
def _find_host_from_config(hass, filename): | |
"""Attempt to detect host based on existing configuration.""" | |
path = hass.config.path(filename) | |
if not os.path.isfile(path): | |
return None | |
try: | |
with open(path) as inp: | |
return next(json.loads(''.join(inp)).keys().__iter__()) | |
except (ValueError, AttributeError, StopIteration): | |
# ValueError if can't parse as JSON | |
# AttributeError if JSON value is not a dict | |
# StopIteration if no keys | |
return None | |
def setup(hass, config): | |
"""Setup is called when Home Assistant is loading our component.""" | |
import phue | |
path = hass.config.path('phue.conf') | |
host = _find_host_from_config(hass, 'phue.con') | |
bridge = phue.Bridge(host, config_file_path=path) | |
def handle_run_scene(call): | |
group = call.data.get('group', '') | |
scene = call.data.get('scene', '') | |
if group and scene: | |
bridge.run_scene(group, scene) | |
hass.services.register(DOMAIN, 'run_scene', handle_run_scene) | |
# Return boolean to indicate that initialization was successfully. | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment