viri-leds-dmx-sacn/conf.py

63 lines
2.1 KiB
Python

try:
# python 3.11
from tomllib import loads as toml_load
except ImportError:
from rtoml import load as toml_load
import logging
from sys import exit
LOG = logging.getLogger('Config')
class ConfigWrapper:
# simple class to wrap a dict into a object
def __init__(self, **kwargs):
for k, v in kwargs.items():
setattr(self, k, v)
def load_and_validate_config(path):
try:
with open(path, 'r') as cf:
config = toml_load(cf.read())
except Exception as e:
LOG.error(f'{path} is no valid toml configuration file')
exit(1)
if not config.get('mqtt', {}).get('host'):
LOG.error(
f'configuration option "mqtt" "host" is missing in config, but required to exist'
)
exit(1)
conf = ConfigWrapper(
mqtt=ConfigWrapper(
host=config['mqtt']['host'],
user=config['mqtt'].get('user'),
password=config['mqtt'].get('password'),
topic=config['mqtt'].get('topic', '/voc/alert'),
),
sacn=ConfigWrapper(
multicast=bool(config.get('sacn', {}).get('multicast', False) is True),
target=config.get('sacn', {}).get('target', '127.0.0.1'),
universe=int(config.get('sacn', {}).get('universe', 1)),
),
alerts=ConfigWrapper(
brightness=max(int(config.get('alerts', {}).get('brightness', 255)), 10),
filters=sorted(config.get('alerts', {}).get('filters', set())),
),
rainbow=ConfigWrapper(
enable=bool(config.get('rainbow', {}).get('enable', True) is True),
intensity=max(int(config.get('rainbow', {}).get('intensity', 100)), 10),
brightness=max(int(config.get('rainbow', {}).get('brightness', 150)), 10),
speed=int(config.get('rainbow', {}).get('speed', 25)),
),
lights=config.get('lights', {}),
)
if conf.alerts.brightness < conf.rainbow.brightness:
LOG.error('alerts brightness must be equal or above rainbow brightness')
exit(1)
return conf