Skip to content

Commit e88159f

Browse files
authored
Handle null-valued ins and outs for miotaction (#1943)
This PR parses null-valued ins & outs for miotactions as empty lists to avoid crashing on unexpected schemas.
1 parent 31a4b81 commit e88159f

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

miio/miot_models.py

+9
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ class MiotAction(MiotBaseModel):
159159
inputs: Any = Field(alias="in")
160160
outputs: Any = Field(alias="out")
161161

162+
@root_validator(pre=True)
163+
def default_null_to_empty(cls, values):
164+
"""Coerce null values for in&out to empty lists."""
165+
if values["in"] is None:
166+
values["in"] = []
167+
if values["out"] is None:
168+
values["out"] = []
169+
return values
170+
162171
def fill_from_parent(self, service: "MiotService"):
163172
"""Overridden to convert inputs and outputs to property references."""
164173
super().fill_from_parent(service)

miio/tests/test_miot_models.py

+20
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,26 @@ def test_action():
138138
assert act.plain_name == "dummy-action"
139139

140140

141+
def test_action_with_nulls():
142+
"""Test that actions with null ins and outs are parsed correctly."""
143+
simple_action = """\
144+
{
145+
"iid": 1,
146+
"type": "urn:miot-spec-v2:action:dummy-action:0000001:dummy:1",
147+
"description": "Description",
148+
"in": null,
149+
"out": null
150+
}"""
151+
act = MiotAction.parse_raw(simple_action)
152+
assert act.aiid == 1
153+
assert act.urn.type == "action"
154+
assert act.description == "Description"
155+
assert act.inputs == []
156+
assert act.outputs == []
157+
158+
assert act.plain_name == "dummy-action"
159+
160+
141161
@pytest.mark.parametrize(
142162
("urn_string", "unexpected"),
143163
[

0 commit comments

Comments
 (0)