-
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #139 from bdraco/test_more_devices
Add support for wall switches
- Loading branch information
Showing
7 changed files
with
257 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Tests for the Bulb API with a Hero.""" | ||
from typing import AsyncGenerator | ||
|
||
import pytest | ||
|
||
from pywizlight import wizlight | ||
from pywizlight.bulblibrary import BulbClass, BulbType, Features, KelvinRange | ||
from pywizlight.tests.fake_bulb import startup_bulb | ||
|
||
|
||
@pytest.fixture() | ||
async def hero() -> AsyncGenerator[wizlight, None]: | ||
shutdown, port = await startup_bulb( | ||
module_name="ESP20_SHRGB_01BT", firmware_version="1.23.70" | ||
) | ||
bulb = wizlight(ip="127.0.0.1", port=port) | ||
yield bulb | ||
await bulb.async_close() | ||
shutdown() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_model_description_squire(hero: wizlight) -> None: | ||
"""Test fetching the model description for a hero.""" | ||
bulb_type = await hero.get_bulbtype() | ||
assert bulb_type == BulbType( | ||
features=Features( | ||
color=True, color_tmp=True, effect=True, brightness=True, dual_head=False | ||
), | ||
name="ESP20_SHRGB_01BT", | ||
kelvin_range=KelvinRange(max=6500, min=2200), | ||
bulb_type=BulbClass.RGB, | ||
fw_version="1.23.70", | ||
white_channels=2, | ||
white_to_color_ratio=20, | ||
) |
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,36 @@ | ||
"""Tests for the Bulb API with a wall switch dimmer.""" | ||
from typing import AsyncGenerator | ||
|
||
import pytest | ||
|
||
from pywizlight import wizlight | ||
from pywizlight.bulblibrary import BulbClass, BulbType, Features, KelvinRange | ||
from pywizlight.tests.fake_bulb import startup_bulb | ||
|
||
|
||
@pytest.fixture() | ||
async def wall_switch() -> AsyncGenerator[wizlight, None]: | ||
shutdown, port = await startup_bulb( | ||
module_name="ESP01_DIMTRIACS_01", firmware_version="1.16.68" | ||
) | ||
bulb = wizlight(ip="127.0.0.1", port=port) | ||
yield bulb | ||
await bulb.async_close() | ||
shutdown() | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_model_description_wall_switch(wall_switch: wizlight) -> None: | ||
"""Test fetching the model description wall switch.""" | ||
bulb_type = await wall_switch.get_bulbtype() | ||
assert bulb_type == BulbType( | ||
features=Features( | ||
color=False, color_tmp=False, effect=False, brightness=True, dual_head=False | ||
), | ||
name="ESP01_DIMTRIACS_01", | ||
kelvin_range=KelvinRange(max=2700, min=2700), | ||
bulb_type=BulbClass.DW, | ||
fw_version="1.16.68", | ||
white_channels=1, | ||
white_to_color_ratio=20, | ||
) |
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,94 @@ | ||
"""Tests for discovery.""" | ||
import asyncio | ||
import contextlib | ||
import logging | ||
from typing import AsyncGenerator, Tuple | ||
from unittest.mock import MagicMock, patch | ||
|
||
import pytest | ||
|
||
from pywizlight.discovery import ( | ||
BroadcastProtocol, | ||
DiscoveredBulb, | ||
discover_lights, | ||
find_wizlights, | ||
) | ||
|
||
logging.getLogger("pywizlight").setLevel(logging.DEBUG) | ||
|
||
|
||
@pytest.fixture | ||
async def mock_discovery_aio_protocol() -> AsyncGenerator: | ||
"""Fixture to mock an asyncio connection.""" | ||
loop = asyncio.get_running_loop() | ||
future: asyncio.Future[ | ||
Tuple[asyncio.DatagramProtocol, BroadcastProtocol] | ||
] = asyncio.Future() | ||
|
||
async def _wait_for_connection(): | ||
transport, protocol = await future | ||
await asyncio.sleep(0) | ||
await asyncio.sleep(0) | ||
return transport, protocol | ||
|
||
async def _mock_create_datagram_endpoint(func, sock=None): | ||
protocol: BroadcastProtocol = func() | ||
transport = MagicMock() | ||
protocol.connection_made(transport) | ||
with contextlib.suppress(asyncio.InvalidStateError): | ||
future.set_result((transport, protocol)) | ||
return transport, protocol | ||
|
||
with patch.object(loop, "create_datagram_endpoint", _mock_create_datagram_endpoint): | ||
yield _wait_for_connection | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_find_wizlights(mock_discovery_aio_protocol): | ||
"""Test find_wizlights.""" | ||
task = asyncio.create_task( | ||
find_wizlights(wait_time=0.02, broadcast_address="192.168.213.252") | ||
) | ||
transport_protocol = await mock_discovery_aio_protocol() | ||
protocol: BroadcastProtocol = transport_protocol[1] | ||
protocol.datagram_received( | ||
b'{"method":"registration","env":"pro","result":{"mac":"d8a01199cf31","success":true}}', | ||
("1.3.4.2", 1234), | ||
) | ||
protocol.datagram_received( | ||
b"garbage", | ||
("1.3.4.2", 1234), | ||
) | ||
protocol.connection_lost(None) | ||
bulbs = await task | ||
assert bulbs == [DiscoveredBulb(ip_address="1.3.4.2", mac_address="d8a01199cf31")] | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_discover_lights_fails(mock_discovery_aio_protocol): | ||
"""Test discover_lights.""" | ||
task = asyncio.create_task(discover_lights(wait_time=0.02)) | ||
transport_protocol = await mock_discovery_aio_protocol() | ||
protocol: BroadcastProtocol = transport_protocol[1] | ||
protocol.connection_lost(OSError) | ||
with pytest.raises(OSError): | ||
await task | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_discover_lights(mock_discovery_aio_protocol): | ||
"""Test discover_lights success.""" | ||
task = asyncio.create_task(discover_lights(wait_time=0.02)) | ||
transport_protocol = await mock_discovery_aio_protocol() | ||
protocol: BroadcastProtocol = transport_protocol[1] | ||
protocol.datagram_received( | ||
b'{"method":"registration","env":"pro","result":{"mac":"d8a01199cf31","success":true}}', | ||
("1.3.4.2", 1234), | ||
) | ||
protocol.datagram_received( | ||
b'{"method":"registration","env":"pro","result":{"mac":"d8a01199cf32","success":true}}', | ||
("1.3.4.3", 1234), | ||
) | ||
protocol.connection_lost(None) | ||
bulbs = await task | ||
assert len(bulbs) == 2 |