forked from unfoldedcircle/integration-python-library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
194 lines (157 loc) · 5.13 KB
/
ui.py
File metadata and controls
194 lines (157 loc) · 5.13 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""
User interface definitions.
:copyright: (c) 2024 by Unfolded Circle ApS.
:license: MPL-2.0, see LICENSE for more details.
"""
from dataclasses import KW_ONLY, dataclass
from enum import Enum
@dataclass
class EntityCommand:
"""Remote command definition for a button mapping or UI page definition."""
cmd_id: str
params: dict[str, str | int | list[str]] | None = None
class Buttons(str, Enum):
"""Physical buttons."""
BACK = "BACK"
HOME = "HOME"
VOICE = "VOICE"
VOLUME_UP = "VOLUME_UP"
VOLUME_DOWN = "VOLUME_DOWN"
MUTE = "MUTE"
DPAD_UP = "DPAD_UP"
DPAD_DOWN = "DPAD_DOWN"
DPAD_LEFT = "DPAD_LEFT"
DPAD_RIGHT = "DPAD_RIGHT"
DPAD_MIDDLE = "DPAD_MIDDLE"
GREEN = "GREEN"
YELLOW = "YELLOW"
RED = "RED"
BLUE = "BLUE"
CHANNEL_UP = "CHANNEL_UP"
CHANNEL_DOWN = "CHANNEL_DOWN"
PREV = "PREV"
PLAY = "PLAY"
NEXT = "NEXT"
POWER = "POWER"
STOP = "STOP"
RECORD = "RECORD"
MENU = "MENU"
@dataclass
class DeviceButtonMapping:
"""Physical button command mapping."""
button: str
"""Physical button identifier. See Buttons for Remote Two identifiers."""
short_press: EntityCommand | None = None
"""Short press command of the button."""
long_press: EntityCommand | None = None
"""Long press command of the button."""
def create_btn_mapping(
button: Buttons,
short: str | EntityCommand | None = None,
long: str | EntityCommand | None = None,
) -> DeviceButtonMapping:
"""
Create a physical button command mapping.
:param button: physical button identifier.
:param short: associated short-press command to the physical button.
A string parameter corresponds to a simple command, whereas an
``EntityCommand`` allows to customize the command.
:param long: associated long-press command to the physical button
:return: the created DeviceButtonMapping
"""
if isinstance(short, str):
short = EntityCommand(short)
if isinstance(long, str):
long = EntityCommand(long)
return DeviceButtonMapping(button.value, short_press=short, long_press=long)
@dataclass
class Size:
"""Item size in the button grid. Default size if not specified: 1x1."""
width: int = 1
height: int = 1
@dataclass
class Location:
"""Button placement in the grid with 0-based coordinates."""
x: int
y: int
@dataclass
class UiItem:
"""
A user interface item is either an icon or text.
- Icon and text items can be static or linked to a command specified in the
`command` field.
- Default size is 1x1 if not specified.
"""
type: str
location: Location
size: Size | None = None
icon: str | None = None
text: str | None = None
command: EntityCommand | None = None
def create_ui_text(
text: str,
x: int,
y: int,
size: Size | None = None,
cmd: str | EntityCommand | None = None,
) -> UiItem:
"""
Create a text UI item.
:param text: the text to show in the UI item.
:param x: x-position, 0-based.
:param y: y-position, 0-based.
:param size: item size, defaults to 1 x 1 if not specified.
:param cmd: associated command to the text item. A string parameter corresponds to
a simple command, whereas an ``EntityCommand`` allows to customize the
command for example with number of repeats.
:return: the created UiItem
"""
if isinstance(cmd, str):
cmd = EntityCommand(cmd)
return UiItem("text", Location(x, y), size=size, text=text, command=cmd)
def create_ui_icon(
icon: str,
x: int,
y: int,
size: Size | None = None,
cmd: str | EntityCommand | None = None,
) -> UiItem:
"""
Create an icon UI item.
The icon identifier consists of a prefix and a resource identifier,
separated by `:`. Available prefixes:
- `uc:` - integrated icon font
- `custom:` - custom resource
:param icon: the icon identifier of the icon to show in the UI item.
:param x: x-position, 0-based.
:param y: y-position, 0-based.
:param size: item size, defaults to 1 x 1 if not specified.
:param cmd: associated command to the text item. A string parameter corresponds to
a simple command, whereas an ``EntityCommand`` allows to customize the
command for example with number of repeats.
:return: the created UiItem
"""
if isinstance(cmd, str):
cmd = EntityCommand(cmd)
return UiItem("icon", Location(x, y), size=size, icon=icon, command=cmd)
@dataclass
class UiPage:
"""
Definition of a complete user interface page.
Default grid size is 4x6 if not specified.
"""
page_id: str
name: str
_: KW_ONLY
grid: Size = None
items: list[UiItem] = None
def __post_init__(self):
"""Post initialization to set required fields."""
# grid and items are required Integration-API fields
if self.grid is None:
self.grid = Size(4, 6)
if self.items is None:
self.items = []
def add(self, item: UiItem):
"""Append the given UiItem to the page items."""
self.items.append(item)