diff --git a/eternalegypt/eternalegypt.py b/eternalegypt/eternalegypt.py index 17c9598..33e3037 100644 --- a/eternalegypt/eternalegypt.py +++ b/eternalegypt/eternalegypt.py @@ -83,7 +83,6 @@ class LB2120: listeners = attr.ib(init=False, factory=list) max_sms_id = attr.ib(init=False, default=None) - task = attr.ib(init=False, default=None) @property def _baseurl(self): @@ -151,20 +150,61 @@ async def sms(self, phone, message): async with self.websession.post(url, data=data) as response: _LOGGER.debug("Sent message with status %d", response.status) - @autologin - async def delete_sms(self, sms_id): - """Delete a message.""" - + def _config_call(self, key, value): + """Set a configuration key to a certain value.""" url = self._url('Forms/config') data = { - 'sms.deleteId': sms_id, + key: value, 'err_redirect': '/error.json', 'ok_redirect': '/success.json', 'token': self.token } - async with self.websession.post(url, data=data) as response: + return self.websession.post(url, data=data) + + @autologin + async def connect_lte(self): + """Do an LTE reconnect.""" + async with self._config_call('wwan.connect', 'DefaultProfile') as response: + _LOGGER.debug("Connected to LTE with status %d", response.status) + + @autologin + async def delete_sms(self, sms_id): + """Delete a message.""" + async with self._config_call('sms.deleteId', sms_id) as response: _LOGGER.debug("Delete %d with status %d", sms_id, response.status) + @autologin + async def set_failover_mode(self, mode): + """Set failover mode.""" + modes = { + 'auto': 'Auto', + 'wire': 'WAN', + 'mobile': 'LTE', + } + + if mode not in modes.keys(): + _LOGGER.error("Invalid mode %s not %s", mode, "/".join(modes.keys())) + return + + async with self._config_call('failover.mode', modes[mode]) as response: + _LOGGER.debug("Set mode to %s", mode) + + @autologin + async def set_autoconnect_mode(self, mode): + """Set autoconnect mode.""" + modes = { + 'never': 'Never', + 'home': 'HomeNetwork', + 'always': 'Always', + } + + if mode not in modes.keys(): + _LOGGER.error("Invalid mode %s not %s", mode, "/".join(modes.keys())) + return + + async with self._config_call('wwan.autoconnect', modes[mode]) as response: + _LOGGER.debug("Set mode to %s", mode) + def _build_information(self, data): """Read the bits we need from returned data.""" if 'wwan' not in data: @@ -172,10 +212,11 @@ def _build_information(self, data): result = Information() + result.serial_number = data['general']['FSN'] result.usage = data['wwan']['dataUsage']['generic']['dataTransferred'] result.upstream = data['failover']['backhaul'] - result.serial_number = data['general']['FSN'] - result.connection = data['wwan']['connection'] + result.wire_connected = 'Connected' if data['failover']['wanConnected'] else 'Disconnected' + result.mobile_connected = data['wwan']['connection'] result.connection_text = data['wwan']['connectionText'] result.connection_type = data['wwan']['connectionType'] result.current_nw_service_type = data['wwan']['currentNWserviceType'] diff --git a/examples/auto_connect.py b/examples/auto_connect.py new file mode 100755 index 0000000..2453668 --- /dev/null +++ b/examples/auto_connect.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +"""Example file for eternalegypt library.""" + +import sys +import asyncio +import aiohttp +import logging + +import eternalegypt + +async def set_autoconnect_mode(mode): + """Example of printing the current upstream.""" + jar = aiohttp.CookieJar(unsafe=True) + websession = aiohttp.ClientSession(cookie_jar=jar) + + try: + modem = eternalegypt.Modem(hostname=sys.argv[1], websession=websession) + await modem.login(password=sys.argv[2]) + + await modem.set_autoconnect_mode(mode) + + await modem.logout() + except eternalegypt.Error: + print("Could not login") + + await websession.close() + +if len(sys.argv) != 4: + print("{}: ".format(sys.argv[0])) +else: + asyncio.get_event_loop().run_until_complete(set_autoconnect_mode(sys.argv[3])) diff --git a/examples/connect_lte.py b/examples/connect_lte.py new file mode 100755 index 0000000..571630e --- /dev/null +++ b/examples/connect_lte.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +"""Example file for eternalegypt library.""" + +import sys +import asyncio +import aiohttp +import logging + +import eternalegypt + +async def connect(): + """Example of printing the current upstream.""" + jar = aiohttp.CookieJar(unsafe=True) + websession = aiohttp.ClientSession(cookie_jar=jar) + + try: + modem = eternalegypt.Modem(hostname=sys.argv[1], websession=websession) + await modem.login(password=sys.argv[2]) + + await modem.connect_lte() + + await modem.logout() + except eternalegypt.Error: + print("Could not login") + + await websession.close() + +if len(sys.argv) != 3: + print("{}: ".format(sys.argv[0])) +else: + asyncio.get_event_loop().run_until_complete(connect()) diff --git a/examples/failover.py b/examples/failover.py new file mode 100755 index 0000000..a48ba6a --- /dev/null +++ b/examples/failover.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +"""Example file for eternalegypt library.""" + +import sys +import asyncio +import aiohttp +import logging + +import eternalegypt + +async def set_failover_mode(mode): + """Example of printing the current upstream.""" + jar = aiohttp.CookieJar(unsafe=True) + websession = aiohttp.ClientSession(cookie_jar=jar) + + try: + modem = eternalegypt.Modem(hostname=sys.argv[1], websession=websession) + await modem.login(password=sys.argv[2]) + + await modem.set_failover_mode(mode) + + await modem.logout() + except eternalegypt.Error: + print("Could not login") + + await websession.close() + +if len(sys.argv) != 4: + print("{}: ".format(sys.argv[0])) +else: + asyncio.get_event_loop().run_until_complete(set_failover_mode(sys.argv[3])) diff --git a/examples/status.py b/examples/status.py index 2e1b410..91fd9ee 100755 --- a/examples/status.py +++ b/examples/status.py @@ -20,7 +20,8 @@ async def get_information(): result = await modem.information() print("upstream: {}".format(result.upstream)) print("serial_number: {}".format(result.serial_number)) - print("connection: {}".format(result.connection)) + print("wire_connected: {}".format(result.wire_connected)) + print("mobile_connected: {}".format(result.mobile_connected)) print("connection_text: {}".format(result.connection_text)) print("connection_type: {}".format(result.connection_type)) print("current_nw_service_type: {}".format(result.current_nw_service_type))