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"]
|
cntrl = hass.data[DOMAIN][entry.entry_id]["cntrl"]
|
||||||
entities = []
|
entities = []
|
||||||
for channel in cntrl.get_all("light"):
|
for channel in cntrl.get_all("light"):
|
||||||
entities.append(VelbusLight(channel, False))
|
entities.append(VelbusLight(channel))
|
||||||
for channel in cntrl.get_all("led"):
|
for channel in cntrl.get_all("led"):
|
||||||
entities.append(VelbusLight(channel, True))
|
entities.append(VelbusButtonLight(channel))
|
||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class VelbusLight(VelbusEntity, LightEntity):
|
class VelbusLight(VelbusEntity, LightEntity):
|
||||||
"""Representation of a Velbus light."""
|
"""Representation of a Velbus light."""
|
||||||
|
|
||||||
def __init__(self, channel, led):
|
_attr_supported_feature = SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION
|
||||||
"""Initialize a light Velbus entity."""
|
|
||||||
|
def __init__(self, channel):
|
||||||
|
"""Initialize the dimmer."""
|
||||||
super().__init__(channel)
|
super().__init__(channel)
|
||||||
self._is_led = led
|
self._attr_name = self._channel.get_name()
|
||||||
|
|
||||||
@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
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
|
@ -61,17 +49,6 @@ class VelbusLight(VelbusEntity, LightEntity):
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Instruct the Velbus light to turn on."""
|
"""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"
|
|
||||||
else:
|
|
||||||
attr, *args = "set_led_state", "on"
|
|
||||||
else:
|
|
||||||
if ATTR_BRIGHTNESS in kwargs:
|
if ATTR_BRIGHTNESS in kwargs:
|
||||||
# Make sure a low but non-zero value is not rounded down to zero
|
# Make sure a low but non-zero value is not rounded down to zero
|
||||||
if kwargs[ATTR_BRIGHTNESS] == 0:
|
if kwargs[ATTR_BRIGHTNESS] == 0:
|
||||||
|
@ -92,12 +69,49 @@ class VelbusLight(VelbusEntity, LightEntity):
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
"""Instruct the velbus light to turn off."""
|
"""Instruct the velbus light to turn off."""
|
||||||
if self._is_led:
|
|
||||||
attr, *args = "set_led_state", "off"
|
|
||||||
else:
|
|
||||||
attr, *args = (
|
attr, *args = (
|
||||||
"set_dimmer_state",
|
"set_dimmer_state",
|
||||||
0,
|
0,
|
||||||
kwargs.get(ATTR_TRANSITION, 0),
|
kwargs.get(ATTR_TRANSITION, 0),
|
||||||
)
|
)
|
||||||
await getattr(self._channel, attr)(*args)
|
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