Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle null-valued ins and outs for miotaction #1943

Merged
merged 3 commits into from
Oct 26, 2024
Merged
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
9 changes: 9 additions & 0 deletions miio/miot_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ class MiotAction(MiotBaseModel):
inputs: Any = Field(alias="in")
outputs: Any = Field(alias="out")

@root_validator(pre=True)
def default_null_to_empty(cls, values):
"""Coerce null values for in&out to empty lists."""
if values["in"] is None:
values["in"] = []
if values["out"] is None:
values["out"] = []
return values

def fill_from_parent(self, service: "MiotService"):
"""Overridden to convert inputs and outputs to property references."""
super().fill_from_parent(service)
Expand Down
20 changes: 20 additions & 0 deletions miio/tests/test_miot_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ def test_action():
assert act.plain_name == "dummy-action"


def test_action_with_nulls():
"""Test that actions with null ins and outs are parsed correctly."""
simple_action = """\
{
"iid": 1,
"type": "urn:miot-spec-v2:action:dummy-action:0000001:dummy:1",
"description": "Description",
"in": null,
"out": null
}"""
act = MiotAction.parse_raw(simple_action)
assert act.aiid == 1
assert act.urn.type == "action"
assert act.description == "Description"
assert act.inputs == []
assert act.outputs == []

assert act.plain_name == "dummy-action"


@pytest.mark.parametrize(
("urn_string", "unexpected"),
[
Expand Down
Loading