1 """Handle Hue Service calls."""
3 from __future__
import annotations
8 from aiohue
import HueBridgeV1, HueBridgeV2
9 import voluptuous
as vol
15 from .bridge
import HueBridge
22 SERVICE_HUE_ACTIVATE_SCENE,
25 LOGGER = logging.getLogger(__name__)
29 """Register services for Hue integration."""
31 async
def hue_activate_scene(call: ServiceCall, skip_reload=
True) ->
None:
32 """Handle activation of Hue scene."""
34 group_name = call.data[ATTR_GROUP_NAME]
35 scene_name = call.data[ATTR_SCENE_NAME]
36 transition = call.data.get(ATTR_TRANSITION)
37 dynamic = call.data.get(ATTR_DYNAMIC,
False)
42 if bridge.api_version == 1
44 bridge, group_name, scene_name, transition, dynamic
46 for bridge
in hass.data[DOMAIN].values()
47 if isinstance(bridge, HueBridge)
49 results = await asyncio.gather(*tasks)
53 if True not in results:
55 "No bridge was able to activate scene %s in group %s",
60 if not hass.services.has_service(DOMAIN, SERVICE_HUE_ACTIVATE_SCENE):
62 hass.services.async_register(
64 SERVICE_HUE_ACTIVATE_SCENE,
68 vol.Required(ATTR_GROUP_NAME): cv.string,
69 vol.Required(ATTR_SCENE_NAME): cv.string,
70 vol.Optional(ATTR_TRANSITION): cv.positive_int,
71 vol.Optional(ATTR_DYNAMIC): cv.boolean,
81 transition: int |
None =
None,
82 is_retry: bool =
False,
84 """Service for V1 bridge to call directly into bridge to set scenes."""
85 api: HueBridgeV1 = bridge.api
86 if api.scenes
is None:
87 LOGGER.warning(
"Hub %s does not support scenes", api.host)
91 (group
for group
in api.groups.values()
if group.name == group_name),
98 for scene
in api.scenes.values()
99 if scene.name == scene_name
100 and group
is not None
101 and sorted(scene.lights) == sorted(group.lights)
106 if not is_retry
and (group
is None or scene
is None):
107 await bridge.async_request_call(api.groups.update)
108 await bridge.async_request_call(api.scenes.update)
110 bridge, group_name, scene_name, transition, is_retry=
True
113 if group
is None or scene
is None:
115 "Unable to find scene %s for group %s on bridge %s",
122 await bridge.async_request_call(
123 group.set_action, scene=scene.id, transitiontime=transition
132 transition: int |
None =
None,
133 dynamic: bool =
True,
135 """Service for V2 bridge to call scene by name."""
138 "Use of service_call '%s' is deprecated and will be removed "
139 "in a future release. Please use scene entities instead"
141 SERVICE_HUE_ACTIVATE_SCENE,
143 api: HueBridgeV2 = bridge.api
144 for scene
in api.scenes:
145 if scene.metadata.name.lower() != scene_name.lower():
147 group = api.scenes.get_group(scene.id)
148 if group.metadata.name.lower() != group_name.lower():
152 transition = transition * 1000
153 await bridge.async_request_call(
154 api.scenes.recall, scene.id, dynamic=dynamic, duration=transition
158 "Unable to find scene %s for group %s on bridge %s",
None async_register_services(HomeAssistant hass)
bool hue_activate_scene_v1(HueBridge bridge, str group_name, str scene_name, int|None transition=None, bool is_retry=False)
bool hue_activate_scene_v2(HueBridge bridge, str group_name, str scene_name, int|None transition=None, bool dynamic=True)
Callable[[Callable[[ServiceCall], Any]], Callable[[ServiceCall], Any]] verify_domain_control(HomeAssistant hass, str domain)