From 4efbe7135c53d87d90670b64bb061a73ba1332a2 Mon Sep 17 00:00:00 2001 From: brefra Date: Mon, 6 Jan 2020 01:28:10 +0100 Subject: [PATCH] Add LED control of push buttons and bump velbus-library (#30445) * Add LED control * Bump python-velbus library to 2.0.35 To have LED control available in library * Apply black formating * Fix no-else-return pylint error * Changed to f-string and more dry code * Rewrite turn_on for LED control --- homeassistant/components/velbus/light.py | 56 ++++++++++++++++--- homeassistant/components/velbus/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 4 files changed, 52 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/velbus/light.py b/homeassistant/components/velbus/light.py index 6b34182e559..7db79e74d5b 100644 --- a/homeassistant/components/velbus/light.py +++ b/homeassistant/components/velbus/light.py @@ -5,8 +5,12 @@ from velbus.util import VelbusException from homeassistant.components.light import ( ATTR_BRIGHTNESS, + ATTR_FLASH, ATTR_TRANSITION, + FLASH_LONG, + FLASH_SHORT, SUPPORT_BRIGHTNESS, + SUPPORT_FLASH, SUPPORT_TRANSITION, Light, ) @@ -36,11 +40,27 @@ async def async_setup_entry(hass, entry, async_add_entities): class VelbusLight(VelbusEntity, Light): """Representation of a Velbus light.""" + @property + def name(self): + """Return the display name of this entity.""" + if self._module.light_is_buttonled(self._channel): + return f"LED {self._module.get_name(self._channel)}" + return self._module.get_name(self._channel) + @property def supported_features(self): """Flag supported features.""" + if self._module.light_is_buttonled(self._channel): + return SUPPORT_FLASH return SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION + @property + def entity_registry_enabled_default(self): + """Disable Button LEDs by default.""" + if self._module.light_is_buttonled(self._channel): + return False + return True + @property def is_on(self): """Return true if the light is on.""" @@ -53,25 +73,47 @@ class VelbusLight(VelbusEntity, Light): def turn_on(self, **kwargs): """Instruct the Velbus light to turn on.""" - try: + if self._module.light_is_buttonled(self._channel): + if ATTR_FLASH in kwargs: + if kwargs[ATTR_FLASH] == FLASH_LONG: + attr, *args = "set_led_state", self._channel, "slow" + elif kwargs[ATTR_FLASH] == FLASH_SHORT: + attr, *args = "set_led_state", self._channel, "fast" + else: + attr, *args = "set_led_state", self._channel, "on" + else: + attr, *args = "set_led_state", self._channel, "on" + else: if ATTR_BRIGHTNESS in kwargs: - self._module.set_dimmer_state( + attr, *args = ( + "set_dimmer_state", self._channel, kwargs[ATTR_BRIGHTNESS], kwargs.get(ATTR_TRANSITION, 0), ) else: - self._module.restore_dimmer_state( - self._channel, kwargs.get(ATTR_TRANSITION, 0), + attr, *args = ( + "restore_dimmer_state", + self._channel, + kwargs.get(ATTR_TRANSITION, 0), ) + try: + getattr(self._module, attr)(*args) except VelbusException as err: _LOGGER.error("A Velbus error occurred: %s", err) def turn_off(self, **kwargs): """Instruct the velbus light to turn off.""" - try: - self._module.set_dimmer_state( - self._channel, 0, kwargs.get(ATTR_TRANSITION, 0), + if self._module.light_is_buttonled(self._channel): + attr, *args = "set_led_state", self._channel, "off" + else: + attr, *args = ( + "set_dimmer_state", + self._channel, + 0, + kwargs.get(ATTR_TRANSITION, 0), ) + try: + getattr(self._module, attr)(*args) except VelbusException as err: _LOGGER.error("A Velbus error occurred: %s", err) diff --git a/homeassistant/components/velbus/manifest.json b/homeassistant/components/velbus/manifest.json index 007ca421276..ba1dde9b015 100644 --- a/homeassistant/components/velbus/manifest.json +++ b/homeassistant/components/velbus/manifest.json @@ -3,7 +3,7 @@ "name": "Velbus", "documentation": "https://www.home-assistant.io/integrations/velbus", "requirements": [ - "python-velbus==2.0.32" + "python-velbus==2.0.35" ], "config_flow": true, "dependencies": [], diff --git a/requirements_all.txt b/requirements_all.txt index afacd33482c..bcda899fb06 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1635,7 +1635,7 @@ python-telnet-vlc==1.0.4 python-twitch-client==0.6.0 # homeassistant.components.velbus -python-velbus==2.0.32 +python-velbus==2.0.35 # homeassistant.components.vlc python-vlc==1.1.2 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 7c682b2bcda..26efb9364bf 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -534,7 +534,7 @@ python-miio==0.4.8 python-nest==4.1.0 # homeassistant.components.velbus -python-velbus==2.0.32 +python-velbus==2.0.35 # homeassistant.components.awair python_awair==0.0.4