Refactor velbus light code to make it more clear and readable (#58483)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
ea657e6656
commit
22248f891d
1 changed files with 66 additions and 52 deletions
|
@ -21,33 +21,21 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||
cntrl = hass.data[DOMAIN][entry.entry_id]["cntrl"]
|
||||
entities = []
|
||||
for channel in cntrl.get_all("light"):
|
||||
entities.append(VelbusLight(channel, False))
|
||||
entities.append(VelbusLight(channel))
|
||||
for channel in cntrl.get_all("led"):
|
||||
entities.append(VelbusLight(channel, True))
|
||||
entities.append(VelbusButtonLight(channel))
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class VelbusLight(VelbusEntity, LightEntity):
|
||||
"""Representation of a Velbus light."""
|
||||
|
||||
def __init__(self, channel, led):
|
||||
"""Initialize a light Velbus entity."""
|
||||
_attr_supported_feature = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
|
||||
|
||||
def __init__(self, channel):
|
||||
"""Initialize the dimmer."""
|
||||
super().__init__(channel)
|
||||
self._is_led = led
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the display name of this entity."""
|
||||
if self._is_led:
|
||||
return f"LED {self._channel.get_name()}"
|
||||
return self._channel.get_name()
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
"""Flag supported features."""
|
||||
if self._is_led:
|
||||
return SUPPORT_FLASH
|
||||
return SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
|
||||
self._attr_name = self._channel.get_name()
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
|
@ -61,43 +49,69 @@ class VelbusLight(VelbusEntity, LightEntity):
|
|||
|
||||
async def async_turn_on(self, **kwargs):
|
||||
"""Instruct the Velbus light to turn on."""
|
||||
if self._is_led:
|
||||
if ATTR_FLASH in kwargs:
|
||||
if kwargs[ATTR_FLASH] == FLASH_LONG:
|
||||
attr, *args = "set_led_state", "slow"
|
||||
elif kwargs[ATTR_FLASH] == FLASH_SHORT:
|
||||
attr, *args = "set_led_state", "fast"
|
||||
else:
|
||||
attr, *args = "set_led_state", "on"
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
# Make sure a low but non-zero value is not rounded down to zero
|
||||
if kwargs[ATTR_BRIGHTNESS] == 0:
|
||||
brightness = 0
|
||||
else:
|
||||
attr, *args = "set_led_state", "on"
|
||||
brightness = max(int((kwargs[ATTR_BRIGHTNESS] * 100) / 255), 1)
|
||||
attr, *args = (
|
||||
"set_dimmer_state",
|
||||
brightness,
|
||||
kwargs.get(ATTR_TRANSITION, 0),
|
||||
)
|
||||
else:
|
||||
if ATTR_BRIGHTNESS in kwargs:
|
||||
# Make sure a low but non-zero value is not rounded down to zero
|
||||
if kwargs[ATTR_BRIGHTNESS] == 0:
|
||||
brightness = 0
|
||||
else:
|
||||
brightness = max(int((kwargs[ATTR_BRIGHTNESS] * 100) / 255), 1)
|
||||
attr, *args = (
|
||||
"set_dimmer_state",
|
||||
brightness,
|
||||
kwargs.get(ATTR_TRANSITION, 0),
|
||||
)
|
||||
else:
|
||||
attr, *args = (
|
||||
"restore_dimmer_state",
|
||||
kwargs.get(ATTR_TRANSITION, 0),
|
||||
)
|
||||
attr, *args = (
|
||||
"restore_dimmer_state",
|
||||
kwargs.get(ATTR_TRANSITION, 0),
|
||||
)
|
||||
await getattr(self._channel, attr)(*args)
|
||||
|
||||
async def async_turn_off(self, **kwargs):
|
||||
"""Instruct the velbus light to turn off."""
|
||||
if self._is_led:
|
||||
attr, *args = "set_led_state", "off"
|
||||
else:
|
||||
attr, *args = (
|
||||
"set_dimmer_state",
|
||||
0,
|
||||
kwargs.get(ATTR_TRANSITION, 0),
|
||||
)
|
||||
attr, *args = (
|
||||
"set_dimmer_state",
|
||||
0,
|
||||
kwargs.get(ATTR_TRANSITION, 0),
|
||||
)
|
||||
await getattr(self._channel, attr)(*args)
|
||||
|
||||
|
||||
class VelbusButtonLight(VelbusEntity, LightEntity):
|
||||
"""Representation of a Velbus light."""
|
||||
|
||||
_attr_entity_registry_enabled_default = False
|
||||
_attr_supported_feature = SUPPORT_FLASH
|
||||
|
||||
def __init__(self, channel):
|
||||
"""Initialize the button light (led)."""
|
||||
super().__init__(channel)
|
||||
self._attr_name = f"LED {self._channel.get_name()}"
|
||||
|
||||
@property
|
||||
def is_on(self):
|
||||
"""Return true if the light is on."""
|
||||
return self._channel.is_on()
|
||||
|
||||
@property
|
||||
def brightness(self):
|
||||
"""Return the brightness of the light."""
|
||||
return int((self._channel.get_dimmer_state() * 255) / 100)
|
||||
|
||||
async def async_turn_on(self, **kwargs):
|
||||
"""Instruct the Velbus light to turn on."""
|
||||
if ATTR_FLASH in kwargs:
|
||||
if kwargs[ATTR_FLASH] == FLASH_LONG:
|
||||
attr, *args = "set_led_state", "slow"
|
||||
elif kwargs[ATTR_FLASH] == FLASH_SHORT:
|
||||
attr, *args = "set_led_state", "fast"
|
||||
else:
|
||||
attr, *args = "set_led_state", "on"
|
||||
else:
|
||||
attr, *args = "set_led_state", "on"
|
||||
await getattr(self._channel, attr)(*args)
|
||||
|
||||
async def async_turn_off(self, **kwargs):
|
||||
"""Instruct the velbus light to turn off."""
|
||||
attr, *args = "set_led_state", "off"
|
||||
await getattr(self._channel, attr)(*args)
|
||||
|
|
Loading…
Add table
Reference in a new issue