2019-02-13 20:21:14 +00:00
|
|
|
"""Support for EnOcean light sources."""
|
2016-05-29 21:28:03 +00:00
|
|
|
import math
|
|
|
|
|
2016-08-25 04:35:09 +00:00
|
|
|
import voluptuous as vol
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
from homeassistant.components.light import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BRIGHTNESS,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SUPPORT_BRIGHTNESS,
|
2020-04-26 16:49:41 +00:00
|
|
|
LightEntity,
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2019-04-24 22:30:46 +00:00
|
|
|
from homeassistant.const import CONF_ID, CONF_NAME
|
2016-08-25 04:35:09 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2020-07-09 00:46:38 +00:00
|
|
|
from .device import EnOceanEntity
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_SENDER_ID = "sender_id"
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "EnOcean Light"
|
2016-08-16 06:07:07 +00:00
|
|
|
SUPPORT_ENOCEAN = SUPPORT_BRIGHTNESS
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_ID, default=[]): vol.All(cv.ensure_list, [vol.Coerce(int)]),
|
|
|
|
vol.Required(CONF_SENDER_ID): vol.All(cv.ensure_list, [vol.Coerce(int)]),
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-08-25 04:35:09 +00:00
|
|
|
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-05-01 03:10:08 +00:00
|
|
|
"""Set up the EnOcean light platform."""
|
2016-08-25 04:35:09 +00:00
|
|
|
sender_id = config.get(CONF_SENDER_ID)
|
2019-04-24 22:30:46 +00:00
|
|
|
dev_name = config.get(CONF_NAME)
|
2016-08-25 04:35:09 +00:00
|
|
|
dev_id = config.get(CONF_ID)
|
2016-05-29 21:28:03 +00:00
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
add_entities([EnOceanLight(sender_id, dev_id, dev_name)])
|
2016-05-29 21:28:03 +00:00
|
|
|
|
|
|
|
|
2020-07-09 00:46:38 +00:00
|
|
|
class EnOceanLight(EnOceanEntity, LightEntity):
|
2016-05-29 21:28:03 +00:00
|
|
|
"""Representation of an EnOcean light source."""
|
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
def __init__(self, sender_id, dev_id, dev_name):
|
2016-05-29 21:28:03 +00:00
|
|
|
"""Initialize the EnOcean light source."""
|
2019-04-24 22:30:46 +00:00
|
|
|
super().__init__(dev_id, dev_name)
|
2016-05-29 21:28:03 +00:00
|
|
|
self._on_state = False
|
|
|
|
self._brightness = 50
|
|
|
|
self._sender_id = sender_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the device if any."""
|
2019-04-24 22:30:46 +00:00
|
|
|
return self.dev_name
|
2016-05-29 21:28:03 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def brightness(self):
|
|
|
|
"""Brightness of the light.
|
|
|
|
|
|
|
|
This method is optional. Removing it indicates to Home Assistant
|
|
|
|
that brightness is not supported for this light.
|
|
|
|
"""
|
|
|
|
return self._brightness
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self):
|
|
|
|
"""If light is on."""
|
|
|
|
return self._on_state
|
|
|
|
|
2016-08-16 06:07:07 +00:00
|
|
|
@property
|
|
|
|
def supported_features(self):
|
|
|
|
"""Flag supported features."""
|
|
|
|
return SUPPORT_ENOCEAN
|
|
|
|
|
2016-05-29 21:28:03 +00:00
|
|
|
def turn_on(self, **kwargs):
|
|
|
|
"""Turn the light source on or sets a specific dimmer value."""
|
|
|
|
brightness = kwargs.get(ATTR_BRIGHTNESS)
|
|
|
|
if brightness is not None:
|
|
|
|
self._brightness = brightness
|
|
|
|
|
|
|
|
bval = math.floor(self._brightness / 256.0 * 100.0)
|
|
|
|
if bval == 0:
|
|
|
|
bval = 1
|
2019-07-31 19:25:30 +00:00
|
|
|
command = [0xA5, 0x02, bval, 0x01, 0x09]
|
2016-05-29 21:28:03 +00:00
|
|
|
command.extend(self._sender_id)
|
|
|
|
command.extend([0x00])
|
|
|
|
self.send_command(command, [], 0x01)
|
|
|
|
self._on_state = True
|
|
|
|
|
|
|
|
def turn_off(self, **kwargs):
|
|
|
|
"""Turn the light source off."""
|
2019-07-31 19:25:30 +00:00
|
|
|
command = [0xA5, 0x02, 0x00, 0x01, 0x09]
|
2016-05-29 21:28:03 +00:00
|
|
|
command.extend(self._sender_id)
|
|
|
|
command.extend([0x00])
|
|
|
|
self.send_command(command, [], 0x01)
|
|
|
|
self._on_state = False
|
|
|
|
|
2019-04-24 22:30:46 +00:00
|
|
|
def value_changed(self, packet):
|
|
|
|
"""Update the internal state of this device.
|
|
|
|
|
|
|
|
Dimmer devices like Eltako FUD61 send telegram in different RORGs.
|
|
|
|
We only care about the 4BS (0xA5).
|
|
|
|
"""
|
2019-07-31 19:25:30 +00:00
|
|
|
if packet.data[0] == 0xA5 and packet.data[1] == 0x02:
|
2019-04-24 22:30:46 +00:00
|
|
|
val = packet.data[2]
|
|
|
|
self._brightness = math.floor(val / 100.0 * 256.0)
|
|
|
|
self._on_state = bool(val != 0)
|
|
|
|
self.schedule_update_ha_state()
|