| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import asyncio |
| 4 |
from collections.abc import Callable |
| 5 |
from datetime import datetime, timezone |
| 6 |
from typing import Any |
| 7 |
|
| 8 |
from .light_capabilities import GoveeLightCapabilities, ON_OFF_CAPABILITIES |
| 9 |
from .message import DevStatusResponse |
| 10 |
|
| 11 |
|
| 12 |
class GoveeSegment: |
| 13 |
def __init__(self, is_on: bool, color: tuple[int, int, int]) -> None: |
| 14 |
self.is_on = is_on |
| 15 |
self.color = color |
| 16 |
|
| 17 |
def as_dict(self) -> dict[str, Any]: |
| 18 |
return {"is_on": self.is_on, "color": self.color} |
| 19 |
|
| 20 |
def __str__(self) -> str: |
| 21 |
return f"<GoveeSegment is_on={self.is_on}, color={self.color}>" |
| 22 |
|
| 23 |
|
| 24 |
class GoveeDevice: |
| 25 |
def __init__( |
| 26 |
self, |
| 27 |
controller, |
| 28 |
ip: str, |
| 29 |
fingerprint: str, |
| 30 |
sku: str, |
| 31 |
capabilities: GoveeLightCapabilities = ON_OFF_CAPABILITIES, |
| 32 |
) -> None: |
| 33 |
self._controller = controller |
| 34 |
self._fingerprint = fingerprint |
| 35 |
self._sku = sku |
| 36 |
self._ip = ip |
| 37 |
self._lastseen: datetime = datetime.now(timezone.utc) |
| 38 |
self._capabilities: GoveeLightCapabilities = capabilities |
| 39 |
|
| 40 |
self._is_on: bool = False |
| 41 |
self._rgb_color = (0, 0, 0) |
| 42 |
self._temperature_color = 0 |
| 43 |
self._brightness = 0 |
| 44 |
self._transport: asyncio.DatagramTransport | None = None |
| 45 |
self._update_callback: Callable[[GoveeDevice], None] | None = None |
| 46 |
self.is_manual: bool = False |
| 47 |
|
| 48 |
@property |
| 49 |
def controller(self): |
| 50 |
return self._controller |
| 51 |
|
| 52 |
@property |
| 53 |
def transport(self) -> asyncio.DatagramTransport | None: |
| 54 |
return self._transport |
| 55 |
|
| 56 |
def update_transport(self, transport: asyncio.DatagramTransport) -> None: |
| 57 |
self._transport = transport |
| 58 |
|
| 59 |
@property |
| 60 |
def capabilities(self) -> GoveeLightCapabilities: |
| 61 |
return self._capabilities |
| 62 |
|
| 63 |
@property |
| 64 |
def ip(self) -> str: |
| 65 |
return self._ip |
| 66 |
|
| 67 |
@property |
| 68 |
def fingerprint(self) -> str: |
| 69 |
return self._fingerprint |
| 70 |
|
| 71 |
@property |
| 72 |
def sku(self) -> str: |
| 73 |
return self._sku |
| 74 |
|
| 75 |
@property |
| 76 |
def lastseen(self) -> datetime: |
| 77 |
return self._lastseen |
| 78 |
|
| 79 |
@property |
| 80 |
def on(self) -> bool: |
| 81 |
return self._is_on |
| 82 |
|
| 83 |
@property |
| 84 |
def rgb_color(self) -> tuple[int, int, int]: |
| 85 |
return self._rgb_color |
| 86 |
|
| 87 |
@property |
| 88 |
def brightness(self) -> int: |
| 89 |
return self._brightness |
| 90 |
|
| 91 |
@property |
| 92 |
def temperature_color(self) -> int: |
| 93 |
return self._temperature_color |
| 94 |
|
| 95 |
@property |
| 96 |
def update_callback(self) -> Callable[[GoveeDevice], None] | None: |
| 97 |
return self._update_callback |
| 98 |
|
| 99 |
@property |
| 100 |
def is_connected(self) -> bool: |
| 101 |
"""Return True if the device has been seen within the controller's |
| 102 |
evict interval, so "connected" and "evicted" can't contradict each |
| 103 |
other when a custom interval is configured.""" |
| 104 |
threshold = getattr(self._controller, "evict_interval", 30) |
| 105 |
return (datetime.now(timezone.utc) - self._lastseen).total_seconds() < threshold |
| 106 |
|
| 107 |
def set_update_callback( |
| 108 |
self, callback: Callable[[GoveeDevice], None] | None |
| 109 |
) -> Callable[[GoveeDevice], None] | None: |
| 110 |
old_callback = self._update_callback |
| 111 |
self._update_callback = callback |
| 112 |
return old_callback |
| 113 |
|
| 114 |
async def turn_on(self) -> None: |
| 115 |
await self._controller.turn_on_off(self, True) |
| 116 |
self._is_on = True |
| 117 |
|
| 118 |
async def set_segment_rgb_color( |
| 119 |
self, segment: int, red: int, green: int, blue: int |
| 120 |
) -> None: |
| 121 |
rgb: tuple[int, int, int] = (red, green, blue) |
| 122 |
await self._controller.set_segment_rgb_color(self, segment, rgb) |
| 123 |
|
| 124 |
async def turn_segment_off(self, segment: int) -> None: |
| 125 |
await self._controller.set_segment_rgb_color(self, segment, (0, 0, 0)) |
| 126 |
|
| 127 |
async def turn_off(self) -> None: |
| 128 |
await self._controller.turn_on_off(self, False) |
| 129 |
self._is_on = False |
| 130 |
|
| 131 |
async def set_brightness(self, value: int) -> None: |
| 132 |
await self._controller.set_brightness(self, value) |
| 133 |
self._brightness = value |
| 134 |
|
| 135 |
async def set_rgb_color(self, red: int, green: int, blue: int) -> None: |
| 136 |
rgb = (red, green, blue) |
| 137 |
await self._controller.set_color(self, rgb=rgb, temperature=None) |
| 138 |
self._rgb_color = rgb |
| 139 |
|
| 140 |
async def set_temperature(self, temperature: int) -> None: |
| 141 |
await self._controller.set_color(self, temperature=temperature, rgb=None) |
| 142 |
self._temperature_color = temperature |
| 143 |
|
| 144 |
async def set_scene(self, scene: str) -> None: |
| 145 |
await self._controller.set_scene(self, scene) |
| 146 |
|
| 147 |
async def send_raw_command(self, command: str) -> None: |
| 148 |
await self._controller.send_raw_command(self, command) |
| 149 |
|
| 150 |
def update(self, message: DevStatusResponse) -> None: |
| 151 |
self._is_on = message.is_on |
| 152 |
self._brightness = message.brightness |
| 153 |
self._rgb_color = message.color |
| 154 |
self._temperature_color = message.color_temperature |
| 155 |
self.update_lastseen() |
| 156 |
if self._update_callback and callable(self._update_callback): |
| 157 |
self._update_callback(self) |
| 158 |
|
| 159 |
def update_lastseen(self) -> None: |
| 160 |
self._lastseen = datetime.now(timezone.utc) |
| 161 |
|
| 162 |
def update_ip(self, ip: str) -> None: |
| 163 |
self._ip = ip |
| 164 |
|
| 165 |
def as_dict(self) -> dict[str, Any]: |
| 166 |
return { |
| 167 |
"ip": self._ip, |
| 168 |
"fingerprint": self._fingerprint, |
| 169 |
"sku": self._sku, |
| 170 |
"lastseen": self._lastseen, |
| 171 |
"on": self._is_on, |
| 172 |
"brightness": self._brightness, |
| 173 |
"color": self._rgb_color, |
| 174 |
"colorTemperature": self._temperature_color, |
| 175 |
} |
| 176 |
|
| 177 |
def __str__(self) -> str: |
| 178 |
result = f"<GoveeDevice ip={self.ip}, fingerprint={self.fingerprint}, sku={self.sku}, lastseen={self._lastseen}, is_on={self._is_on}" |
| 179 |
return result + ( |
| 180 |
f", brightness={self._brightness}, color={self._rgb_color}, temperature={self._temperature_color}>" |
| 181 |
if self._is_on |
| 182 |
else ">" |
| 183 |
) |