-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsc-custom-cards.allium
More file actions
482 lines (389 loc) · 14.2 KB
/
sc-custom-cards.allium
File metadata and controls
482 lines (389 loc) · 14.2 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
-- allium: 1
-- sc-custom-cards.allium
-- Home Assistant custom dashboard cards with visual configuration editors
-- Scope: Area Card and History Bars Card configuration, rendering, and editing
-- Includes: Card configuration schema, visual editors, rendering logic, localization
-- Excludes:
-- - Home Assistant core platform (external dependency)
-- - LitElement web components framework (implementation detail)
-- - Build tooling and deployment (infrastructure)
-- ============================================================================
-- Area Card Domain
-- ============================================================================
-- An Area Card displays summary information about a Home Assistant area,
-- including temperature, humidity, and grouped entity status summaries.
entity AreaCard {
area: Area
style: header | full = full
color: Color?
summaries: Set<EntitySummary>
tap_action: Action?
hold_action: Action?
double_tap_action: Action?
-- Derived: whether any alarm entity is active
has_active_alarm: summaries.flatMap(s => s.alarm_entities).any(e => e.state = on)
}
entity EntitySummary {
name: String
icon: Icon
entities: Set<EntityId>
alarm_entities: Set<EntityId>
tap_action: Action?
hold_action: Action?
double_tap_action: Action?
-- Constraints
invariant: entities.size > 0 or alarm_entities.size > 0
}
-- Area Card configuration migration from deprecated preset fields
rule MigratePresetToSummary {
when: AreaCard.configured(
config: contains(presence | alarm | door | light)
)
let preset_definitions = {
presence: {name: "Presence", icon: "mdi:account-multiple"},
alarm: {name: "Alarms", icon: "mdi:alarm-bell", is_alarm: true},
door: {name: "Doors", icon: "mdi:door"},
light: {name: "Lights", icon: "mdi:lightbulb"}
}
ensures:
for preset_key in [presence, alarm, door, light]:
if config[preset_key] exists:
let def = preset_definitions[preset_key]
EntitySummary.created(
name: def.name,
icon: def.icon,
entities: config[preset_key],
alarm_entities: if def.is_alarm then config[preset_key] else []
)
ensures: config[presence, alarm, door, light] removed
}
-- ============================================================================
-- History Bars Card Domain
-- ============================================================================
-- A History Bars Card displays entity values as horizontal bars with
-- color-coded thresholds.
entity BarsCard {
max: Number
entities: Set<EntityBar>
thresholds: Set<Threshold>
tap_action: Action?
hold_action: Action?
double_tap_action: Action?
-- Constraints
invariant: max > 0
invariant: entities.size > 0
}
entity EntityBar {
entity: EntityId
name: String?
icon: Icon?
color: Color?
min: Number?
max: Number?
hide_name: Boolean = false
thresholds: Set<Threshold>
tap_action: Action?
hold_action: Action?
double_tap_action: Action?
-- Derived: effective thresholds (entity-level overrides card-level)
effective_thresholds: if thresholds.size > 0 then thresholds else parent_card.thresholds
-- Derived: color based on thresholds
bar_color:
if color then color
else effective_thresholds
.filter(t => entity.value >= t.value)
.max_by(t => t.value)
.color
}
entity Threshold {
value: Number
color: Color
invariant: value >= 0
}
-- ============================================================================
-- Visual Editor Domain
-- ============================================================================
-- Visual editors allow users to configure cards through a UI instead of YAML.
entity CardEditor {
card_config: AreaCard | BarsCard
editing_state: viewing | editing_summary(index: Integer) | editing_entity(index: Integer)
locale: Language
}
-- Area Card Editor: Adding a summary group
rule AddSummary {
when: User.clicks(add_summary_button)
on: editor: CardEditor(card_config: AreaCard)
ensures: EntitySummary.created(
name: "New Summary",
icon: "mdi:help-circle",
entities: [],
alarm_entities: []
)
ensures: editor.card_config.summaries += created_summary
}
-- Area Card Editor: Quick-add preset summaries
rule QuickAddPresetSummary {
when: User.clicks(quick_add_button(preset_type))
on: editor: CardEditor(card_config: AreaCard)
requires: preset_type in [presence, light, door, alarm]
let preset_definitions = {
presence: {name: "Presence", icon: "mdi:account-multiple"},
alarm: {name: "Alarms", icon: "mdi:alarm-bell", is_alarm: true},
door: {name: "Doors", icon: "mdi:door"},
light: {name: "Lights", icon: "mdi:lightbulb"}
}
let def = preset_definitions[preset_type]
ensures: EntitySummary.created(
name: def.name,
icon: def.icon,
entities: [],
alarm_entities: []
)
ensures: editor.card_config.summaries += created_summary
}
-- Area Card Editor: Delete summary with confirmation
rule DeleteSummary {
when: User.confirms(delete_summary(summary))
on: editor: CardEditor(card_config: AreaCard)
ensures: editor.card_config.summaries -= summary
}
-- Area Card Editor: Reorder summaries via drag-and-drop
rule ReorderSummaries {
when: User.drags(summary, from: old_index, to: new_index)
on: editor: CardEditor(card_config: AreaCard)
ensures: editor.card_config.summaries reordered(from: old_index, to: new_index)
}
-- Area Card Editor: Edit summary details
rule EditSummary {
when: User.clicks(edit_summary_button(summary))
on: editor: CardEditor(card_config: AreaCard)
ensures: editor.editing_state = editing_summary(index: summary.index)
}
rule SaveSummaryEdits {
when: User.changes(summary_field, value)
on: editor: CardEditor(editing_state: editing_summary(index))
ensures: editor.card_config.summaries[index][summary_field] = value
ensures: CardConfigChanged.fired(config: editor.card_config)
}
rule NavigateBackFromSummary {
when: User.clicks(back_button)
on: editor: CardEditor(editing_state: editing_summary(_))
ensures: editor.editing_state = viewing
}
-- Bars Card Editor: Add entity via picker
rule AddEntityToBar {
when: User.selects(entity_picker, entity_id)
on: editor: CardEditor(card_config: BarsCard)
ensures: EntityBar.created(
entity: entity_id,
name: "",
icon: "",
color: ""
)
ensures: editor.card_config.entities += created_entity_bar
ensures: entity_picker.value = cleared
}
-- Bars Card Editor: Delete entity bar
rule DeleteEntityBar {
when: User.confirms(delete_entity(entity_bar))
on: editor: CardEditor(card_config: BarsCard)
ensures: editor.card_config.entities -= entity_bar
}
-- Bars Card Editor: Reorder entities via drag-and-drop
rule ReorderEntities {
when: User.drags(entity_bar, from: old_index, to: new_index)
on: editor: CardEditor(card_config: BarsCard)
ensures: editor.card_config.entities reordered(from: old_index, to: new_index)
}
-- Bars Card Editor: Add threshold
rule AddThreshold {
when: User.clicks(add_threshold_button)
on: editor: CardEditor(card_config: BarsCard)
ensures: Threshold.created(
value: 0,
color: "#ff0000"
)
ensures: editor.card_config.thresholds += created_threshold
}
-- Bars Card Editor: Delete threshold
rule DeleteThreshold {
when: User.confirms(delete_threshold(threshold))
on: editor: CardEditor(card_config: BarsCard)
ensures: editor.card_config.thresholds -= threshold
}
-- Bars Card Editor: Reorder thresholds via drag-and-drop
rule ReorderThresholds {
when: User.drags(threshold, from: old_index, to: new_index)
on: editor: CardEditor(card_config: BarsCard)
ensures: editor.card_config.thresholds reordered(from: old_index, to: new_index)
}
-- ============================================================================
-- Localization Domain
-- ============================================================================
-- Multi-language support for editor labels and UI text.
entity Localization {
supported_languages: Set<Language> = {en, es, gl, fr, de, ca, eu}
translations: Map<Language, Map<String, String>>
}
-- Label resolution with fallback chain
rule ResolveLabel {
when: Editor.needs_label(key, user_language)
let translation =
translations[user_language][key]
or translations[en][key] -- fallback to English
or key -- fallback to key itself
ensures: Label.resolved(text: translation)
}
-- Field labels use computeLabel callback pattern
entity FieldLabel {
field_name: String
translation_key: String
-- Mapping from field names to translation keys
label_map: Map<String, String> = {
area: "config.area",
style: "config.style",
color: "config.color",
name: "config.name",
icon: "config.icon",
entities: "config.entities",
alarm_entities: "config.alarm_entities",
tap_action: "config.tap",
hold_action: "config.hold",
double_tap_action: "config.double_tap",
max: "config.max",
min: "config.min",
value: "config.value",
hideName: "config.hideName"
}
}
-- Helper text for specific fields
rule ShowFieldHelper {
when: Editor.renders_field(field_name)
on: editor: CardEditor
let helper_text =
if field_name = "entities" then translations[editor.locale]["config.entities_helper"]
else if field_name = "alarm_entities" then translations[editor.locale]["config.alarm_entities_helper"]
else undefined
ensures: if helper_text then HelperText.displayed(text: helper_text)
}
-- ============================================================================
-- Entity Filtering Domain
-- ============================================================================
-- Entity selectors filter by area when editing summaries.
rule FilterEntitiesByArea {
when: Editor.renders_entity_selector(field)
on: editor: CardEditor(
card_config: AreaCard(area: selected_area),
editing_state: editing_summary(_)
)
ensures: EntitySelector.filtered(
area: selected_area,
field: field
)
}
-- ============================================================================
-- Card Rendering Domain
-- ============================================================================
-- How cards display based on configuration and entity state.
rule RenderAreaCard {
when: AreaCard.render(style)
ensures:
if style = full then FullCardView.displayed(
header: area.name,
temperature: area.temperature,
humidity: area.humidity,
summaries: for s in summaries render EntitySummaryView(s)
)
else if style = header then HeaderOnlyView.displayed(
header: area.name,
temperature: area.temperature,
humidity: area.humidity
)
}
rule RenderEntitySummary {
when: EntitySummary.render()
on: summary: EntitySummary
let active_count = summary.entities.filter(e => e.state = on).size
let total_count = summary.entities.size
ensures: EntitySummaryView.displayed(
name: summary.name,
icon: summary.icon,
count: "{active_count}/{total_count}",
alarm_active: summary.alarm_entities.any(e => e.state = on)
)
}
rule RenderBarsCard {
when: BarsCard.render()
on: card: BarsCard
ensures: BarsCardView.displayed(
bars: for entity_bar in card.entities render EntityBarView(entity_bar)
)
}
rule RenderEntityBar {
when: EntityBar.render()
on: bar: EntityBar
let value = bar.entity.value or 0
let max_value = bar.max or parent_card.max
let min_value = bar.min or 0
let percentage = ((value - min_value) / (max_value - min_value)) * 100
ensures: EntityBarView.displayed(
name: if not bar.hide_name then (bar.name or bar.entity.friendly_name),
icon: bar.icon or bar.entity.icon,
color: bar.bar_color,
width: percentage,
value: value
)
}
-- ============================================================================
-- Action Handling Domain
-- ============================================================================
-- Cards and summaries can respond to user interactions.
rule HandleTapAction {
when: User.taps(element)
on: element: AreaCard | EntitySummary | BarsCard | EntityBar
requires: element.tap_action exists
ensures: Action.executed(element.tap_action)
}
rule HandleHoldAction {
when: User.holds(element)
on: element: AreaCard | EntitySummary | BarsCard | EntityBar
requires: element.hold_action exists
ensures: Action.executed(element.hold_action)
}
rule HandleDoubleClick {
when: User.double_clicks(element)
on: element: AreaCard | EntitySummary | BarsCard | EntityBar
requires: element.double_tap_action exists
ensures: Action.executed(element.double_tap_action)
}
-- ============================================================================
-- External Types
-- ============================================================================
-- Types provided by Home Assistant platform
external entity Area {
area_id: String
name: String
temperature: Number?
humidity: Number?
}
external entity EntityId {
friendly_name: String
state: String
value: Number?
icon: Icon?
}
external type Icon = String -- MDI icon name (e.g., "mdi:lightbulb")
external type Color = String -- CSS color (e.g., "#ff0000" or "red")
external type Language = en | es | gl | fr | de | ca | eu
external type Action -- Home Assistant action configuration
external event User.clicks(target)
external event User.taps(target)
external event User.holds(target)
external event User.double_clicks(target)
external event User.drags(item, from: Integer, to: Integer)
external event User.confirms(operation)
external event User.selects(picker, value)
external event User.changes(field, value)
external event CardConfigChanged.fired(config)
external event HelperText.displayed(text: String)
external event Label.resolved(text: String)