Skip to content

Commit

Permalink
Reorganize yeelight specs file (#1166)
Browse files Browse the repository at this point in the history
* Reorganizing information in a specs file

* remove unused

* Fix typo

* night_light was moved to device scope

* move main light spec on the top level
  • Loading branch information
Kirmas authored Nov 9, 2021
1 parent b1f8f68 commit 452d436
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 97 deletions.
9 changes: 2 additions & 7 deletions miio/integrations/yeelight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,13 @@
from miio.exceptions import DeviceException
from miio.utils import int_to_rgb, rgb_to_int

from .spec_helper import YeelightSpecHelper
from .spec_helper import YeelightSpecHelper, YeelightSubLightType


class YeelightException(DeviceException):
pass


class YeelightSubLightType(IntEnum):
Main = 1
Background = 2


SUBLIGHT_PROP_PREFIX = {
YeelightSubLightType.Main: "",
YeelightSubLightType.Background: "bg_",
Expand Down Expand Up @@ -325,7 +320,7 @@ def status(self) -> YeelightStatus:
def on(self, transition=0, mode=0):
"""Power on."""
"""
set_power ["on|off", "smooth", time_in_ms, mode]
set_power ["on|off", "sudden|smooth", time_in_ms, mode]
where mode:
0: last mode
1: normal mode
Expand Down
45 changes: 34 additions & 11 deletions miio/integrations/yeelight/spec_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
import os
from enum import IntEnum
from typing import Dict, NamedTuple

import attr
Expand All @@ -8,20 +9,29 @@
_LOGGER = logging.getLogger(__name__)


class YeelightSubLightType(IntEnum):
Main = 0
Background = 1


class ColorTempRange(NamedTuple):
"""Color temperature range."""

min: int
max: int


@attr.s(auto_attribs=True)
class YeelightLampInfo:
color_temp: ColorTempRange
supports_color: bool


@attr.s(auto_attribs=True)
class YeelightModelInfo:
model: str
color_temp: ColorTempRange
night_light: bool
background_light: bool
supports_color: bool
lamps: Dict[YeelightSubLightType, YeelightLampInfo]


class YeelightSpecHelper:
Expand All @@ -33,20 +43,33 @@ def __init__(self):

def _parse_specs_yaml(self):
generic_info = YeelightModelInfo(
"generic", ColorTempRange(1700, 6500), False, False, False
"generic",
False,
{
YeelightSubLightType.Main: YeelightLampInfo(
ColorTempRange(1700, 6500), False
)
},
)
YeelightSpecHelper._models["generic"] = generic_info
# read the yaml file to populate the internal model cache
with open(os.path.dirname(__file__) + "/specs.yaml") as filedata:
models = yaml.safe_load(filedata)
for key, value in models.items():
info = YeelightModelInfo(
key,
ColorTempRange(*value["color_temp"]),
value["night_light"],
value["background_light"],
value["supports_color"],
)
lamps = {
YeelightSubLightType.Main: YeelightLampInfo(
ColorTempRange(*value["color_temp"]),
value["supports_color"],
)
}

if "background" in value:
lamps[YeelightSubLightType.Background] = YeelightLampInfo(
ColorTempRange(*value["background"]["color_temp"]),
value["background"]["supports_color"],
)

info = YeelightModelInfo(key, value["night_light"], lamps)
YeelightSpecHelper._models[key] = info

@property
Expand Down
123 changes: 51 additions & 72 deletions miio/integrations/yeelight/specs.yaml
Original file line number Diff line number Diff line change
@@ -1,180 +1,159 @@
yeelink.light.bslamp1:
color_temp: [1700, 6500]
night_light: False
background_light: False
color_temp: [1700, 6500]
supports_color: True
yeelink.light.bslamp2:
color_temp: [1700, 6500]
night_light: True
background_light: False
color_temp: [1700, 6500]
supports_color: True
yeelink.light.bslamp3:
color_temp: [1700, 6500]
night_light: True
background_light: False
color_temp: [1700, 6500]
supports_color: True
yeelink.light.ceil26:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceila:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceiling1:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceiling2:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceiling3:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceiling4:
color_temp: [2700, 6500]
night_light: True
background_light: True
color_temp: [2700, 6500]
supports_color: False
background:
color_temp: [1700, 6500]
supports_color: True
yeelink.light.ceiling5:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceiling6:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceiling10:
color_temp: [2700, 6500]
night_light: True
background_light: True
color_temp: [2700, 6500]
supports_color: False
background:
color_temp: [1700, 6500]
supports_color: True
yeelink.light.ceiling13:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceiling15:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceiling18:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceiling19:
color_temp: [2700, 6500]
night_light: True
background_light: True
color_temp: [2700, 6500]
supports_color: False
background:
color_temp: [1700, 6500]
supports_color: True
yeelink.light.ceiling20:
color_temp: [2700, 6500]
night_light: True
background_light: True
color_temp: [2700, 6500]
supports_color: False
background:
color_temp: [1700, 6500]
supports_color: True
yeelink.light.ceiling22:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ceiling24:
color_temp: [2700, 6500]
night_light: True
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.color1:
color_temp: [1700, 6500]
night_light: False
background_light: False
color_temp: [1700, 6500]
supports_color: True
yeelink.light.color2:
color_temp: [2700, 6500]
night_light: False
background_light: False
color_temp: [2700, 6500]
supports_color: True
yeelink.light.color4:
color_temp: [1700, 6500]
night_light: False
background_light: False
color_temp: [1700, 6500]
supports_color: True
yeelink.light.colorc:
color_temp: [2700, 6500]
night_light: False
background_light: False
color_temp: [2700, 6500]
supports_color: True
yeelink.light.color:
color_temp: [1700, 6500]
night_light: False
background_light: False
color_temp: [1700, 6500]
supports_color: True
yeelink.light.ct_bulb:
color_temp: [2700, 6500]
night_light: False
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.ct2:
color_temp: [2700, 6500]
night_light: False
background_light: False
color_temp: [2700, 6500]
supports_color: False
yeelink.light.lamp1:
color_temp: [2700, 5000]
night_light: False
background_light: False
color_temp: [2700, 5000]
supports_color: False
yeelink.light.lamp4:
color_temp: [2600, 5000]
night_light: False
background_light: False
color_temp: [2600, 5000]
supports_color: False
yeelink.light.lamp15:
color_temp: [2700, 6500]
night_light: False
background_light: True
color_temp: [2700, 6500]
supports_color: False
background:
color_temp: [1700, 6500]
supports_color: True
yeelink.light.mono1:
color_temp: [2700, 2700]
night_light: False
background_light: False
color_temp: [2700, 2700]
supports_color: False
yeelink.light.mono5:
color_temp: [2700, 2700]
night_light: False
background_light: False
color_temp: [2700, 2700]
supports_color: False
yeelink.light.mono:
color_temp: [2700, 2700]
night_light: False
background_light: False
color_temp: [2700, 2700]
supports_color: False
yeelink.light.monob:
color_temp: [2700, 2700]
night_light: False
background_light: False
color_temp: [2700, 2700]
supports_color: False
yeelink.light.strip1:
color_temp: [1700, 6500]
night_light: False
background_light: False
color_temp: [1700, 6500]
supports_color: True
yeelink.light.strip2:
color_temp: [2700, 6500]
night_light: False
background_light: False
color_temp: [2700, 6500]
supports_color: True
yeelink.light.strip4:
color_temp: [2700, 6500]
night_light: False
background_light: False
color_temp: [2700, 6500]
supports_color: True
18 changes: 11 additions & 7 deletions miio/integrations/yeelight/tests/test_yeelight_spec_helper.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
from ..spec_helper import ColorTempRange, YeelightSpecHelper
from ..spec_helper import ColorTempRange, YeelightSpecHelper, YeelightSubLightType


def test_get_model_info():
spec_helper = YeelightSpecHelper()
model_info = spec_helper.get_model_info("yeelink.light.bslamp1")
assert model_info.model == "yeelink.light.bslamp1"
assert model_info.color_temp == ColorTempRange(1700, 6500)
assert model_info.night_light is False
assert model_info.background_light is False
assert model_info.supports_color is True
assert model_info.lamps[YeelightSubLightType.Main].color_temp == ColorTempRange(
1700, 6500
)
assert model_info.lamps[YeelightSubLightType.Main].supports_color is True
assert YeelightSubLightType.Background not in model_info.lamps


def test_get_unknown_model_info():
spec_helper = YeelightSpecHelper()
model_info = spec_helper.get_model_info("notreal")
assert model_info.model == "generic"
assert model_info.color_temp == ColorTempRange(1700, 6500)
assert model_info.night_light is False
assert model_info.background_light is False
assert model_info.supports_color is False
assert model_info.lamps[YeelightSubLightType.Main].color_temp == ColorTempRange(
1700, 6500
)
assert model_info.lamps[YeelightSubLightType.Main].supports_color is False
assert YeelightSubLightType.Background not in model_info.lamps

0 comments on commit 452d436

Please sign in to comment.