Skip to content

Commit

Permalink
feat: full sensor data, restart connection/router
Browse files Browse the repository at this point in the history
feat: full sensor data, restart connection/router
  • Loading branch information
chemelli74 authored Sep 7, 2023
2 parents e17d579 + 3f40512 commit db68478
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
9 changes: 7 additions & 2 deletions library_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ async def main() -> None:
print("Logged:", logged)

print("-" * 20)
devices = await api.get_all_devices()
devices = await api.get_devices_data()
print("Devices:", devices)
print("-" * 20)
data = await api.get_user_data()
data = await api.get_sensor_data()
print("Data:", data)
print("-" * 20)
print("Serial #:", data["sys_serial_number"])
print("Firmware:", data["sys_firmware_version"])
print("Hardware:", data["sys_hardware_version"])
print("Uptime :", await api.convert_uptime(data["sys_uptime"]))
print("-" * 20)
print("Logout & close session")
await api.logout()
await api.close()
Expand Down
41 changes: 36 additions & 5 deletions src/aiovodafone/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
import urllib.parse
from dataclasses import dataclass
from datetime import datetime
from datetime import datetime, timedelta
from http.cookies import SimpleCookie
from typing import Any

Expand Down Expand Up @@ -50,6 +50,7 @@ def __init__(self, host: str, username: str, password: str) -> None:
self.csrf_token: str = ""
self.encryption_key: str = ""
self._unique_id: str | None = None
self._overview: dict[str, Any] = {}
self._devices: dict[str, VodafoneStationDevice] = {}

def _base_url(self) -> str:
Expand Down Expand Up @@ -209,19 +210,28 @@ async def _get_page_result(self, page: str) -> dict[Any, Any]:
_LOGGER.debug("GET reply %s: %s", page, reply_json)
return await self._list_2_dict(reply_json)

async def get_user_data(self) -> dict[Any, Any]:
async def get_sensor_data(self) -> dict[Any, Any]:
"""Load user_data page information."""
_LOGGER.debug("Getting sensor data for host %s", self.host)

reply_dict = await self._get_page_result("/data/user_data.json")
return reply_dict
reply_dict_1 = await self._get_page_result("/data/user_data.json")
reply_dict_2 = await self._get_page_result("/data/statussupportstatus.json")
reply_dict_3 = await self._get_page_result("/data/statussupportrestart.json")

async def get_all_devices(self) -> dict[str, VodafoneStationDevice]:
return reply_dict_1 | reply_dict_2 | reply_dict_3 | self._overview

async def get_devices_data(self) -> dict[str, VodafoneStationDevice]:
"""Get all connected devices."""

_LOGGER.debug("Getting all devices for host %s", self.host)
return_dict = await self._get_page_result("/data/overview.json")

# Cleanup sensor data from devices in order to be merged later
self._overview.update(return_dict)
for info in ["wifi_user", "wifi_guest", "ethernet"]:
if info in self._overview:
self._overview.pop(info)

if (
"wifi_user" not in return_dict
and "wifi_guest" not in return_dict
Expand Down Expand Up @@ -264,6 +274,14 @@ async def get_all_devices(self) -> dict[str, VodafoneStationDevice]:

return self._devices

async def convert_uptime(self, uptime: str) -> datetime:
"""Convert router uptime to last boot datetime."""
d = int(uptime.split(":")[0])
h = int(uptime.split(":")[1])
m = int(uptime.split(":")[2])

return datetime.utcnow() - timedelta(days=d, hours=h, minutes=m)

async def login(self) -> bool:
"""Router login."""
_LOGGER.debug("Logging into %s", self.host)
Expand Down Expand Up @@ -294,6 +312,19 @@ async def login(self) -> bool:

return logged

async def restart_connection(self) -> None:
"""Internet Connection restart."""
payload = {"fiber_reconnect": "1"}
await self._post_page_result("/data/statussupportrestart.json", payload)

async def restart_router(self) -> None:
"""Router restart."""
payload = {"restart_device": "1"}
try:
await self._post_page_result("/data/statussupportrestart.json", payload)
except asyncio.exceptions.TimeoutError:
pass

async def logout(self) -> None:
"""Router logout."""
self.session.cookie_jar.clear()
Expand Down

0 comments on commit db68478

Please sign in to comment.