Skip to content

Commit

Permalink
Handle null-valued ins and outs for miotaction (#1943)
Browse files Browse the repository at this point in the history
This PR parses null-valued ins & outs for miotactions as empty lists to
avoid crashing on unexpected schemas.
  • Loading branch information
rytilahti authored Oct 26, 2024
1 parent 31a4b81 commit e88159f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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

0 comments on commit e88159f

Please sign in to comment.