1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
"""
Model(s) for button resource on HUE bridge.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_button
"""
from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from .resource import ResourceIdentifier, ResourceTypes
class ButtonEvent(Enum):
"""Enum with possible button events."""
INITIAL_PRESS = "initial_press"
REPEAT = "repeat"
SHORT_RELEASE = "short_release"
LONG_PRESS = "long_press"
LONG_RELEASE = "long_release"
DOUBLE_SHORT_RELEASE = "double_short_release"
UNKNOWN = "unknown"
@classmethod
def _missing_(cls: type, value: object): # noqa: ARG003
"""Set default enum member if an unknown value is provided."""
return ButtonEvent.UNKNOWN
@dataclass
class ButtonReport:
"""
Represent ButtonReport as retrieved from api.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_button_get
"""
updated: datetime
event: ButtonEvent
@dataclass
class ButtonFeature:
"""Represent ButtonFeature object as used by the Hue api."""
button_report: ButtonReport | None = None
last_event: ButtonEvent | None = None # deprecated
repeat_interval: int | None = None
event_values: list[ButtonEvent] | None = None
@property
def value(self) -> ButtonEvent:
"""Return the actual/current value."""
# prefer new style attribute (not available on older firmware versions)
if self.button_report is not None:
return self.button_report.event
if self.last_event is not None:
return self.last_event
return ButtonEvent.UNKNOWN
@dataclass
class ButtonMetadata:
"""Represent ButtonMetadata object as used by the Button resource."""
# number of control within switch (value between 0..8).
# Meaning in combination with type
# - dots: Number of dots
# - number: Number printed on device
# - other: a logical order of controls in switch
control_id: int
@dataclass
class Button:
"""
Represent a (full) `Button` resource when retrieved from the api.
https://developers.meethue.com/develop/hue-api-v2/api-reference/#resource_button_get
"""
id: str
owner: ResourceIdentifier
metadata: ButtonMetadata
button: ButtonFeature | None = None
id_v1: str | None = None
type: ResourceTypes = ResourceTypes.BUTTON
|