Skip to content

Commit

Permalink
Merge pull request #48 from chemelli74/chemelli74-rework-uptime
Browse files Browse the repository at this point in the history
fix: add timezone to convert_uptime function
  • Loading branch information
chemelli74 authored Oct 17, 2023
2 parents 99743c5 + f2abc82 commit 2a6c731
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion library_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async def main() -> None:
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("Uptime :", api.convert_uptime(data["sys_uptime"]))
print("-" * 20)
print("Logout & close session")
await api.logout()
Expand Down
16 changes: 10 additions & 6 deletions src/aiovodafone/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import urllib.parse
from abc import ABC, abstractmethod
from dataclasses import dataclass
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from http.cookies import SimpleCookie
from typing import Any

Expand Down Expand Up @@ -99,7 +99,7 @@ async def _get_page_result(self, page: str) -> aiohttp.ClientResponse:
)

@abstractmethod
async def convert_uptime(self, uptime: str) -> datetime:
def convert_uptime(self, uptime: str) -> datetime:
pass

async def close(self) -> None:
Expand Down Expand Up @@ -238,8 +238,10 @@ async def get_sensor_data(self) -> dict[Any, Any]:
data["sys_uptime"] = status_json["data"]["uptime"]
return data

async def convert_uptime(self, uptime: str) -> datetime:
return datetime.utcnow() - timedelta(seconds=int(uptime))
def convert_uptime(self, uptime: str) -> datetime:
return datetime.utcnow().replace(tzinfo=timezone.utc) - timedelta(
seconds=int(uptime)
)

async def logout(self) -> None:
# Logout
Expand Down Expand Up @@ -446,13 +448,15 @@ async def get_devices_data(self) -> dict[str, VodafoneStationDevice]:

return self._devices

async def convert_uptime(self, uptime: str) -> datetime:
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)
return datetime.utcnow().replace(tzinfo=timezone.utc) - timedelta(
days=d, hours=h, minutes=m
)

async def login(self) -> bool:
"""Router login."""
Expand Down

0 comments on commit 2a6c731

Please sign in to comment.