Skip to content

Commit

Permalink
Allow retrieve temperature and humidity with a single connect
Browse files Browse the repository at this point in the history
As far as sensor return temperature and humidity within a single
response (notification) - `data` property was added to the client.
  • Loading branch information
gdyuldin committed Jan 30, 2020
1 parent f491a7c commit 09df306
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
10 changes: 9 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ print(client.temperature)
print(client.humidity)
```

Read temperature as humidity from a single notification

```python
data = client.data
print(data.temperature)
print(data.humidity)
```

## Available properties

* `client.temperature` – Sensor's temperature data in Celsius degrees. Updates with timeout (See below)
Expand All @@ -59,7 +67,7 @@ print(client.humidity)

Client may be initialized with additional kwargs.

* `notification_timeout` – timeout to wait for `temperature` and `humidity` requests. If sensor responds slower
* `notification_timeout` – timeout to wait for `temperature` and `humidity` requests. If sensor responds slower
then timeout data would not updated. Default value is 5 second.
* `data_request_timeout``temperature` and `humidity` are cached for this period. Default value is 15 second.

Expand Down
40 changes: 17 additions & 23 deletions lywsd02/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections
import logging
import struct
import time
Expand All @@ -15,6 +16,11 @@
UUID_DATA = 'EBE0CCC1-7A0A-4B0C-8A1A-6FF2997DA3A6' # 3 bytes READ NOTIFY


class SensorData(
collections.namedtuple('SensorDataBase', ['temperature', 'humidity'])):
__slots__ = ()


class Lywsd02Client:
UNITS = {
b'\x01': 'F',
Expand All @@ -32,31 +38,21 @@ def __init__(self, mac, notification_timeout=5.0, data_request_timeout=15.0):
self._request_timeout = data_request_timeout
self._handles = {}
self._tz_offset = None
self._temperature = None
self._humidity = None
self._data = SensorData(None, None)
self._last_request = None

@staticmethod
def parse_humidity(value):
return int(value)

@property
def temperature(self):
self._get_sensor_data()
return self._temperature

@temperature.setter
def temperature(self, value):
self._temperature = struct.unpack('h', value)[0] / 100
return self.data.temperature

@property
def humidity(self):
self._get_sensor_data()
return self._humidity
return self.data.humidity

@humidity.setter
def humidity(self, value):
self._humidity = value
@property
def data(self):
self._get_sensor_data()
return self._data

@property
@with_connect
Expand Down Expand Up @@ -149,16 +145,14 @@ def _subscribe(self, uuid, callback):
self._handles[ch.getHandle()] = callback
desc = ch.getDescriptors(forUUID=0x2902)[0]

desc.write(0x01.to_bytes(2, byteorder="little"), withResponse=True)
desc.write(bytes([0, 1]), withResponse=True)

def _process_sensor_data(self, data):
temp_bytes = data[:2]
humid_bytes = data[2]

self.temperature = temp_bytes
self.humidity = humid_bytes
temperature, humidity = struct.unpack_from('HB', data)
temperature /= 100

self._last_request = datetime.now().timestamp()
self._data = SensorData(temperature=temperature, humidity=humidity)

def _process_history_data(self, data):
# TODO: Process history data
Expand Down

0 comments on commit 09df306

Please sign in to comment.