Support for multiple devices in AutoRemote plugin

This commit is contained in:
Fabio Manganiello 2018-10-22 09:36:30 +00:00
parent fec684210c
commit 4d592fff17

View file

@ -18,33 +18,53 @@ class AutoremotePlugin(Plugin):
_AUTOREMOTE_MESSAGE_URL = _AUTOREMOTE_BASE_URL + '/sendmessage' _AUTOREMOTE_MESSAGE_URL = _AUTOREMOTE_BASE_URL + '/sendmessage'
_AUTOREMOTE_NOTIFICATION_URL = _AUTOREMOTE_BASE_URL + '/sendnotification' _AUTOREMOTE_NOTIFICATION_URL = _AUTOREMOTE_BASE_URL + '/sendnotification'
def __init__(self, key, password=None, *args, **kwargs): def __init__(self, devices=None, key=None, password=None, *args, **kwargs):
""" """
:param key: The key associated to your device. Open the link in your AutoRemote app and copy the key in the target URL :param devices: Set this attribute if you want to control multiple AutoRemote devices. This will be a map in the format:
{
'device_name': {
'key': 'AUTOREMOTE_KEY',
'password': 'DEVICE_PASSWORD'
},
...
}
:type devices: dict
:param key: The key associated to your device. Open the link in your AutoRemote app and copy the key in the target URL. Set this value if you want to communicate with only one AutoRemote device.
:type key: str :type key: str
:param password: AutoRemote password configured on the device (default: None) :param password: AutoRemote password configured on the device (default: None). Set this value if you want to communicate with only one AutoRemote device.
:type password: str :type password: str
""" """
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.key = key
self.password = password if key:
self.devices = { key: { 'key': key, 'password': password } }
elif devices:
self.devices = devices
else:
raise RuntimeError('Either <key,password> or devices attributes should be set')
@action @action
def send_message(self, msg, key=None, password=None, target=None, def send_message(self, msg, key=None, password=None, devices=None, target=None,
sender=None, ttl=None, group=None, *args, **kwargs): sender=None, ttl=None, group=None, *args, **kwargs):
""" """
Sends a message to AutoRemote. Sends a message to AutoRemote.
:param msg: Message to send :param msg: Message to send
:param key: Set it if you want to override the default key (default: None) :param key: Set it if you want to override the default devices (default: None, message sent to all the configured devices)
:type key: str :type key: str
:param password: Set it if you want to override the default password (default: None) :param password: Set it if you want to override the default password (default: None)
:type password: str :type password: str
:param devices: Set it if you want to send the message to a specific list of configured devices (default: None, message sent to all the configured devices)
:type devices: list
:param target: Message target (default: None) :param target: Message target (default: None)
:type target: str :type target: str
@ -58,14 +78,29 @@ class AutoremotePlugin(Plugin):
:type group: str :type group: str
""" """
target_devices = []
if devices:
target_devices = [self.devices[name] for name in self.devices.keys()
if name in devices]
elif key:
device = { 'key': key }
if password:
device['password'] = password
target_devices = [device]
else:
target_devices = self.devices.values()
for device in target_devices:
args = { args = {
'message': msg, 'message': msg,
'key': key or self.key, 'key': device['key'],
'sender': sender or Config.get('device_id'), 'sender': sender or Config.get('device_id'),
} }
if password or self.password: if device.get('password'):
args['password'] = password or self.password args['password'] = device['password']
if target: args['target'] = target if target: args['target'] = target
if ttl: args['ttl'] = ttl if ttl: args['ttl'] = ttl
@ -75,10 +110,10 @@ class AutoremotePlugin(Plugin):
self.logger.info('Received response from AutoRemote: {}'.format(response.text)) self.logger.info('Received response from AutoRemote: {}'.format(response.text))
@action @action
def send_notification(self, text=None, key=None, password=None, title=None, def send_notification(self, text=None, key=None, password=None, devices=None,
target=None, sender=None, ttl=None, group=None, title=None, target=None, sender=None, ttl=None,
sound=None, vibration=None, url=None, id=None, group=None, sound=None, vibration=None, url=None,
action=None, icon=None, led=None, ledon=None, id=None, action=None, icon=None, led=None, ledon=None,
ledoff=None, picture=None, share=False, msg=None, ledoff=None, picture=None, share=False, msg=None,
action1=None, action1_name=None, action1_icon=None, action1=None, action1_name=None, action1_icon=None,
action2=None, action2_name=None, action2_icon=None, action2=None, action2_name=None, action2_icon=None,
@ -93,14 +128,29 @@ class AutoremotePlugin(Plugin):
Send Notification for a detailed explanation of the attributes. Send Notification for a detailed explanation of the attributes.
""" """
target_devices = []
if devices:
target_devices = [self.devices[name] for name in self.devices.keys()
if name in devices]
elif key:
device = { 'key': key }
if password:
device['password'] = password
target_devices = [device]
else:
target_devices = self.devices.values()
for device in target_devices:
args = { args = {
'text': text, 'text': text,
'key': key or self.key, 'key': device.get('key'),
'sender': sender or Config.get('device_id'), 'sender': sender or Config.get('device_id'),
} }
if password or self.password: if device.get('password'):
args['password'] = password or self.password args['password'] = device['password']
if target: args['target'] = target if target: args['target'] = target
if ttl: args['ttl'] = ttl if ttl: args['ttl'] = ttl