Last active
October 3, 2017 21:01
-
-
Save zen/b34afc581dc76b684363f9d8b8b7aa74 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 logging | |
import voluptuous as vol | |
# Import the device class from the component that you want to support | |
from homeassistant.components.light import ATTR_BRIGHTNESS, Light, PLATFORM_SCHEMA | |
from homeassistant.const import CONF_HOST | |
import homeassistant.helpers.config_validation as cv | |
# Home Assistant depends on 3rd party packages for API specific code. | |
REQUIREMENTS = ['urllib3==1.22'] | |
_LOGGER = logging.getLogger(__name__) | |
# Validation of the user's configuration | |
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | |
vol.Required(CONF_HOST): cv.string, | |
}) | |
def setup_platform(hass, config, add_devices, discovery_info=None): | |
"""Setup the wLightBoxS platform.""" | |
import urllib3 | |
import json | |
# Assign configuration variables. The configuration check takes care they are | |
# present. | |
host = config.get(CONF_HOST) | |
# Setup connection with device | |
device = 'http://{}/'.format(host) | |
# Verify that passed in configuration works | |
if urllib3.urlopen(device).getcode() != 200: | |
_LOGGER.error("Could not connect to wLightBoxS") | |
return False | |
elif json.load(urllib3.urlopen(device + "api/device/state"))['device']['type'] != 'wLightBoxS': | |
_LOGGER.error("Not a wLightBoxS device") | |
# Add devices | |
add_devices(wLightBoxS(device)) | |
class wLightBoxS(Light): | |
"""Representation of an wLightBoxS.""" | |
def __init__(self, light): | |
"""Initialize a wLightBoxS.""" | |
''' | |
deviceState = json.load(urllib3.urlopen(light + "api/device/state")) | |
deviceName = deviceState['device']['deviceName'] | |
brightnessRaw = deviceState['light']['currentColor'] | |
brightness = int(brightnessRaw, 16) | |
if brightness > 0: | |
state = True | |
else: | |
state = False | |
''' | |
self._light = light | |
self._name = None | |
self._state = None | |
self._brightness = None | |
@property | |
def name(self): | |
"""Return the display name of this light.""" | |
return self._name | |
@property | |
def brightness(self): | |
"""Return the brightness of the light. | |
""" | |
deviceState = json.load(urllib3.urlopen(self._light + "api/device/state")) | |
brightnessRaw = deviceState['light']['currentColor'] | |
brightness = int(brightnessRaw, 16) | |
self._brightness = brightness | |
return self._brightness | |
@property | |
def is_on(self): | |
"""Return true if light is on.""" | |
deviceState = json.load(urllib3.urlopen(light + "api/device/state")) | |
brightnessRaw = deviceState['light']['currentColor'] | |
brightness = int(brightnessRaw, 16) | |
if brightness > 0: | |
state = True | |
else: | |
state = False | |
self._state = state | |
return self._state | |
def turn_on(self, **kwargs): | |
"""Instruct the light to turn on. | |
""" | |
brightness = kwargs.get(ATTR_BRIGHTNESS, 255) | |
brigthnessHex = '{0:02x}'.format(setBrightness) | |
json.load(urllib3.urlopen(self._light + "s/" + brigthnessHex)) | |
def turn_off(self, **kwargs): | |
"""Instruct the light to turn off.""" | |
json.load(urllib3.urlopen(self._light + "s/" + brigthnessHex)) | |
def update(self): | |
"""Fetch new state data for this light. | |
This is the only method that should fetch new data for Home Assistant. | |
""" | |
deviceState = json.load(urllib3.urlopen(self._light + "api/device/state")) | |
deviceName = deviceState['device']['deviceName'] | |
brightnessRaw = deviceState['light']['currentColor'] | |
brightness = int(brightnessRaw, 16) | |
if brightness > 0: | |
state = True | |
else: | |
state = False | |
self._state = state | |
self._brightness = brightness |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment