Skip to content

Instantly share code, notes, and snippets.

/dmx.py Secret

Created December 27, 2017 21:08
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 anonymous/bfdc0cfbfedfcf70d6a3826b14b230ee to your computer and use it in GitHub Desktop.
Save anonymous/bfdc0cfbfedfcf70d6a3826b14b230ee 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 Light, PLATFORM_SCHEMA, SUPPORT_RGB_COLOR, SUPPORT_TRANSITION
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_USERNAME, CONF_PASSWORD
import homeassistant.helpers.config_validation as cv
# Home Assistant depends on 3rd party packages for API specific code.
REQUIREMENTS = ['requests']
_LOGGER = logging.getLogger(__name__)
# Validation of the user's configuration
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
vol.Required(CONF_HOST): cv.url,
})
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Awesome Light platform."""
import requests
# Assign configuration variables. The configuration check takes care they are
# present.
host = config.get(CONF_HOST)
session = requests.Session()
r = session.get(host)
add_devices(DMXLight(index, session, host) for index,light in enumerate(r.json()["lamps"]))
class DMXLight(Light):
"""Representation of an DMX Light."""
def __init__(self, index, session, host):
self._index = index
self._state = None
self._session = session
self._host = host
@property
def name(self):
"""Return the display name of this light."""
return "Lamp {}".format(self._index)
@property
def is_on(self):
"""Return true if light is on."""
return self._state != {"red": 0, "green": 0, "blue": 0}
@property
def rgb(self):
return [self._state["red"], self._state["green"], self._state["blue"]]
@property
def supported_features(self):
"""Flag supported features."""
return [SUPPORT_RGB_COLOR, SUPPORT_TRANSITION]
def turn_on(self, rgb_color=None, transition=None, **kwargs):
"""Instruct the light to turn on.
You can skip the brightness part if your light does not support
brightness control.
"""
print("turn_on", rgb_color)
if not rgb_color:
rgb_color = [255, 255, 255]
if not transition:
transition = 0
r = self._session.post(self._host, data={"lamp_id": self._index, "red": rgb_color[0], "green": rgb_color[1], "blue": rgb_color[2], "fade": transition})
def turn_off(self, **kwargs):
"""Instruct the light to turn off."""
r = self._session.post(self._host, data={"lamp_id": self._index, "red": 0, "green": 0, "blue": 0, "fade": 0})
def update(self):
"""Fetch new state data for this light.
This is the only method that should fetch new data for Home Assistant.
"""
#self._light.update()
#self._state = self._light.is_on()
#self._brightness = self._light.brightness
r = self._session.get(self._host)
self._state = r.json()["lamps"][self._index]
print(self._state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment