1 """Support for scene platform for Hue scenes (V2 only)."""
3 from __future__
import annotations
7 from aiohue.v2
import HueBridgeV2
8 from aiohue.v2.controllers.events
import EventType
9 from aiohue.v2.controllers.scenes
import ScenesController
10 from aiohue.v2.models.scene
import Scene
as HueScene, ScenePut
as HueScenePut
11 from aiohue.v2.models.smart_scene
import SmartScene
as HueSmartScene, SmartSceneState
12 import voluptuous
as vol
20 async_get_current_platform,
23 from .bridge
import HueBridge
24 from .const
import DOMAIN
25 from .v2.entity
import HueBaseEntity
26 from .v2.helpers
import normalize_hue_brightness, normalize_hue_transition
28 SERVICE_ACTIVATE_SCENE =
"activate_scene"
29 ATTR_DYNAMIC =
"dynamic"
31 ATTR_BRIGHTNESS =
"brightness"
36 config_entry: ConfigEntry,
37 async_add_entities: AddEntitiesCallback,
39 """Set up scene platform from Hue group scenes."""
40 bridge: HueBridge = hass.data[DOMAIN][config_entry.entry_id]
41 api: HueBridgeV2 = bridge.api
43 if bridge.api_version == 1:
45 raise NotImplementedError(
"Scene support is only available for V2 bridges")
50 event_type: EventType, resource: HueScene | HueSmartScene
52 """Add entity from Hue resource."""
53 if isinstance(resource, HueSmartScene):
59 for item
in api.scenes:
60 async_add_entity(EventType.RESOURCE_ADDED, item)
63 config_entry.async_on_unload(
64 api.scenes.subscribe(async_add_entity, event_filter=EventType.RESOURCE_ADDED)
69 platform.async_register_entity_service(
70 SERVICE_ACTIVATE_SCENE,
72 vol.Optional(ATTR_DYNAMIC): vol.Coerce(bool),
73 vol.Optional(ATTR_SPEED): vol.All(
74 vol.Coerce(int), vol.Range(min=0, max=100)
76 vol.Optional(ATTR_TRANSITION): vol.All(
77 vol.Coerce(float), vol.Range(min=0, max=3600)
79 vol.Optional(ATTR_BRIGHTNESS): vol.All(
80 vol.Coerce(int), vol.Range(min=1, max=255)
88 """Base Representation of a Scene entity from Hue Scenes."""
90 _attr_has_entity_name =
True
95 controller: ScenesController,
96 resource: HueScene | HueSmartScene,
98 """Initialize the entity."""
99 super().
__init__(bridge, controller, resource)
106 identifiers={(DOMAIN, self.
groupgroup.id)},
110 """Call when entity is added."""
114 self.
bridgebridge.api.groups.subscribe(
117 (EventType.RESOURCE_UPDATED),
123 """Return name of the scene."""
128 """Representation of a Scene entity from Hue Scenes."""
132 """Return if this scene has a dynamic color palette."""
148 """Activate Hue scene."""
153 dynamic = kwargs.get(ATTR_DYNAMIC,
False)
154 speed = kwargs.get(ATTR_SPEED)
157 if speed
is not None:
158 await self.
bridgebridge.async_request_call(
161 HueScenePut(speed=speed / 100),
164 await self.
bridgebridge.async_request_call(
169 brightness=brightness,
174 """Return the optional state attributes."""
178 brightness = palette.dimming[0].brightness
179 if brightness
is None:
182 if action.action.dimming:
183 brightness = action.action.dimming.brightness
185 if brightness
is not None:
187 brightness = round((brightness / 100) * 255)
189 "group_name": self.
groupgroup.metadata.name,
190 "group_type": self.
groupgroup.type.value,
193 "brightness": brightness,
199 """Representation of a Smart Scene entity from Hue Scenes."""
203 """Return if this smart scene is currently active."""
207 """Activate Hue Smart scene."""
209 await self.
bridgebridge.async_request_call(
216 """Return the optional state attributes."""
218 "group_name": self.
groupgroup.metadata.name,
219 "group_type": self.
groupgroup.type.value,
224 res[
"active_timeslot_id"] = self.
resourceresourceresource.active_timeslot.timeslot_id
225 res[
"active_timeslot_name"] = self.
resourceresourceresource.active_timeslot.weekday.value
230 for timeslot
in day_timeslot.timeslots:
236 if active_scene
is not None:
237 res[
"active_scene"] = active_scene.metadata.name
None async_added_to_hass(self)
None __init__(self, HueBridge bridge, ScenesController controller, HueScene|HueSmartScene resource)
dict[str, Any]|None extra_state_attributes(self)
None async_activate(self, **Any kwargs)
dict[str, Any]|None extra_state_attributes(self)
None async_activate(self, **Any kwargs)
None _handle_event(self, EventType event_type, HueResource resource)
None async_on_remove(self, CALLBACK_TYPE func)
web.Response get(self, web.Request request, str config_key)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
float|None normalize_hue_brightness(float|None brightness)
float|None normalize_hue_transition(float|None transition)