From 4e9e093bcb4e5fd2a5051c2626d0925019e1b5f1 Mon Sep 17 00:00:00 2001 From: Keptenkurk Date: Mon, 28 Mar 2016 22:20:46 +0200 Subject: [PATCH] Added updating to Domoticz Wen configured in ini file program now updates virtual BWR102 scale sensor to enable graphing. Emailing of weight data is now optional by config in ini. --- BS440.ini | 21 ++++++++++++++++++--- BS440.py | 10 +++++++--- BS440domoticz.py | 38 ++++++++++++++++++++++++++++++++++++++ BS440mail.py | 6 +++--- README.md | 6 ++++-- 5 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 BS440domoticz.py diff --git a/BS440.ini b/BS440.ini index 3aa7d1e..fa3b4b7 100644 --- a/BS440.ini +++ b/BS440.ini @@ -5,13 +5,16 @@ # The persons secion lists all the scale users # The list maybe extended to 8 persons +# Domoticz index is only used if domoticz IP is set [Person1] username: John useremail: johndoe@gmail.com +domoticz_idx: 55 [Person2] username: Jill useremail: jilldoe@gmail.com +domoticz_idx: 56 # Scale properties [Scale] @@ -19,10 +22,22 @@ ble_address: aa:bb:cc:11:22:33 device_name: 0202B6332211CCBBAA # Program behaviour -# Sender email/pwd: gmail account info # Loglevel: debug | info | critical | error [Program] -sender_email: yourmail@gmail.com -sender_pwd: yourpassword loglevel: debug logfile: BS440.log + +# Sender email/pwd: gmail account info +# Uncomment [Email] to have emails sent +[Email] +sender_email: yourmail@gmail.com +sender_pwd: yourpassword + +# Uncomment [Domoticz] to enable updating +# DomoticzIP: IP adres of Domoticz server +# DomoticzUser: Username of Domoticz server +# DomoticzPwd: Password of Domoticz server +#[Domoticz] +#domoticz_url: 192.168.0.10:8080 +#domoticz_user: admin +#domoticz_pwd: admin diff --git a/BS440.py b/BS440.py index 4d27678..8763c5b 100644 --- a/BS440.py +++ b/BS440.py @@ -8,6 +8,7 @@ from binascii import hexlify from BS440decode import * from BS440mail import * +from BS440domoticz import * def processIndication(handle, values): @@ -146,8 +147,11 @@ def init_ble_mode(): time.sleep(30) device.disconnect() log.info('Done receiving data from scale') - # mail data if all received well + # process data if all received well if persondata and weightdata and bodydata: - BS440mail(config, persondata, weightdata, bodydata) + if config.has_section('Email'): + BS440mail(config, persondata, weightdata, bodydata) + if config.has_section('Domoticz'): + UpdateDomoticz(config, weightdata) else: - log.error('Incomplete data received. Unable to send mail') + log.error('Unreliable data received. Unable to process') diff --git a/BS440domoticz.py b/BS440domoticz.py new file mode 100644 index 0000000..2e91b2b --- /dev/null +++ b/BS440domoticz.py @@ -0,0 +1,38 @@ +''' +BS440domoticz.py +Update weight value to Domoticz home automation system +''' +import urllib2 +import base64 +import logging + + +def UpdateDomoticz(config, weightdata): + log = logging.getLogger(__name__) + # sort to have list starting with most recent weight + wds = sorted(weightdata, key=lambda k: k['timestamp'], reverse=True) + domoticzurl = config.get('Domoticz', 'domoticz_url') + domoticzuser = config.get('Domoticz', 'domoticz_user') + domoticzpwd = config.get('Domoticz', 'domoticz_pwd') + personsection = 'Person' + str(wds[0]['person']) + if config.has_section(personsection): + domoticzidx = config.get(personsection, 'domoticz_idx') + scaleuser = config.get(personsection, 'username') + else: + log.error('Unable to update Domoticz: No details found in ini file ' + 'for person %d' % (wds[0]['person'])) + return + try: + log.info('Updating Domoticz for user %s at index %s with weight %s' % ( + scaleuser, domoticzidx, wds[0]['weight'])) + url = 'http://%s/json.htm?type=command¶m=udevice&hid=2&' \ + 'did=%s&dunit=4&dtype=93&dsubtype=1&nvalue=0&svalue=%s' % ( + domoticzurl, domoticzidx, wds[0]['weight']) + req = urllib2.Request(url) + base64string = base64.encodestring('%s:%s' % ( + domoticzuser, domoticzpwd)).replace('\n', '') + req.add_header('Authorization', 'Basic %s' % base64string) + resp = urllib2.urlopen(req) + log.info('Domoticz succesfully updated') + except: + log.error('Unable to update Domoticz: Error sending data.') diff --git a/BS440mail.py b/BS440mail.py index e755199..a55baf8 100644 --- a/BS440mail.py +++ b/BS440mail.py @@ -53,9 +53,9 @@ def rowdata(header, dataset, property, bib): def BS440mail(config, persondata, weightdata, bodydata): log = logging.getLogger(__name__) - FromAddr = config.get('Program', 'sender_email') - Password = config.get('Program', 'sender_pwd') - CcAddr = [config.get('Program', 'sender_email')] + FromAddr = config.get('Email', 'sender_email') + Password = config.get('Email', 'sender_pwd') + CcAddr = [config.get('Email', 'sender_email')] personsection = 'Person' + str(persondata[0]['person']) if config.has_section(personsection): diff --git a/README.md b/README.md index 448c7c5..81fd902 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,10 @@ https://keptenkurk.wordpress.com/2016/02/07/connecting-the-medisana-bs440-blueto # Description In it's current state this program listens for data from a BS440 -bluetooth scale. Once connected, data is read from the scale and -the last 3 stored sets of data will be mailed to the user. +bluetooth scale. Once connected, data is read from the scale. Depending +on the config in ini the program will +* mail the last 3 stored sets of data to the user +* update a virtual sensor in Domoticz home automation system # ini file Before using this program change the settings in the ini file