Skip to content

Commit

Permalink
Merge pull request #15 from 53RT/master
Browse files Browse the repository at this point in the history
Decoding of Historic Data
  • Loading branch information
h4 authored Apr 8, 2020
2 parents 1b0449f + 43ec145 commit 3577184
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ with client.connect():
* `client.units` – Current temperature units displayed on screen. Returns `'C'` for Celsius and `'F'` for Fahrenheit
* `client.time` – Current time and timezone offset. Returns as tuple of `datetime.datetime` and `int`
* `client.battery` – Sensor's battery level in percent (0 to 100).
* `client.historic_data` – Ordered Dictionary of hourly minimum and maximum including timestamp for temperature and humidity

## Available setters

Expand Down
17 changes: 11 additions & 6 deletions lywsd02/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,20 @@ def _subscribe(self, uuid, callback):
desc.write(0x01.to_bytes(2, byteorder="little"), withResponse=True)

def _process_sensor_data(self, data):
temperature, humidity = struct.unpack_from('HB', data)
temperature, humidity = struct.unpack_from('hB', data)
temperature /= 100

self._data = SensorData(temperature=temperature, humidity=humidity)

def _process_history_data(self, data):
(idx, ts, max_temp, max_hum, min_temp, _, min_hum) = struct.unpack_from(
'IIHBBBB', data)

# TODO unpacking with IIhBhB in one step doesn't work
(idx, ts) = struct.unpack_from('II', data[0:8])
(max_temp, max_hum) = struct.unpack_from('hB', data[8:11])
(min_temp, min_hum) = struct.unpack_from('hB', data[11:14])

ts = datetime.fromtimestamp(ts)
max_temp /= 10
min_temp /= 10
self._history_data[idx] = [ts, max_temp, max_hum, min_temp, min_hum]
min_temp /= 100
max_temp /= 100

self._history_data[idx] = [ts, min_temp, min_hum, max_temp, max_hum]

0 comments on commit 3577184

Please sign in to comment.