feat: add set colors action for animation automations

This commit is contained in:
Niklas 2025-01-17 16:17:03 +01:00
parent 1d8fe7a6ab
commit f013cc969b
3 changed files with 101 additions and 2 deletions

View File

@ -165,6 +165,17 @@ class SmartHomeLight(SmartHomeEntity, LightEntity):
# Always maintain 5 colors array, set first color and pad with black
data["colors"] = [color_hex] + ['000000'] * 4
# Handle multiple color update from set_colors service
elif "colors" in kwargs:
colors = ['000000'] * 5 # Default to all black
# Update each color at its specified index
for color_data in kwargs["colors"]:
r, g, b = color_data["rgb_color"]
color_hex = f"{r:02x}{g:02x}{b:02x}"
colors[color_data["colors_index"]] = color_hex
data["colors"] = colors
if ATTR_EFFECT in kwargs:
# Convert HA effect name back to API animation mode
effect_name = kwargs[ATTR_EFFECT]

View File

@ -1,4 +1,4 @@
"""Service for adjusting animation speed of RGB lights."""
"""Service for adjusting animation speed and colors of RGB lights."""
from __future__ import annotations
import logging
@ -14,7 +14,13 @@ from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
SERVICE_SET_ANIMATION_SPEED = "set_animation_speed"
SERVICE_SET_COLORS = "set_colors"
ATTR_SPEED = "speed"
ATTR_COLOR1 = "color1"
ATTR_COLOR2 = "color2"
ATTR_COLOR3 = "color3"
ATTR_COLOR4 = "color4"
ATTR_COLOR5 = "color5"
SERVICE_SCHEMA_ANIMATION_SPEED = vol.Schema({
vol.Required(ATTR_ENTITY_ID): cv.comp_entity_ids,
@ -23,13 +29,22 @@ SERVICE_SCHEMA_ANIMATION_SPEED = vol.Schema({
),
})
SERVICE_SCHEMA_SET_COLORS = vol.Schema({
vol.Required(ATTR_ENTITY_ID): cv.comp_entity_ids,
vol.Required(ATTR_COLOR1): vol.All(list, vol.Length(min=3, max=3)),
vol.Optional(ATTR_COLOR2): vol.All(list, vol.Length(min=3, max=3)),
vol.Optional(ATTR_COLOR3): vol.All(list, vol.Length(min=3, max=3)),
vol.Optional(ATTR_COLOR4): vol.All(list, vol.Length(min=3, max=3)),
vol.Optional(ATTR_COLOR5): vol.All(list, vol.Length(min=3, max=3)),
})
async def async_setup_services(hass: HomeAssistant) -> None:
"""Set up the Light Animation services."""
# Register services
async def service_handler(call: ServiceCall) -> None:
"""Handle the services."""
# coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
if call.service == SERVICE_SET_ANIMATION_SPEED:
# Get all light entities from the call
target_entities = call.data.get(ATTR_ENTITY_ID)
@ -46,6 +61,30 @@ async def async_setup_services(hass: HomeAssistant) -> None:
entity_id
)
elif call.service == SERVICE_SET_COLORS:
# Get all light entities from the call
target_entities = call.data.get(ATTR_ENTITY_ID)
# Build list of colors and their indices
colors = []
for i, color_attr in enumerate([ATTR_COLOR1, ATTR_COLOR2, ATTR_COLOR3, ATTR_COLOR4, ATTR_COLOR5]):
if color_attr in call.data:
colors.append({
'rgb_color': call.data[color_attr],
'colors_index': i
})
for entity_id in target_entities:
entity = hass.data[LIGHT_DOMAIN].get_entity(entity_id)
if entity and hasattr(entity, "async_turn_on"):
# Handle color updates
if colors:
await entity.async_turn_on(colors=colors)
else:
_LOGGER.warning(
"Entity %s doesn't support color adjustment",
entity_id
)
hass.services.async_register(
DOMAIN,
SERVICE_SET_ANIMATION_SPEED,
@ -53,6 +92,15 @@ async def async_setup_services(hass: HomeAssistant) -> None:
schema=SERVICE_SCHEMA_ANIMATION_SPEED,
)
hass.services.async_register(
DOMAIN,
SERVICE_SET_COLORS,
service_handler,
schema=SERVICE_SCHEMA_SET_COLORS,
)
async def async_unload_services(hass: HomeAssistant) -> None:
# Unregister services
hass.services.async_remove(DOMAIN, SERVICE_SET_ANIMATION_SPEED)
hass.services.async_remove(DOMAIN, SERVICE_SET_COLORS)

View File

@ -17,3 +17,43 @@ set_animation_speed:
min: 0
max: 255
mode: slider
set_colors:
name: Set colors
description: Set up to 5 colors for an RGB light.
target:
entity:
domain: light
supported_features:
- light.LightEntityFeature.EFFECT
fields:
color1:
name: Color 1
description: First color in RGB format [r, g, b]
required: true
selector:
color_rgb:
color2:
name: Color 2
description: Second color in RGB format [r, g, b]
required: false
selector:
color_rgb:
color3:
name: Color 3
description: Third color in RGB format [r, g, b]
required: false
selector:
color_rgb:
color4:
name: Color 4
description: Fourth color in RGB format [r, g, b]
required: false
selector:
color_rgb:
color5:
name: Color 5
description: Fifth color in RGB format [r, g, b]
required: false
selector:
color_rgb: