Skip to content

Commit

Permalink
Fix cmd interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre0512 committed Mar 16, 2024
1 parent 1f5c529 commit 75c64bb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@
[![PyPI - Downloads](https://img.shields.io/pypi/dm/speedport-api)](https://pypistats.org/packages/speedport-api)
Control Telekom Speedport routers with Python!


| ⚠️ **Use v0.4.x for cmd interface** ⚠️ |
|----------------------------------------|

I'm developing a [Speedport Home Assistant integration](https://github.com/Andre0512/speedport), currently this breaks the cmd interface functionality. I will fix this soon!

### Installation
```commandline
pip install speedport-api
Expand Down Expand Up @@ -69,6 +63,18 @@ $ speedport devices
| 10.5.12.227 | andre-xps | wlan5 | 1 |
```


#### Print calls
```commandline
$ speedport calls
+-------------+--------+----------+---------------------+
| number | type | duration | date |
+-------------+--------+----------+---------------------+
| 01578212345 | missed | 0 | 2024-04-04 09:34:35 |
| 026361245 | taken | 1337 | 2024-04-06 05:12:53 |
| 7866 | dialed | 20 | 2024-04-06 18:39:00 |
```

### Library

#### Reconnect example
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

setup(
name="speedport-api",
version="0.5.9",
version="0.6.0",
author="Andre Basche",
description="Control Telekom Speedport routers with Python",
long_description=long_description,
Expand Down
34 changes: 23 additions & 11 deletions speedport/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env python

import argparse
import asyncio
import logging
Expand Down Expand Up @@ -48,7 +49,7 @@ def data_table(data, keys):
for d in data:
text += (
"| "
+ " | ".join([f"{d[key]:<{length}}" for key, length in columns.items()])
+ " | ".join([f"{str(d[key]):<{length}}" for key, length in columns.items()])
+ " |\n"
)
return text + line
Expand Down Expand Up @@ -108,6 +109,8 @@ def get_arguments():
wps.add_argument("wps", help="Turn on wps for 2 minutes", action="store_true")
devices = subparser.add_parser("devices", help="Output devices")
devices.add_argument("devices", help="List connected devices", action="store_true")
calls = subparser.add_parser("calls", help="Output calls")
calls.add_argument("calls", help="List last calls", action="store_true")
return vars(parser.parse_args())


Expand All @@ -128,6 +131,16 @@ async def check_args(speedport, args):
["ipv4", "name", "type", "connected"],
)
)
if args.get("calls"):
if args.get("batch"):
batch_output(await speedport.devices)
else:
print(
data_table(
await speedport.calls,
["number", "type", "duration", "date"],
)
)


async def check_wifi_args(speedport, args):
Expand All @@ -148,26 +161,25 @@ async def check_wifi_args(speedport, args):
async def main():
args = get_arguments()
set_logger(args)
speedport = Speedport(args["host"], args.get("https"))
password = ""
if not args.get("devices"):
if not (password := args["password"]):
password = getpass("Password of Speedports webinterface: ")
if not await speedport.login(password=password):
print("Can't login! Wrong password?")
return
await check_wifi_args(speedport, args)
await check_args(speedport, args)
async with Speedport(args["host"], password, args.get("https")) as speedport:
await check_wifi_args(speedport, args)
await check_args(speedport, args)


async def reconnect(args, speedport):
if not args.get("quiet"):
ip_data = await speedport.ip_data
_LOGGER.info(f"{ip_data['public_ip_v4']} / {ip_data['public_ip_v6']}")
await speedport.update_ip_data()
_LOGGER.info(f"{speedport.public_ip_v4} / {speedport.public_ip_v6}")
await speedport.reconnect()
if not args.get("quiet"):
for i in range(240):
if (ip_data := await speedport.ip_data)["onlinestatus"] == "online":
_LOGGER.info(f"{ip_data['public_ip_v4']} / {ip_data['public_ip_v6']}")
await speedport.update_ip_data()
if await speedport.online_status == "online":
_LOGGER.info(f"{speedport.public_ip_v4} / {speedport.public_ip_v6}")
break
await asyncio.sleep(0.5)
print(f"Connecting.{'.' * (i % 3)} ", end="\r", flush=True)
Expand Down
4 changes: 2 additions & 2 deletions speedport/call.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ def __getitem__(self, item):
try:
return self.__getattribute__(item)
except AttributeError:
return self._data.get(f"{self._call_type}_{item}", self._data.get(item))
return self._data.get(f"{self._call_type}_{item}", self.__getattribute__(item))

@property
def data(self):
return self._data

@property
def call_type(self):
def type(self):
return self._call_type.replace("calls","")

@property
Expand Down

0 comments on commit 75c64bb

Please sign in to comment.