forked from vincenzosuraci/hassio_meross_sensor_switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
470 lines (364 loc) · 18.1 KB
/
__init__.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
import inspect
from datetime import timedelta
import logging
import voluptuous as vol
import time
import datetime
from meross_iot.cloud.exceptions.CommandTimeoutException import CommandTimeoutException
from meross_iot.cloud.exceptions.StatusTimeoutException import StatusTimeoutException
from requests.exceptions import ConnectionError
from homeassistant.core import callback
from homeassistant.const import (CONF_USERNAME, CONF_PASSWORD, CONF_SCAN_INTERVAL)
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers import discovery
from homeassistant.helpers.dispatcher import (async_dispatcher_connect)
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import async_track_time_interval
from meross_iot.api import UnauthorizedException
from meross_iot.manager import MerossManager
from meross_iot.meross_event import MerossEventType
from meross_iot.cloud.devices.power_plugs import GenericPlug
from meross_iot.logger import set_log_level
# Setting log
_LOGGER = logging.getLogger('meross_init')
_LOGGER.setLevel(logging.DEBUG)
set_log_level(root=logging.INFO, connection=logging.INFO, network=logging.INFO)
# This is needed to ensure meross_iot library is always updated
# Ref: https://developers.home-assistant.io/docs/en/creating_integration_manifest.html
REQUIREMENTS = ['meross_iot==0.3.0.0b0']
# This is needed, it impact on the name to be called in configurations.yaml
# Ref: https://developers.home-assistant.io/docs/en/creating_integration_manifest.html
DOMAIN = 'meross'
MEROSS_MANAGER = 'manager'
HA_SWITCH = 'switch'
HA_SENSOR = 'sensor'
SIGNAL_DELETE_ENTITY = 'meross_delete'
SIGNAL_UPDATE_ENTITY = 'meross_update'
DEFAULT_SCAN_INTERVAL = timedelta(seconds=10)
CONF_MEROSS_DEVICES_SCAN_INTERVAL = 'meross_devices_scan_interval'
DEFAULT_MEROSS_DEVICES_SCAN_INTERVAL = timedelta(minutes=15)
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_PASSWORD): cv.string,
vol.Required(CONF_USERNAME): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=DEFAULT_SCAN_INTERVAL): cv.time_period,
vol.Optional(CONF_MEROSS_DEVICES_SCAN_INTERVAL, default=DEFAULT_MEROSS_DEVICES_SCAN_INTERVAL): cv.time_period,
})
}, extra=vol.ALLOW_EXTRA)
# ----------------------------------------------------------------------------------------------------------------------
#
# ASYNC SETUP
#
# ----------------------------------------------------------------------------------------------------------------------
async def async_setup(hass, config):
_LOGGER.debug('async_setup() >>> STARTED')
# create the MerossManager object
hass.data[DOMAIN] = MerossPlatform(hass, config)
_LOGGER.debug('async_setup() <<< TERMINATED')
return True
# ----------------------------------------------------------------------------------------------------------------------
#
# MEROSS PLUG
#
# ----------------------------------------------------------------------------------------------------------------------
class MerossPlug:
def __init__(self, hass, config, meross_device):
# homeassistant
self._hass = hass
self._config = config
# device
self.device = meross_device
self.uuid = meross_device.uuid
self.name = meross_device.name
self.was_available = meross_device.online
# async register sensors & switches
self.sensor_states = {}
self.switch_states = {}
self.sensor_switch_added = False
if self.was_available:
self.add_sensor_and_switches()
else:
_LOGGER.info(self.name + ' is offline >>> no sensor or switch added')
def add_sensor_and_switches(self):
self._hass.async_create_task(
discovery.async_load_platform(self._hass,
HA_SENSOR,
DOMAIN,
{'meross_device_uuid': self.uuid},
self._config))
self._hass.async_create_task(
discovery.async_load_platform(self._hass,
HA_SWITCH,
DOMAIN,
{'meross_device_uuid': self.uuid},
self._config))
self.sensor_switch_added = True
@property
def available(self):
return self.device.online
def set_availability(self, available):
if self.was_available != available:
self.was_available = available
if available:
_LOGGER.info(self.name + ' is online')
if not self.sensor_switch_added:
self.add_sensor_and_switches()
else:
_LOGGER.info(self.name + ' is offline')
for name, sensor in self.sensor_states.items():
sensor['available'] = available
for channel, switch in self.switch_states.items():
switch['available'] = available
async def async_update_status(self):
_LOGGER.debug(self.name + ' async_update_status() >>> STARTED')
available = self.available
self.set_availability(available)
if available:
self.update_switch_status()
self.update_sensor_status()
_LOGGER.debug(self.name + ' async_update_status() >>> TERMINATED')
return True
def update_switch_status(self):
_LOGGER.debug(self.name + 'update_switch_status() >>> STARTED')
# for each channel (switch), update its status (on/off)
for channel, switch_state in self.switch_states.items():
try:
# update the Meross Device switch status
# WARNING: potentially blocking >>> CommandTimeoutException expected
channel_status = self.device.get_channel_status(channel)
switch_state['is_on'] = channel_status
_LOGGER.debug(self.name + ' >>> channel ' +
str(channel) + ' >>> ' +
str(channel_status))
except StatusTimeoutException:
# Handle a StatusTimeoutException
handle_status_timeout_exception(inspect.stack()[0][3])
except CommandTimeoutException:
# Handle a CommandTimeoutException
handle_command_timeout_exception(inspect.stack()[0][3])
_LOGGER.debug(self.name + 'update_switch_status() <<< STARTED')
def update_sensor_status(self):
if len(self.sensor_states) > 0:
try:
# for each electricity <key,value> pair, save it in hass object
# WARNING: potentially blocking >>> CommandTimeoutException expected
_LOGGER.debug(self.name + ' >>> get_electricity()')
electricity = self.device.get_electricity()
for key, value in electricity.items():
if key in self.sensor_states:
self.sensor_states[key]['value'] = value
except CommandTimeoutException:
handle_command_timeout_exception(inspect.stack()[0][3])
# ----------------------------------------------------------------------------------------------------------------------
#
# MEROSS PLATFORM
#
# ----------------------------------------------------------------------------------------------------------------------
class MerossPlatform:
def __init__(self, hass, config):
self._hass = hass
self._config = config
self._username = config[DOMAIN][CONF_USERNAME]
self._password = config[DOMAIN][CONF_PASSWORD]
self.update_status_interval = config[DOMAIN][CONF_SCAN_INTERVAL]
self.discover_plugs_interval = config[DOMAIN][CONF_MEROSS_DEVICES_SCAN_INTERVAL]
# start meross manager
self._meross_manager = None
self.start_meross_manager()
# first discover plugs
self.meross_plugs_by_uuid = {}
hass.async_create_task(self.async_discover_plugs())
# first update
hass.async_create_task(self.async_update_plugs())
# starting timers
hass.async_create_task(self.async_start_timer())
async def async_start_timer(self):
# This is used to update the Meross Devices status periodically
_LOGGER.info('Meross devices status will be updated each ' + str(self.update_status_interval))
async_track_time_interval(self._hass,
self.async_update_plugs,
self.update_status_interval)
# This is used to discover new Meross Devices periodically
_LOGGER.info('Meross devices list will be updated each ' + str(self.discover_plugs_interval))
async_track_time_interval(self._hass,
self.async_discover_plugs,
self.discover_plugs_interval)
return True
async def async_update_plugs(self, now=None):
# monitor the duration in millis
# registering starting timestamp in ms
start_ms = int(round(time.time() * 1000))
_LOGGER.debug('async_update_plugs() >>> STARTED at ' + str(now))
for meross_device_uuid, meross_plug in self.meross_plugs_by_uuid.items():
_LOGGER.debug(meross_plug.name + ' plug status update >>> STARTED')
await meross_plug.async_update_status()
_LOGGER.debug(meross_plug.name + ' plug status update <<< TERMINATED')
_LOGGER.debug('async_update_plugs() <<< TERMINATED')
# registering ending timestamp in ms
end_ms = int(round(time.time() * 1000))
duration_ms = end_ms - start_ms
duration_s = int(round(duration_ms / 1000))
duration_td = datetime.timedelta(seconds=duration_s)
if duration_td > self.update_status_interval:
_LOGGER.warning('Updating the Meross plug status took ' + str(duration_td))
return True
async def async_discover_plugs(self, now=None):
_LOGGER.debug('async_discover_plugs >>> STARTED at ' + str(now))
# get all the registered meross_plugs
meross_plugs = self._meross_manager.get_devices_by_kind(GenericPlug)
# check each registered meross plug
for meross_plug in meross_plugs:
# get meross plug uuid
meross_plug_uuid = meross_plug.uuid
# Check if the meross plug uuid has been already discovered
if meross_plug_uuid not in self.meross_plugs_by_uuid:
self.meross_plugs_by_uuid[meross_plug_uuid] = MerossPlug(self._hass,
self._config,
meross_plug)
_LOGGER.debug('async_discover_plugs <<< FINISHED')
return True
def meross_event_handler(self, eventobj):
_LOGGER.info(str(eventobj.event_type) + " event detected")
if eventobj.event_type == MerossEventType.CLIENT_CONNECTION:
# Fired when the MQTT client connects/disconnects to the MQTT broker
# do nothing...
pass
elif eventobj.event_type == MerossEventType.DEVICE_ONLINE_STATUS:
_LOGGER.info("Device online status changed: %s went %s" % (eventobj.device.name, eventobj.status))
meross_device_uuid = eventobj.device.uuid
meross_device_availability = eventobj.status
if meross_device_uuid in self.meross_plugs_by_uuid:
# the device has been already discovered >>> update its availability
meross_plug = self.meross_plugs_by_uuid[meross_device_uuid]
meross_plug.set_availability(meross_device_availability)
else:
# the device has not yet been discovered >>> add it
self._hass.async_create_task(self.async_discover_plugs())
pass
elif eventobj.event_type == MerossEventType.DEVICE_SWITCH_STATUS:
_LOGGER.info("Switch state changed: Device %s (channel %d) went %s" %
(eventobj.device.name, eventobj.channel_id, eventobj.switch_state))
meross_device_uuid = eventobj.device.uuid
channel = eventobj.channel_id
channel_status = eventobj.switch_state
meross_plug = self.meross_plugs_by_uuid[meross_device_uuid]
meross_plug.switch_states[channel]['is_on'] = channel_status
else:
_LOGGER.warning(str(eventobj.event_type) + " is an unknown event!")
pass
def start_meross_manager(self):
try:
# Create the manager
self._meross_manager = MerossManager(self._username, self._password)
# Starts the manager
self._meross_manager.start()
# Register event handlers for the manager...
self._meross_manager.register_event_handler(self.meross_event_handler)
except CommandTimeoutException:
handle_command_timeout_exception('start_meross_manager()')
except UnauthorizedException:
handle_unauthorized_exception('start_meross_manager()')
except ConnectionError:
handle_connection_error_exception('start_meross_manager()')
pass
# ----------------------------------------------------------------------------------------------------------------------
#
# MEROSS ENTITY
#
# ----------------------------------------------------------------------------------------------------------------------
class MerossEntity(Entity):
# Meross entity ( sensor / switch )
def __init__(self, hass, meross_device_uuid, meross_device_name, meross_entity_id, meross_entity_name, available):
self.hass = hass
self.entity_id = meross_entity_id
"""Register the physical Meross device id"""
self._meross_device_uuid = meross_device_uuid
self._meross_entity_name = meross_entity_name
self._meross_device_name = meross_device_name
self._available = available
_LOGGER.debug(self._meross_device_name + ' >>> ' + self._meross_entity_name + ' >>> __init__()')
async def async_added_to_hass(self):
# Called when an entity has their entity_id and hass object assigned, before it is written to the state
# machine for the first time. Example uses: restore the state or subscribe to updates.
_LOGGER.debug(self._meross_device_name + ' >>> ' +
self._meross_entity_name + ' >>> async_added_to_hass()')
_LOGGER.debug(self._meross_device_name + ' >>> ' +
self._meross_entity_name + ' >>> entity_id: ' +
self.entity_id)
async_dispatcher_connect(self.hass,
SIGNAL_DELETE_ENTITY,
self._delete_callback)
async_dispatcher_connect(self.hass,
SIGNAL_UPDATE_ENTITY,
self._update_callback)
return True
async def async_will_remove_from_hass(self):
# Called when an entity is about to be removed from Home Assistant. Example use: disconnect from the server or
# unsubscribe from updates
_LOGGER.debug(self._meross_device_name + ' >>> ' +
self._meross_entity_name + ' >>> async_will_remove_from_hass()')
return True
async def async_update(self):
# update is done in the update function
_LOGGER.debug(self._meross_device_name + ' >>> ' +
self._meross_entity_name + ' >>> async_update()')
return True
@property
def device_id(self):
# Return Meross device id.
_LOGGER.debug(self._meross_device_name + ' >>> ' +
self._meross_entity_name + ' >>> device_id() >>> ' +
self._meross_device_uuid)
return self._meross_device_uuid
@property
def unique_id(self):
# Return a unique ID."
_LOGGER.debug(self._meross_device_name + ' >>> ' +
self._meross_entity_name + ' >>> unique_id() >>> ' +
self.entity_id)
return self.entity_id
@property
def name(self):
# Return Meross device name.
_LOGGER.debug(self._meross_device_name + ' >>> ' +
self._meross_entity_name + ' >>> name() >>> ' +
self._meross_device_name)
return self._meross_device_name
@property
def available(self):
# Return if the device is available.
_LOGGER.debug(self._meross_device_name + ' >>> ' +
self._meross_entity_name + ' >>> available() >>> ' +
str(self._available))
return self._available
@callback
def _delete_callback(self, entity_id):
# Remove this entity.
if entity_id == self.entity_id:
_LOGGER.debug(self._meross_device_name + ' >>> ' +
self._meross_entity_name + ' >>> _delete_callback()')
self.hass.async_create_task(self.async_remove())
@callback
def _update_callback(self):
# Call update method.
_LOGGER.debug(self._meross_device_name + ' >>> ' +
self._meross_entity_name + ' >>> _update_callback()')
self.async_schedule_update_ha_state(True)
# ----------------------------------------------------------------------------------------------------------------------
#
# EXCEPTION HANDLING FUNCTIONS
#
# ----------------------------------------------------------------------------------------------------------------------
def handle_unauthorized_exception(caller):
_LOGGER.warning('UnauthorizedException when executing ' + caller + ' >>> check: a) internet connection, b) Meross '
'account credentials')
pass
def handle_command_timeout_exception(caller):
_LOGGER.warning('CommandTimeoutException when executing ' + caller)
pass
def handle_connection_error_exception(caller):
_LOGGER.warning('ConnectionError when executing ' + caller + ' >>> check internet connection')
pass
def handle_status_timeout_exception(caller):
_LOGGER.warning('StatusTimeout when executing ' + caller + ' >>> check internet connection')
pass