Insteon support for brightness (#2169)

* Insteon support for brightness

* Farcy fix for unused constants.

* Remove unused constant and fix whitespace.

* Prevent toggle switches from jumping between states.

* 255 not 256
This commit is contained in:
Warren Konkel 2016-05-29 14:31:14 -07:00 committed by Paulus Schoutsen
parent 8a577c8e0d
commit 952436aa0b
2 changed files with 52 additions and 42 deletions

View File

@ -11,7 +11,6 @@ from homeassistant.const import (
ATTR_DISCOVERED, ATTR_SERVICE, CONF_API_KEY, CONF_PASSWORD, CONF_USERNAME, ATTR_DISCOVERED, ATTR_SERVICE, CONF_API_KEY, CONF_PASSWORD, CONF_USERNAME,
EVENT_PLATFORM_DISCOVERED) EVENT_PLATFORM_DISCOVERED)
from homeassistant.helpers import validate_config from homeassistant.helpers import validate_config
from homeassistant.helpers.entity import ToggleEntity
from homeassistant.loader import get_component from homeassistant.loader import get_component
DOMAIN = "insteon_hub" DOMAIN = "insteon_hub"
@ -53,43 +52,3 @@ def setup(hass, config):
EVENT_PLATFORM_DISCOVERED, EVENT_PLATFORM_DISCOVERED,
{ATTR_SERVICE: discovery, ATTR_DISCOVERED: {}}) {ATTR_SERVICE: discovery, ATTR_DISCOVERED: {}})
return True return True
class InsteonToggleDevice(ToggleEntity):
"""An abstract Class for an Insteon node."""
def __init__(self, node):
"""Initialize the device."""
self.node = node
self._value = 0
@property
def name(self):
"""Return the the name of the node."""
return self.node.DeviceName
@property
def unique_id(self):
"""Return the ID of this insteon node."""
return self.node.DeviceID
def update(self):
"""Update state of the sensor."""
resp = self.node.send_command('get_status', wait=True)
try:
self._value = resp['response']['level']
except KeyError:
pass
@property
def is_on(self):
"""Return the boolean response if the node is on."""
return self._value != 0
def turn_on(self, **kwargs):
"""Turn device on."""
self.node.send_command('on')
def turn_off(self, **kwargs):
"""Turn device off."""
self.node.send_command('off')

View File

@ -4,7 +4,8 @@ Support for Insteon Hub lights.
For more details about this platform, please refer to the documentation at For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/insteon_hub/ https://home-assistant.io/components/insteon_hub/
""" """
from homeassistant.components.insteon_hub import INSTEON, InsteonToggleDevice from homeassistant.components.insteon_hub import INSTEON
from homeassistant.components.light import ATTR_BRIGHTNESS, Light
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
@ -16,3 +17,53 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if device.DeviceCategory == "Dimmable Lighting Control": if device.DeviceCategory == "Dimmable Lighting Control":
devs.append(InsteonToggleDevice(device)) devs.append(InsteonToggleDevice(device))
add_devices(devs) add_devices(devs)
class InsteonToggleDevice(Light):
"""An abstract Class for an Insteon node."""
def __init__(self, node):
"""Initialize the device."""
self.node = node
self._value = 0
@property
def name(self):
"""Return the the name of the node."""
return self.node.DeviceName
@property
def unique_id(self):
"""Return the ID of this insteon node."""
return self.node.DeviceID
@property
def brightness(self):
"""Return the brightness of this light between 0..255."""
return self._value / 100 * 255
def update(self):
"""Update state of the sensor."""
resp = self.node.send_command('get_status', wait=True)
try:
self._value = resp['response']['level']
except KeyError:
pass
@property
def is_on(self):
"""Return the boolean response if the node is on."""
return self._value != 0
def turn_on(self, **kwargs):
"""Turn device on."""
if ATTR_BRIGHTNESS in kwargs:
self._value = kwargs[ATTR_BRIGHTNESS] / 255 * 100
self.node.send_command('on', self._value)
else:
self._value = 100
self.node.send_command('on')
def turn_off(self, **kwargs):
"""Turn device off."""
self.node.send_command('off')