From 9d8dfc2d716437f2f4b660cb1cab01eb804d832b Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 16 Nov 2022 15:07:02 -0600 Subject: [PATCH] Add effects support to led-ble (#82224) --- homeassistant/components/led_ble/const.py | 4 ++++ homeassistant/components/led_ble/light.py | 20 +++++++++++++++++-- .../components/led_ble/manifest.json | 2 +- requirements_all.txt | 4 +--- requirements_test_all.txt | 4 +--- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/led_ble/const.py b/homeassistant/components/led_ble/const.py index ad3ea8f6707..64c28d1ada5 100644 --- a/homeassistant/components/led_ble/const.py +++ b/homeassistant/components/led_ble/const.py @@ -1,5 +1,7 @@ """Constants for the LED BLE integration.""" +from typing import Final + DOMAIN = "led_ble" DEVICE_TIMEOUT = 30 @@ -8,3 +10,5 @@ LOCAL_NAMES = {"LEDnet", "BLE-LED", "LEDBLE", "Triones", "LEDBlue"} UNSUPPORTED_SUB_MODEL = "LEDnetWF" UPDATE_SECONDS = 15 + +DEFAULT_EFFECT_SPEED: Final = 50 diff --git a/homeassistant/components/led_ble/light.py b/homeassistant/components/led_ble/light.py index 4a8ff3f01af..22a52e61b63 100644 --- a/homeassistant/components/led_ble/light.py +++ b/homeassistant/components/led_ble/light.py @@ -7,10 +7,12 @@ from led_ble import LEDBLE from homeassistant.components.light import ( ATTR_BRIGHTNESS, + ATTR_EFFECT, ATTR_RGB_COLOR, ATTR_WHITE, ColorMode, LightEntity, + LightEntityFeature, ) from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant, callback @@ -22,7 +24,7 @@ from homeassistant.helpers.update_coordinator import ( DataUpdateCoordinator, ) -from .const import DOMAIN +from .const import DEFAULT_EFFECT_SPEED, DOMAIN from .models import LEDBLEData @@ -41,6 +43,7 @@ class LEDBLEEntity(CoordinatorEntity, LightEntity): _attr_supported_color_modes = {ColorMode.RGB, ColorMode.WHITE} _attr_has_entity_name = True + _attr_supported_features = LightEntityFeature.EFFECT def __init__( self, coordinator: DataUpdateCoordinator, device: LEDBLE, name: str @@ -51,7 +54,7 @@ class LEDBLEEntity(CoordinatorEntity, LightEntity): self._attr_unique_id = device.address self._attr_device_info = DeviceInfo( name=name, - model=hex(device.model_num), + model=f"{device.model_data.description} {hex(device.model_num)}", sw_version=hex(device.version_num), connections={(dr.CONNECTION_BLUETOOTH, device.address)}, ) @@ -65,10 +68,23 @@ class LEDBLEEntity(CoordinatorEntity, LightEntity): self._attr_brightness = device.brightness self._attr_rgb_color = device.rgb_unscaled self._attr_is_on = device.on + self._attr_effect = device.effect + self._attr_effect_list = device.effect_list + + async def _async_set_effect(self, effect: str, brightness: int) -> None: + """Set an effect.""" + await self._device.async_set_effect( + effect, + self._device.speed or DEFAULT_EFFECT_SPEED, + round(brightness / 255 * 100), + ) async def async_turn_on(self, **kwargs: Any) -> None: """Instruct the light to turn on.""" brightness = kwargs.get(ATTR_BRIGHTNESS, self.brightness) + if effect := kwargs.get(ATTR_EFFECT): + await self._async_set_effect(effect, brightness) + return if ATTR_RGB_COLOR in kwargs: rgb = kwargs[ATTR_RGB_COLOR] await self._device.set_rgb(rgb, brightness) diff --git a/homeassistant/components/led_ble/manifest.json b/homeassistant/components/led_ble/manifest.json index d59570c6257..ea272dcacc0 100644 --- a/homeassistant/components/led_ble/manifest.json +++ b/homeassistant/components/led_ble/manifest.json @@ -3,7 +3,7 @@ "name": "LED BLE", "config_flow": true, "documentation": "https://www.home-assistant.io/integrations/led_ble/", - "requirements": ["bluetooth-data-tools==0.2.0", "led-ble==1.0.0"], + "requirements": ["bluetooth-data-tools==0.3.0", "led-ble==1.0.0"], "dependencies": ["bluetooth"], "codeowners": ["@bdraco"], "bluetooth": [ diff --git a/requirements_all.txt b/requirements_all.txt index aec80b757c9..10c5aa6b375 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -452,10 +452,8 @@ bluetooth-adapters==0.7.0 # homeassistant.components.bluetooth bluetooth-auto-recovery==0.4.0 -# homeassistant.components.led_ble -bluetooth-data-tools==0.2.0 - # homeassistant.components.bluetooth +# homeassistant.components.led_ble bluetooth-data-tools==0.3.0 # homeassistant.components.bond diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 07540d1bc6c..259c607c031 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -366,10 +366,8 @@ bluetooth-adapters==0.7.0 # homeassistant.components.bluetooth bluetooth-auto-recovery==0.4.0 -# homeassistant.components.led_ble -bluetooth-data-tools==0.2.0 - # homeassistant.components.bluetooth +# homeassistant.components.led_ble bluetooth-data-tools==0.3.0 # homeassistant.components.bond