2019-02-14 04:35:12 +00:00
|
|
|
"""Support for LCN lights."""
|
2019-04-24 02:20:20 +00:00
|
|
|
import pypck
|
|
|
|
|
2018-12-28 11:39:06 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
ATTR_TRANSITION,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
|
|
|
SUPPORT_TRANSITION,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-12-28 11:39:06 +00:00
|
|
|
from homeassistant.const import CONF_ADDRESS
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
from . import LcnEntity
|
2019-03-21 05:56:46 +00:00
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_CONNECTIONS,
|
|
|
|
CONF_DIMMABLE,
|
|
|
|
CONF_OUTPUT,
|
|
|
|
CONF_TRANSITION,
|
|
|
|
DATA_LCN,
|
|
|
|
OUTPUT_PORTS,
|
|
|
|
)
|
2019-05-25 09:40:44 +00:00
|
|
|
from .helpers import get_connection
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2020-11-29 15:30:17 +00:00
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
2018-12-28 11:39:06 +00:00
|
|
|
|
2019-02-14 04:35:12 +00:00
|
|
|
async def async_setup_platform(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, hass_config, async_add_entities, discovery_info=None
|
|
|
|
):
|
2018-12-28 11:39:06 +00:00
|
|
|
"""Set up the LCN light platform."""
|
2019-01-20 23:49:28 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2018-12-28 11:39:06 +00:00
|
|
|
devices = []
|
|
|
|
for config in discovery_info:
|
|
|
|
address, connection_id = config[CONF_ADDRESS]
|
|
|
|
addr = pypck.lcn_addr.LcnAddr(*address)
|
|
|
|
connections = hass.data[DATA_LCN][CONF_CONNECTIONS]
|
|
|
|
connection = get_connection(connections, connection_id)
|
|
|
|
address_connection = connection.get_address_conn(addr)
|
|
|
|
|
2019-01-20 13:50:29 +00:00
|
|
|
if config[CONF_OUTPUT] in OUTPUT_PORTS:
|
|
|
|
device = LcnOutputLight(config, address_connection)
|
|
|
|
else: # in RELAY_PORTS
|
|
|
|
device = LcnRelayLight(config, address_connection)
|
|
|
|
|
|
|
|
devices.append(device)
|
|
|
|
|
2018-12-28 11:39:06 +00:00
|
|
|
async_add_entities(devices)
|
|
|
|
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
class LcnOutputLight(LcnEntity, LightEntity):
|
2018-12-28 11:39:06 +00:00
|
|
|
"""Representation of a LCN light for output ports."""
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
def __init__(self, config, device_connection):
|
2018-12-28 11:39:06 +00:00
|
|
|
"""Initialize the LCN light."""
|
2020-12-05 11:57:49 +00:00
|
|
|
super().__init__(config, device_connection)
|
2018-12-28 11:39:06 +00:00
|
|
|
|
2019-05-25 09:40:44 +00:00
|
|
|
self.output = pypck.lcn_defs.OutputPort[config[CONF_OUTPUT]]
|
2018-12-28 11:39:06 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._transition = pypck.lcn_defs.time_to_ramp_value(config[CONF_TRANSITION])
|
2018-12-28 11:39:06 +00:00
|
|
|
self.dimmable = config[CONF_DIMMABLE]
|
|
|
|
|
|
|
|
self._brightness = 255
|
|
|
|
self._is_on = None
|
|
|
|
self._is_dimming_to_zero = False
|
|
|
|
|
2019-01-20 13:50:29 +00:00
|
|
|
async def async_added_to_hass(self):
|
2018-12-28 11:39:06 +00:00
|
|
|
"""Run when entity about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
2021-01-06 17:01:06 +00:00
|
|
|
if not self.device_connection.is_group:
|
|
|
|
await self.device_connection.activate_status_request_handler(self.output)
|
2018-12-28 11:39:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
if self.dimmable:
|
2020-11-09 12:19:29 +00:00
|
|
|
return SUPPORT_TRANSITION | SUPPORT_BRIGHTNESS
|
|
|
|
return SUPPORT_TRANSITION
|
2018-12-28 11:39:06 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Return the brightness of this light between 0..255."""
|
|
|
|
return self._brightness
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return True if entity is on."""
|
|
|
|
return self._is_on
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Turn the entity on."""
|
|
|
|
if ATTR_BRIGHTNESS in kwargs:
|
2019-07-31 19:25:30 +00:00
|
|
|
percent = int(kwargs[ATTR_BRIGHTNESS] / 255.0 * 100)
|
2018-12-28 11:39:06 +00:00
|
|
|
else:
|
|
|
|
percent = 100
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
2019-05-25 09:40:44 +00:00
|
|
|
transition = pypck.lcn_defs.time_to_ramp_value(
|
2019-07-31 19:25:30 +00:00
|
|
|
kwargs[ATTR_TRANSITION] * 1000
|
|
|
|
)
|
2018-12-28 11:39:06 +00:00
|
|
|
else:
|
|
|
|
transition = self._transition
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
if not await self.device_connection.dim_output(
|
2020-11-29 15:30:17 +00:00
|
|
|
self.output.value, percent, transition
|
|
|
|
):
|
|
|
|
return
|
|
|
|
self._is_on = True
|
|
|
|
self._is_dimming_to_zero = False
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2018-12-28 11:39:06 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Turn the entity off."""
|
|
|
|
if ATTR_TRANSITION in kwargs:
|
2019-05-25 09:40:44 +00:00
|
|
|
transition = pypck.lcn_defs.time_to_ramp_value(
|
2019-07-31 19:25:30 +00:00
|
|
|
kwargs[ATTR_TRANSITION] * 1000
|
|
|
|
)
|
2018-12-28 11:39:06 +00:00
|
|
|
else:
|
|
|
|
transition = self._transition
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
if not await self.device_connection.dim_output(
|
2020-11-29 15:30:17 +00:00
|
|
|
self.output.value, 0, transition
|
|
|
|
):
|
|
|
|
return
|
2018-12-28 11:39:06 +00:00
|
|
|
self._is_dimming_to_zero = bool(transition)
|
2020-11-29 15:30:17 +00:00
|
|
|
self._is_on = False
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2018-12-28 11:39:06 +00:00
|
|
|
|
|
|
|
def input_received(self, input_obj):
|
|
|
|
"""Set light state when LCN input object (command) is received."""
|
2019-07-31 19:25:30 +00:00
|
|
|
if (
|
|
|
|
not isinstance(input_obj, pypck.inputs.ModStatusOutput)
|
|
|
|
or input_obj.get_output_id() != self.output.value
|
|
|
|
):
|
2018-12-28 11:39:06 +00:00
|
|
|
return
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._brightness = int(input_obj.get_percent() / 100.0 * 255)
|
2018-12-28 11:39:06 +00:00
|
|
|
if self.brightness == 0:
|
|
|
|
self._is_dimming_to_zero = False
|
|
|
|
if not self._is_dimming_to_zero:
|
|
|
|
self._is_on = self.brightness > 0
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|
2019-01-20 13:50:29 +00:00
|
|
|
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
class LcnRelayLight(LcnEntity, LightEntity):
|
2019-01-20 13:50:29 +00:00
|
|
|
"""Representation of a LCN light for relay ports."""
|
|
|
|
|
2020-12-05 11:57:49 +00:00
|
|
|
def __init__(self, config, device_connection):
|
2019-01-20 13:50:29 +00:00
|
|
|
"""Initialize the LCN light."""
|
2020-12-05 11:57:49 +00:00
|
|
|
super().__init__(config, device_connection)
|
2019-01-20 13:50:29 +00:00
|
|
|
|
2019-05-25 09:40:44 +00:00
|
|
|
self.output = pypck.lcn_defs.RelayPort[config[CONF_OUTPUT]]
|
2019-01-20 13:50:29 +00:00
|
|
|
|
|
|
|
self._is_on = None
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Run when entity about to be added to hass."""
|
|
|
|
await super().async_added_to_hass()
|
2021-01-06 17:01:06 +00:00
|
|
|
if not self.device_connection.is_group:
|
|
|
|
await self.device_connection.activate_status_request_handler(self.output)
|
2019-01-20 13:50:29 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""Return True if entity is on."""
|
|
|
|
return self._is_on
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
|
|
"""Turn the entity on."""
|
2019-05-25 09:40:44 +00:00
|
|
|
states = [pypck.lcn_defs.RelayStateModifier.NOCHANGE] * 8
|
|
|
|
states[self.output.value] = pypck.lcn_defs.RelayStateModifier.ON
|
2020-12-05 11:57:49 +00:00
|
|
|
if not await self.device_connection.control_relays(states):
|
2020-11-29 15:30:17 +00:00
|
|
|
return
|
|
|
|
self._is_on = True
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2019-01-20 13:50:29 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
|
|
"""Turn the entity off."""
|
2019-05-25 09:40:44 +00:00
|
|
|
states = [pypck.lcn_defs.RelayStateModifier.NOCHANGE] * 8
|
|
|
|
states[self.output.value] = pypck.lcn_defs.RelayStateModifier.OFF
|
2020-12-05 11:57:49 +00:00
|
|
|
if not await self.device_connection.control_relays(states):
|
2020-11-29 15:30:17 +00:00
|
|
|
return
|
|
|
|
self._is_on = False
|
2020-04-03 07:34:50 +00:00
|
|
|
self.async_write_ha_state()
|
2019-01-20 13:50:29 +00:00
|
|
|
|
|
|
|
def input_received(self, input_obj):
|
|
|
|
"""Set light state when LCN input object (command) is received."""
|
2019-05-25 09:40:44 +00:00
|
|
|
if not isinstance(input_obj, pypck.inputs.ModStatusRelays):
|
2019-01-20 13:50:29 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self._is_on = input_obj.get_state(self.output.value)
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|