Skip to content

Commit 41406a6

Browse files
committed
Fix live Entity State widget preview
1 parent af3777d commit 41406a6

20 files changed

Lines changed: 594 additions & 65 deletions

‎CHANGELOG.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
# Changelog
22

3+
## 0.3.2
4+
5+
- Make the designer preview use the live Liquid/TRMNL HTML composed by the
6+
integration instead of separate frontend sample values.
7+
- Rebuild Entity State as an adaptive square, wide, or tall tile with current
8+
Home Assistant state, unit, friendly name, and a local device-class icon.
9+
- Add Entity State controls for icon/name visibility and preserve their
10+
defaults for projects created with earlier versions.
11+
- Add the missing TRMNL screen-size classes and verify the 800 × 480 output in
12+
the persistent Renderer App at 178.5 ms warm render time.
13+
314
## 0.3.1
415

516
- Stop bundling and registering a private WebAwesome copy in the Home Assistant

‎custom_components/opendisplay_studio/composer.py‎

Lines changed: 99 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from homeassistant.core import HomeAssistant
1010
from homeassistant.exceptions import HomeAssistantError
1111

12-
from .data_providers import CalendarProvider, EntityStateProvider
12+
from .data_providers import ICON_PATHS, CalendarProvider, EntityStateProvider
1313
from .liquid_renderer import LIQUID
1414
from .projects import Project
1515
from .widgets import TEMPLATES, definition, with_defaults
@@ -29,6 +29,67 @@ class ProjectComposeError(Exception):
2929
"""Raised when current Home Assistant data cannot be resolved."""
3030

3131

32+
STUDIO_STYLES = """
33+
<style>
34+
.studio-screen{width:var(--studio-width)!important;height:var(--studio-height)!important;margin:0!important;padding:0!important;overflow:hidden!important;background:#fff;box-sizing:border-box}
35+
.studio-screen .view--full{width:100%!important;height:100%!important;margin:0!important;padding:0!important;overflow:hidden!important}
36+
.studio-grid{display:grid;width:100%;height:100%;padding:var(--studio-gap);gap:var(--studio-gap);box-sizing:border-box}
37+
.studio-region{position:relative;min-width:0;min-height:0;overflow:hidden;border:1px solid #111;background:#fff;box-sizing:border-box;container-type:size}
38+
.studio-region>.item{width:100%!important;height:100%!important;margin:0!important;padding:0!important}
39+
.studio-entity,.studio-entity__content{width:100%;height:100%;box-sizing:border-box}
40+
.studio-entity__content{display:flex!important;align-items:center;justify-content:center;gap:clamp(4px,4cqh,14px);padding:clamp(7px,7cqh,22px)!important;overflow:hidden}
41+
.studio-entity__icon{display:block;flex:0 0 auto;width:clamp(20px,22cqh,58px);height:clamp(20px,22cqh,58px);fill:currentColor}
42+
.studio-entity__name{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-size:clamp(9px,7cqh,20px)!important;font-weight:700!important;line-height:1!important;text-transform:none!important}
43+
.studio-entity__reading{display:flex;min-width:0;align-items:baseline;justify-content:center;gap:.18em;white-space:nowrap}
44+
.studio-entity__value{font-size:clamp(25px,34cqh,84px)!important;font-weight:500!important;line-height:.9!important;letter-spacing:-.045em!important}
45+
.studio-entity__unit{font-size:clamp(9px,10cqh,24px)!important;font-weight:700!important;line-height:1!important}
46+
.studio-entity__rule{display:none;background:currentColor}
47+
.studio-entity--square .studio-entity__content{flex-direction:column}
48+
.studio-entity--square .studio-entity__icon{width:clamp(22px,20cqw,64px);height:clamp(22px,20cqw,64px)}
49+
.studio-entity--square .studio-entity__name{max-width:100%;font-size:clamp(9px,7cqw,19px)!important}
50+
.studio-entity--square .studio-entity__value{font-size:clamp(25px,27cqw,78px)!important}
51+
.studio-entity--wide .studio-entity__content{flex-direction:row}
52+
.studio-entity--wide .studio-entity__name{flex:1 1 auto}
53+
.studio-entity--wide .studio-entity__rule{display:block;width:1px;height:60%;flex:0 0 1px}
54+
.studio-entity--wide .studio-entity__reading{flex:0 1 auto;margin-left:auto}
55+
.studio-entity--tall .studio-entity__content{flex-direction:column}
56+
.studio-entity--tall .studio-entity__icon{width:clamp(22px,32cqw,62px);height:clamp(22px,32cqw,62px)}
57+
.studio-entity--tall .studio-entity__name{max-width:100%;font-size:clamp(8px,12cqw,18px)!important}
58+
.studio-entity--tall .studio-entity__rule{display:block;width:70%;height:1px;flex:0 0 1px}
59+
.studio-entity--tall .studio-entity__reading{flex-direction:column;align-items:center;gap:2px}
60+
.studio-entity--tall .studio-entity__value{font-size:clamp(24px,35cqw,66px)!important}
61+
.studio-entity--tall .studio-entity__unit{font-size:clamp(9px,15cqw,20px)!important}
62+
</style>
63+
"""
64+
65+
66+
def _screen_size(width: int, height: int) -> str:
67+
"""Map arbitrary display dimensions to the nearest TRMNL size tier."""
68+
longest = max(width, height)
69+
if longest <= 400:
70+
return "screen--sm"
71+
if longest >= 1_000:
72+
return "screen--lg"
73+
return "screen--md"
74+
75+
76+
def _region_shape(project: Project, region: dict[str, Any], *, gap: int) -> str:
77+
"""Classify a region using its physical aspect ratio, not grid spans alone."""
78+
grid = project["grid"]
79+
project_width = int(project.get("width", 800))
80+
project_height = int(project.get("height", 480))
81+
cell_width = (project_width - gap * (grid["columns"] + 1)) / grid["columns"]
82+
cell_height = (project_height - gap * (grid["rows"] + 1)) / grid["rows"]
83+
width = cell_width * region["columnSpan"] + gap * (region["columnSpan"] - 1)
84+
height = cell_height * region["rowSpan"] + gap * (region["rowSpan"] - 1)
85+
ratio = width / max(1, height)
86+
if ratio >= 1.55:
87+
return "wide"
88+
if ratio <= 0.72:
89+
return "tall"
90+
return "square"
91+
92+
3293
def _requirement_sources(
3394
config: dict[str, Any], requirement: dict[str, Any]
3495
) -> list[str]:
@@ -99,10 +160,24 @@ def _resolve_widget_data(
99160
values = [
100161
entities.get(
101162
source,
102-
{"state": "Unavailable", "unit": "", "name": source},
163+
{
164+
"state": "Unavailable",
165+
"unit": "",
166+
"name": source,
167+
"iconPath": ICON_PATHS["default"],
168+
},
103169
)
104170
for source in sources
105171
]
172+
if not values and requirement.get("cardinality") != "many":
173+
values = [
174+
{
175+
"state": "Unavailable",
176+
"unit": "",
177+
"name": "Choose an entity",
178+
"iconPath": ICON_PATHS["default"],
179+
}
180+
]
106181
elif requirement["provider"] == "calendar":
107182
range_key = requirement.get("rangeConfigKey", "days")
108183
values = [
@@ -138,24 +213,37 @@ async def async_compose_project(
138213

139214
liquid_ms = 0.0
140215
fragments: list[str] = []
216+
width = int(project.get("width", 800))
217+
height = int(project.get("height", 480))
218+
gap = max(3, min(10, round(min(width, height) / 60)))
141219
for region in project["regions"]:
142220
widget = region.get("widget")
143221
fragment = ""
144222
if widget is not None:
145223
widget_type = widget["type"]
146224
config = with_defaults(widget_type, widget["config"])
147225
data = _resolve_widget_data(widget_type, config, entities, calendars)
226+
if widget_type == "entity-state" and data.get("entity") is not None:
227+
title = str(config.get("title", "")).strip()
228+
data["entity"]["displayName"] = title or data["entity"]["name"]
148229
result = LIQUID.render(
149-
TEMPLATES[widget_type], {"config": config, "data": data}
230+
TEMPLATES[widget_type],
231+
{
232+
"config": config,
233+
"data": data,
234+
"region": {"shape": _region_shape(project, region, gap=gap)},
235+
},
150236
)
151237
fragment = result.html
152238
liquid_ms += result.milliseconds
153239
style = (
154240
f"grid-row:{region['row']} / span {region['rowSpan']};"
155241
f"grid-column:{region['column']} / span {region['columnSpan']};"
156242
)
243+
shape = _region_shape(project, region, gap=gap)
157244
fragments.append(
158-
f'<section class="studio-region" style="{style}">{fragment}</section>'
245+
f'<section class="studio-region studio-region--{shape}" '
246+
f'style="{style}">{fragment}</section>'
159247
)
160248

161249
mode = {
@@ -166,12 +254,15 @@ async def async_compose_project(
166254
}.get(project["palette"], "screen--1bit")
167255
grid = project["grid"]
168256
body = "".join(fragments)
257+
size = _screen_size(width, height)
258+
portrait = " screen--portrait" if height > width else ""
169259
html = (
170-
f'<main class="screen {mode}"><div class="view view--full">'
171-
f'<div class="studio-grid" style="display:grid;grid-template-columns:'
260+
f'<main class="screen {mode} {size}{portrait} studio-screen" '
261+
f'style="--studio-width:{width}px;--studio-height:{height}px;'
262+
f'--studio-gap:{gap}px">{STUDIO_STYLES}<div class="view view--full">'
263+
f'<div class="studio-grid" style="grid-template-columns:'
172264
f"repeat({grid['columns']},minmax(0,1fr));grid-template-rows:"
173-
f"repeat({grid['rows']},minmax(0,1fr));gap:8px;width:100%;height:100%;"
174-
'padding:8px;box-sizing:border-box">'
265+
f'repeat({grid["rows"]},minmax(0,1fr))">'
175266
f"{body}</div></div></main>"
176267
)
177268
compose_ms = (perf_counter() - started) * 1_000

‎custom_components/opendisplay_studio/const.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
DOMAIN = "opendisplay_studio"
88
NAME = "OpenDisplay Studio"
9-
INTEGRATION_VERSION = "0.3.1"
9+
INTEGRATION_VERSION = "0.3.2"
1010

1111
API_VERSION = 1
1212
DEFAULT_WIDTH = 800

‎custom_components/opendisplay_studio/data_providers.py‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,55 @@
99
from homeassistant.core import HomeAssistant
1010
from homeassistant.util import dt as dt_util
1111

12+
ICON_PATHS = {
13+
"calendar": "M7 11H9V13H7V11M21 5V19C21 20.11 20.11 21 19 21H5C3.89 21 3 20.1 3 19V5C3 3.9 3.9 3 5 3H6V1H8V3H16V1H18V3H19C20.11 3 21 3.9 21 5M5 7H19V5H5V7M19 19V9H5V19H19M15 13V11H17V13H15M11 13V11H13V13H11M7 15H9V17H7V15M15 17V15H17V17H15M11 17V15H13V17H11Z",
14+
"default": "M11,18H13V16H11V18M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M12,20C7.59,20 4,16.41 4,12C4,7.59 7.59,4 12,4C16.41,4 20,7.59 20,12C20,16.41 16.41,20 12,20M12,6A4,4 0 0,0 8,10H10A2,2 0 0,1 12,8A2,2 0 0,1 14,10C14,12 11,11.75 11,15H13C13,12.75 16,12.5 16,10A4,4 0 0,0 12,6Z",
15+
"heat": "M7.95,3L6.53,5.19L7.95,7.4H7.94L5.95,10.5L4.22,9.6L5.64,7.39L4.22,5.19L6.22,2.09L7.95,3M13.95,2.89L12.53,5.1L13.95,7.3L13.94,7.31L11.95,10.4L10.22,9.5L11.64,7.3L10.22,5.1L12.22,2L13.95,2.89M20,2.89L18.56,5.1L20,7.3V7.31L18,10.4L16.25,9.5L17.67,7.3L16.25,5.1L18.25,2L20,2.89M2,22V14A2,2 0 0,1 4,12H20A2,2 0 0,1 22,14V22H20V20H4V22H2M6,14A1,1 0 0,0 5,15V17A1,1 0 0,0 6,18A1,1 0 0,0 7,17V15A1,1 0 0,0 6,14M10,14A1,1 0 0,0 9,15V17A1,1 0 0,0 10,18A1,1 0 0,0 11,17V15A1,1 0 0,0 10,14M14,14A1,1 0 0,0 13,15V17A1,1 0 0,0 14,18A1,1 0 0,0 15,17V15A1,1 0 0,0 14,14M18,14A1,1 0 0,0 17,15V17A1,1 0 0,0 18,18A1,1 0 0,0 19,17V15A1,1 0 0,0 18,14Z",
16+
"humidity": "M12,3.25C12,3.25 6,10 6,14C6,17.32 8.69,20 12,20A6,6 0 0,0 18,14C18,10 12,3.25 12,3.25M14.47,9.97L15.53,11.03L9.53,17.03L8.47,15.97M9.75,10A1.25,1.25 0 0,1 11,11.25A1.25,1.25 0 0,1 9.75,12.5A1.25,1.25 0 0,1 8.5,11.25A1.25,1.25 0 0,1 9.75,10M14.25,14.5A1.25,1.25 0 0,1 15.5,15.75A1.25,1.25 0 0,1 14.25,17A1.25,1.25 0 0,1 13,15.75A1.25,1.25 0 0,1 14.25,14.5Z",
17+
"illuminance": "M12,9A3,3 0 0,1 15,12A3,3 0 0,1 12,15A3,3 0 0,1 9,12A3,3 0 0,1 12,9M12,4.5C17,4.5 21.27,7.61 23,12C21.27,16.39 17,19.5 12,19.5C7,19.5 2.73,16.39 1,12C2.73,7.61 7,4.5 12,4.5M3.18,12C4.83,15.36 8.24,17.5 12,17.5C15.76,17.5 19.17,15.36 20.82,12C19.17,8.64 15.76,6.5 12,6.5C8.24,6.5 4.83,8.64 3.18,12Z",
18+
"light": "M12,2A7,7 0 0,1 19,9C19,11.38 17.81,13.47 16,14.74V17A1,1 0 0,1 15,18H9A1,1 0 0,1 8,17V14.74C6.19,13.47 5,11.38 5,9A7,7 0 0,1 12,2M9,21V20H15V21A1,1 0 0,1 14,22H10A1,1 0 0,1 9,21M12,4A5,5 0 0,0 7,9C7,11.05 8.23,12.81 10,13.58V16H14V13.58C15.77,12.81 17,11.05 17,9A5,5 0 0,0 12,4Z",
19+
"power": "M7,2H17L13.5,9H17L10,22V14H7V2M9,4V12H12V14.66L14,11H10.24L13.76,4H9Z",
20+
"pressure": "M12,2A10,10 0 0,0 2,12A10,10 0 0,0 12,22A10,10 0 0,0 22,12A10,10 0 0,0 12,2M12,4A8,8 0 0,1 20,12C20,14.4 19,16.5 17.3,18C15.9,16.7 14,16 12,16C10,16 8.2,16.7 6.7,18C5,16.5 4,14.4 4,12A8,8 0 0,1 12,4M14,5.89C13.62,5.9 13.26,6.15 13.1,6.54L11.81,9.77L11.71,10C11,10.13 10.41,10.6 10.14,11.26C9.73,12.29 10.23,13.45 11.26,13.86C12.29,14.27 13.45,13.77 13.86,12.74C14.12,12.08 14,11.32 13.57,10.76L13.67,10.5L14.96,7.29L14.97,7.26C15.17,6.75 14.92,6.17 14.41,5.96C14.28,5.91 14.15,5.89 14,5.89M10,6A1,1 0 0,0 9,7A1,1 0 0,0 10,8A1,1 0 0,0 11,7A1,1 0 0,0 10,6M7,9A1,1 0 0,0 6,10A1,1 0 0,0 7,11A1,1 0 0,0 8,10A1,1 0 0,0 7,9M17,9A1,1 0 0,0 16,10A1,1 0 0,0 17,11A1,1 0 0,0 18,10A1,1 0 0,0 17,9Z",
21+
"switch": "M17 6H7C3.69 6 1 8.69 1 12S3.69 18 7 18H17C20.31 18 23 15.31 23 12S20.31 6 17 6M17 16H7C4.79 16 3 14.21 3 12S4.79 8 7 8H17C19.21 8 21 9.79 21 12S19.21 16 17 16M17 9C15.34 9 14 10.34 14 12S15.34 15 17 15 20 13.66 20 12 18.66 9 17 9Z",
22+
"temperature": "M15 13V5A3 3 0 0 0 9 5V13A5 5 0 1 0 15 13M12 4A1 1 0 0 1 13 5V8H11V5A1 1 0 0 1 12 4Z",
23+
}
24+
25+
MDI_ICON_KEYS = {
26+
"mdi:flash-outline": "power",
27+
"mdi:gauge": "pressure",
28+
"mdi:lightbulb-outline": "light",
29+
"mdi:radiator": "heat",
30+
"mdi:thermometer": "temperature",
31+
"mdi:toggle-switch-outline": "switch",
32+
"mdi:water-percent": "humidity",
33+
}
34+
35+
36+
def _entity_icon_path(entity_id: str, attributes: dict[str, object]) -> str:
37+
"""Choose a deterministic local icon without external image requests."""
38+
configured = attributes.get("icon")
39+
if isinstance(configured, str) and configured in MDI_ICON_KEYS:
40+
return ICON_PATHS[MDI_ICON_KEYS[configured]]
41+
device_class = str(attributes.get("device_class", ""))
42+
key = {
43+
"current": "power",
44+
"energy": "power",
45+
"humidity": "humidity",
46+
"illuminance": "illuminance",
47+
"power": "power",
48+
"pressure": "pressure",
49+
"temperature": "temperature",
50+
"voltage": "power",
51+
}.get(device_class)
52+
if key is None:
53+
key = {
54+
"calendar": "calendar",
55+
"climate": "heat",
56+
"light": "light",
57+
"switch": "switch",
58+
}.get(entity_id.partition(".")[0], "default")
59+
return ICON_PATHS[key]
60+
1261

1362
class EntityStateProvider:
1463
"""Normalize current state-machine values."""
@@ -27,12 +76,14 @@ def get_many(self, entity_ids: set[str]) -> dict[str, dict[str, str]]:
2776
"state": "Unavailable",
2877
"unit": "",
2978
"name": entity_id,
79+
"iconPath": ICON_PATHS["default"],
3080
}
3181
continue
3282
result[entity_id] = {
3383
"state": state.state,
3484
"unit": str(state.attributes.get("unit_of_measurement", "")),
3585
"name": str(state.attributes.get("friendly_name", entity_id)),
86+
"iconPath": _entity_icon_path(entity_id, state.attributes),
3687
}
3788
return result
3889

0 commit comments

Comments
 (0)