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

Fix smart task start action response dimmer attribute #614

Merged
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
12 changes: 8 additions & 4 deletions pytradfri/smart_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class StartActionResponse(BaseResponse):
"""Represent a start action response."""

transition_time: Optional[int] = Field(alias=ATTR_TRANSITION_TIME)
dimmer: int = Field(alias=ATTR_LIGHT_DIMMER)
dimmer: Optional[int] = Field(alias=ATTR_LIGHT_DIMMER)


class TimeIntervalResponse(BaseModel):
Expand Down Expand Up @@ -309,7 +309,8 @@ def devices_list(self) -> list[dict[str, int]]:
if idx != self.index:
list_record: dict[str, int] = {}
list_record[ATTR_ID] = record.id
list_record[ATTR_LIGHT_DIMMER] = record.dimmer
if record.dimmer is not None:
list_record[ATTR_LIGHT_DIMMER] = record.dimmer

if record.transition_time is not None:
list_record[ATTR_TRANSITION_TIME] = record.transition_time
Expand Down Expand Up @@ -342,7 +343,7 @@ def transition_time(self) -> int | None:
return None

@property
def dimmer(self) -> int:
def dimmer(self) -> int | None:
"""Return dimmer level."""
return self.raw.dimmer

Expand Down Expand Up @@ -404,10 +405,13 @@ def set_transition_time(self, transition_time: int) -> Command[None]:
root_start_action_list: list[dict[str, int]] = [
{
ATTR_ID: self.raw.id,
ATTR_LIGHT_DIMMER: self.raw.dimmer,
ATTR_TRANSITION_TIME: transition_time * 10 * 60,
}
]

if self.raw.dimmer is not None:
root_start_action_list[0][ATTR_LIGHT_DIMMER] = self.raw.dimmer

root_start_action_list.extend(self.devices_list)

command: dict[str, dict[str, Any]] = {
Expand Down
27 changes: 27 additions & 0 deletions tests/test_smart_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@
"9044": [{"9046": 8, "9047": 15}],
}

TASK_OPTIONAL_DIMMER = {
"9001": "Light and dark",
"9002": 1613335145,
"9003": 318615,
"5850": 1,
"9040": 2,
"9041": 127,
"9042": {"5850": 1, "15013": [{"9003": 65553}]},
"9043": {"5850": 0, "15013": [{"9003": 65553}]},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the response captured in the issue. Do we know what 9043 represents? We don't have that one in our const module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm unable to test using the app for the next few weeks unfortunately, I suspect it relates to controlling an outlet via smart task. Is there a way I can confirm this somehow, given my setup?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know, but if you have time later to dig in a bit, perhaps try removing the outlet device from the gateway and then reset the gateway and/or app. Then try adding a new smart task before adding the outlet device again and see if the response is different.

"9044": [{"9046": 15, "9047": 0, "9048": 7, "9049": 0, "9226": 0}],
}


WEEKDAYS = BitChoices(
(
Expand Down Expand Up @@ -114,3 +126,18 @@ def test_smart_task_set_state():

cmd = task_control.set_state(False)
assert cmd.data == {"5850": 0, "9001": "Sample Name"}


def test_optional_dimmer():
"""Test a smart task with missing dimmer attribute."""
gateway = Gateway()
task = SmartTask(gateway, TASK_OPTIONAL_DIMMER).task_control.tasks[0]

assert task.id == 65553
assert task.index == 0
assert task.state is True
assert task.path == ["15010", "318615"]
assert task.dimmer is None
assert task.transition_time is None
devices_list = task.devices_list
assert devices_list == []