Skip to content

Commit 0753f1b

Browse files
committed
feat: add Device.get_command_definition() helper (#766)
1 parent 00a257f commit 0753f1b

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

pyoverkiz/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,14 @@ def select_first_command(self, commands: list[str | OverkizCommand]) -> str | No
440440
return None
441441
return self.definition.commands.select(commands)
442442

443+
def get_command_definition(
444+
self, command: str | OverkizCommand
445+
) -> CommandDefinition | None:
446+
"""Return the CommandDefinition for a command, or None if unavailable."""
447+
if self.definition is None:
448+
return None
449+
return self.definition.commands.get(str(command))
450+
443451
def get_state_value(self, state: str) -> StateType | None:
444452
"""Get value of a single state, or None if not found or None."""
445453
return self.states.select_value([state])

tests/test_models.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,3 +981,56 @@ def test_persisted_action_group_id_property_returns_str(self):
981981
result = group.id
982982
assert isinstance(result, str)
983983
assert result == "abc-123"
984+
985+
986+
def test_get_command_definition_found():
987+
"""Device.get_command_definition() returns CommandDefinition when command exists."""
988+
from pyoverkiz.models import CommandDefinition
989+
990+
device = _make_device(
991+
{
992+
**RAW_DEVICES,
993+
"definition": {
994+
**RAW_DEVICES["definition"],
995+
"commands": [{"commandName": "open", "nparams": 0}],
996+
},
997+
}
998+
)
999+
cd = device.get_command_definition("open")
1000+
assert cd is not None
1001+
assert isinstance(cd, CommandDefinition)
1002+
assert cd.nparams == 0
1003+
1004+
1005+
def test_get_command_definition_not_found():
1006+
"""Device.get_command_definition() returns None when command doesn't exist."""
1007+
device = _make_device(
1008+
{
1009+
**RAW_DEVICES,
1010+
"definition": {
1011+
**RAW_DEVICES["definition"],
1012+
"commands": [],
1013+
},
1014+
}
1015+
)
1016+
assert device.get_command_definition("open") is None
1017+
1018+
1019+
def test_get_command_definition_no_definition():
1020+
"""Device.get_command_definition() returns None when device has no definition."""
1021+
from pyoverkiz.enums import ProductType
1022+
from pyoverkiz.models import States
1023+
1024+
device = Device(
1025+
attributes=States(),
1026+
available=True,
1027+
enabled=True,
1028+
label="Test",
1029+
device_url="io://1234-5678-9012/1",
1030+
controllable_name="test",
1031+
definition=None,
1032+
type=ProductType.ACTUATOR,
1033+
widget="SomeWidget",
1034+
ui_class="RollerShutter",
1035+
)
1036+
assert device.get_command_definition("open") is None

0 commit comments

Comments
 (0)