Skip to content

Instantly share code, notes, and snippets.

@AdamStrojek
Created February 24, 2019 15:56
Show Gist options
  • Save AdamStrojek/53502f703e48f26db37fe56cc8ad6cd4 to your computer and use it in GitHub Desktop.
Save AdamStrojek/53502f703e48f26db37fe56cc8ad6cd4 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, SUPPORT_BRIGHTNESS)
from homeassistant.const import CONF_HOST, CONF_NAME
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,
vol.Required(CONF_NAME): 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)
name = config.get(CONF_NAME)
# 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 switchBox")
return False
elif requests.get(device + "api/device/state").json()['device']['type'] != 'switchBox':
_LOGGER.error("Not a switchBox device")
# Add devices
devices = [device]
add_devices(SwitchBox(device, name) for device in devices)
class SwitchBox(Light):
"""Representation of an switchBox."""
def __init__(self, host, name):
"""Initialize a switchBox"""
self._host = host
self._name = name
self._state = None
@property
def name(self):
"""Return the display name of this light."""
return self._name
@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 requests
requests.get(self._host + "s/1")
def turn_off(self, **kwargs):
"""Instruct the light to turn off."""
import requests
requests.get(self._host + "s/0")
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._host + "api/device/state").json()
device_name = device_state['device']['deviceName']
state = bool(device_state['relays'][0]['state'])
self._state = state
self._name = device_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment