summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormakefu <github@syntax-fehler.de>2019-02-09 22:39:51 +0100
committermakefu <github@syntax-fehler.de>2019-02-09 22:39:51 +0100
commit04e1c8c38ffe53175ae719121ad88534a8a662db (patch)
tree15aa8ef25ce2ae3b2714a1679f772ee93d088ff4
parenta66bd3934d9ebe858c503adf48bf50f5208e8fe2 (diff)
add mqtt publishing
-rwxr-xr-xampel/google_muell.py48
1 files changed, 30 insertions, 18 deletions
diff --git a/ampel/google_muell.py b/ampel/google_muell.py
index e7ff843..33ca138 100755
--- a/ampel/google_muell.py
+++ b/ampel/google_muell.py
@@ -1,11 +1,12 @@
#! /usr/bin/env python
""" usage: google-muell [options]
- --esp=HOST esp8266 ip [Default: 192.168.1.23]
+ --config=PATH path to google-muell config
--client-secrets=FILE Path to client secrets file (from application) [Default: licht-kalender-secrets.json]
--credential-path=PATH Path to newly generated (or existing) credentials [Default: licht-creds.json]
--sleepval=SEC seconds to sleep [Default: 900]
--lol=LOL set log level [Default: debug]
+ --default-color=R,G,B set default color as integers, separated by comma
you can create the client-secrets by creating a new google application, create
oauth2 token and download it ( url here)
@@ -42,10 +43,10 @@ colormap = {
"4": [ 230, 124, 115 ], # light red
"5": [ 255, 183, 0 ], # banana, gelber sack
"6": [ 244, 81, 30 ], # mandarin
- "7": [3, 155, 229], # pfau
+ "7": [ 3, 155, 229], # pfau
"8": [ 128,128,128 ], # grey
"9": [ 63, 81, 181 ], # heidelbeere
- "10": [0x0B,0x80,0x43 ], # basilicum
+ "10": [ 0x0B,0x80,0x43 ], # basilicum
"11": [ 231,0,0 ], # true red
}
@@ -109,30 +110,41 @@ def next_date(credentials):
def to_payload(arr):
- return {"r":arr[0],"g":arr[1],"b":arr[2]}
+ return "{:02x}{:02x}{:02x}".format(*arr)
+import paho.mqtt.publish as publish
+import paho.mqtt.client as mqtt
def main():
args = docopt(__doc__)
set_lol(args['--lol'])
client_secrets = args['--client-secrets']
sleepval = float(args["--sleepval"])
- esp = args["--esp"]
cred_path = args["--credential-path"]
+ with open(args["--config"]) as c:
+ import json
+ config = json.load(c)
creds = get_creds(client_secrets,cred_path)
while True:
- try:
- log.info("Fetching next Date")
- t = next_date(creds)
- if t:
- color = colormap[str(t.get('colorId',-1))]
- log.info(f"setting color to {color} according to colormap")
- else:
- color = colormap["-1"]
- log.info(f"setting default color {color}")
- requests.get(f"http://{esp}/color",params=to_payload(color))
-
- except Exception as e:
- log.error(f"Something went wrong while calculating the next date:{e}")
+ #try:
+ log.info("Fetching next Date")
+ t = next_date(creds)
+ if t:
+ log.debug("current entry is {}".format(t))
+ color = colormap[str(t.get('colorId',-1))]
+ log.info(f"setting color to {color} according to colormap")
+ else:
+ color = colormap["-1"] if not args['--default-color'] else map(int,args['--default-color'].split(','))
+ log.info(f"setting default color {color}")
+ topic = config['mq_topic']
+ if config.get('mq_password',None):
+ auth = {"username": config['mq_username'], "password": config['mq_password']}
+ else:
+ auth = None
+ publish.single(topic, payload=to_payload(color), hostname=config.get('mq_hostname','localhost'),
+ port=config.get('mq_port',1883), client_id="", keepalive=60,
+ auth=auth, protocol=mqtt.MQTTv311)
+ #except Exception as e:
+ # log.error(f"Something went wrong while calculating the next date:{e}")
log.info(f"sleeping for {sleepval} seconds")
sleep(sleepval)