Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print helpful messages on config errors #48

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions app/src/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Config():
Get named parts of the config with get()'''

config = {}
def_config = {}
conf_schema = Schema({
'tsun': {
'enabled': Use(bool),
Expand Down Expand Up @@ -93,8 +94,16 @@ def read(cls) -> None:

# 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'] | \
Expand All @@ -105,6 +114,7 @@ def read(cls) -> None:
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:
Expand All @@ -120,3 +130,9 @@ def get(cls, member: str = None):
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)
20 changes: 14 additions & 6 deletions app/src/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -70,8 +70,8 @@ async def __loop(self) -> None:
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(
Expand All @@ -83,11 +83,19 @@ async def __loop(self) -> None:
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")
Expand Down