Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion custom_components/opendisplay/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,35 @@ def _refresh_type_value(value: Any) -> RefreshMode:
)


def _is_y_alias(key: Any) -> bool:
"""Return whether a payload key is an unquoted `y` that YAML read as true."""
return key is True or key in {"true", "True"}


def _restore_y_key(element: Any) -> Any:
"""Return an element with a boolean-parsed `y` key named `y` again."""
if not isinstance(element, dict):
return element
restored = {k: v for k, v in element.items() if not _is_y_alias(k)}
aliased = [v for k, v in element.items() if _is_y_alias(k)]
return {"y": aliased[0], **restored} if aliased else restored


def _restore_y_keys(payload: list[Any]) -> list[Any]:
"""Undo YAML 1.1 turning an unquoted `y:` into a boolean key.

The action editor can deliver `y: 10` as `true: 10`, which would otherwise
reach the renderer with no y coordinate at all.
"""
return [_restore_y_key(element) for element in payload]


SCHEMA_DRAWCUSTOM = vol.Schema(
{
vol.Optional("device_id", default=[]): vol.All(cv.ensure_list, [cv.string]),
vol.Optional("label_id", default=[]): vol.All(cv.ensure_list, [cv.string]),
vol.Optional("area_id", default=[]): vol.All(cv.ensure_list, [cv.string]),
vol.Required("payload"): list,
vol.Required("payload"): vol.All(list, _restore_y_keys),
vol.Optional("background", default="white"): cv.string,
vol.Optional("rotate", default=0): vol.All(
vol.Coerce(int), vol.In([0, 90, 180, 270])
Expand Down
60 changes: 60 additions & 0 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -1112,3 +1112,63 @@ async def test_drawcustom_transposes_the_canvas_for_a_rotated_panel(

assert mock_render.await_args.kwargs["width"] == 128
assert mock_render.await_args.kwargs["height"] == 296


@pytest.mark.parametrize(
("element", "expected_y"),
[
({"type": "text", "value": "a", "x": 1, True: 10}, 10),
({"type": "text", "value": "a", "x": 1, "true": 20}, 20),
({"type": "text", "value": "a", "x": 1, "True": 30}, 30),
({"type": "text", "value": "a", "x": 1, "y": 40}, 40),
({"type": "text", "value": "a", "x": 1, "y": 50, "true": 99}, 50),
],
ids=["bool_key", "lowercase_key", "capitalized_key", "plain_y", "explicit_y_wins"],
)
async def test_drawcustom_restores_a_y_key_parsed_as_a_boolean(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_upload_device: MagicMock,
mock_render: MagicMock,
element: dict,
expected_y: int,
) -> None:
"""An unquoted `y:` that YAML read as true still positions the element."""
await hass.services.async_call(
DOMAIN,
"drawcustom",
{
"device_id": [_device_id(hass, mock_config_entry)],
"payload": [element],
},
blocking=True,
)

rendered = mock_render.await_args.kwargs["elements"][0]
assert rendered["y"] == expected_y
assert rendered["x"] == 1
assert not {True, "true", "True"} & rendered.keys()


async def test_drawcustom_payload_keeps_a_true_value_under_other_keys(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
mock_upload_device: MagicMock,
mock_render: MagicMock,
) -> None:
"""Only the key is restored; a value of true is left alone."""
await hass.services.async_call(
DOMAIN,
"drawcustom",
{
"device_id": [_device_id(hass, mock_config_entry)],
"payload": [{"type": "text", "visible": True, "y": 5}],
},
blocking=True,
)

assert mock_render.await_args.kwargs["elements"][0] == {
"type": "text",
"visible": True,
"y": 5,
}