-
Notifications
You must be signed in to change notification settings - Fork 0
/
huizuo.py
153 lines (122 loc) · 4.27 KB
/
huizuo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"""
Basic implementation for HUAYI HUIZUO PISCES For Bedroom (huayi.light.pis123) lamp
This lamp is white color only and supports dimming and control of the temperature from 3000K to 6400K
Specs: https://miot-spec.org/miot-spec-v2/instance?type=urn:miot-spec-v2:device:light:0000A001:huayi-pis123:1
"""
import logging
from typing import Any, Dict
import click
from .click_common import command, format_output
from .exceptions import DeviceException
from .miot_device import MiotDevice
_LOGGER = logging.getLogger(__name__)
_MAPPING = {
"power": {"siid": 2, "piid": 1},
"brightness": {"siid": 2, "piid": 2},
"color_temp": {"siid": 2, "piid": 3},
}
MODEL_HUIZUO_PIS123 = "huayi.light.pis123"
MODELS_SUPPORTED = [MODEL_HUIZUO_PIS123]
class HuizuoException(DeviceException):
pass
class HuizuoStatus:
def __init__(self, data: Dict[str, Any]) -> None:
"""
Response of a Huizuo Pisces For Bedroom (huayi.light.pis123)
{'id': 1, 'result': [
{'did': '', 'siid': 2, 'piid': 1, 'code': 0, 'value': False},
{'did': '', 'siid': 2, 'piid': 2, 'code': 0, 'value': 94},
{'did': '', 'siid': 2, 'piid': 3, 'code': 0, 'value': 6400}
]
}
Explanation (line-by-line):
power = '{"siid":2,"piid":1}' values = true,false
brightless(%) = '{"siid":2,"piid":2}' values = 1-100
color temperature(Kelvin) = '{"siid":2,"piid":3}' values = 3000-6400
"""
self.data = data
@property
def is_on(self) -> bool:
"""Return True if device is on."""
return self.data["power"]
@property
def brightness(self) -> int:
"""Return current brightness."""
return self.data["brightness"]
@property
def color_temp(self) -> int:
"""Return current color temperature."""
return self.data["color_temp"]
def __repr__(self):
s = "<Huizuo on=%s brightness=%s color_temp=%s>" % (
self.is_on,
self.brightness,
self.color_temp,
)
return s
class Huizuo(MiotDevice):
"""A support for Huizuo PIS123."""
def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
model: str = MODEL_HUIZUO_PIS123,
) -> None:
super().__init__(_MAPPING, ip, token, start_id, debug, lazy_discover)
if model in MODELS_SUPPORTED:
self.model = model
else:
self.model = MODEL_HUIZUO_PIS123
_LOGGER.error(
"Device model %s unsupported. Falling back to %s.", model, self.model
)
@command(
default_output=format_output("Powering on"),
)
def on(self):
"""Power on."""
return self.set_property("power", True)
@command(
default_output=format_output("Powering off"),
)
def off(self):
"""Power off."""
return self.set_property("power", False)
@command(
default_output=format_output(
"\n",
"Power: {result.is_on}\n"
"Brightness: {result.brightness}\n"
"Color Temperature: {result.color_temp}\n"
"\n",
)
)
def status(self) -> HuizuoStatus:
"""Retrieve properties."""
return HuizuoStatus(
{
prop["did"]: prop["value"] if prop["code"] == 0 else None
for prop in self.get_properties_for_mapping()
}
)
@command(
click.argument("level", type=int),
default_output=format_output("Setting brightness to {level}"),
)
def set_brightness(self, level):
"""Set brightness."""
if level < 0 or level > 100:
raise HuizuoException("Invalid brightness: %s" % level)
return self.set_property("brightness", level)
@command(
click.argument("color_temp", type=int),
default_output=format_output("Setting color temperature to {color_temp}"),
)
def set_color_temp(self, color_temp):
"""Set color temp in kelvin."""
if color_temp < 3000 or color_temp > 6400:
raise HuizuoException("Invalid color temperature: %s" % color_temp)
return self.set_property("color_temp", color_temp)