-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathclimate.py
328 lines (243 loc) · 7.85 KB
/
climate.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/local/bin/python3
'''
Author: zacharyrs
How to install:
Refer to README.md
License:
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
'''
import logging
from homeassistant.components.climate.const import (
HVAC_MODE_OFF,
HVAC_MODE_AUTO,
HVAC_MODE_COOL,
HVAC_MODE_DRY,
HVAC_MODE_HEAT,
HVAC_MODE_FAN_ONLY,
FAN_AUTO,
FAN_LOW,
FAN_MEDIUM,
FAN_HIGH,
SUPPORT_FAN_MODE,
SUPPORT_TARGET_TEMPERATURE
)
from homeassistant.components.climate import ClimateEntity
from homeassistant.const import (
ATTR_TEMPERATURE,
PRECISION_HALVES,
PRECISION_WHOLE,
STATE_OFF,
TEMP_CELSIUS
)
from .melview import MelViewAuthentication, MelView, MODE, FAN
_LOGGER = logging.getLogger(__name__)
DOMAIN = 'melview'
REQUIREMENTS = ['requests']
DEPENDENCIES = []
HVAC_MODES = [HVAC_MODE_AUTO, HVAC_MODE_COOL, HVAC_MODE_DRY, HVAC_MODE_FAN_ONLY, HVAC_MODE_HEAT, HVAC_MODE_OFF]
# ---------------------------------------------------------------
class MelViewClimate(ClimateEntity):
""" Melview handler for HomeAssistants
"""
def __init__(self, device):
self._device = device
self._name = 'MelView {}'.format(device.get_friendly_name())
self._unique_id = device.get_id()
self._operations_list = [x for x in MODE] + [HVAC_MODE_OFF]
self._speeds_list = [x for x in FAN]
self._precision = PRECISION_WHOLE
self._target_step = 1.0
if self._device.get_precision_halves():
self._precision = PRECISION_HALVES
self._target_step = 0.5
self._current_temp = self._device.get_room_temperature()
self._target_temp = self._device.get_temperature()
self._mode = self._device.get_mode()
self._speed = self._device.get_speed()
self._state = STATE_OFF
if self._device.is_power_on():
self._state = self._mode
def update(self):
""" Update device properties
"""
_LOGGER.debug('updating state')
self._device.force_update()
self._precision = PRECISION_WHOLE
self._target_step = 1.0
if self._device.get_precision_halves():
self._precision = PRECISION_HALVES
self._target_step = 0.5
self._current_temp = self._device.get_room_temperature()
self._target_temp = self._device.get_temperature()
self._mode = self._device.get_mode()
self._speed = self._device.get_speed()
self._state = self._mode
if not self._device.is_power_on():
self._mode = 'off'
self._state = STATE_OFF
@property
def name(self):
""" Diplay name for HASS
"""
return self._name
@property
def unique_id(self):
""" Get unique_id for HASS
"""
return self._unique_id
@property
def supported_features(self):
""" Let HASS know feature support
TODO: Handle looking at the device features?
"""
return (SUPPORT_TARGET_TEMPERATURE | SUPPORT_FAN_MODE)
@property
def should_poll(self):
""" Ensure HASS polls the unit
"""
return True
@property
def state(self):
""" Return the current state.
"""
return self._state
@property
def is_on(self):
""" Check unit is on
"""
return self._state != STATE_OFF
@property
def precision(self):
""" Return the precision of the system.
"""
return self._precision
@property
def temperature_unit(self):
""" Define unit for temperature
"""
return TEMP_CELSIUS
@property
def current_temperature(self):
""" Get the current room temperature
"""
return self._current_temp
@property
def target_temperature(self):
""" Get the target temperature
"""
return self._target_temp
# TODO
# @property
# def min_temp(self):
# """ Return the minimum temperature
# """
# return convert_temperature(DEFAULT_MIN_TEMP, TEMP_CELSIUS,
# self.temperature_unit)
# @property
# def max_temp(self):
# """ Return the maximum temperature
# """
# return convert_temperature(DEFAULT_MAX_TEMP, TEMP_CELSIUS,
# self.temperature_unit)
@property
def target_temperature_step(self):
""" Return the supported step of target temperature
"""
return self._target_step
@property
def hvac_mode(self):
""" Get the current operating mode
"""
return self._mode
@property
def hvac_modes(self):
""" Get possible operating modes
"""
return self._operations_list
@property
def fan_mode(self):
""" Check the unit fan speed
"""
return self._speed
@property
def fan_modes(self):
""" Get the possible fan speeds
"""
return self._speeds_list
def set_temperature(self, **kwargs):
""" Set the target temperature
"""
temp = kwargs.get(ATTR_TEMPERATURE)
if temp is not None:
_LOGGER.debug('setting temp %d', temp)
if self._device.set_temperature(temp):
self._current_temp = temp
def set_fan_mode(self, speed):
""" Set the fan speed
"""
_LOGGER.debug('set fan mode: %s', speed)
if self._device.set_speed(speed):
self._speed = speed
self._mode = self._device.get_mode()
self._state = self._mode
def set_hvac_mode(self, mode):
""" Set the operation mode
"""
_LOGGER.debug('set mode: %s', mode)
if mode == 'off':
self.turn_off()
elif self._device.set_mode(mode):
self._mode = mode
self._state = mode
def turn_on(self):
""" Turn on the unit
"""
_LOGGER.debug('power on')
if self._device.power_on():
self._mode = self._device.get_mode()
self._state = self._mode
def turn_off(self):
""" Turn off the unit
"""
_LOGGER.debug('power off')
if self._device.power_off():
self._mode = 'off'
self._state = STATE_OFF
# ---------------------------------------------------------------
def setup_platform(hass, config, add_devices, discovery_info=None):
""" Set up the HASS component
"""
_LOGGER.debug('adding component')
email = config.get('email')
password = config.get('password')
local = config.get('local')
if email is None:
_LOGGER.error('no email provided')
return False
if password is None:
_LOGGER.error('no password provided')
return False
if local is None:
_LOGGER.warning('local unspecified, defaulting to false')
local = False
mv_auth = MelViewAuthentication(email, password)
if not mv_auth.login():
_LOGGER.error('login combination')
return False
melview = MelView(mv_auth, localcontrol=local)
device_list = []
devices = melview.get_devices_list()
for device in devices:
_LOGGER.debug('new device: %s', device.get_friendly_name())
device_list.append(MelViewClimate(device))
add_devices(device_list)
_LOGGER.debug('component successfully added')
return True
# ---------------------------------------------------------------