Merge pull request #48 from s-allius/s-allius/issue46

print helpful messages on config errors
This commit is contained in:
Stefan Allius
2024-04-10 22:47:38 +02:00
committed by GitHub
2 changed files with 32 additions and 8 deletions

View File

@@ -13,6 +13,7 @@ class Config():
Get named parts of the config with get()'''
config = {}
def_config = {}
conf_schema = Schema({
'tsun': {
'enabled': Use(bool),
@@ -93,8 +94,16 @@ class Config():
# overwrite the default values, with values from
# the config.toml file
with open("config/config.toml", "rb") as f:
usr_config = tomllib.load(f)
try:
with open("config/config.toml", "rb") as f:
usr_config = tomllib.load(f)
except Exception as error:
logging.error(f'Config.read: {error}')
logging.info(
'\n To create the missing config.toml file, '
'you can rename the template config.example.toml\n'
' and customize it for your scenario.\n')
usr_config = def_config
config['tsun'] = def_config['tsun'] | usr_config['tsun']
config['solarman'] = def_config['solarman'] | \
@@ -105,6 +114,7 @@ class Config():
usr_config['inverters']
cls.config = cls.conf_schema.validate(config)
cls.def_config = cls.conf_schema.validate(def_config)
# logging.debug(f'Readed config: "{cls.config}" ')
except Exception as error:
@@ -120,3 +130,9 @@ class Config():
return cls.config.get(member, {})
else:
return cls.config
@classmethod
def is_default(cls, member: str) -> bool:
'''Check if the member is the default value'''
return cls.config.get(member) == cls.def_config.get(member)

View File

@@ -24,7 +24,7 @@ class Mqtt(metaclass=Singleton):
def __init__(self, cb_MqttIsUp):
logger_mqtt.debug('MQTT: __init__')
if cb_MqttIsUp:
self.cb_MqttIsUp = cb_MqttIsUp
self.__cb_MqttIsUp = cb_MqttIsUp
loop = asyncio.get_event_loop()
self.task = loop.create_task(self.__loop())
self.ha_restarts = 0
@@ -70,8 +70,8 @@ class Mqtt(metaclass=Singleton):
async with self.__client:
logger_mqtt.info('MQTT broker connection established')
if self.cb_MqttIsUp:
await self.cb_MqttIsUp()
if self.__cb_MqttIsUp:
await self.__cb_MqttIsUp()
# async with self.__client.messages() as messages:
await self.__client.subscribe(
@@ -83,11 +83,19 @@ class Mqtt(metaclass=Singleton):
f' {status}')
if status == 'online':
self.ha_restarts += 1
await self.cb_MqttIsUp()
await self.__cb_MqttIsUp()
except aiomqtt.MqttError:
logger_mqtt.info(f"Connection lost; Reconnecting in {interval}"
" seconds ...")
if Config.is_default('mqtt'):
logger_mqtt.info(
"MQTT is unconfigured; Check your config.toml!")
interval = 30
else:
interval = 5 # Seconds
logger_mqtt.info(
f"Connection lost; Reconnecting in {interval}"
" seconds ...")
await asyncio.sleep(interval)
except asyncio.CancelledError:
logger_mqtt.debug("MQTT task cancelled")