From 5dfad92a2d5e686782edc38d8b439ed7462e7004 Mon Sep 17 00:00:00 2001 From: ysard Date: Wed, 10 Nov 2021 02:15:22 +0100 Subject: [PATCH] Add bluetooth autoconnect ability --- .../resource.language.en_gb/strings.po | 8 ++++ .../resource.language.fr_fr/strings.po | 8 ++++ resources/lib/dbus_bluez.py | 4 ++ resources/lib/modules/bluetooth.py | 47 +++++++++++++++++-- resources/lib/oe.py | 9 ++++ service.py | 29 ++++++------ 6 files changed, 87 insertions(+), 18 deletions(-) diff --git a/resources/language/resource.language.en_gb/strings.po b/resources/language/resource.language.en_gb/strings.po index 61646bff2..9bffdca03 100644 --- a/resources/language/resource.language.en_gb/strings.po +++ b/resources/language/resource.language.en_gb/strings.po @@ -1016,6 +1016,14 @@ msgctxt "#32390" msgid "Settings addon is not yet ready, please try again later." msgstr "" +msgctxt "#32391" +msgid "Disable autoconnect" +msgstr "" + +msgctxt "#32392" +msgid "Enable autoconnect" +msgstr "" + msgctxt "#32393" msgid "** SAFE MODE! ** SAFE MODE! ** SAFE MODE! **" msgstr "" diff --git a/resources/language/resource.language.fr_fr/strings.po b/resources/language/resource.language.fr_fr/strings.po index d8525046e..b86cb1ec1 100644 --- a/resources/language/resource.language.fr_fr/strings.po +++ b/resources/language/resource.language.fr_fr/strings.po @@ -1147,6 +1147,14 @@ msgctxt "#32390" msgid "Settings addon is not yet ready, please try again later." msgstr "Le greffon des paramètres n'est pas encore prêt, veuillez ré-essayer plus tard." +msgctxt "#32391" +msgid "Disable autoconnect" +msgstr "Désactiver l'autoconnexion" + +msgctxt "#32392" +msgid "Enable autoconnect" +msgstr "Activer l'autoconnexion" + msgctxt "#32393" msgid "** SAFE MODE! ** SAFE MODE! ** SAFE MODE! **" msgstr "** MODE SANS ÉCHEC! ** MODE SANS ÉCHEC! ** MODE SANS ÉCHEC! **" diff --git a/resources/lib/dbus_bluez.py b/resources/lib/dbus_bluez.py index cdb18dbd5..d8c9f7f8e 100644 --- a/resources/lib/dbus_bluez.py +++ b/resources/lib/dbus_bluez.py @@ -178,6 +178,10 @@ def device_get_connected(path): return device_get_property(path, 'Connected') +def device_get_trusted(path): + return device_get_property(path, 'Trusted') + + def device_connect(path): return dbus_utils.run_method(BUS_NAME, path, INTERFACE_DEVICE, 'Connect') diff --git a/resources/lib/modules/bluetooth.py b/resources/lib/modules/bluetooth.py index d4cf247e4..75e7bc138 100644 --- a/resources/lib/modules/bluetooth.py +++ b/resources/lib/modules/bluetooth.py @@ -196,6 +196,22 @@ def disable_device_standby(self, listItem=None): devices.remove(listItem.getProperty('entry')) oe.write_setting('bluetooth', 'standby', ','.join(devices)) + @log.log_function() + def enable_device_autoconnect(self, listItem=None): + devices = oe.read_setting('bluetooth', 'autoconnect') + devices = devices.split(',') if devices else [] + if not listItem.getProperty('entry') in devices: + devices.append(listItem.getProperty('entry')) + oe.write_setting('bluetooth', 'autoconnect', ','.join(devices)) + + @log.log_function() + def disable_device_autoconnect(self, listItem=None): + devices = oe.read_setting('bluetooth', 'autoconnect') + devices = devices.split(',') if devices else [] + if listItem.getProperty('entry') in devices: + devices.remove(listItem.getProperty('entry')) + oe.write_setting('bluetooth', 'autoconnect', ','.join(devices)) + @log.log_function() def pair_device(self, path): try: @@ -383,10 +399,7 @@ def open_context_menu(self, listItem): 'action': 'disconnect_device', } devices = oe.read_setting('bluetooth', 'standby') - if devices is not None: - devices = devices.split(',') - else: - devices = [] + devices = devices.split(',') if devices else [] if listItem.getProperty('entry') in devices: values[4] = { 'text': oe._(32389), @@ -397,6 +410,20 @@ def open_context_menu(self, listItem): 'text': oe._(32388), 'action': 'enable_device_standby', } + + devices = oe.read_setting('bluetooth', 'autoconnect') + devices = devices.split(',') if devices else [] + if listItem.getProperty('entry') in devices: + values[7] = { + 'text': oe._(32391), + 'action': 'disable_device_autoconnect', + } + else: + values[7] = { + 'text': oe._(32392), + 'action': 'enable_device_autoconnect', + } + elif listItem.getProperty('Paired') == '1': values[1] = { 'text': oe._(32144), @@ -454,6 +481,18 @@ def standby_devices(self): if dbus_bluez.device_get_connected(device): self.disconnect_device_by_path(device) + def autoconnect_devices(self): + if not self.dbusBluezAdapter: + return + devices = oe.read_setting('bluetooth', 'autoconnect') + if not devices: + return + for path in devices.split(','): + if not dbus_bluez.device_get_connected(path) and dbus_bluez.device_get_trusted(path): + try: + dbus_bluez.device_connect(path) + except DBusError as e: + pass #################################################################### ## Bluez Listener class diff --git a/resources/lib/oe.py b/resources/lib/oe.py index e57e3fc55..01ba61ebf 100644 --- a/resources/lib/oe.py +++ b/resources/lib/oe.py @@ -637,6 +637,15 @@ def standby_devices(): except Exception as e: dbg_log('oe::standby_devices', f'ERROR: ({repr(e)})') +def autoconnect_devices(): + global dictModules + try: + if 'bluetooth' in dictModules: + dictModules['bluetooth'].autoconnect_devices() + except Exception as e: + dbg_log('oe::autoconnect_devices', f'ERROR: ({repr(e)})') + + def load_config(): try: global conf_lock diff --git a/service.py b/service.py index d22ca4798..28019e86d 100644 --- a/service.py +++ b/service.py @@ -81,23 +81,24 @@ def run(self): oe.start_service() service_thread = Service_Thread() service_thread.start() + oe.autoconnect_devices() while not self.abortRequested(): - if self.waitForAbort(60): + if self.waitForAbort(30): break - if not oe.read_setting('bluetooth', 'standby'): - continue - timeout = oe.read_setting('bluetooth', 'idle_timeout') - if not timeout: - continue - try: - timeout = int(timeout) - except: - continue - if timeout < 1: - continue - if xbmc.getGlobalIdleTime() / 60 >= timeout: - log.log(f'Idle timeout reached', log.DEBUG) + standby_devices = oe.read_setting('bluetooth', 'standby') + autoconnect_devices = oe.read_setting('bluetooth', 'autoconnect') + timeout = None + if standby_devices: + try: + timeout = int(oe.read_setting('bluetooth', 'idle_timeout')) + except TypeError: + pass + if timeout and xbmc.getGlobalIdleTime() / 60 >= timeout: + log.log('Idle timeout reached', log.DEBUG) oe.standby_devices() + else: + log.log('Autoconnect triggered', log.DEBUG) + oe.autoconnect_devices() if hasattr(oe, 'winOeMain') and hasattr(oe.winOeMain, 'visible'): if oe.winOeMain.visible == True: oe.winOeMain.close()