-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
118 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
"""Tests for the Bulb API with a socket.""" | ||
import asyncio | ||
import logging | ||
from typing import AsyncGenerator, cast | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from pywizlight import wizlight | ||
from pywizlight.bulb import PilotParser | ||
from pywizlight.bulblibrary import BulbClass, BulbType, Features, KelvinRange | ||
from pywizlight.protocol import WizProtocol | ||
from pywizlight.push_manager import PushManager | ||
from pywizlight.tests.fake_bulb import startup_bulb | ||
from pywizlight.utils import to_wiz_json | ||
|
||
logging.getLogger("pywizlight").setLevel(logging.DEBUG) | ||
|
||
|
||
@pytest.fixture() | ||
async def socket_push() -> AsyncGenerator[wizlight, None]: | ||
shutdown, port = await startup_bulb( | ||
module_name="ESP10_SOCKET_06", firmware_version="1.25.0" | ||
) | ||
with patch("pywizlight.push_manager.RESPOND_PORT", port): | ||
bulb = wizlight(ip="127.0.0.1", port=port) | ||
yield bulb | ||
await bulb.async_close() | ||
shutdown() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_push_updates(socket_push: wizlight) -> None: | ||
"""Test push updates.""" | ||
bulb_type = await socket_push.get_bulbtype() | ||
assert bulb_type == BulbType( | ||
features=Features(color=False, color_tmp=False, effect=False, brightness=False), | ||
name="ESP10_SOCKET_06", | ||
kelvin_range=KelvinRange(max=2700, min=2700), | ||
bulb_type=BulbClass.SOCKET, | ||
fw_version="1.25.0", | ||
white_channels=2, | ||
white_to_color_ratio=20, | ||
) | ||
last_data = PilotParser({}) | ||
data_event = asyncio.Event() | ||
|
||
def _on_push(data: PilotParser) -> None: | ||
nonlocal last_data | ||
last_data = data | ||
data_event.set() | ||
|
||
with patch("pywizlight.push_manager.LISTEN_PORT", 0): | ||
await socket_push.start_push(_on_push) | ||
|
||
push_manager = PushManager().get() | ||
push_port = push_manager.push_transport.get_extra_info("sockname")[1] | ||
push_in_transport_proto = await asyncio.get_event_loop().create_datagram_endpoint( | ||
lambda: WizProtocol(on_response=lambda resp, addr: None), | ||
remote_addr=("127.0.0.1", push_port), | ||
) | ||
push_transport = cast(asyncio.DatagramTransport, push_in_transport_proto[0]) | ||
params = { | ||
"mac": "a8bb5006033d", | ||
"rssi": -71, | ||
"src": "hb", | ||
"mqttCd": 255, | ||
"ts": 1644593327, | ||
"state": False, | ||
"sceneId": 0, | ||
"temp": 6500, | ||
"dimming": 100, | ||
} | ||
|
||
push_transport.sendto( | ||
to_wiz_json( | ||
{ | ||
"method": "syncPilot", | ||
"env": "pro", | ||
"params": params, | ||
} | ||
).encode(), | ||
("127.0.0.1", push_port), | ||
) | ||
await asyncio.wait_for(data_event.wait(), timeout=1) | ||
assert last_data.pilotResult == params | ||
update = await socket_push.updateState() | ||
assert update is not None | ||
assert update.pilotResult == params | ||
push_transport.close() |