1 """Hue event entities from Button resources."""
3 from __future__
import annotations
7 from aiohue.v2
import HueBridgeV2
8 from aiohue.v2.controllers.events
import EventType
9 from aiohue.v2.models.button
import Button
10 from aiohue.v2.models.relative_rotary
import RelativeRotary, RelativeRotaryDirection
15 EventEntityDescription,
21 from .bridge
import HueBridge
22 from .const
import DEFAULT_BUTTON_EVENT_TYPES, DEVICE_SPECIFIC_EVENT_TYPES, DOMAIN
23 from .v2.entity
import HueBaseEntity
28 config_entry: ConfigEntry,
29 async_add_entities: AddEntitiesCallback,
31 """Set up event platform from Hue button resources."""
32 bridge: HueBridge = hass.data[DOMAIN][config_entry.entry_id]
33 api: HueBridgeV2 = bridge.api
35 if bridge.api_version == 1:
37 raise NotImplementedError(
"Event support is only available for V2 bridges")
42 event_type: EventType,
43 resource: Button | RelativeRotary,
45 """Add entity from Hue resource."""
46 if isinstance(resource, RelativeRotary):
55 for controller
in (api.sensors.button, api.sensors.relative_rotary):
57 for item
in controller:
58 async_add_entity(EventType.RESOURCE_ADDED, item)
61 config_entry.async_on_unload(
63 async_add_entity, event_filter=EventType.RESOURCE_ADDED
69 """Representation of a Hue Event entity from a button resource."""
73 device_class=EventDeviceClass.BUTTON,
74 translation_key=
"button",
78 def __init__(self, *args: Any, **kwargs: Any) ->
None:
79 """Initialize the entity."""
83 model_id = self.
bridgebridge.api.devices[hue_dev_id].product_data.product_name
84 self._attr_event_types: list[str] = [
86 for event_type
in DEVICE_SPECIFIC_EVENT_TYPES.get(
87 model_id, DEFAULT_BUTTON_EVENT_TYPES
91 "button_id": self.
resourceresource.metadata.control_id
95 def _handle_event(self, event_type: EventType, resource: Button) ->
None:
96 """Handle status event for this resource (or it's parent)."""
97 if event_type == EventType.RESOURCE_UPDATED
and resource.id == self.
resourceresource.id:
98 if resource.button
is None or resource.button.button_report
is None:
100 self.
_trigger_event_trigger_event(resource.button.button_report.event.value)
107 """Representation of a Hue Event entity from a RelativeRotary resource."""
111 device_class=EventDeviceClass.BUTTON,
112 translation_key=
"rotary",
114 RelativeRotaryDirection.CLOCK_WISE.value,
115 RelativeRotaryDirection.COUNTER_CLOCK_WISE.value,
117 has_entity_name=
True,
121 def _handle_event(self, event_type: EventType, resource: RelativeRotary) ->
None:
122 """Handle status event for this resource (or it's parent)."""
123 if event_type == EventType.RESOURCE_UPDATED
and resource.id == self.
resourceresource.id:
125 resource.relative_rotary
is None
126 or resource.relative_rotary.rotary_report
is None
129 event_key = resource.relative_rotary.rotary_report.rotation.direction.value
131 "duration": resource.relative_rotary.rotary_report.rotation.duration,
132 "steps": resource.relative_rotary.rotary_report.rotation.steps,
133 "action": resource.relative_rotary.rotary_report.action.value,
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
None _handle_event(self, EventType event_type, RelativeRotary resource)
None async_write_ha_state(self)
DeviceEntry get_device(HomeAssistant hass, str unique_id)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)