Add systemd journal logging

This commit is contained in:
Nicolas Duhamel 2021-03-13 10:30:47 +01:00
parent 40d2858fe9
commit 3844835077
4 changed files with 43 additions and 32 deletions

View File

@ -1,6 +1,6 @@
[metadata]
name = citadel.devices
version = 0.0.1
version = 0.0.2
[options]
package_dir =

View File

@ -1,6 +1,10 @@
import setuptools
setuptools.setup(
'typer==0.3.2'
'citadel.mqtt @ git+https://git.quimbo.fr/citadel/mqtt.git@master#egg=citadel.mqtt',
install_requires = [
'citadel.mqtt @ git+https://git.quimbo.fr/citadel/mqtt.git@master#egg=citadel.mqtt',
'systemd-python==234',
'typer==0.3.2'
]
)

View File

@ -10,11 +10,11 @@ class Light:
self._id = _id
self.STATE_TOPIC = kwargs.get('STATE_TOPIC', 'stat/{id}/POWER1')
self.STATE_TOPIC = kwargs.get('STATE_TOPIC', 'stat/tasmota/light/{id}/POWER')
self.STATE_ON_PAYLOAD = kwargs.get('STATE_ON_PAYLOAD', 'ON')
self.STATE_OFF_PAYLOAD = kwargs.get('STATE_OFF_PAYLOAD', 'OFF')
self.CMND_TOPIC = kwargs.get('CMND_TOPIC', 'cmnd/{id}/POWER1')
self.CMND_TOPIC = kwargs.get('CMND_TOPIC', 'cmnd/tasmota/light/{id}/POWER')
self.CMND_ON_PAYLOAD = kwargs.get('CMND_ON_PAYLOAD', 'ON')
self.CMND_OFF_PAYLOAD = kwargs.get('CMND_OFF_PAYLOAD', 'OFF')
@ -63,7 +63,7 @@ class LightsGroup:
l.setChangeCallback(self.onLightStateChange)
self._client = client
self._client.subscribe_callback('cmnd/%s/POWER' % self._id, self.cmndPower)
self._client.subscribe_callback('cmnd/devices/light/%s/POWER' % self._id, self.cmndPower)
self._previous_state = all([l.state for l in self._lights])
@ -74,14 +74,14 @@ class LightsGroup:
elif value == 'OFF':
self.off()
elif value == '':
self._client.publish('stat/%s/POWER' % self.id, 'ON' if self.state else 'OFF' )
self._client.publish('stat/devices/light/%s/POWER' % self.id, 'ON' if self.state else 'OFF' )
else:
raise Exception("Unexpected value %s for stat topic" % value)
def onLightStateChange(self, light):
state = all([l.state for l in self._lights])
if state != self._previous_state:
self._client.publish('stat/%s/POWER' % self.id, 'ON' if state else 'OFF')
self._client.publish('stat/devices/light/%s/POWER' % self.id, 'ON' if state else 'OFF')
self._previous_state = state
def on(self):

View File

@ -6,47 +6,54 @@ from citadel.mqtt import Client
from . import Light, LightsGroup
import systemd.journal
def main(\
mqtt_user: str = typer.Option(... , envvar="DEVICES_MQTT_USER"),\
mqtt_pwd: str = typer.Option(... , envvar="DEVICES_MQTT_PWD"),\
mqtt_host: str = typer.Option(... , envvar="DEVICES_MQTT_HOST"),\
mqtt_port: int = typer.Option(... , envvar="DEVICES_MQTT_PORT")\
mqtt_port: int = typer.Option(... , envvar="DEVICES_MQTT_PORT"),\
is_systemd: bool = typer.Option(False, help="Is running as systemd unit", envvar="LAUNCHED_BY_SYSTEMD")\
):
client = Client()
client.setup(mqtt_host, mqtt_port, mqtt_user, mqtt_pwd)
lights1 = LightsGroup('KitchenLed',
client, [Light('KitchenCeiling1', client),
Light('KitchenCeiling2', client)])
client, [Light('KitchenCeiling1', client,
STATE_TOPIC='stat/tasmota/light/{id}/POWER1',
CMND_TOPIC='cmnd/tasmota/light/{id}/POWER1'),
Light('KitchenCeiling2', client,
STATE_TOPIC='stat/tasmota/light/{id}/POWER1',
CMND_TOPIC='cmnd/tasmota/light/{id}/POWER1')])
lights2 = LightsGroup('KitchenCelling',
client, [
Light('KitchenCeiling1', client,
STATE_TOPIC='stat/{id}/POWER2',
CMND_TOPIC='cmnd/{id}/POWER2'),
STATE_TOPIC='stat/tasmota/light/{id}/POWER2',
CMND_TOPIC='cmnd/tasmota/light/{id}/POWER2'),
Light('KitchenCeiling2', client,
STATE_TOPIC='stat/{id}/POWER2',
CMND_TOPIC='cmnd/{id}/POWER2')])
STATE_TOPIC='stat/tasmota/light/{id}/POWER2',
CMND_TOPIC='cmnd/tasmota/light/{id}/POWER2')])
client.loop_forever()
if is_systemd:
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
logger.addHandler(systemd.journal.JournalHandler())
else:
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
if __name__ == "__main__":
# create logger
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler(ch)
typer.run(main)