Refactor logging to use pp function for improved consistency

This commit is contained in:
2025-02-01 23:52:47 +08:00
parent 928f1a56d4
commit d34588467b

View File

@@ -2,9 +2,9 @@
import argparse
import asyncio
import datetime
import json
import time
import datetime
from kasa import Discover
from kasa.exceptions import KasaException
@@ -37,7 +37,7 @@ class TplinkT315:
await self.hubDev.update()
except KasaException as e:
self.hubDev = None
print(f"[TplinkT315::connect] Hub Error: {e}")
pp(f"[TplinkT315::connect] Hub Error: {e}")
return False
try:
@@ -45,7 +45,7 @@ class TplinkT315:
await self.t315Dev.update()
except KasaException as e:
self.t315Dev = None
print(f"[TplinkT315::connect] Device Error: {e}")
pp(f"[TplinkT315::connect] Device Error: {e}")
return False
return True
@@ -70,6 +70,7 @@ class TplinkT315:
if self.t315Dev is None:
return None
await self.hubDev.update()
await self.t315Dev.update()
featuresOfTempDev = self.t315Dev.features
@@ -92,7 +93,7 @@ def getSecrets(fileanme):
return secrets
except FileNotFoundError:
print(f"File not found: {fileanme}")
pp(f"File not found: {fileanme}")
return None
@@ -100,10 +101,10 @@ def getDatetime():
return datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
async def main(configs):
print(f"Start: {getDatetime()}")
pp(f"Start: {getDatetime()}")
secrect = getSecrets(configs.get("secret_filepath", DEFAULT_POLLING_INTERVAL))
if secrect is None:
print(f'Cannot read secret file({configs.get("secret_filepath", DEFAULT_POLLING_INTERVAL)})')
pp(f'Cannot read secret file({configs.get("secret_filepath", DEFAULT_POLLING_INTERVAL)})')
return
hubIp = secrect["ip"]
@@ -113,7 +114,7 @@ async def main(configs):
t315 = TplinkT315(hubIp, username, password, devName)
if await t315.connect() is False:
print(f"Failed to connect to {hubIp}")
pp(f"Failed to connect to {hubIp}")
return
time.sleep(10)
@@ -150,11 +151,11 @@ async def main(configs):
time.sleep(pollingInterval)
except KeyboardInterrupt:
print("\nUser stopped process.")
pp("\nUser stopped process.")
break
await t315.disconnect()
print(f"End: {getDatetime()}")
pp(f"End: {getDatetime()}")
if __name__ == "__main__":