forked from smaship/yeelight-ha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yeelight.py
143 lines (115 loc) · 4.2 KB
/
yeelight.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
"""
Support for Yeelight lights.
"""
import socket
import voluptuous as vol
import logging
from homeassistant.components.light import (ATTR_COLOR_TEMP, ATTR_BRIGHTNESS,ATTR_TRANSITION,SUPPORT_TRANSITION,SUPPORT_BRIGHTNESS,Light, SUPPORT_COLOR_TEMP, SUPPORT_RGB_COLOR, ATTR_RGB_COLOR)
import homeassistant.helpers.config_validation as cv
# Map ip to request id for configuring
_CONFIGURING = {}
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['https://github.com/mxtra/pyyeelight/archive/v1.5.zip#pyyeelight==1.5']
ATTR_NAME = 'name'
DOMAIN = "yeelight"
DEFAULT_TRANSITION_TIME = 350
DEVICE_SCHEMA = vol.Schema({
vol.Optional(ATTR_NAME): cv.string,
vol.Optional('transition', default=350): vol.Range(min=30, max=180000),
})
PLATFORM_SCHEMA = vol.Schema({
vol.Required('platform'): DOMAIN,
vol.Optional('devices', default={}): {cv.string: DEVICE_SCHEMA},
vol.Optional('transition', default=350): vol.Range(min=30, max=180000),
}, extra=vol.ALLOW_EXTRA)
SUPPORT_YEELIGHT_LED = (SUPPORT_BRIGHTNESS | SUPPORT_TRANSITION | SUPPORT_COLOR_TEMP | SUPPORT_RGB_COLOR)
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Setup the Yeelight lights."""
ylights = []
ylight_ips = []
if discovery_info is not None:
ipaddr = discovery_info.split(":")[0]
device = {}
device['ipaddr'] = ipaddr
device['name'] = socket.gethostbyaddr(ipaddr)[0]
device['transition'] = DEFAULT_TRANSITION_TIME
ylight = Yeelight(device)
if ylight.is_valid:
ylights.append(ylight)
ylight_ips.append(ipaddr)
else:
for ipaddr, device_config in config["devices"].items():
device = {}
device['name'] = device_config[ATTR_NAME]
device['ipaddr'] = ipaddr
if 'transition' in device_config:
device['transition'] = device_config['transition']
else:
device['transition'] = config['transition']
ylight = Yeelight(device)
if ylight.is_valid:
ylights.append(ylight)
ylight_ips.append(ipaddr)
add_devices_callback(ylights)
class Yeelight(Light):
"""Representation of a Yeelight light."""
# pylint: disable=too-many-argument
def __init__(self, device):
"""Initialize the light."""
import pyyeelight
self._name = device['name']
self._ipaddr = device['ipaddr']
self._transition = device['transition']
self.is_valid = True
self.bulb = None
try:
self.bulb = pyyeelight.YeelightBulb(self._ipaddr)
except socket.error:
self.is_valid = False
_LOGGER.error("Failed to connect to bulb %s, %s",
self._ipaddr, self._name)
@property
def unique_id(self):
return "{}.{}".format(self.__class__, self._ipaddr)
@property
def name(self):
return self._name
@property
def transition(self):
return self._transition
@property
def is_on(self):
"""Return true if bulb is on."""
return self.bulb.isOn()
@property
def brightness(self):
"""Return the brightness of this bulb."""
return self.bulb.brightness
@property
def supported_features(self):
"""Return supported features."""
return SUPPORT_YEELIGHT_LED
def turn_on(self, **kwargs):
"""Turn the bulb on"""
if not self.is_on:
self.bulb.turnOn()
transtime = 0
if ATTR_TRANSITION in kwargs:
transtime = kwargs[ATTR_TRANSITION]
elif self.transition:
transtime = self.transition
if ATTR_RGB_COLOR in kwargs:
self.bulb.setRgb(kwargs[ATTR_RGB_COLOR], transtime)
elif ATTR_COLOR_TEMP in kwargs:
self.bulb.setColorTemp(kwargs[ATTR_COLOR_TEMP],transtime)
if ATTR_BRIGHTNESS in kwargs:
self.bulb.setBrightness(kwargs[ATTR_BRIGHTNESS],transtime)
def turn_off(self, **kwargs):
"""Turn the bulb off."""
self.bulb.turnOff()
@property
def color_temp(self):
return self.bulb.ct
@property
def rgb_color(self):
return self.bulb.rgb