Skip to content

Instantly share code, notes, and snippets.

@zen
Created October 3, 2017 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zen/e3e7dbb43ebdf8895012e54f6c95e1c8 to your computer and use it in GitHub Desktop.
Save zen/e3e7dbb43ebdf8895012e54f6c95e1c8 to your computer and use it in GitHub Desktop.
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 = ['requests==2.18.4']
_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 requests
# 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
status_code = requests.get(device).status_code
if status_code != 200:
_LOGGER.error("Could not connect to wLightBoxS")
return False
elif requests.get(device + "api/device/state").json()['device']['type'] != 'wLightBoxS':
_LOGGER.error("Not a wLightBoxS device")
# Add devices
devices = [device]
add_devices(wLightBoxS(device) for device in devices)
# add_devices(AwesomeLight(light) for light in hub.lights())
class wLightBoxS(Light):
"""Representation of an wLightBoxS."""
def __init__(self, light):
"""Initialize a wLightBoxS."""
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.
"""
import requests
device_state = requests.get(self._light + "api/device/state")
brightness_raw = device_state.json()['light']['currentColor']
brightness = int(brightness_raw, 16)
self._brightness = brightness
return self._brightness
@property
def is_on(self):
"""Return true if light is on."""
import requests
device_state = requests.get(self._light + "api/device/state")
brightness_raw = device_state.json()['light']['currentColor']
brightness = int(brightness_raw, 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.
"""
import requests
brightness = kwargs.get(ATTR_BRIGHTNESS, 255)
brigthness_hex = '{0:02x}'.format(brightness)
requests.get(self._light + "s/" + brigthness_hex)
def turn_off(self, **kwargs):
"""Instruct the light to turn off."""
import requests
requests.get(self._light + "s/00")
def update(self):
"""Fetch new state data for this light.
This is the only method that should fetch new data for Home Assistant.
"""
import requests
device_state = requests.get(self._light + "api/device/state")
device_name = device_state.json()['device']['deviceName']
brightness_raw = device_state.json()['light']['currentColor']
brightness = int(brightness_raw, 16)
if brightness > 0:
state = True
else:
state = False
self._state = state
self._brightness = brightness
self._name = device_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment