diff --git a/README.md b/README.md index a220c7e..fc37f5a 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 ``` @@ -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) \ No newline at end of file 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..23664d3 --- /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": ["@JonasR", "@salleq", "@PieterGit", "@smaisidoro"], + "requirements": ["https://github.com/ttu/ruuvitag-sensor/archive/bleson-ble-communication.zip#ruuvitag_sensor==1.0.0"] + } \ No newline at end of file diff --git a/ruuvi-hass.py b/custom_components/ruuvi/sensor.py similarity index 71% rename from ruuvi-hass.py rename to custom_components/ruuvi/sensor.py index caed209..ed3cb60 100644 --- a/ruuvi-hass.py +++ b/custom_components/ruuvi/sensor.py @@ -11,7 +11,7 @@ CONF_FORCE_UPDATE, CONF_MONITORED_CONDITIONS, CONF_NAME, CONF_MAC ) -REQUIREMENTS = ['ruuvitag_sensor'] +from ruuvitag_sensor.ruuvi import RuuviTagSensor, RunFlag _LOGGER = logging.getLogger(__name__) @@ -19,7 +19,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 @@ -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: @@ -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() @@ -110,6 +137,3 @@ def update(self): self.poller.poll() self._state = self.poller.conditions.get(self.mac_address, {}).get(self.sensor_type) - - -