Skip to content

Commit 767e118

Browse files
authored
fix: Add a common base type for all switches (#548)
1 parent fe463c3 commit 767e118

File tree

8 files changed

+52
-5
lines changed

8 files changed

+52
-5
lines changed

roborock/devices/traits/v1/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from .maps import MapsTrait
2525
from .rooms import RoomsTrait
2626
from .status import StatusTrait
27+
from .valley_electricity_timer import ValleyElectricityTimerTrait
2728
from .volume import SoundVolumeTrait
2829

2930
_LOGGER = logging.getLogger(__name__)
@@ -44,6 +45,7 @@
4445
"ChildLockTrait",
4546
"FlowLedStatusTrait",
4647
"LedStatusTrait",
48+
"ValleyElectricityTimerTrait",
4749
]
4850

4951

@@ -71,6 +73,7 @@ class PropertiesApi(Trait):
7173
child_lock: ChildLockTrait | None = None
7274
led_status: LedStatusTrait | None = None
7375
flow_led_status: FlowLedStatusTrait | None = None
76+
valley_electricity_timer: ValleyElectricityTimerTrait | None = None
7477

7578
def __init__(
7679
self,

roborock/devices/traits/v1/child_lock.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
_STATUS_PARAM = "lock_status"
66

77

8-
class ChildLockTrait(ChildLockStatus, common.V1TraitMixin):
8+
class ChildLockTrait(ChildLockStatus, common.V1TraitMixin, common.RoborockSwitchBase):
99
"""Trait for controlling the child lock of a Roborock device."""
1010

1111
command = RoborockCommand.GET_CHILD_LOCK_STATUS
1212
requires_feature = "is_set_child_supported"
1313

14+
@property
15+
def is_on(self) -> bool:
16+
"""Return whether the child lock is enabled."""
17+
return self.lock_status == 1
18+
1419
async def enable(self) -> None:
1520
"""Enable the child lock."""
1621
await self.rpc_channel.send_command(RoborockCommand.SET_CHILD_LOCK_STATUS, params={_STATUS_PARAM: 1})

roborock/devices/traits/v1/common.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55

66
import logging
7-
from abc import ABC
7+
from abc import ABC, abstractmethod
88
from dataclasses import dataclass, fields
99
from typing import ClassVar, Self
1010

@@ -124,6 +124,23 @@ def _parse_response(cls, response: V1ResponseData) -> Self:
124124
return cls(**{value_field: response})
125125

126126

127+
class RoborockSwitchBase(ABC):
128+
"""Base class for traits that represent a boolean switch."""
129+
130+
@property
131+
@abstractmethod
132+
def is_on(self) -> bool:
133+
"""Return whether the switch is on."""
134+
135+
@abstractmethod
136+
async def enable(self) -> None:
137+
"""Enable the switch."""
138+
139+
@abstractmethod
140+
async def disable(self) -> None:
141+
"""Disable the switch."""
142+
143+
127144
def mqtt_rpc_channel(cls):
128145
"""Decorator to mark a function as cloud only.
129146

roborock/devices/traits/v1/do_not_disturb.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
_ENABLED_PARAM = "enabled"
66

77

8-
class DoNotDisturbTrait(DnDTimer, common.V1TraitMixin):
8+
class DoNotDisturbTrait(DnDTimer, common.V1TraitMixin, common.RoborockSwitchBase):
99
"""Trait for managing Do Not Disturb (DND) settings on Roborock devices."""
1010

1111
command = RoborockCommand.GET_DND_TIMER
1212

13+
@property
14+
def is_on(self) -> bool:
15+
"""Return whether the Do Not Disturb (DND) timer is enabled."""
16+
return self.enabled == 1
17+
1318
async def set_dnd_timer(self, dnd_timer: DnDTimer) -> None:
1419
"""Set the Do Not Disturb (DND) timer settings of the device."""
1520
await self.rpc_channel.send_command(RoborockCommand.SET_DND_TIMER, params=dnd_timer.as_dict())

roborock/devices/traits/v1/flow_led_status.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
_STATUS_PARAM = "status"
66

77

8-
class FlowLedStatusTrait(FlowLedStatus, common.V1TraitMixin):
8+
class FlowLedStatusTrait(FlowLedStatus, common.V1TraitMixin, common.RoborockSwitchBase):
99
"""Trait for controlling the Flow LED status of a Roborock device."""
1010

1111
command = RoborockCommand.GET_FLOW_LED_STATUS
1212
requires_feature = "is_flow_led_setting_supported"
1313

14+
@property
15+
def is_on(self) -> bool:
16+
"""Return whether the Flow LED status is enabled."""
17+
return self.status == 1
18+
1419
async def enable(self) -> None:
1520
"""Enable the Flow LED status."""
1621
await self.rpc_channel.send_command(RoborockCommand.SET_FLOW_LED_STATUS, params={_STATUS_PARAM: 1})

roborock/devices/traits/v1/led_status.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
from .common import V1ResponseData
66

77

8-
class LedStatusTrait(LedStatus, common.V1TraitMixin):
8+
class LedStatusTrait(LedStatus, common.V1TraitMixin, common.RoborockSwitchBase):
99
"""Trait for controlling the LED status of a Roborock device."""
1010

1111
command = RoborockCommand.GET_LED_STATUS
1212
requires_feature = "is_led_status_switch_supported"
1313

14+
@property
15+
def is_on(self) -> bool:
16+
"""Return whether the LED status is enabled."""
17+
return self.status == 1
18+
1419
async def enable(self) -> None:
1520
"""Enable the LED status."""
1621
await self.rpc_channel.send_command(RoborockCommand.SET_LED_STATUS, params=[1])

roborock/devices/traits/v1/valley_electricity_timer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class ValleyElectricityTimerTrait(ValleyElectricityTimer, common.V1TraitMixin):
1111
command = RoborockCommand.GET_VALLEY_ELECTRICITY_TIMER
1212
requires_feature = "is_supported_valley_electricity"
1313

14+
@property
15+
def is_on(self) -> bool:
16+
"""Return whether the Valley Electricity Timer is enabled."""
17+
return self.enabled == 1
18+
1419
async def set_timer(self, timer: ValleyElectricityTimer) -> None:
1520
"""Set the Valley Electricity Timer settings of the device."""
1621
await self.rpc_channel.send_command(RoborockCommand.SET_VALLEY_ELECTRICITY_TIMER, params=timer.as_dict())

tests/devices/traits/v1/test_dnd.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ async def test_get_dnd_timer_success(
4646
assert dnd_trait.end_hour == 8
4747
assert dnd_trait.end_minute == 0
4848
assert dnd_trait.enabled == 1
49+
assert dnd_trait.is_on
4950

5051
# Verify the RPC call was made correctly
5152
mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_DND_TIMER)
@@ -65,6 +66,7 @@ async def test_get_dnd_timer_disabled(dnd_trait: DoNotDisturbTrait, mock_rpc_cha
6566
await dnd_trait.refresh()
6667

6768
assert dnd_trait.enabled == 0
69+
assert not dnd_trait.is_on
6870
mock_rpc_channel.send_command.assert_called_once_with(RoborockCommand.GET_DND_TIMER)
6971

7072

0 commit comments

Comments
 (0)