Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Experimental] Use ruuvi tag experimental bleson BLE interface (Requires python build) #4

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# ruuvi_hass
RuuviTag sensor for hass.io

Copy ruuvi-hass.py to <config folder>/custom_components/sensor/ (e.g. /home/homeassistant/.homeassistant/custom_components/sensor/ruuvi-hass.py)
Copy the contents of `custom_components` in this repo to `<config folder>/custom_components` (e.g. `/home/homeassistant/.homeassistant/custom_components/`).

The configuration.yaml has to be edited like this
```
Expand All @@ -14,3 +14,21 @@ sensor:
mac: 'MA:CA:DD:RE:SS:01'
name: 'bathroom'
```

If you need you can pass the ble adapter as well.
Run `hciconfig` to see which ones are available on your machine / env
Defaults to ruuvi ble_communicator default (at this point is `hci0`)

```
- platform: ruuvi-hass
mac: 'MA:CA:DD:RE:SS:01'
name: 'balcony'
adapter: 'hci0'
```


## Contributors
[JonasR-](https://github.com/JonasR-) (author)
[PieterGit](https://github.com/PieterGit)
[salleq](https://github.com/salleq)
[smaisidoro](https://github.com/sergioisidoro)
1 change: 1 addition & 0 deletions custom_components/ruuvi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Ruuvi sensor integration."""
9 changes: 9 additions & 0 deletions custom_components/ruuvi/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

{
"domain": "ruuvi",
"name": "Ruuvi tag Sensor",
"documentation": "https://github.com/JonasR-/ruuvi_hass",
"dependencies": [],
"codeowners": ["@JonasR", "@salleq", "@PieterGit", "@smaisidoro"],
"requirements": ["https://github.com/ttu/ruuvitag-sensor/archive/bleson-ble-communication.zip#ruuvitag_sensor==1.0.0"]
}
42 changes: 33 additions & 9 deletions ruuvi-hass.py → custom_components/ruuvi/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@
CONF_FORCE_UPDATE, CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_MAC
)

REQUIREMENTS = ['ruuvitag_sensor']
from ruuvitag_sensor.ruuvi import RuuviTagSensor, RunFlag

_LOGGER = logging.getLogger(__name__)

CONF_ADAPTER = 'adapter'
CONF_TIMEOUT = 'timeout'
CONF_POLL_INTERVAL = 'poll_interval'

DEFAULT_ADAPTER = 'hci0'
# In Ruuvi ble this defaults to hci0, so let's ruuvi decide on defaults
# https://github.com/ttu/ruuvitag-sensor/blob/master/ruuvitag_sensor/ble_communication.py#L51
DEFAULT_ADAPTER = ''
DEFAULT_FORCE_UPDATE = False
DEFAULT_NAME = 'RuuviTag'
DEFAULT_TIMEOUT = 3
Expand All @@ -44,13 +46,18 @@


def setup_platform(hass, config, add_devices, discovery_info=None):
from ruuvitag_sensor.ruuvi import RuuviTagSensor

mac_addresses = config.get(CONF_MAC)
if not isinstance(mac_addresses, list):
mac_addresses = [mac_addresses]

probe = RuuviProbe(RuuviTagSensor, mac_addresses, config.get(CONF_TIMEOUT), config.get(CONF_POLL_INTERVAL))
probe = RuuviProbe(
RuuviTagSensor,
mac_addresses,
config.get(CONF_TIMEOUT),
config.get(CONF_POLL_INTERVAL),
config.get(CONF_ADAPTER)
)

devs = []
for mac_address in mac_addresses:
Expand All @@ -65,21 +72,41 @@ def setup_platform(hass, config, add_devices, discovery_info=None):


class RuuviProbe(object):
def __init__(self, RuuviTagSensor, mac_addresses, timeout, max_poll_interval):
def __init__(self, RuuviTagSensor, mac_addresses, timeout, max_poll_interval, adapter):
self.RuuviTagSensor = RuuviTagSensor
self.mac_addresses = mac_addresses
self.timeout = timeout
self.max_poll_interval = max_poll_interval
self.last_poll = datetime.datetime.now()
self.adapter = adapter

default_condition = {'humidity': None, 'identifier': None, 'pressure': None, 'temperature': None}
self.conditions = {mac: default_condition for mac in mac_addresses}
self.current_datas = {}
self.run_flag = RunFlag()
self.counter = 0

def handle_data(self, found_data):
# current datas allways replaces old datas with new ones.
# keys are the mac addresses
self.current_datas[found_data[0]] = found_data[1]
self.counter = self.counter - 1
if self.counter < 0:
self.run_flag.running = False

def consume_datas(self):
polled_datas = self.current_datas.copy()
self.current_datas = {}
return polled_datas

def poll(self):
if (datetime.datetime.now() - self.last_poll).total_seconds() < self.max_poll_interval:
return
try:
self.conditions = self.RuuviTagSensor.get_data_for_sensors(self.mac_addresses, self.timeout)
self.counter = len(self.mac_addresses) * 2
self.run_flag.running = True
RuuviTagSensor.get_datas(self.handle_data, self.mac_addresses, self.run_flag)
self.conditions = self.consume_datas()
except:
_LOGGER.exception("Error on polling sensors")
self.last_poll = datetime.datetime.now()
Expand Down Expand Up @@ -110,6 +137,3 @@ def update(self):
self.poller.poll()

self._state = self.poller.conditions.get(self.mac_address, {}).get(self.sensor_type)