diff --git a/pytradfri/smart_task.py b/pytradfri/smart_task.py index cc84037c..cb86ebf5 100644 --- a/pytradfri/smart_task.py +++ b/pytradfri/smart_task.py @@ -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): @@ -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 @@ -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 @@ -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]] = { diff --git a/tests/test_smart_task.py b/tests/test_smart_task.py index 250d3cb9..5f9e9c84 100644 --- a/tests/test_smart_task.py +++ b/tests/test_smart_task.py @@ -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}]}, + "9044": [{"9046": 15, "9047": 0, "9048": 7, "9049": 0, "9226": 0}], +} + WEEKDAYS = BitChoices( ( @@ -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 == []