Skip to content

Commit

Permalink
Allow passing custom name for miotdevice.set_property_by (#1576)
Browse files Browse the repository at this point in the history
This allows passing a name to use instead of the hardcoded
`set-{siid}-{piid}` as `did` for miotdevice requests:
```
dev = MiotDevice(...)
dev.get_property_by(111, 222, 333, name="dummy-name")
```
will cause request did to be set to `dummy_name`.
```
{"did": "dummy-name", "siid": 111, "piid": 222, "value": 333}
```

**Breaking change**
The `set_property_by`'s `value_type` is converted to keyword-only
argument.
  • Loading branch information
rytilahti authored Nov 7, 2022
1 parent ed592a0 commit 7447020
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion miio/miot_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,16 @@ def get_property_by(self, siid: int, piid: int):
click.argument(
"value_type", type=EnumType(MiotValueType), required=False, default=None
),
click.option("--name", required=False),
)
def set_property_by(
self,
siid: int,
piid: int,
value: Union[int, float, str, bool],
*,
value_type: Any = None,
name: str = None,
):
"""Set a single property (siid/piid) to given value.
Expand All @@ -166,9 +169,12 @@ def set_property_by(
if value_type is not None:
value = value_type.value(value)

if name is None:
name = f"set-{siid}-{piid}"

return self.send(
"set_properties",
[{"did": f"set-{siid}-{piid}", "siid": siid, "piid": piid, "value": value}],
[{"did": name, "siid": siid, "piid": piid, "value": value}],
)

def set_property(self, property_key: str, value):
Expand Down
14 changes: 13 additions & 1 deletion miio/tests/test_miotdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_get_property_by(dev):
def test_set_property_by(dev, value_type, value):
siid = 1
piid = 1
_ = dev.set_property_by(siid, piid, value, value_type)
_ = dev.set_property_by(siid, piid, value, value_type=value_type)

if value_type is not None:
value = value_type.value(value)
Expand All @@ -72,6 +72,18 @@ def test_set_property_by(dev, value_type, value):
)


def test_set_property_by_name(dev):
siid = 1
piid = 1
value = 1
_ = dev.set_property_by(siid, piid, value, name="test-name")

dev.send.assert_called_with(
"set_properties",
[{"did": "test-name", "siid": siid, "piid": piid, "value": value}],
)


def test_call_action_by(dev):
siid = 1
aiid = 1
Expand Down

0 comments on commit 7447020

Please sign in to comment.