61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
import logging
|
|
import tomllib
|
|
from argparse import ArgumentParser
|
|
from pathlib import Path
|
|
|
|
from hasspy.mqtt import HassClient, HassSystemClient, HassUserClient
|
|
|
|
|
|
def main() -> None:
|
|
parser = ArgumentParser(
|
|
prog="HassPy",
|
|
description="Home Assistant MQTT client",
|
|
)
|
|
parser.add_argument("-c", "--config", help="Path to configuration file")
|
|
parser.add_argument(
|
|
"-v", "--verbose", help="Enable verbose logging", action="count", default=0
|
|
)
|
|
parser.add_argument("-u", "--user", help="User mode client", action="store_true")
|
|
args = parser.parse_args()
|
|
|
|
if args.config:
|
|
config_file = Path(args.config)
|
|
else:
|
|
config_file = next(
|
|
(
|
|
x
|
|
for x in (Path("/etc/hasspy.toml"), Path("/etc/hasspy/config.toml"))
|
|
if x.exists()
|
|
),
|
|
Path("config.toml"),
|
|
)
|
|
|
|
if not config_file or not config_file.exists():
|
|
raise FileNotFoundError("No configuration file found")
|
|
|
|
with open(config_file, "rb") as file:
|
|
config = tomllib.load(file)
|
|
|
|
if isinstance(config.get("log_level"), str):
|
|
config["log_level"] = getattr(logging, config["log_level"])
|
|
logging.basicConfig(
|
|
level=config.get("log_level", logging.INFO) - (args.verbose * 10)
|
|
)
|
|
|
|
ha: HassClient
|
|
if not args.user:
|
|
ha = HassSystemClient(
|
|
"orchomenos",
|
|
config=config,
|
|
)
|
|
else:
|
|
ha = HassUserClient(
|
|
"orchomenos",
|
|
config=config,
|
|
)
|
|
|
|
ha.loop_forever()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|