Caching switch.tplink devices and supporting lookup by alias

This commit is contained in:
Fabio Manganiello 2018-06-26 22:59:33 +02:00
parent 65f9610cce
commit 4163a414bf
2 changed files with 44 additions and 25 deletions

View file

@ -50,11 +50,12 @@ $(document).ready(function() {
$tplinkContainer.on('click touch', '.switch-ctrl-container', function() { $tplinkContainer.on('click touch', '.switch-ctrl-container', function() {
var $input = $(this).find('.switch-ctrl'); var $input = $(this).find('.switch-ctrl');
var devAddr = $input.data('name'); var devAddr = $input.data('name');
var action = $input.prop('checked') ? 'off' : 'on';
execute( execute(
{ {
type: 'request', type: 'request',
action: 'switch.tplink.toggle', action: 'switch.tplink.' + action,
args: { device: devAddr } args: { device: devAddr }
}, },

View file

@ -14,8 +14,37 @@ class SwitchTplinkPlugin(SwitchPlugin):
* **pyHS100** (``pip install pyHS100``) * **pyHS100** (``pip install pyHS100``)
""" """
_ip_to_dev = {}
_alias_to_dev = {}
def _scan(self): def _scan(self):
return Discover.discover() devices = Discover.discover()
self._ip_to_dev = {}
self._alias_to_dev = {}
for (ip, dev) in devices.items():
self._ip_to_dev[ip] = dev
self._alias_to_dev[dev.alias] = dev
return devices
def _get_device(self, device, use_cache=True):
if not use_cache:
self._scan()
if device in self._ip_to_dev:
return self._ip_to_dev[device]
if device in self._alias_to_dev:
return self._alias_to_dev[device]
if use_cache:
return self._get_device(device, use_cache=False)
else:
raise RuntimeError('Device {} not found'.format(device))
def status(self): def status(self):
""" """
@ -38,15 +67,12 @@ class SwitchTplinkPlugin(SwitchPlugin):
""" """
Turn on a device Turn on a device
:param device: Device IP or hostname :param device: Device IP, hostname or alias
:type device: str :type device: str
""" """
devices = self._scan() device = self._get_device(device)
if device not in devices: device.turn_on()
raise RuntimeError('Device {} not found'.format(device))
devices[device].turn_on()
return Response(output={'status':'on'}) return Response(output={'status':'on'})
@ -54,15 +80,12 @@ class SwitchTplinkPlugin(SwitchPlugin):
""" """
Turn off a device Turn off a device
:param device: Device IP or hostname :param device: Device IP, hostname or alias
:type device: str :type device: str
""" """
devices = self._scan() device = self._get_device(device)
if device not in devices: device.turn_off()
raise RuntimeError('Device {} not found'.format(device))
devices[device].turn_off()
return Response(output={'status':'off'}) return Response(output={'status':'off'})
@ -70,23 +93,18 @@ class SwitchTplinkPlugin(SwitchPlugin):
""" """
Toggle the state of a device (on/off) Toggle the state of a device (on/off)
:param device: Device IP or hostname :param device: Device IP, hostname or alias
:type device: str :type device: str
""" """
devices = self._scan() device = self._get_device(device, use_cache=False)
if device not in devices:
raise RuntimeError('Device {} not found'.format(device))
dev = devices[device] if device.is_on:
is_on = dev.is_on device.turn_off()
if is_on:
dev.turn_off()
else: else:
dev.turn_on() device.turn_on()
return Response(output={'status': 'off' if is_on else 'on'}) return Response(output={'status': 'off' if device.is_off else 'on'})
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: