Skip to content

Commit

Permalink
Merge pull request #121 from bdraco/scene_fix
Browse files Browse the repository at this point in the history
Fix reporting available scenes on RGB devices
  • Loading branch information
sbidy committed Feb 11, 2022
2 parents 0b11c5f + 74bba21 commit ee31e83
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 16 deletions.
6 changes: 5 additions & 1 deletion pywizlight/bulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import time
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, cast

from pywizlight.exceptions import WizLightNotKnownBulb

from pywizlight.bulblibrary import BulbType
from pywizlight.exceptions import (
WizLightConnectionError,
Expand Down Expand Up @@ -453,7 +455,9 @@ async def get_bulbtype(self) -> BulbType:
bulb_config = await self.getBulbConfig()
result = bulb_config["result"]
if "moduleName" not in result:
raise ValueError("Unable to determine bulb type.")
raise WizLightNotKnownBulb(
"Unable to determine the bulb type; moduleName is missing from getSystemConfig"
)
white_range = await self.getExtendedWhiteRange()
white_to_color_ratio = None
white_channels = None
Expand Down
22 changes: 14 additions & 8 deletions pywizlight/bulblibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,14 @@ def from_data(
white_channels: Optional[int],
white_to_color_ratio: Optional[int],
) -> "BulbType":
if kelvin_list:
kelvin_range: Optional[KelvinRange] = KelvinRange(
min=int(min(kelvin_list)), max=int(max(kelvin_list))
)
else:
kelvin_range = None

try:
# parse the features from name
_identifier = module_name.split("_")[1]
# Throw exception if index can not be found
except IndexError:
raise WizLightNotKnownBulb("The bulb type can not be determined!")
raise WizLightNotKnownBulb(
f"The bulb type could not be determined from the module name: {module_name}"
)

if "RGB" in _identifier: # full RGB bulb
bulb_type = BulbClass.RGB
Expand All @@ -105,6 +100,17 @@ def from_data(
else: # Plain brightness-only bulb
bulb_type = BulbClass.DW

if kelvin_list:
kelvin_range: Optional[KelvinRange] = KelvinRange(
min=int(min(kelvin_list)), max=int(max(kelvin_list))
)
elif bulb_type in (BulbClass.RGB, BulbClass.TW):
raise WizLightNotKnownBulb(
f"Unable to determine required kelvin range for a {bulb_type.value} device"
)
else:
kelvin_range = None

features = FEATURE_MAP[bulb_type]

return BulbType(
Expand Down
2 changes: 1 addition & 1 deletion pywizlight/scenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
DW_SCENES = [9, 10, 13, 14, 29, 30, 31, 32]

SCENES_BY_CLASS: Dict[BulbClass, List[str]] = {
BulbClass.RGB: list(cast(Iterable, SCENES)),
BulbClass.RGB: list(cast(Iterable, SCENES.values())),
BulbClass.TW: [SCENES[key] for key in TW_SCENES],
BulbClass.DW: [SCENES[key] for key in DW_SCENES],
}
Expand Down
151 changes: 145 additions & 6 deletions pywizlight/tests/fake_bulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,61 @@
"drvIface": 0,
},
},
("ESP21_SHTW_01", "1.25.0"): {
"method": "getModelConfig",
"env": "pro",
"result": {
"ps": 2,
"pwmFreq": 5000,
"pwmRange": [1, 100],
"wcr": 20,
"nowc": 1,
"cctRange": [2700, 2700, 5000, 5000],
"renderFactor": [255, 0, 255, 255, 0, 0, 0, 0, 0, 0],
"drvIface": 0,
},
},
("MISSING", "1.16.64"): {
"method": "getModelConfig",
"env": "pro",
"result": {
"ps": 2,
"pwmFreq": 5000,
"pwmRange": [1, 100],
"wcr": 20,
"nowc": 1,
"cctRange": [2700, 2700, 5000, 5000],
"renderFactor": [255, 0, 255, 255, 0, 0, 0, 0, 0, 0],
"drvIface": 0,
},
},
("MISSING_KELVIN", "1.16.64"): {
"method": "getModelConfig",
"env": "pro",
"result": {
"ps": 2,
"pwmFreq": 5000,
"pwmRange": [1, 100],
"wcr": 20,
"nowc": 1,
"renderFactor": [255, 0, 255, 255, 0, 0, 0, 0, 0, 0],
"drvIface": 0,
},
},
("INVALID", "1.16.64"): {
"method": "getModelConfig",
"env": "pro",
"result": {
"ps": 2,
"pwmFreq": 5000,
"pwmRange": [1, 100],
"wcr": 20,
"nowc": 1,
"cctRange": [2700, 2700, 5000, 5000],
"renderFactor": [255, 0, 255, 255, 0, 0, 0, 0, 0, 0],
"drvIface": 0,
},
},
}

SYSTEM_CONFIGS = {
Expand All @@ -68,7 +123,7 @@
"mac": "a8bb5006033d",
"homeId": 653906,
"roomId": 989983,
"moduleName": "",
"moduleName": "ESP01_SHRGB_03",
"fwVersion": "1.25.0",
"groupId": 0,
"drvConf": [30, 1],
Expand All @@ -84,7 +139,7 @@
"mac": "a8bb5006033d",
"homeId": 653906,
"roomId": 989983,
"moduleName": "",
"moduleName": "ESP10_SOCKET_06",
"fwVersion": "1.25.0",
"groupId": 0,
"drvConf": [20, 2],
Expand All @@ -100,7 +155,7 @@
"mac": "a8bb5006033d",
"homeId": 653906,
"roomId": 989983,
"moduleName": "",
"moduleName": "ESP05_SHDW_21",
"fwVersion": "1.25.0",
"groupId": 0,
"drvConf": [20, 1],
Expand All @@ -116,7 +171,7 @@
"mac": "a8bb5006033d",
"homeId": 653906,
"roomId": 989983,
"moduleName": "",
"moduleName": "ESP20_SHRGB_01ABI",
"fwVersion": "1.25.0",
"groupId": 0,
"drvConf": [80, 2],
Expand All @@ -132,7 +187,7 @@
"mac": "a8bb5006033d",
"homeId": 653906,
"roomId": 989983,
"moduleName": "",
"moduleName": "ESP20_SHRGB_01ABI",
"fwVersion": "1.21.4",
"groupId": 0,
"drvConf": [80, 2],
Expand All @@ -157,6 +212,77 @@
"drvConf": [20, 1],
},
},
("ESP20_SHRGBC_01", "1.21.4"): {
"method": "getSystemConfig",
"env": "pro",
"result": {
"mac": "6c2990e493bc",
"homeId": 5385975,
"roomId": 8016844,
"moduleName": "ESP20_SHRGBC_01",
"fwVersion": "1.21.4",
"groupId": 0,
"drvConf": [30, 1],
"ewf": [200, 255, 150, 255, 0, 0, 40],
"ewfHex": "c8ff96ff000028",
"ping": 0,
},
},
("ESP21_SHTW_01", "1.25.0"): {
"method": "getSystemConfig",
"env": "pro",
"result": {
"mac": "6c29905a067c",
"homeId": 5385975,
"roomId": 8016844,
"rgn": "eu",
"moduleName": "ESP21_SHTW_01",
"fwVersion": "1.25.0",
"groupId": 0,
"ping": 0,
},
},
("MISSING_KELVIN", "1.16.64"): {
"method": "getSystemConfig",
"env": "pro",
"result": {
"mac": "6c29905a067c",
"homeId": 5385975,
"roomId": 8016844,
"rgn": "eu",
"moduleName": "ESP21_SHTW_01",
"fwVersion": "1.25.0",
"groupId": 0,
"ping": 0,
},
},
("MISSING", "1.16.64"): {
"method": "getSystemConfig",
"env": "pro",
"result": {
"mac": "6c29905a067c",
"homeId": 5385975,
"roomId": 8016844,
"rgn": "eu",
"fwVersion": "1.25.0",
"groupId": 0,
"ping": 0,
},
},
("INVALID", "1.16.64"): {
"method": "getSystemConfig",
"env": "pro",
"result": {
"mac": "6c29905a067c",
"homeId": 5385975,
"roomId": 8016844,
"rgn": "eu",
"moduleName": "INVALID",
"fwVersion": "1.25.0",
"groupId": 0,
"ping": 0,
},
},
}

USER_CONFIGS = {
Expand Down Expand Up @@ -189,6 +315,20 @@
"po": False,
},
},
("ESP20_SHRGBC_01", "1.21.4"): {
"method": "getUserConfig",
"env": "pro",
"result": {
"fadeIn": 500,
"fadeOut": 500,
"dftDim": 100,
"pwmRange": [0, 100],
"whiteRange": [2700, 6500],
"extRange": [2200, 6500],
"opMode": 0,
"po": True,
},
},
}


Expand Down Expand Up @@ -291,7 +431,6 @@ def make_udp_fake_bulb_server(
sys_config = get_initial_sys_config(module_name, firmware_version)
model_config = get_initial_model_config(module_name, firmware_version)
user_config = get_initial_user_config(module_name, firmware_version)
sys_config["result"]["moduleName"] = module_name

BulbUDPRequestHandler = type(
"BulbUDPRequestHandler",
Expand Down
15 changes: 15 additions & 0 deletions pywizlight/tests/test_bulb_dimmable_white.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ async def test_model_description_dimmable_bulb(dimmable_bulb: wizlight) -> None:
white_channels=1,
white_to_color_ratio=20,
)


@pytest.mark.asyncio
async def test_supported_scenes(dimmable_bulb: wizlight) -> None:
"""Test supported scenes."""
assert await dimmable_bulb.getSupportedScenes() == [
"Wake up",
"Bedtime",
"Cool white",
"Night light",
"Candlelight",
"Golden white",
"Pulse",
"Steampunk",
]
24 changes: 24 additions & 0 deletions pywizlight/tests/test_bulb_invalid_module_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Tests for the Bulb API with a light strip."""
from typing import AsyncGenerator

import pytest

from pywizlight import wizlight
from pywizlight.tests.fake_bulb import startup_bulb
from pywizlight.exceptions import WizLightNotKnownBulb


@pytest.fixture()
async def no_module_bulb() -> AsyncGenerator[wizlight, None]:
shutdown = startup_bulb(module_name="MISSING", firmware_version="1.16.64")
bulb = wizlight(ip="127.0.0.1")
yield bulb
await bulb.async_close()
shutdown()


@pytest.mark.asyncio
async def test_model_description_no_module_bulb(no_module_bulb: wizlight) -> None:
"""Test fetching the model description for a bulb missing the module."""
with pytest.raises(WizLightNotKnownBulb):
await no_module_bulb.get_bulbtype()
40 changes: 40 additions & 0 deletions pywizlight/tests/test_bulb_light_strip_1_25_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,46 @@ async def test_setting_rgbww(light_strip: wizlight) -> None:
assert state and state.get_rgbww() == (1, 2, 3, 4, 5)


@pytest.mark.asyncio
async def test_supported_scenes(light_strip: wizlight) -> None:
"""Test supported scenes."""
assert await light_strip.getSupportedScenes() == [
"Ocean",
"Romance",
"Sunset",
"Party",
"Fireplace",
"Cozy",
"Forest",
"Pastel Colors",
"Wake up",
"Bedtime",
"Warm White",
"Daylight",
"Cool white",
"Night light",
"Focus",
"Relax",
"True colors",
"TV time",
"Plantgrowth",
"Spring",
"Summer",
"Fall",
"Deepdive",
"Jungle",
"Mojito",
"Club",
"Christmas",
"Halloween",
"Candlelight",
"Golden white",
"Pulse",
"Steampunk",
"Rhythm",
]


@pytest.mark.asyncio
async def test_model_description_light_strip(light_strip: wizlight) -> None:
"""Test fetching the model description for a light strip."""
Expand Down
Loading

0 comments on commit ee31e83

Please sign in to comment.