11from __future__ import annotations
22
3+ import random
4+ import voluptuous as vol
5+
36from 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
69from homeassistant .const import Platform
10+ from homeassistant .helpers import config_validation as cv
711
812from .const import DOMAIN , CONF_RESET , CONF_DELAY , CONF_MODEL
913from .elkbledom import BLEDOMInstance
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+
2143async 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
48137async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
0 commit comments