Skip to content

Commit 2738a64

Browse files
author
dave-code-ruiz
committed
set rgb color and set random color #132 and #76
1 parent 2297231 commit 2738a64

File tree

2 files changed

+168
-2
lines changed

2 files changed

+168
-2
lines changed

custom_components/elkbledom/__init__.py

Lines changed: 91 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from __future__ import annotations
22

3+
import random
4+
import voluptuous as vol
5+
36
from homeassistant.config_entries import ConfigEntry
4-
from homeassistant.core import HomeAssistant, Event
5-
from homeassistant.const import CONF_MAC, EVENT_HOMEASSISTANT_STOP
7+
from homeassistant.core import HomeAssistant, Event, ServiceCall
8+
from homeassistant.const import CONF_MAC, EVENT_HOMEASSISTANT_STOP, ATTR_ENTITY_ID
69
from homeassistant.const import Platform
10+
from homeassistant.helpers import config_validation as cv
711

812
from .const import DOMAIN, CONF_RESET, CONF_DELAY, CONF_MODEL
913
from .elkbledom import BLEDOMInstance
@@ -18,6 +22,24 @@
1822
Platform.SWITCH,
1923
]
2024

25+
# Service names
26+
SERVICE_SET_RANDOM_COLOR = "set_random_color"
27+
SERVICE_SET_RGB_COLOR = "set_rgb_color"
28+
29+
# Service schemas
30+
SERVICE_SET_RANDOM_COLOR_SCHEMA = vol.Schema({
31+
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
32+
vol.Optional("brightness", default=255): vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
33+
})
34+
35+
SERVICE_SET_RGB_COLOR_SCHEMA = vol.Schema({
36+
vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
37+
vol.Required("r"): vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
38+
vol.Required("g"): vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
39+
vol.Required("b"): vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
40+
vol.Optional("brightness", default=255): vol.All(vol.Coerce(int), vol.Range(min=0, max=255)),
41+
})
42+
2143
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2244
"""Set up ElkBLEDOM from a config entry."""
2345
reset = entry.options.get(CONF_RESET, None) or entry.data.get(CONF_RESET, None)
@@ -43,6 +65,73 @@ async def _async_stop(event: Event) -> None:
4365
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop)
4466
)
4567

68+
# Register services (only once for all entries)
69+
if not hass.services.has_service(DOMAIN, SERVICE_SET_RANDOM_COLOR):
70+
async def handle_set_random_color(call: ServiceCall) -> None:
71+
"""Handle the set_random_color service call."""
72+
entity_ids = call.data[ATTR_ENTITY_ID]
73+
brightness = call.data.get("brightness", 255)
74+
75+
# Generate random RGB values
76+
r = random.randint(0, 255)
77+
g = random.randint(0, 255)
78+
b = random.randint(0, 255)
79+
80+
# Call light.turn_on with random color for each entity
81+
await hass.services.async_call(
82+
"light",
83+
"turn_on",
84+
{
85+
ATTR_ENTITY_ID: entity_ids,
86+
"rgb_color": [r, g, b],
87+
"brightness": brightness,
88+
},
89+
blocking=True,
90+
)
91+
LOGGER.debug(
92+
"Random color set to RGB(%d, %d, %d) with brightness %d for entities: %s",
93+
r, g, b, brightness, entity_ids
94+
)
95+
96+
hass.services.async_register(
97+
DOMAIN,
98+
SERVICE_SET_RANDOM_COLOR,
99+
handle_set_random_color,
100+
schema=SERVICE_SET_RANDOM_COLOR_SCHEMA,
101+
)
102+
103+
if not hass.services.has_service(DOMAIN, SERVICE_SET_RGB_COLOR):
104+
async def handle_set_rgb_color(call: ServiceCall) -> None:
105+
"""Handle the set_rgb_color service call."""
106+
entity_ids = call.data[ATTR_ENTITY_ID]
107+
r = call.data["r"]
108+
g = call.data["g"]
109+
b = call.data["b"]
110+
brightness = call.data.get("brightness", 255)
111+
112+
# Call light.turn_on with specified RGB color
113+
await hass.services.async_call(
114+
"light",
115+
"turn_on",
116+
{
117+
ATTR_ENTITY_ID: entity_ids,
118+
"rgb_color": [r, g, b],
119+
"brightness": brightness,
120+
},
121+
blocking=True,
122+
)
123+
LOGGER.debug(
124+
"RGB color set to (%d, %d, %d) with brightness %d for entities: %s",
125+
r, g, b, brightness, entity_ids
126+
)
127+
128+
hass.services.async_register(
129+
DOMAIN,
130+
SERVICE_SET_RGB_COLOR,
131+
handle_set_rgb_color,
132+
schema=SERVICE_SET_RGB_COLOR_SCHEMA,
133+
)
134+
46135
return True
47136

48137
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
set_random_color:
2+
name: Set Random Color
3+
description: Set a random RGB color to the LED strip
4+
fields:
5+
entity_id:
6+
name: Entity
7+
description: The light entity to control
8+
required: true
9+
example: "light.led1"
10+
selector:
11+
entity:
12+
domain: light
13+
brightness:
14+
name: Brightness
15+
description: Brightness level (0-255)
16+
required: false
17+
default: 255
18+
example: 200
19+
selector:
20+
number:
21+
min: 0
22+
max: 255
23+
mode: slider
24+
25+
set_rgb_color:
26+
name: Set RGB Color
27+
description: Set a specific RGB color to the LED strip
28+
fields:
29+
entity_id:
30+
name: Entity
31+
description: The light entity to control
32+
required: true
33+
example: "light.led1"
34+
selector:
35+
entity:
36+
domain: light
37+
r:
38+
name: Red
39+
description: Red value (0-255)
40+
required: true
41+
example: 255
42+
selector:
43+
number:
44+
min: 0
45+
max: 255
46+
mode: slider
47+
g:
48+
name: Green
49+
description: Green value (0-255)
50+
required: true
51+
example: 128
52+
selector:
53+
number:
54+
min: 0
55+
max: 255
56+
mode: slider
57+
b:
58+
name: Blue
59+
description: Blue value (0-255)
60+
required: true
61+
example: 64
62+
selector:
63+
number:
64+
min: 0
65+
max: 255
66+
mode: slider
67+
brightness:
68+
name: Brightness
69+
description: Brightness level (0-255)
70+
required: false
71+
default: 255
72+
example: 200
73+
selector:
74+
number:
75+
min: 0
76+
max: 255
77+
mode: slider

0 commit comments

Comments
 (0)