Skip to content

Commit a8a4005

Browse files
authored
Use an enum for light_mode (#18)
Enum for light mode
1 parent fcda34a commit a8a4005

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

letpot/converters.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from letpot.exceptions import LetPotException
1111
from letpot.models import (
1212
DeviceFeature,
13+
LightMode,
1314
TemperatureUnit,
1415
LetPotDeviceErrors,
1516
LetPotDeviceStatus,
@@ -115,7 +116,7 @@ def get_update_status_message(self, status: LetPotDeviceStatus) -> list[int]:
115116
2,
116117
1 if status.system_on else 0,
117118
status.pump_mode,
118-
status.light_mode,
119+
status.light_mode.value,
119120
math.floor(status.plant_days / 256),
120121
status.plant_days % 256,
121122
status.light_schedule_start.hour,
@@ -143,7 +144,7 @@ def convert_hex_to_status(self, message: PayloadType) -> LetPotDeviceStatus | No
143144
return LetPotDeviceStatus(
144145
raw=data,
145146
light_brightness=256 * data[17] + data[18],
146-
light_mode=data[10],
147+
light_mode=LightMode(data[10]),
147148
light_schedule_end=time(hour=data[15], minute=data[16]),
148149
light_schedule_start=time(hour=data[13], minute=data[14]),
149150
online=data[6] == 0,
@@ -192,7 +193,7 @@ def get_update_status_message(self, status: LetPotDeviceStatus) -> list[int]:
192193
2,
193194
1 if status.system_on else 0,
194195
status.pump_mode,
195-
status.light_mode,
196+
status.light_mode.value,
196197
math.floor(status.plant_days / 256),
197198
status.plant_days % 256,
198199
status.light_schedule_start.hour,
@@ -216,7 +217,7 @@ def convert_hex_to_status(self, message: PayloadType) -> LetPotDeviceStatus | No
216217
return LetPotDeviceStatus(
217218
raw=data,
218219
light_brightness=None,
219-
light_mode=data[10],
220+
light_mode=LightMode(data[10]),
220221
light_schedule_end=time(hour=data[15], minute=data[16]),
221222
light_schedule_start=time(hour=data[13], minute=data[14]),
222223
online=data[6] == 0,
@@ -264,7 +265,7 @@ def get_update_status_message(self, status: LetPotDeviceStatus) -> list[int]:
264265
2,
265266
1 if status.system_on else 0,
266267
status.pump_mode,
267-
status.light_mode,
268+
status.light_mode.value,
268269
math.floor(status.plant_days / 256),
269270
status.plant_days % 256,
270271
status.light_schedule_start.hour,
@@ -290,7 +291,7 @@ def convert_hex_to_status(self, message: PayloadType) -> LetPotDeviceStatus | No
290291
return LetPotDeviceStatus(
291292
raw=data,
292293
light_brightness=256 * data[18] + data[19],
293-
light_mode=data[10],
294+
light_mode=LightMode(data[10]),
294295
light_schedule_end=time(hour=data[15], minute=data[16]),
295296
light_schedule_start=time(hour=data[13], minute=data[14]),
296297
online=data[6] == 0,
@@ -344,7 +345,7 @@ def get_update_status_message(self, status: LetPotDeviceStatus) -> list[int]:
344345
2,
345346
1 if status.system_on else 0,
346347
status.pump_mode,
347-
status.light_mode,
348+
0 if status.light_mode is LightMode.FLOWER else 1,
348349
math.floor(status.plant_days / 256),
349350
status.plant_days % 256,
350351
status.light_schedule_start.hour,
@@ -367,7 +368,7 @@ def convert_hex_to_status(self, message: PayloadType) -> LetPotDeviceStatus | No
367368
return LetPotDeviceStatus(
368369
raw=data,
369370
light_brightness=256 * data[18] + data[19],
370-
light_mode=data[10],
371+
light_mode=LightMode(data[10]),
371372
light_schedule_end=time(hour=data[15], minute=data[16]),
372373
light_schedule_start=time(hour=data[13], minute=data[14]),
373374
online=data[6] == 0,

letpot/deviceclient.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from letpot.models import (
2121
AuthenticationInfo,
2222
LetPotDeviceInfo,
23+
LightMode,
2324
TemperatureUnit,
2425
LetPotDeviceStatus,
2526
)
@@ -362,7 +363,7 @@ async def set_light_brightness(self, serial: str, level: int) -> None:
362363
)
363364
await self._publish_status(serial, status)
364365

365-
async def set_light_mode(self, serial: str, mode: int) -> None:
366+
async def set_light_mode(self, serial: str, mode: LightMode) -> None:
366367
"""Set the light mode for this device (flower/vegetable)."""
367368
status = dataclasses.replace(self._get_publish_status(serial), light_mode=mode)
368369
await self._publish_status(serial, status)

letpot/models.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ class DeviceFeature(IntFlag):
1919
WATER_LEVEL = auto()
2020

2121

22+
class LightMode(IntEnum):
23+
"""Device light mode."""
24+
25+
FLOWER = 0
26+
"""Fruits/flowers light mode (red/white LEDs)."""
27+
VEGETABLE = 1
28+
"""Vegetables/herbs light mode (red/blue/white LEDs)."""
29+
30+
2231
class TemperatureUnit(IntEnum):
2332
"""Device temperate sensor/display unit."""
2433

@@ -80,7 +89,7 @@ class LetPotDeviceStatus:
8089

8190
errors: LetPotDeviceErrors
8291
light_brightness: int | None
83-
light_mode: int
92+
light_mode: LightMode
8493
light_schedule_end: time
8594
light_schedule_start: time
8695
online: bool

tests/test_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from letpot.converters import CONVERTERS, LPHx1Converter, LetPotDeviceConverter
88
from letpot.exceptions import LetPotException
9-
from letpot.models import LetPotDeviceErrors, LetPotDeviceStatus
9+
from letpot.models import LetPotDeviceErrors, LetPotDeviceStatus, LightMode
1010

1111

1212
SUPPORTED_DEVICE_TYPES = [
@@ -25,7 +25,7 @@
2525
DEVICE_STATUS = LetPotDeviceStatus(
2626
errors=LetPotDeviceErrors(low_water=True),
2727
light_brightness=500,
28-
light_mode=1,
28+
light_mode=LightMode.VEGETABLE,
2929
light_schedule_end=time(17, 0),
3030
light_schedule_start=time(7, 30),
3131
online=True,

0 commit comments

Comments
 (0)