diff --git a/.coveragerc b/.coveragerc index fba75b62bfe567ecf4545fe8a8dcb362a4e18d9d..4751ddce2190d22b1c9beeff2f7b8b020351135f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -365,6 +365,7 @@ omit = homeassistant/components/light/decora.py homeassistant/components/light/decora_wifi.py homeassistant/components/light/flux_led.py + homeassistant/components/light/greenwave.py homeassistant/components/light/hue.py homeassistant/components/light/hyperion.py homeassistant/components/light/lifx.py diff --git a/homeassistant/components/light/greenwave.py b/homeassistant/components/light/greenwave.py new file mode 100644 index 0000000000000000000000000000000000000000..0e99a49eaa911e7619be80e8f462503f2f40a456 --- /dev/null +++ b/homeassistant/components/light/greenwave.py @@ -0,0 +1,112 @@ +""" +Support for Greenwave Reality (TCP Connected) lights. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/light.greenwave/ +""" +import logging + +import voluptuous as vol + +from homeassistant.components.light import ( + ATTR_BRIGHTNESS, Light, PLATFORM_SCHEMA, SUPPORT_BRIGHTNESS) +from homeassistant.const import CONF_HOST +import homeassistant.helpers.config_validation as cv + +SUPPORTED_FEATURES = (SUPPORT_BRIGHTNESS) + +REQUIREMENTS = ['greenwavereality==0.2.9'] +_LOGGER = logging.getLogger(__name__) + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_HOST): cv.string, + vol.Required("version"): cv.positive_int, +}) + + +def setup_platform(hass, config, add_devices, discovery_info=None): + """Setup Greenwave Reality Platform.""" + import greenwavereality as greenwave + import os + host = config.get(CONF_HOST) + tokenfile = hass.config.path('.greenwave') + if config.get("version") == 3: + if os.path.exists(tokenfile): + tokenfile = open(tokenfile) + token = tokenfile.read() + tokenfile.close() + else: + token = greenwave.grab_token(host, 'hass', 'homeassistant') + tokenfile = open(tokenfile, "w+") + tokenfile.write(token) + tokenfile.close() + else: + token = None + doc = greenwave.grab_xml(host, token) + add_devices(GreenwaveLight(device, host, token) for device in doc) + + +class GreenwaveLight(Light): + """Representation of an Greenwave Reality Light.""" + + def __init__(self, light, host, token): + """Initialize a Greenwave Reality Light.""" + import greenwavereality as greenwave + self._did = light['did'] + self._name = light['name'] + self._state = int(light['state']) + self._brightness = greenwave.hass_brightness(light) + self._host = host + self._online = greenwave.check_online(light) + self.token = token + + @property + def supported_features(self): + """Flag supported features.""" + return SUPPORTED_FEATURES + + @property + def available(self): + """Return True if entity is available.""" + return self._online + + @property + def name(self): + """Return the display name of this light.""" + return self._name + + @property + def brightness(self): + """Return the brightness of the light.""" + return self._brightness + + @property + def is_on(self): + """Return true if light is on.""" + return self._state + + def turn_on(self, **kwargs): + """Instruct the light to turn on.""" + import greenwavereality as greenwave + temp_brightness = int((kwargs.get(ATTR_BRIGHTNESS, 255) + / 255) * 100) + greenwave.set_brightness(self._host, self._did, + temp_brightness, self.token) + greenwave.turn_on(self._host, self._did, self.token) + + def turn_off(self, **kwargs): + """Instruct the light to turn off.""" + import greenwavereality as greenwave + greenwave.turn_off(self._host, self._did, self.token) + + def update(self): + """Fetch new state data for this light.""" + import greenwavereality as greenwave + doc = greenwave.grab_xml(self._host, self.token) + + for device in doc: + if device['did'] == self._did: + self._state = int(device['state']) + self._brightness = greenwave.hass_brightness(device) + self._online = greenwave.check_online(device) + self._name = device['name'] diff --git a/requirements_all.txt b/requirements_all.txt index 0c9f63b327df411face08b46bf32b0e563b00122..5dc034b9989ad8973d9881eef29cc919d6ad5f27 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -315,6 +315,9 @@ googlemaps==2.5.1 # homeassistant.components.sensor.gpsd gps3==0.33.3 +# homeassistant.components.light.greenwave +greenwavereality==0.2.9 + # homeassistant.components.media_player.gstreamer gstreamer-player==1.1.0