Skip to content

Commit

Permalink
Add manual add device
Browse files Browse the repository at this point in the history
  • Loading branch information
Galorhallen committed Feb 3, 2023
1 parent 9e24d49 commit b4352ea
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
# Govee Local API

[![Upload Python Package](https://github.com/Galorhallen/govee-local-api/actions/workflows/deploy.yml/badge.svg?event=release)](https://github.com/Galorhallen/govee-local-api/actions/workflows/deploy.yml)

# Requirements

* Python >= 3.9
* Govee Local API enabled. Refer to https://app-h5.govee.com/user-manual/wlan-guide
- Python >= 3.9
- Govee Local API enabled. Refer to https://app-h5.govee.com/user-manual/wlan-guide

# Installaction

From your terminal, run
From your terminal, run

pip install govee-local-api

or

python3 -m pip install govee-local-api


python3 -m pip install govee-local-api
6 changes: 3 additions & 3 deletions example/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import functools

from govee_local_api import GoveeController, GoveeDevice
from src.govee_local_api import GoveeController, GoveeDevice


def discovered_callback(device: GoveeDevice, is_new: bool):
Expand All @@ -20,7 +20,7 @@ async def main(controller: GoveeController):
await controller.start()
await asyncio.sleep(5)

device: GoveeDevice = controller.get_device_by_sku("H615A")
device: GoveeDevice = controller.get_device_by_ip("10.0.0.183")
await device.turn_on()
# await asyncio.sleep(5)
await print_status(controller)
Expand All @@ -30,7 +30,7 @@ async def main(controller: GoveeController):
loop = asyncio.new_event_loop()
controller: GoveeController = GoveeController(
loop,
autodiscovery=True,
discovery=True,
discovered_callback=discovered_callback,
evicted_callback=lambda device: print(f"Evicted {device}"),
)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "govee-local-api"
version = "1.0.0"
version = "1.1.0"
authors = [
{ name="Galorhallen", email="andrea.ponte1987@gmail.com" },
]
Expand Down
2 changes: 1 addition & 1 deletion src/govee_local_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .controller import GoveeController
from .device import GoveeDevice

__version__ = "1.0.0"
__version__ = "1.1.0"
25 changes: 18 additions & 7 deletions src/govee_local_api/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def clenaup(self):
self._transport.close()
self._devices.clear()

def add_device(self, ip: str, sku: str, fingerprint: str = None) -> None:
device: GoveeDevice = GoveeDevice(self, ip, fingerprint, sku)
self._devices[ip] = device

def set_discovery(self, enabled: bool) -> None:
self._discovery = enabled
if enabled:
Expand Down Expand Up @@ -160,11 +164,18 @@ async def set_color(
self._send_message(ColorMessage(rgb=None, temperature=temperature), device)

def get_device_by_ip(self, ip: str) -> GoveeDevice | None:
return next(device for device in self._devices.values() if device.ip == ip)
return self._devices.get(ip, None)

def get_device_by_sku(self, sku: str) -> GoveeDevice | None:
return next(device for device in self._devices.values() if device.sku == sku)

def get_device_by_fingerprint(self, fingerprint: str) -> GoveeDevice | None:
return next(
device
for device in self._devices.values()
if device.fingerprint == fingerprint
)

@property
def devices(self) -> list(GoveeDevice):
return list(self._devices.values())
Expand Down Expand Up @@ -209,16 +220,16 @@ def _handle_status_update_response(self, message: StatusResponse, addr):
device.update(message)

async def _handle_scan_response(self, message: ScanResponse) -> None:
fingerprint = message.device
device = self._devices.get(fingerprint, None)
ip = message.ip
device = self._devices.get(ip, None)

if device:
device.update_lastseen()
is_new = False
self._logger.debug("Device updated: %s", device)
else:
device = GoveeDevice(self, message.ip, message.device, message.sku)
self._devices[message.device] = device
device = GoveeDevice(self, ip, message.device, message.sku)
self._devices[ip] = device
is_new = True
self._logger.debug("Device discovered: %s", device)

Expand All @@ -234,11 +245,11 @@ def _send_message(self, message: GoveeMessage, device: GoveeDevice) -> None:

def _evict(self):
now = datetime.now()
for id, device in self._devices.items():
for ip, device in self._devices.items():
diff: timedelta = now - device.lastseen
if diff.total_seconds() >= self._eviction_interval:
device._controller = None
del self._devices[id]
del self._devices[ip]
if self._device_evicted_callback and callable(
self._device_evicted_callback
):
Expand Down

0 comments on commit b4352ea

Please sign in to comment.