From ce148285b486f0875e2f23560a0d6d810bcf17a8 Mon Sep 17 00:00:00 2001 From: sergioisidoro Date: Sat, 21 Dec 2019 23:42:21 +0200 Subject: [PATCH 1/7] Use custom_component template --- README.md | 2 +- custom_components/ruuvi/__init__.py | 1 + custom_components/ruuvi/manifest.json | 9 +++++++++ ruuvi-hass.py => custom_components/ruuvi/sensor.py | 2 -- 4 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 custom_components/ruuvi/__init__.py create mode 100644 custom_components/ruuvi/manifest.json rename ruuvi-hass.py => custom_components/ruuvi/sensor.py (98%) diff --git a/README.md b/README.md index a220c7e..a9627fa 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # ruuvi_hass RuuviTag sensor for hass.io -Copy ruuvi-hass.py to /custom_components/sensor/ (e.g. /home/homeassistant/.homeassistant/custom_components/sensor/ruuvi-hass.py) +Copy the contents of `custom_components` in this repo to `/custom_components` (e.g. `/home/homeassistant/.homeassistant/custom_components/`). The configuration.yaml has to be edited like this ``` diff --git a/custom_components/ruuvi/__init__.py b/custom_components/ruuvi/__init__.py new file mode 100644 index 0000000..c1e0629 --- /dev/null +++ b/custom_components/ruuvi/__init__.py @@ -0,0 +1 @@ +"""Ruuvi sensor integration.""" \ No newline at end of file diff --git a/custom_components/ruuvi/manifest.json b/custom_components/ruuvi/manifest.json new file mode 100644 index 0000000..50d6b3e --- /dev/null +++ b/custom_components/ruuvi/manifest.json @@ -0,0 +1,9 @@ + +{ + "domain": "ruuvi", + "name": "Ruuvi tag Sensor", + "documentation": "https://github.com/JonasR-/ruuvi_hass", + "dependencies": [], + "codeowners": [], + "requirements": ["ruuvitag_sensor"] + } \ No newline at end of file diff --git a/ruuvi-hass.py b/custom_components/ruuvi/sensor.py similarity index 98% rename from ruuvi-hass.py rename to custom_components/ruuvi/sensor.py index caed209..937dcf2 100644 --- a/ruuvi-hass.py +++ b/custom_components/ruuvi/sensor.py @@ -11,8 +11,6 @@ CONF_FORCE_UPDATE, CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_MAC ) -REQUIREMENTS = ['ruuvitag_sensor'] - _LOGGER = logging.getLogger(__name__) CONF_ADAPTER = 'adapter' From 4a9b87ce117d08d93e0a295d428136ff20aab0a4 Mon Sep 17 00:00:00 2001 From: sergioisidoro Date: Sat, 21 Dec 2019 23:47:02 +0200 Subject: [PATCH 2/7] Passing ble adapter to ruuvi ble_communication lib --- custom_components/ruuvi/sensor.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/custom_components/ruuvi/sensor.py b/custom_components/ruuvi/sensor.py index 937dcf2..7918b09 100644 --- a/custom_components/ruuvi/sensor.py +++ b/custom_components/ruuvi/sensor.py @@ -17,7 +17,9 @@ 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 @@ -48,7 +50,13 @@ def setup_platform(hass, config, add_devices, discovery_info=None): 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: @@ -63,12 +71,13 @@ 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} @@ -77,7 +86,7 @@ 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.conditions = self.RuuviTagSensor.get_data_for_sensors(self.mac_addresses, self.timeout, self.adapter) except: _LOGGER.exception("Error on polling sensors") self.last_poll = datetime.datetime.now() @@ -108,6 +117,3 @@ def update(self): self.poller.poll() self._state = self.poller.conditions.get(self.mac_address, {}).get(self.sensor_type) - - - From 8c27e7c5a586318c6943c49fe2ad0e8eaaad688e Mon Sep 17 00:00:00 2001 From: sergioisidoro Date: Sat, 21 Dec 2019 23:49:25 +0200 Subject: [PATCH 3/7] update readme --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index a9627fa..ae66e43 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,19 @@ 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' + adapted: 'hci0' +``` + + +## Contributors +[JonasR-](https://github.com/JonasR-) (author) +[smaisidoro](https://github.com/sergioisidoro) \ No newline at end of file From e3eda10893e5d9045cd87e40141d8a911e71e328 Mon Sep 17 00:00:00 2001 From: sergioisidoro Date: Sun, 22 Dec 2019 00:01:42 +0200 Subject: [PATCH 4/7] Update contributors --- README.md | 2 ++ custom_components/ruuvi/manifest.json | 2 +- custom_components/ruuvi/sensor.py | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ae66e43..a3d9569 100644 --- a/README.md +++ b/README.md @@ -29,4 +29,6 @@ Defaults to ruuvi ble_communicator default (at this point is `hci0`) ## Contributors [JonasR-](https://github.com/JonasR-) (author) +[PieterGit](https://github.com/PieterGit) +[salleq](https://github.com/salleq) [smaisidoro](https://github.com/sergioisidoro) \ No newline at end of file diff --git a/custom_components/ruuvi/manifest.json b/custom_components/ruuvi/manifest.json index 50d6b3e..9cf824d 100644 --- a/custom_components/ruuvi/manifest.json +++ b/custom_components/ruuvi/manifest.json @@ -4,6 +4,6 @@ "name": "Ruuvi tag Sensor", "documentation": "https://github.com/JonasR-/ruuvi_hass", "dependencies": [], - "codeowners": [], + "codeowners": ["@JonasR", "@salleq", "@PieterGit", "@smaisidoro"], "requirements": ["ruuvitag_sensor"] } \ No newline at end of file diff --git a/custom_components/ruuvi/sensor.py b/custom_components/ruuvi/sensor.py index 7918b09..1d31d9c 100644 --- a/custom_components/ruuvi/sensor.py +++ b/custom_components/ruuvi/sensor.py @@ -11,6 +11,8 @@ CONF_FORCE_UPDATE, CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_MAC ) +from ruuvitag_sensor.ruuvi import RuuviTagSensor + _LOGGER = logging.getLogger(__name__) CONF_ADAPTER = 'adapter' @@ -44,7 +46,6 @@ 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): From c9ef715d268b25aa8408bdd33bb357096dbcf11d Mon Sep 17 00:00:00 2001 From: sergioisidoro Date: Sun, 22 Dec 2019 00:07:11 +0200 Subject: [PATCH 5/7] Typo in docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a3d9569..fc37f5a 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Defaults to ruuvi ble_communicator default (at this point is `hci0`) - platform: ruuvi-hass mac: 'MA:CA:DD:RE:SS:01' name: 'balcony' - adapted: 'hci0' + adapter: 'hci0' ``` From 57ed851ff62c9fcec3194f259e5e175f76d166bf Mon Sep 17 00:00:00 2001 From: sergioisidoro Date: Sun, 22 Dec 2019 18:52:19 +0200 Subject: [PATCH 6/7] Use bleson as native python ble interface --- custom_components/ruuvi/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/ruuvi/manifest.json b/custom_components/ruuvi/manifest.json index 9cf824d..23664d3 100644 --- a/custom_components/ruuvi/manifest.json +++ b/custom_components/ruuvi/manifest.json @@ -5,5 +5,5 @@ "documentation": "https://github.com/JonasR-/ruuvi_hass", "dependencies": [], "codeowners": ["@JonasR", "@salleq", "@PieterGit", "@smaisidoro"], - "requirements": ["ruuvitag_sensor"] + "requirements": ["https://github.com/ttu/ruuvitag-sensor/archive/bleson-ble-communication.zip#ruuvitag_sensor==1.0.0"] } \ No newline at end of file From 5f493b9e8083ee06db516ac644139de7665cc6d6 Mon Sep 17 00:00:00 2001 From: Sergio Isidoro Date: Sat, 22 Feb 2020 22:59:30 +0200 Subject: [PATCH 7/7] Attempt to use get_datas instead of get_data_for_sensors --- custom_components/ruuvi/sensor.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/custom_components/ruuvi/sensor.py b/custom_components/ruuvi/sensor.py index 1d31d9c..ed3cb60 100644 --- a/custom_components/ruuvi/sensor.py +++ b/custom_components/ruuvi/sensor.py @@ -11,7 +11,7 @@ CONF_FORCE_UPDATE, CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_MAC ) -from ruuvitag_sensor.ruuvi import RuuviTagSensor +from ruuvitag_sensor.ruuvi import RuuviTagSensor, RunFlag _LOGGER = logging.getLogger(__name__) @@ -82,12 +82,31 @@ def __init__(self, RuuviTagSensor, mac_addresses, timeout, max_poll_interval, ad 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.adapter) + 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()