print helful messages on config errors
This commit is contained in:
@@ -13,6 +13,7 @@ class Config():
|
|||||||
Get named parts of the config with get()'''
|
Get named parts of the config with get()'''
|
||||||
|
|
||||||
config = {}
|
config = {}
|
||||||
|
def_config = {}
|
||||||
conf_schema = Schema({
|
conf_schema = Schema({
|
||||||
'tsun': {
|
'tsun': {
|
||||||
'enabled': Use(bool),
|
'enabled': Use(bool),
|
||||||
@@ -93,8 +94,16 @@ class Config():
|
|||||||
|
|
||||||
# overwrite the default values, with values from
|
# overwrite the default values, with values from
|
||||||
# the config.toml file
|
# the config.toml file
|
||||||
with open("config/config.toml", "rb") as f:
|
try:
|
||||||
usr_config = tomllib.load(f)
|
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['tsun'] = def_config['tsun'] | usr_config['tsun']
|
||||||
config['solarman'] = def_config['solarman'] | \
|
config['solarman'] = def_config['solarman'] | \
|
||||||
@@ -105,6 +114,7 @@ class Config():
|
|||||||
usr_config['inverters']
|
usr_config['inverters']
|
||||||
|
|
||||||
cls.config = cls.conf_schema.validate(config)
|
cls.config = cls.conf_schema.validate(config)
|
||||||
|
cls.def_config = cls.conf_schema.validate(def_config)
|
||||||
# logging.debug(f'Readed config: "{cls.config}" ')
|
# logging.debug(f'Readed config: "{cls.config}" ')
|
||||||
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
@@ -120,3 +130,9 @@ class Config():
|
|||||||
return cls.config.get(member, {})
|
return cls.config.get(member, {})
|
||||||
else:
|
else:
|
||||||
return cls.config
|
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)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class Mqtt(metaclass=Singleton):
|
|||||||
def __init__(self, cb_MqttIsUp):
|
def __init__(self, cb_MqttIsUp):
|
||||||
logger_mqtt.debug('MQTT: __init__')
|
logger_mqtt.debug('MQTT: __init__')
|
||||||
if cb_MqttIsUp:
|
if cb_MqttIsUp:
|
||||||
self.cb_MqttIsUp = cb_MqttIsUp
|
self.__cb_MqttIsUp = cb_MqttIsUp
|
||||||
loop = asyncio.get_event_loop()
|
loop = asyncio.get_event_loop()
|
||||||
self.task = loop.create_task(self.__loop())
|
self.task = loop.create_task(self.__loop())
|
||||||
self.ha_restarts = 0
|
self.ha_restarts = 0
|
||||||
@@ -70,8 +70,8 @@ class Mqtt(metaclass=Singleton):
|
|||||||
async with self.__client:
|
async with self.__client:
|
||||||
logger_mqtt.info('MQTT broker connection established')
|
logger_mqtt.info('MQTT broker connection established')
|
||||||
|
|
||||||
if self.cb_MqttIsUp:
|
if self.__cb_MqttIsUp:
|
||||||
await self.cb_MqttIsUp()
|
await self.__cb_MqttIsUp()
|
||||||
|
|
||||||
# async with self.__client.messages() as messages:
|
# async with self.__client.messages() as messages:
|
||||||
await self.__client.subscribe(
|
await self.__client.subscribe(
|
||||||
@@ -83,11 +83,19 @@ class Mqtt(metaclass=Singleton):
|
|||||||
f' {status}')
|
f' {status}')
|
||||||
if status == 'online':
|
if status == 'online':
|
||||||
self.ha_restarts += 1
|
self.ha_restarts += 1
|
||||||
await self.cb_MqttIsUp()
|
await self.__cb_MqttIsUp()
|
||||||
|
|
||||||
except aiomqtt.MqttError:
|
except aiomqtt.MqttError:
|
||||||
logger_mqtt.info(f"Connection lost; Reconnecting in {interval}"
|
if Config.is_default('mqtt'):
|
||||||
" seconds ...")
|
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)
|
await asyncio.sleep(interval)
|
||||||
except asyncio.CancelledError:
|
except asyncio.CancelledError:
|
||||||
logger_mqtt.debug("MQTT task cancelled")
|
logger_mqtt.debug("MQTT task cancelled")
|
||||||
|
|||||||
Reference in New Issue
Block a user