2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Greenwave Reality (TCP Connected) lights."""
|
2017-12-24 00:11:45 +00:00
|
|
|
import logging
|
2018-01-25 23:00:32 +00:00
|
|
|
from datetime import timedelta
|
2017-12-24 00:11:45 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.light import (
|
2018-01-21 06:35:38 +00:00
|
|
|
ATTR_BRIGHTNESS, PLATFORM_SCHEMA, SUPPORT_BRIGHTNESS, Light)
|
2017-12-24 00:11:45 +00:00
|
|
|
from homeassistant.const import CONF_HOST
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2018-01-25 23:00:32 +00:00
|
|
|
from homeassistant.util import Throttle
|
2017-12-24 00:11:45 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2018-01-21 06:35:38 +00:00
|
|
|
CONF_VERSION = 'version'
|
|
|
|
|
2018-01-25 23:00:32 +00:00
|
|
|
SUPPORTED_FEATURES = SUPPORT_BRIGHTNESS
|
2018-01-21 06:35:38 +00:00
|
|
|
|
2017-12-24 00:11:45 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_HOST): cv.string,
|
2018-01-21 06:35:38 +00:00
|
|
|
vol.Required(CONF_VERSION): cv.positive_int,
|
2017-12-24 00:11:45 +00:00
|
|
|
})
|
|
|
|
|
2018-01-25 23:00:32 +00:00
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
|
|
|
|
|
2017-12-24 00:11:45 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2018-01-21 06:35:38 +00:00
|
|
|
"""Set up the Greenwave Reality Platform."""
|
2017-12-24 00:11:45 +00:00
|
|
|
import greenwavereality as greenwave
|
|
|
|
import os
|
|
|
|
host = config.get(CONF_HOST)
|
|
|
|
tokenfile = hass.config.path('.greenwave')
|
2018-01-21 06:35:38 +00:00
|
|
|
if config.get(CONF_VERSION) == 3:
|
2017-12-24 00:11:45 +00:00
|
|
|
if os.path.exists(tokenfile):
|
2018-01-31 10:30:48 +00:00
|
|
|
with open(tokenfile) as tokenfile:
|
|
|
|
token = tokenfile.read()
|
2017-12-24 00:11:45 +00:00
|
|
|
else:
|
2018-01-25 23:00:32 +00:00
|
|
|
try:
|
|
|
|
token = greenwave.grab_token(host, 'hass', 'homeassistant')
|
|
|
|
except PermissionError:
|
|
|
|
_LOGGER.error('The Gateway Is Not In Sync Mode')
|
|
|
|
raise
|
2018-01-31 10:30:48 +00:00
|
|
|
with open(tokenfile, "w+") as tokenfile:
|
|
|
|
tokenfile.write(token)
|
2017-12-24 00:11:45 +00:00
|
|
|
else:
|
|
|
|
token = None
|
2018-01-25 23:00:32 +00:00
|
|
|
bulbs = greenwave.grab_bulbs(host, token)
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(GreenwaveLight(device, host, token, GatewayData(host, token))
|
|
|
|
for device in bulbs.values())
|
2017-12-24 00:11:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GreenwaveLight(Light):
|
|
|
|
"""Representation of an Greenwave Reality Light."""
|
|
|
|
|
2018-01-25 23:00:32 +00:00
|
|
|
def __init__(self, light, host, token, gatewaydata):
|
2017-12-24 00:11:45 +00:00
|
|
|
"""Initialize a Greenwave Reality Light."""
|
|
|
|
import greenwavereality as greenwave
|
2018-01-25 23:00:32 +00:00
|
|
|
self._did = int(light['did'])
|
2017-12-24 00:11:45 +00:00
|
|
|
self._name = light['name']
|
|
|
|
self._state = int(light['state'])
|
|
|
|
self._brightness = greenwave.hass_brightness(light)
|
|
|
|
self._host = host
|
|
|
|
self._online = greenwave.check_online(light)
|
2018-01-25 23:00:32 +00:00
|
|
|
self._token = token
|
|
|
|
self._gatewaydata = gatewaydata
|
2017-12-24 00:11:45 +00:00
|
|
|
|
|
|
|
@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,
|
2018-01-25 23:00:32 +00:00
|
|
|
temp_brightness, self._token)
|
|
|
|
greenwave.turn_on(self._host, self._did, self._token)
|
2017-12-24 00:11:45 +00:00
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Instruct the light to turn off."""
|
|
|
|
import greenwavereality as greenwave
|
2018-01-25 23:00:32 +00:00
|
|
|
greenwave.turn_off(self._host, self._did, self._token)
|
2017-12-24 00:11:45 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Fetch new state data for this light."""
|
|
|
|
import greenwavereality as greenwave
|
2018-01-25 23:00:32 +00:00
|
|
|
self._gatewaydata.update()
|
|
|
|
bulbs = self._gatewaydata.greenwave
|
|
|
|
|
|
|
|
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']
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class GatewayData:
|
2018-01-25 23:00:32 +00:00
|
|
|
"""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
|