Implement KNX dimming functionality (#8371)
* Implement KNX dimming functionality * Formatting * Formatting * Formatting
This commit is contained in:
parent
074e31bcf9
commit
ecf3a9cb36
1 changed files with 70 additions and 7 deletions
|
@ -4,15 +4,22 @@ Support KNX Lighting actuators.
|
||||||
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/Light.knx/
|
https://home-assistant.io/components/Light.knx/
|
||||||
"""
|
"""
|
||||||
|
import logging
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.knx import (KNXConfig, KNXGroupAddress)
|
from homeassistant.components.knx import (KNXConfig, KNXMultiAddressDevice)
|
||||||
from homeassistant.components.light import (Light, PLATFORM_SCHEMA)
|
from homeassistant.components.light import (Light, PLATFORM_SCHEMA,
|
||||||
|
SUPPORT_BRIGHTNESS,
|
||||||
|
ATTR_BRIGHTNESS)
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
CONF_ADDRESS = 'address'
|
CONF_ADDRESS = 'address'
|
||||||
CONF_STATE_ADDRESS = 'state_address'
|
CONF_STATE_ADDRESS = 'state_address'
|
||||||
|
CONF_BRIGHTNESS_ADDRESS = 'brightness_address'
|
||||||
|
CONF_BRIGHTNESS_STATE_ADDRESS = 'brightness_state_address'
|
||||||
|
|
||||||
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFAULT_NAME = 'KNX Light'
|
DEFAULT_NAME = 'KNX Light'
|
||||||
DEPENDENCIES = ['knx']
|
DEPENDENCIES = ['knx']
|
||||||
|
@ -21,6 +28,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||||
vol.Required(CONF_ADDRESS): cv.string,
|
vol.Required(CONF_ADDRESS): cv.string,
|
||||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||||
vol.Optional(CONF_STATE_ADDRESS): cv.string,
|
vol.Optional(CONF_STATE_ADDRESS): cv.string,
|
||||||
|
vol.Optional(CONF_BRIGHTNESS_ADDRESS): cv.string,
|
||||||
|
vol.Optional(CONF_BRIGHTNESS_STATE_ADDRESS): cv.string,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
@ -29,16 +38,41 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
add_devices([KNXLight(hass, KNXConfig(config))])
|
add_devices([KNXLight(hass, KNXConfig(config))])
|
||||||
|
|
||||||
|
|
||||||
class KNXLight(KNXGroupAddress, Light):
|
class KNXLight(KNXMultiAddressDevice, Light):
|
||||||
"""Representation of a KNX Light device."""
|
"""Representation of a KNX Light device."""
|
||||||
|
|
||||||
|
def __init__(self, hass, config):
|
||||||
|
"""Initialize the cover."""
|
||||||
|
KNXMultiAddressDevice.__init__(
|
||||||
|
self, hass, config,
|
||||||
|
[], # required
|
||||||
|
optional=['state', 'brightness', 'brightness_state']
|
||||||
|
)
|
||||||
|
self._hass = hass
|
||||||
|
self._supported_features = 0
|
||||||
|
|
||||||
|
if CONF_BRIGHTNESS_ADDRESS in config.config:
|
||||||
|
_LOGGER.debug("%s is dimmable", self.name)
|
||||||
|
self._supported_features = self._supported_features | \
|
||||||
|
SUPPORT_BRIGHTNESS
|
||||||
|
self._brightness = None
|
||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the switch on.
|
"""Turn the switch on.
|
||||||
|
|
||||||
This sends a value 1 to the group address of the device
|
This sends a value 1 to the group address of the device
|
||||||
"""
|
"""
|
||||||
self.group_write(1)
|
_LOGGER.debug("%s: turn on", self.name)
|
||||||
self._state = [1]
|
self.set_value('base', [1])
|
||||||
|
self._state = 1
|
||||||
|
|
||||||
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
|
self._brightness = kwargs[ATTR_BRIGHTNESS]
|
||||||
|
_LOGGER.debug("turn_on requested brightness for light: %s is: %s ",
|
||||||
|
self.name, self._brightness)
|
||||||
|
assert self._brightness <= 255
|
||||||
|
self.set_value("brightness", [self._brightness])
|
||||||
|
|
||||||
if not self.should_poll:
|
if not self.should_poll:
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
|
@ -47,7 +81,36 @@ class KNXLight(KNXGroupAddress, Light):
|
||||||
|
|
||||||
This sends a value 1 to the group address of the device
|
This sends a value 1 to the group address of the device
|
||||||
"""
|
"""
|
||||||
self.group_write(0)
|
_LOGGER.debug("%s: turn off", self.name)
|
||||||
self._state = [0]
|
self.set_value('base', [0])
|
||||||
|
self._state = 0
|
||||||
if not self.should_poll:
|
if not self.should_poll:
|
||||||
self.schedule_update_ha_state()
|
self.schedule_update_ha_state()
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_on(self):
|
||||||
|
"""Return True if the value is not 0 is on, else False."""
|
||||||
|
return self._state != 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def supported_features(self):
|
||||||
|
"""Flag supported features."""
|
||||||
|
return self._supported_features
|
||||||
|
|
||||||
|
def update(self):
|
||||||
|
"""Update device state."""
|
||||||
|
super().update()
|
||||||
|
if self.has_attribute('brightness_state'):
|
||||||
|
value = self.value('brightness_state')
|
||||||
|
if value is not None:
|
||||||
|
self._brightness = int.from_bytes(value, byteorder='little')
|
||||||
|
_LOGGER.debug("%s: brightness = %d",
|
||||||
|
self.name, self._brightness)
|
||||||
|
|
||||||
|
if self.has_attribute('state'):
|
||||||
|
self._state = self.value("state")[0]
|
||||||
|
_LOGGER.debug("%s: state = %d", self.name, self._state)
|
||||||
|
|
||||||
|
def should_poll(self):
|
||||||
|
"""No polling needed for a KNX light."""
|
||||||
|
return False
|
||||||
|
|
Loading…
Add table
Reference in a new issue