2017-12-13 03:37:28 +01:00
|
|
|
import json
|
2017-11-04 00:13:22 +01:00
|
|
|
|
|
|
|
from ouimeaux.environment import Environment, UnknownDevice
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.plugins import action
|
|
|
|
from platypush.plugins.switch import SwitchPlugin
|
2017-11-04 00:13:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SwitchWemoPlugin(SwitchPlugin):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
Plugin to control a Belkin WeMo smart switch
|
|
|
|
(https://www.belkin.com/us/Products/home-automation/c/wemo-home-automation/)
|
|
|
|
|
|
|
|
Requires:
|
|
|
|
|
|
|
|
* **ouimeaux** (``pip install ouimeaux``)
|
|
|
|
"""
|
|
|
|
|
2018-05-08 09:59:43 +02:00
|
|
|
def __init__(self, discovery_seconds=3, *args, **kwargs):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
:param discovery_seconds: Discovery time when scanning for devices (default: 3)
|
|
|
|
:type discovery_seconds: int
|
|
|
|
"""
|
2017-12-18 01:10:51 +01:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
2017-12-13 03:37:28 +01:00
|
|
|
self.discovery_seconds=discovery_seconds
|
2017-11-04 00:13:22 +01:00
|
|
|
self.env = Environment()
|
|
|
|
self.env.start()
|
2017-12-13 03:37:28 +01:00
|
|
|
self.refresh_devices()
|
|
|
|
|
|
|
|
def refresh_devices(self):
|
2018-06-25 19:57:43 +02:00
|
|
|
""" Update the list of available devices """
|
2018-06-06 20:09:18 +02:00
|
|
|
self.logger.info('Starting WeMo discovery')
|
2017-12-13 03:37:28 +01:00
|
|
|
self.env.discover(seconds=self.discovery_seconds)
|
|
|
|
self.devices = self.env.devices
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-05-08 09:14:02 +02:00
|
|
|
def get_devices(self):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
Get the list of available devices
|
|
|
|
:returns: The list of devices.
|
|
|
|
|
|
|
|
Example output::
|
|
|
|
|
|
|
|
output = {
|
|
|
|
"devices": [
|
|
|
|
{
|
|
|
|
"host": "192.168.1.123",
|
|
|
|
"name": "Switch 1",
|
|
|
|
"state": 1,
|
|
|
|
"model": "Belkin Plugin Socket 1.0",
|
|
|
|
"serialnumber": "123456ABCDEF"
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
# ...
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
"""
|
2018-05-08 09:14:02 +02:00
|
|
|
self.refresh_devices()
|
2018-07-06 02:08:38 +02:00
|
|
|
return {
|
|
|
|
'devices': [
|
2018-05-08 09:59:43 +02:00
|
|
|
{
|
2018-05-08 09:14:02 +02:00
|
|
|
'host': dev.host,
|
2018-05-08 09:59:43 +02:00
|
|
|
'name': dev.name,
|
2018-05-08 09:14:02 +02:00
|
|
|
'state': dev.get_state(),
|
|
|
|
'model': dev.model,
|
|
|
|
'serialnumber': dev.serialnumber,
|
|
|
|
}
|
|
|
|
for (name, dev) in self.devices.items()
|
2018-07-06 02:08:38 +02:00
|
|
|
]
|
|
|
|
}
|
2018-05-08 09:14:02 +02:00
|
|
|
|
2017-12-13 03:37:28 +01:00
|
|
|
def _exec(self, method, device, *args, **kwargs):
|
|
|
|
if device not in self.devices:
|
|
|
|
self.refresh_devices()
|
|
|
|
|
|
|
|
if device not in self.devices:
|
|
|
|
raise RuntimeError('Device {} not found'.format(device))
|
|
|
|
|
2018-06-06 20:09:18 +02:00
|
|
|
self.logger.info('{} -> {}'.format(device, method))
|
2017-12-13 03:37:28 +01:00
|
|
|
dev = self.devices[device]
|
|
|
|
getattr(dev, method)(*args, **kwargs)
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
return {'device': device, 'state': dev.get_state()}
|
2017-11-04 00:13:22 +01:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2017-11-04 12:28:15 +01:00
|
|
|
def on(self, device):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
Turn a switch on
|
|
|
|
|
|
|
|
:param device: Device name
|
|
|
|
:type device: str
|
|
|
|
"""
|
2017-12-13 03:37:28 +01:00
|
|
|
return self._exec('on', device)
|
2017-11-04 00:13:22 +01:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2017-11-04 12:28:15 +01:00
|
|
|
def off(self, device):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
Turn a switch off
|
|
|
|
|
|
|
|
:param device: Device name
|
|
|
|
:type device: str
|
|
|
|
"""
|
2017-12-13 03:37:28 +01:00
|
|
|
return self._exec('off', device)
|
2017-11-04 00:13:22 +01:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2017-11-04 12:28:15 +01:00
|
|
|
def toggle(self, device):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
|
|
|
Toggle the state of a switch (on/off)
|
|
|
|
|
|
|
|
:param device: Device name
|
|
|
|
:type device: str
|
|
|
|
"""
|
2017-12-13 03:37:28 +01:00
|
|
|
return self._exec('toggle', device)
|
2017-11-04 00:13:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|