Skip to content

Commit

Permalink
First attempt of refactoring to Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
bricebou committed Nov 12, 2020
1 parent f79ebe0 commit 005b6ee
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
52 changes: 26 additions & 26 deletions bin/user/netatmo.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
rain, and wind.
"""

from __future__ import with_statement
import Queue

import queue
import json
import re
import socket
import syslog
import threading
import time
from urllib import urlencode
import urllib2
from urllib.parse import urlencode
import urllib.request, urllib.error, urllib.parse

import weewx.drivers
import weewx.engine
Expand Down Expand Up @@ -98,16 +98,16 @@ def default_stanza(self):

def prompt_for_settings(self):
settings = dict()
print "Specify the mode for obtaining data, either 'cloud' or 'sniff'"
print("Specify the mode for obtaining data, either 'cloud' or 'sniff'")
settings['mode'] = self._prompt('mode', 'cloud', ['cloud', 'sniff'])
if settings['mode'] == 'cloud':
print "Specify the username for netatmo.com"
print("Specify the username for netatmo.com")
self._prompt('username')
print "Specify the password for netatmo.com"
print("Specify the password for netatmo.com")
self._prompt('password')
print "Specify the client_id from dev.netatmo.com"
print("Specify the client_id from dev.netatmo.com")
self._prompt('client_id')
print "Specify the client_secret from dev.netatmo.com"
print("Specify the client_secret from dev.netatmo.com")
self._prompt('client_secret')
return settings

Expand Down Expand Up @@ -192,7 +192,7 @@ def genLoopPackets(self):
logdbg('packet: %s' % pkt)
if pkt:
yield pkt
except Queue.Empty:
except queue.Empty:
pass

def data_to_packet(self, data):
Expand All @@ -201,7 +201,7 @@ def data_to_packet(self, data):
packet['dateTime'] = int(time.time() + 0.5)
packet['usUnits'] = weewx.METRIC
for n in self.sensor_map:
label = self._find_match(self.sensor_map[n], data.keys())
label = self._find_match(self.sensor_map[n], list(data.keys()))
if label:
packet[n] = data.get(label)
return packet
Expand Down Expand Up @@ -231,7 +231,7 @@ def _part_match(pattern, value):


class Collector(object):
queue = Queue.Queue()
queue = queue.Queue()

def startup(self):
pass
Expand Down Expand Up @@ -318,13 +318,13 @@ def collect_data(self):
try:
CloudClient.get_data(self._sd, self._device_id)
break
except (socket.error, socket.timeout, urllib2.HTTPError, urllib2.URLError), e:
except (socket.error, socket.timeout, urllib.error.HTTPError, urllib.error.URLError) as e:
logerr("failed attempt %s of %s to get data: %s" %
(tries + 1, self._max_tries, e))
logdbg("waiting %s seconds before retry" %
self._retry_wait)
time.sleep(self._retry_wait)
except Exception, e:
except Exception as e:
logerr("exception in netatmo-client: %s" % e)
weeutil.weeutil.log_traceback('*** ', syslog.LOG_DEBUG)
else:
Expand Down Expand Up @@ -378,7 +378,7 @@ def extract_data(x, units_dict):
try:
func = CloudClient.CONVERSIONS.get(n)
data[n] = getattr(CloudClient, func)(data[n], units_dict)
except ValueError, e:
except ValueError as e:
logerr("unit conversion failed for %s: %s" % (data[n], e))
data[n] = None
return data
Expand Down Expand Up @@ -522,8 +522,8 @@ def post_request(url, params):
headers = {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"}
logdbg("url: %s data: %s hdr: %s" % (url, params, headers))
req = urllib2.Request(url=url, data=params, headers=headers)
resp = urllib2.urlopen(req).read(65535)
req = urllib.request.Request(url=url, data=params, headers=headers)
resp = urllib.request.urlopen(req).read(65535)
resp_obj = json.loads(resp)
logdbg("resp_obj: %s" % resp_obj)
return resp_obj
Expand Down Expand Up @@ -623,7 +623,7 @@ def run_sniff_driver():
import weeutil.weeutil
driver = NetatmoDriver({'mode': 'sniff'})
for pkt in driver.genLoopPackets():
print weeutil.weeutil.timestamp_to_string(pkt['dateTime']), pkt
print(weeutil.weeutil.timestamp_to_string(pkt['dateTime']), pkt)

def run_cloud_driver(username, password, c_id, c_secret):
import weeutil.weeutil
Expand All @@ -633,7 +633,7 @@ def run_cloud_driver(username, password, c_id, c_secret):
username=username, password=password,
client_id=c_id, client_secret=c_secret)
for pkt in driver.genLoopPackets():
print weeutil.weeutil.timestamp_to_string(pkt['dateTime']), pkt
print(weeutil.weeutil.timestamp_to_string(pkt['dateTime']), pkt)
except KeyboardInterrupt:
driver.closePort()

Expand All @@ -646,28 +646,28 @@ def get_json_data(username, password, c_id, c_secret):
auth = CloudClient.ClientAuth(username, password, c_id, c_secret)
params = {'access_token': auth.access_token, 'app_type': 'app_station'}
reply = CloudClient.post_request(CloudClient.DATA_URL, params)
print json.dumps(reply, sort_keys=True, indent=2)
print(json.dumps(reply, sort_keys=True, indent=2))

def test_parse(filename):
lines = []
with open(filename, "r") as f:
while f:
lines.append(f.readline())
print PacketSniffer.Packet.lines2packets(''.join(lines))
print(PacketSniffer.Packet.lines2packets(''.join(lines)))

def ppv(label, x, level=0):
"""pretty-print a variable, recursing if it is a dict"""
indent = ' '
if type(x) is dict:
print "%s%s" % (indent * level, label)
print("%s%s" % (indent * level, label))
for n in x:
ppv(n, x[n], level=level+1)
elif type(x) is list:
print "%s[" % (indent * level)
print("%s[" % (indent * level))
for i, y in enumerate(x):
ppv("%s %s" % (label, i), y, level=level+1)
print "%s]" % (indent * level)
print("%s]" % (indent * level))
else:
print "%s%s=%s" % (indent * level, label, x)
print("%s%s=%s" % (indent * level, label, x))

main()
main()
11 changes: 9 additions & 2 deletions readme
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ obtained via the dev.netatmo.com web site. Using these 4 things, the driver
automatically obtains and updates the tokens needed to get data from the
server.

[bricebou]
This fork aims to be compatible with weewx 4.* and has been "rewritten" in Python 3 thanks to the `2to3` tool.
[/bricebou]


===============================================================================
Installation instructions:

1) download the driver:

wget -O weewx-netatmo.zip https://github.com/matthewwall/weewx-netatmo/archive/master.zip

wget -O weewx-netatmo.zip https://github.com/bricebou/weewx-netatmo/archive/master.zip
2) install the driver:

sudo wee_extension --install weewx-netatmo.zip
Expand All @@ -36,6 +39,10 @@ sudo wee_extension --install weewx-netatmo.zip

sudo wee_config --reconfigure

[bricebou]
It seems that the configuration isn't written so you have to manually edit the ``/etc/weewx/weewx.conf` file.
[/bricebou]

4) start weewx:

sudo /etc/init.d/weewx start

0 comments on commit 005b6ee

Please sign in to comment.