Skip to content

Commit

Permalink
Add auto detect and remove lib. 0.4.5
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidy committed Feb 10, 2021
1 parent 9eddd7d commit 6abebad
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 174 deletions.
2 changes: 1 addition & 1 deletion pywizlight/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

from pywizlight import discovery # noqa: 401
from pywizlight.scenes import SCENES # noqa: 401
from pywizlight.bulblibrary import BulbLib, BulbType # noqa: 401
from pywizlight.bulblibrary import BulbType # noqa: 401

__all__ = ["wizlight"]
2 changes: 1 addition & 1 deletion pywizlight/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""PyPi Version."""
__version__ = "0.4.4"
__version__ = "0.4.5"
59 changes: 38 additions & 21 deletions pywizlight/bulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

import asyncio_dgram

from pywizlight.bulblibrary import BulbType, Features, KelvinRange, BulbClass
from pywizlight.exceptions import (
WizLightConnectionError,
WizLightNotKnownBulb,
WizLightTimeOutError,
)
from pywizlight.scenes import SCENES
from pywizlight.bulblibrary import BulbLib, BulbType, KelvinRange, Features

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -245,7 +245,7 @@ def __init__(self, ip, connect_on_init=False, port=38899, mac=None):
self.port = port
self.state = None
self.mac = mac
self.bulbtype = None
self.bulbtype: BulbType = None
self.whiteRange = None
self.extwhiteRange = None
# check the state on init
Expand Down Expand Up @@ -284,45 +284,44 @@ async def get_bulbtype(self) -> BulbType:
bulb_config = await self.getBulbConfig()
if "moduleName" in bulb_config["result"]:
_bulbtype = bulb_config["result"]["moduleName"]
# set the minimum feature set and name
# define the kelvin range
_kelvin = await self.getExtendedWhiteRange()
# set the minimum features for dimmable bulbs (DW bulbs)
_bulb = BulbType(
bulb_type=BulbClass.DW,
name=_bulbtype,
features=Features(
brightness=True, color=False, effect=False, color_tmp=False
),
kelvin_range=KelvinRange(min=2700, max=6500),
kelvin_range=KelvinRange(min=_kelvin[0], max=_kelvin[1]),
)
# parse the features from name
_identifier = _bulbtype.split("_")[1]
try:
# parse the features from name
_identifier = _bulbtype.split("_")[1]
# Throw exception if index can not be found
except IndexError:
raise WizLightNotKnownBulb("The bulb type can not be determined!")
# go an try to map extensions to the BulbTyp object
# Color support
if "RGB" in _identifier:
_bulb.bulb_type = BulbClass.RGB
_bulb.features.color = True
# Only RGB bulbs can have an extenden 2200k range
_kelvin = await self._geExtendedWhiteRange()
_bulb.kelvin_range.min = _kelvin[0]
_bulb.kelvin_range.max = _kelvin[1]
# RGB supports effects and tunabel white
# RGB supports effects and tuneabel white
_bulb.features.effect = True
_bulb.features.color_tmp = True

# Non RGB but tunable white
# Non RGB but tunable white bulb
if "TW" in _identifier:
_bulb.bulb_type = BulbClass.TW
_bulb.features.color_tmp = True
# non RGB bulb
_bulb.features.color = False
# non RGB can not use 2200k white range
_kelvin = await self._getWhiteRange()
_bulb.kelvin_range.min = _kelvin[0]
_bulb.kelvin_range.max = _kelvin[1]
# RGB supports effects but only "some"
# ToDo: Improve the mapping to supported effects
_bulb.features.effect = True

self.bulbtype = _bulb
return _bulb
raise WizLightNotKnownBulb("The bulb features can not be mapped!")

async def _getWhiteRange(self):
async def getWhiteRange(self):
"""Read the white range from the bulb."""
resp = await self.getUserConfig()
if resp is not None and "result" in resp and self.whiteRange is None:
Expand All @@ -331,7 +330,7 @@ async def _getWhiteRange(self):
self.whiteRange = None
return self.whiteRange

async def _geExtendedWhiteRange(self):
async def getExtendedWhiteRange(self):
"""Read extended withe range from the RGB bulb."""
resp = await self.getUserConfig()
if resp is not None and "result" in resp and self.extwhiteRange is None:
Expand All @@ -340,6 +339,24 @@ async def _geExtendedWhiteRange(self):
self.extwhiteRange = None
return self.extwhiteRange

async def getSupportedScenes(self) -> list:
"""Return the supported scenes based on type.
Lookup: https://docs.pro.wizconnected.com
"""
if self.bulbtype is None:
await self.get_bulbtype()
# retrun for TW
if self.bulbtype.bulb_type == BulbClass.TW:
return [
SCENES[key]
for key in [6, 9, 10, 11, 12, 13, 14, 15, 16, 18, 29, 30, 31, 32]
]
if self.bulbtype.bulb_type == BulbClass.DW:
return [SCENES[key] for key in [9, 10, 13, 14, 29, 30, 31, 32]]
# Must be RGB with all
return SCENES

async def turn_off(self):
"""Turn the light off."""
message = r'{"method":"setPilot","params":{"state":false}}'
Expand Down
179 changes: 28 additions & 151 deletions pywizlight/bulblibrary.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
"""Library with compatible bulb types.
Importand note:
This lib be obsolete in the future. An automatic mapping will come.
Bulb Type detection:
ESP01_SHDW1C_31
ESP01 -- defines the module family, WiFi only bulb this case
SH -- Single Head light
SH -- Single Head light / Most bulbs are singel heads / Stripes
TW -- Tunable White, as in can only control CCT and dimming, no color
DW -- Dimmable White / Most filament bulbs
RGB -- Fullstack bulb
1C -- Specific to the hardware -- defines PWM frequency + way of controlling CCT temperature
31 -- Related to the hardware revision
"""


from pywizlight.scenes import SCENES
from enum import Enum


class Features:
"""Defines the supported features."""

Expand Down Expand Up @@ -41,163 +46,35 @@ def __init__(self, max: int, min: int) -> None:
self.min = min


class BulbClass(Enum):
"""Bulb Types."""

"""Have Cool White and Warm White LEDs."""
TW = "Tunabel White"
"""Have only Dimmable white LEDs."""
DW = "Dimable White"
"""Have only Dimmable white LEDs."""
RGB = "RGB Bulb"


class BulbType:
"""BulbType object to define functions and features of the bulb."""

features: Features
name: str
filament_bulb: bool
kelvin_range: KelvinRange
bulb_type: BulbClass

def __init__(
self, features: Features, name: str, kelvin_range: KelvinRange
self,
features: Features,
name: str,
kelvin_range: KelvinRange,
bulb_type: BulbClass,
) -> None:
"""Create a bulb object with different features."""
self.features = features
self.name = name
self.kelvin_range = kelvin_range


class BulbLib:
"""Provides all existing bulbs."""

BULBLIST = [
# Only Brightness
BulbType(
name="ESP01_SHDW_01",
features=Features(
brightness=True, color=False, effect=False, color_tmp=False
),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP01_SHDW1_31",
features=Features(
brightness=True, color=False, effect=False, color_tmp=False
),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP03_SHDW1_01",
features=Features(
brightness=True, color=False, effect=False, color_tmp=False
),
kelvin_range=KelvinRange(min=2200, max=6500),
),
# Brightness and Effects
BulbType(
name="ESP06_SHDW9_01",
features=Features(
brightness=True, color=False, effect=True, color_tmp=False
),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP06_SHDW1_01",
features=Features(
brightness=True, color=False, effect=True, color_tmp=False
),
kelvin_range=KelvinRange(min=2200, max=6500),
),
# Brightness and Color Temp
BulbType(
name="ESP01_SHTW1C_31",
features=Features(
brightness=True, color=False, effect=False, color_tmp=True
),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP17_SHTW9_01",
features=Features(
brightness=True, color=False, effect=False, color_tmp=True
),
kelvin_range=KelvinRange(min=2000, max=5000),
),
# Brightness, Color Temp and Effect
BulbType(
name="ESP56_SHTW3_01",
features=Features(
brightness=True, color=False, effect=True, color_tmp=True
),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP15_SHTW1_01I",
features=Features(
brightness=True, color=False, effect=True, color_tmp=True
),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP03_SHTW1C_01",
features=Features(
brightness=True, color=False, effect=True, color_tmp=True
),
kelvin_range=KelvinRange(min=2700, max=6500),
),
BulbType(
name="ESP03_SHTW1W_01",
features=Features(
brightness=True, color=False, effect=True, color_tmp=True
),
kelvin_range=KelvinRange(min=2700, max=6500),
),
BulbType(
name="ESP01_SHTW_03",
features=Features(
brightness=True, color=False, effect=True, color_tmp=True
),
kelvin_range=KelvinRange(min=2700, max=6500),
),
# Brightness, Color Temp, Color and Effect / all features
BulbType(
name="ESP01_SHRGB1C_31",
features=Features(brightness=True, color=True, effect=True, color_tmp=True),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP01_SHRGB3_01",
features=Features(brightness=True, color=True, effect=True, color_tmp=True),
kelvin_range=KelvinRange(min=2200, max=6500),
),
# Test device
BulbType(
name="ESP01_SHRGB_03",
features=Features(brightness=True, color=True, effect=True, color_tmp=True),
kelvin_range=KelvinRange(min=2700, max=6500),
),
BulbType(
name="ESP03_SHRGBP_31",
features=Features(brightness=True, color=True, effect=True, color_tmp=True),
kelvin_range=KelvinRange(min=2000, max=6500),
),
BulbType(
name="ESP03_SHRGB1C_01",
features=Features(brightness=True, color=True, effect=True, color_tmp=True),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP03_SHRGB1W_01",
features=Features(brightness=True, color=True, effect=True, color_tmp=True),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP03_SHRGB3_01ABI",
features=Features(brightness=True, color=True, effect=True, color_tmp=True),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP15_SHRGB1S_01I",
features=Features(brightness=True, color=True, effect=True, color_tmp=True),
kelvin_range=KelvinRange(min=2200, max=6500),
),
BulbType(
name="ESP14_SHRGB1C_01",
features=Features(brightness=True, color=True, effect=True, color_tmp=True),
kelvin_range=KelvinRange(min=2200, max=6500),
),
]

def getBulbList(self) -> list:
"""Retrun the list of known bulbs as BulbType list."""
return self.BULBLIST
self.bulb_type = bulb_type

0 comments on commit 6abebad

Please sign in to comment.