From caa16da5c5a345136f44469a0afbe87ebd0a0eac Mon Sep 17 00:00:00 2001 From: David Fiel Date: Thu, 25 Jan 2018 18:00:32 -0500 Subject: [PATCH] Multi-Room Support for Greenwave Reality (#11364) * Multi-Room Support for Greenwave Reality * PEP8 Line Too Long * Update requirements_all.txt * Shared State Object Shared State Object added, and implemented new function of greenwavereality to abstract complex runtimes. * Update requirements_all.txt * Lint issues * Rewrite method to not trigger "Method could be a function" * Remove unnecessary arguments for update call --- homeassistant/components/light/greenwave.py | 69 +++++++++++++++------ requirements_all.txt | 2 +- 2 files changed, 51 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/light/greenwave.py b/homeassistant/components/light/greenwave.py index c8f06a3d0f8..5ad7fd4c317 100644 --- a/homeassistant/components/light/greenwave.py +++ b/homeassistant/components/light/greenwave.py @@ -5,6 +5,7 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/light.greenwave/ """ import logging +from datetime import timedelta import voluptuous as vol @@ -12,20 +13,22 @@ from homeassistant.components.light import ( ATTR_BRIGHTNESS, PLATFORM_SCHEMA, SUPPORT_BRIGHTNESS, Light) from homeassistant.const import CONF_HOST import homeassistant.helpers.config_validation as cv +from homeassistant.util import Throttle -REQUIREMENTS = ['greenwavereality==0.2.9'] - +REQUIREMENTS = ['greenwavereality==0.5.1'] _LOGGER = logging.getLogger(__name__) CONF_VERSION = 'version' -SUPPORTED_FEATURES = (SUPPORT_BRIGHTNESS) +SUPPORTED_FEATURES = SUPPORT_BRIGHTNESS PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ vol.Required(CONF_HOST): cv.string, vol.Required(CONF_VERSION): cv.positive_int, }) +MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1) + def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the Greenwave Reality Platform.""" @@ -39,29 +42,35 @@ def setup_platform(hass, config, add_devices, discovery_info=None): token = tokenfile.read() tokenfile.close() else: - token = greenwave.grab_token(host, 'hass', 'homeassistant') + try: + token = greenwave.grab_token(host, 'hass', 'homeassistant') + except PermissionError: + _LOGGER.error('The Gateway Is Not In Sync Mode') + raise 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) + bulbs = greenwave.grab_bulbs(host, token) + add_devices(GreenwaveLight(device, host, token, GatewayData(host, token)) + for device in bulbs.values()) class GreenwaveLight(Light): """Representation of an Greenwave Reality Light.""" - def __init__(self, light, host, token): + def __init__(self, light, host, token, gatewaydata): """Initialize a Greenwave Reality Light.""" import greenwavereality as greenwave - self._did = light['did'] + self._did = int(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 + self._token = token + self._gatewaydata = gatewaydata @property def supported_features(self): @@ -94,22 +103,44 @@ class GreenwaveLight(Light): 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) + 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) + 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) + self._gatewaydata.update() + bulbs = self._gatewaydata.greenwave - 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'] + self._state = int(bulbs[self._did]['state']) + self._brightness = greenwave.hass_brightness(bulbs[self._did]) + self._online = greenwave.check_online(bulbs[self._did]) + self._name = bulbs[self._did]['name'] + + +class GatewayData(object): + """Handle Gateway data and limit updates.""" + + def __init__(self, host, token): + """Initialize the data object.""" + import greenwavereality as greenwave + self._host = host + self._token = token + self._greenwave = greenwave.grab_bulbs(host, token) + + @property + def greenwave(self): + """Return Gateway API object.""" + return self._greenwave + + @Throttle(MIN_TIME_BETWEEN_UPDATES) + def update(self): + """Get the latest data from the gateway.""" + import greenwavereality as greenwave + self._greenwave = greenwave.grab_bulbs(self._host, self._token) + return self._greenwave diff --git a/requirements_all.txt b/requirements_all.txt index 4d42e2b5db5..1f41333c7e1 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -322,7 +322,7 @@ googlemaps==2.5.1 gps3==0.33.3 # homeassistant.components.light.greenwave -greenwavereality==0.2.9 +greenwavereality==0.5.1 # homeassistant.components.media_player.gstreamer gstreamer-player==1.1.0