Skip to content

Commit

Permalink
refactor: library_test.py params via JSON config file (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
chemelli74 authored Jan 25, 2024
1 parent 032a3ea commit 0f46014
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,6 @@ dmypy.json

# Cython debug symbols
cython_debug/

# Optional config file for library_test.py script
library_test.json
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"type": "python",
"request": "launch",
"program": "library_test.py",
"args": ["-bp", "<your pin>", "-vp", "<your pin"],
"args": ["-cf", "library_test.json"],
"console": "integratedTerminal",
"justMyCode": true
}
Expand Down
60 changes: 41 additions & 19 deletions library_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Test script for aiocomelit library."""
import argparse
import ast
import asyncio
import json
import logging
import os
from argparse import ArgumentParser, Namespace

from colorlog import ColoredFormatter

Expand Down Expand Up @@ -30,9 +31,9 @@
INDEX = 0


def get_arguments() -> tuple[argparse.ArgumentParser, argparse.Namespace]:
def get_arguments() -> tuple[ArgumentParser, Namespace]:
"""Get parsed passed in arguments."""
parser = argparse.ArgumentParser(description="aiovodafone library test")
parser = ArgumentParser(description="aiocomelit library test")
parser.add_argument(
"--bridge",
"-b",
Expand Down Expand Up @@ -78,11 +79,22 @@ def get_arguments() -> tuple[argparse.ArgumentParser, argparse.Namespace]:
parser.add_argument(
"--test",
"-t",
type=ast.literal_eval,
default=True,
type=str,
default="True",
help="Execute test actions",
),
parser.add_argument(
"--configfile",
"-cf",
type=str,
help="Load options from JSON config file. Command line options override those in the file.",
)
arguments = parser.parse_args()
if arguments.configfile:
# Re-parse the command line, taking the options in the optional JSON file as a basis
if os.path.exists(arguments.configfile):
with open(arguments.configfile) as f:
arguments = parser.parse_args(namespace=Namespace(**json.load(f)))

return parser, arguments

Expand All @@ -107,13 +119,8 @@ async def execute_alarm_test(api: ComelitVedoApi, area: ComelitVedoAreaObject) -
print("Status after: ", await api.get_area_status(area))


async def main() -> None:
"""Run main."""
parser, args = get_arguments()

print("-" * 20)
print(f"aiocomelit version: {__version__}")
print("-" * 20)
async def bridge_test(args: Namespace) -> None:
"""Test code for Comelit Serial Bridge."""
bridge_api = ComeliteSerialBridgeApi(args.bridge, args.bridge_port, args.bridge_pin)
logged = False
try:
Expand All @@ -130,7 +137,7 @@ async def main() -> None:
devices = await bridge_api.get_all_devices()
print("Devices:", devices)
print("-" * 20)
if args.test:
if args.test == "True":
for device in devices[LIGHT].values():
if device.index == INDEX:
await execute_device_test(bridge_api, device, LIGHT)
Expand All @@ -152,11 +159,9 @@ async def main() -> None:
await bridge_api.logout()
await bridge_api.close()

# VEDO system mandatorily requires a pin
if not args.vedo_pin:
print("VEDO, missing PIN. Skipping")
return

async def vedo_test(args: Namespace) -> None:
"""Test code for Comelit VEDO system."""
vedo_api = ComelitVedoApi(args.vedo, args.vedo_port, args.vedo_pin)
logged = False
try:
Expand All @@ -179,14 +184,31 @@ async def main() -> None:
for zone in alarm_data[ALARM_ZONES]:
print(alarm_data[ALARM_ZONES][zone])
print("-" * 20)
if args.test:
if args.test == "True":
await execute_alarm_test(vedo_api, alarm_data[ALARM_AREAS][INDEX])
print("-" * 20)
print("Logout & close session")
await vedo_api.logout()
await vedo_api.close()


async def main() -> None:
"""Run main."""
parser, args = get_arguments()

print("-" * 20)
print(f"aiocomelit version: {__version__}")
print("-" * 20)
await bridge_test(args)

# VEDO system mandatorily requires a pin
if not args.vedo_pin:
print("Comelit VEDO System: missing PIN. Skipping tests")
parser.print_help()
return
await vedo_test(args)


def set_logging() -> None:
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("asyncio").setLevel(logging.INFO)
Expand Down

0 comments on commit 0f46014

Please # to comment.