Add transition to LiteJet (#47657)

This commit is contained in:
Jon Caruana 2021-07-24 03:43:10 -07:00 committed by GitHub
parent 0f78004ede
commit 72a3860361
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 163 additions and 18 deletions

View file

@ -3,11 +3,13 @@ import logging
from homeassistant.components.light import (
ATTR_BRIGHTNESS,
ATTR_TRANSITION,
SUPPORT_BRIGHTNESS,
SUPPORT_TRANSITION,
LightEntity,
)
from .const import DOMAIN
from .const import CONF_DEFAULT_TRANSITION, DOMAIN
_LOGGER = logging.getLogger(__name__)
@ -23,7 +25,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
entities = []
for i in system.loads():
name = system.get_load_name(i)
entities.append(LiteJetLight(config_entry.entry_id, system, i, name))
entities.append(LiteJetLight(config_entry, system, i, name))
return entities
async_add_entities(await hass.async_add_executor_job(get_entities, system), True)
@ -32,9 +34,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class LiteJetLight(LightEntity):
"""Representation of a single LiteJet light."""
def __init__(self, entry_id, lj, i, name):
def __init__(self, config_entry, lj, i, name):
"""Initialize a LiteJet light."""
self._entry_id = entry_id
self._config_entry = config_entry
self._lj = lj
self._index = i
self._brightness = 0
@ -57,7 +59,7 @@ class LiteJetLight(LightEntity):
@property
def supported_features(self):
"""Flag supported features."""
return SUPPORT_BRIGHTNESS
return SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
@property
def name(self):
@ -67,7 +69,7 @@ class LiteJetLight(LightEntity):
@property
def unique_id(self):
"""Return a unique identifier for this light."""
return f"{self._entry_id}_{self._index}"
return f"{self._config_entry.entry_id}_{self._index}"
@property
def brightness(self):
@ -91,16 +93,33 @@ class LiteJetLight(LightEntity):
def turn_on(self, **kwargs):
"""Turn on the light."""
if ATTR_BRIGHTNESS in kwargs:
brightness = int(kwargs[ATTR_BRIGHTNESS] / 255 * 99)
self._lj.activate_load_at(self._index, brightness, 0)
else:
# If neither attribute is specified then the simple activate load
# LiteJet API will use the per-light default brightness and
# transition values programmed in the LiteJet system.
if ATTR_BRIGHTNESS not in kwargs and ATTR_TRANSITION not in kwargs:
self._lj.activate_load(self._index)
return
# If either attribute is specified then Home Assistant must
# control both values.
default_transition = self._config_entry.options.get(CONF_DEFAULT_TRANSITION, 0)
transition = kwargs.get(ATTR_TRANSITION, default_transition)
brightness = int(kwargs.get(ATTR_BRIGHTNESS, 255) / 255 * 99)
self._lj.activate_load_at(self._index, brightness, int(transition))
def turn_off(self, **kwargs):
"""Turn off the light."""
if ATTR_TRANSITION in kwargs:
self._lj.activate_load_at(self._index, 0, kwargs[ATTR_TRANSITION])
return
# If transition attribute is not specified then the simple
# deactivate load LiteJet API will use the per-light default
# transition value programmed in the LiteJet system.
self._lj.deactivate_load(self._index)
def update(self):
"""Retrieve the light's brightness from the LiteJet system."""
self._brightness = self._lj.get_load_level(self._index) / 99 * 255
self._brightness = int(self._lj.get_load_level(self._index) / 99 * 255)