From 8d41dc3a7963deb68cdafedff13c26ae4cb38e5c Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Mon, 10 Feb 2020 21:27:59 +0300 Subject: [PATCH 01/14] Separate entities for the battery. Initial commit. --- custom_components/mitemp_bt/const.py | 16 +++--- custom_components/mitemp_bt/sensor.py | 80 +++++++++++++++++++++++++-- 2 files changed, 84 insertions(+), 12 deletions(-) diff --git a/custom_components/mitemp_bt/const.py b/custom_components/mitemp_bt/const.py index 77b45607c..6c078730a 100644 --- a/custom_components/mitemp_bt/const.py +++ b/custom_components/mitemp_bt/const.py @@ -8,6 +8,7 @@ CONF_USE_MEDIAN = "use_median" CONF_ACTIVE_SCAN = "active_scan" CONF_HCI_INTERFACE = "hci_interface" +CONF_BATT_ENTITIES = "batt_entities" # Default values for configuration options DEFAULT_ROUNDING = True @@ -17,6 +18,7 @@ DEFAULT_USE_MEDIAN = False DEFAULT_ACTIVE_SCAN = False DEFAULT_HCI_INTERFACE = 0 +DEFAULT_BATT_ENTITIES = False """Fixed constants.""" @@ -37,12 +39,12 @@ } # Sensor type indexes dictionary -# Temperature, Humidity, Moisture, Conductivity, Illuminance -# Measurement type T H M C I 9 - no measurement +# Temperature, Humidity, Moisture, Conductivity, Illuminance, Battery +# Measurement type T H M C I B 9 - no measurement MMTS_DICT = { - 'HHCCJCY01' : [0, 9, 1, 2, 3], - 'HHCCPOT002': [9, 9, 0, 1, 9], - 'LYWSDCGQ' : [0, 1, 9, 9, 9], - 'LYWSD02' : [0, 1, 9, 9, 9], - 'CGG1' : [0, 1, 9, 9, 9] + 'HHCCJCY01' : [0, 9, 1, 2, 3, 9], + 'HHCCPOT002': [9, 9, 0, 1, 9, 9], + 'LYWSDCGQ' : [0, 1, 9, 9, 9, 2], + 'LYWSD02' : [0, 1, 9, 9, 9, 9], + 'CGG1' : [0, 1, 9, 9, 9, 2] } diff --git a/custom_components/mitemp_bt/sensor.py b/custom_components/mitemp_bt/sensor.py index c981462e2..2d2d3750c 100644 --- a/custom_components/mitemp_bt/sensor.py +++ b/custom_components/mitemp_bt/sensor.py @@ -12,6 +12,7 @@ from homeassistant.const import ( DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_HUMIDITY, + DEVICE_CLASS_BATTERY, TEMP_CELSIUS, ATTR_BATTERY_LEVEL, ) @@ -29,6 +30,7 @@ DEFAULT_USE_MEDIAN, DEFAULT_ACTIVE_SCAN, DEFAULT_HCI_INTERFACE, + DEFAULT_BATT_ENTITIES, CONF_ROUNDING, CONF_DECIMALS, CONF_PERIOD, @@ -36,6 +38,7 @@ CONF_USE_MEDIAN, CONF_ACTIVE_SCAN, CONF_HCI_INTERFACE, + CONF_BATT_ENTITIES, CONF_TMIN, CONF_TMAX, CONF_HMIN, @@ -57,6 +60,7 @@ vol.Optional( CONF_HCI_INTERFACE, default=DEFAULT_HCI_INTERFACE ): cv.positive_int, + vol.Optional(CONF_BATT_ENTITIES, default=DEFAULT_BATT_ENTITIES): cv.boolean, } ) @@ -400,7 +404,7 @@ def discover_ble_devices(config): # fixed entity index for every measurement type # according to the sensor implementation - t_i, h_i, m_i, c_i, i_i = MMTS_DICT[stype[mac]] + t_i, h_i, m_i, c_i, i_i, b_i = MMTS_DICT[stype[mac]] # if necessary, create a list of entities # according to the sensor implementation @@ -422,6 +426,10 @@ def discover_ble_devices(config): sensors = [None] * 2 sensors[t_i] = TemperatureSensor(mac) sensors[h_i] = HumiditySensor(mac) + + if CONF_BATT_ENTITIES and (b_i != 9): + sensors.insert(b_i, BatterySensor(mac)) + except IndexError as error: _LOGGER.error( "Sensor implementation error for %s, %s!", stype[mac], mac @@ -439,10 +447,22 @@ def discover_ble_devices(config): sts.mean(rssi[mac]) ) getattr(sensor, "_device_state_attributes")["sensor type"] = stype[mac] - if mac in batt: - getattr(sensor, "_device_state_attributes")[ - ATTR_BATTERY_LEVEL - ] = batt[mac] + #if mac in batt: + # getattr(sensor, "_device_state_attributes")[ + # ATTR_BATTERY_LEVEL + # ] = batt[mac] + if mac in batt: + if CONF_BATT_ENTITIES: + try: + setattr(sensors[b_i], "_state", batt[mac]) + sensors[b_i].async_schedule_update_ha_state() + except AttributeError: + _LOGGER.debug("BatterySensor %s not yet ready for update", mac) + else: + for sensor in sensors: + getattr(sensor, "_device_state_attributes")[ + ATTR_BATTERY_LEVEL + ] = batt[mac] # averaging and states updating if mac in temp_m_data: success, error = calc_update_state( @@ -769,3 +789,53 @@ def unique_id(self) -> str: def force_update(self): """Force update.""" return True + + +class BatterySensor(Entity): + """Representation of a Sensor.""" + + def __init__(self, mac): + """Initialize the sensor.""" + self._state = None + self._unique_id = "batt_" + mac + self._device_state_attributes = {} + + @property + def name(self): + """Return the name of the sensor.""" + return "mi {}".format(self._unique_id) + + @property + def state(self): + """Return the state of the sensor.""" + return self._state + + @property + def unit_of_measurement(self): + """Return the unit of measurement.""" + return "%" + + @property + def device_class(self): + """Return the unit of measurement.""" + return DEVICE_CLASS_BATTERY + + @property + def should_poll(self): + """No polling needed.""" + return False + + @property + def device_state_attributes(self): + """Return the state attributes.""" + return self._device_state_attributes + + @property + def unique_id(self) -> str: + """Return a unique ID.""" + return self._unique_id + + @property + def force_update(self): + """Force update.""" + return True \ No newline at end of file From 0da22e8b0379c28601e5a4f69f14e6a6b14e4951 Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Mon, 10 Feb 2020 23:40:15 +0300 Subject: [PATCH 02/14] Fix. README update. --- README.md | 5 +++++ custom_components/mitemp_bt/const.py | 2 +- custom_components/mitemp_bt/sensor.py | 6 +++--- info.md | 5 +++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3362bfd99..e700309da 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,7 @@ sensor: use_median: False active_scan: False hci_interface: 0 + batt_entities: False ``` @@ -126,6 +127,10 @@ sensor: (positive integer)(Optional) This parameter is used to select the bt-interface used. 0 for hci0, 1 for hci1 and so on. On most systems, the interface is hci0. Default value: 0 +#### batt_entities + + (boolean)(Optional) If you set this parameter to `True`, then the battery information will be presented as a separate entity as `sensor.mi_batt_ `. By default, the battery information will be presented as a sensor attribute called `battery level` Default value: False + ## Frequently asked questions Still having questions or issues? Please first have a look on our [Frequently Asked Questions (FAQ) page](faq.md) to see if your question is already answered. If your question or issue isn't answered in the FAQ, please open an [issue](https://github.com/custom-components/sensor.mitemp_bt/issues). diff --git a/custom_components/mitemp_bt/const.py b/custom_components/mitemp_bt/const.py index 6c078730a..b5495ca74 100644 --- a/custom_components/mitemp_bt/const.py +++ b/custom_components/mitemp_bt/const.py @@ -38,7 +38,7 @@ '205D01': ["HHCCPOT002", 1] } -# Sensor type indexes dictionary +# Sensor type indexes dictionary # Temperature, Humidity, Moisture, Conductivity, Illuminance, Battery # Measurement type T H M C I B 9 - no measurement MMTS_DICT = { diff --git a/custom_components/mitemp_bt/sensor.py b/custom_components/mitemp_bt/sensor.py index 2d2d3750c..c332932e2 100644 --- a/custom_components/mitemp_bt/sensor.py +++ b/custom_components/mitemp_bt/sensor.py @@ -427,7 +427,7 @@ def discover_ble_devices(config): sensors[t_i] = TemperatureSensor(mac) sensors[h_i] = HumiditySensor(mac) - if CONF_BATT_ENTITIES and (b_i != 9): + if config[CONF_BATT_ENTITIES] and (b_i != 9): sensors.insert(b_i, BatterySensor(mac)) except IndexError as error: @@ -452,7 +452,7 @@ def discover_ble_devices(config): # ATTR_BATTERY_LEVEL # ] = batt[mac] if mac in batt: - if CONF_BATT_ENTITIES: + if config[CONF_BATT_ENTITIES]: try: setattr(sensors[b_i], "_state", batt[mac]) sensors[b_i].async_schedule_update_ha_state() @@ -838,4 +838,4 @@ def unique_id(self) -> str: @property def force_update(self): """Force update.""" - return True \ No newline at end of file + return True diff --git a/info.md b/info.md index ee490b6e8..c5cbf1f09 100644 --- a/info.md +++ b/info.md @@ -115,6 +115,7 @@ sensor: use_median: False active_scan: False hci_interface: 0 + batt_entities: False ``` ### Configuration Variables @@ -153,6 +154,10 @@ sensor: (positive integer)(Optional) This parameter is used to select the bt-interface used. 0 for hci0, 1 for hci1 and so on. On most systems, the interface is hci0. Default value: 0 +#### batt_entities + + (boolean)(Optional) If you set this parameter to `True`, then the battery information will be presented as a separate entity as `sensor.mi_batt_ `. By default, the battery information will be presented as a sensor attribute called `battery level` Default value: False + ## Frequently asked questions Still having questions or issues? Please first have a look on our [Frequently Asked Questions (FAQ) page](faq.md) to see if your question is already answered. If your question or issue isn't answered in the FAQ, please open an [issue](https://github.com/custom-components/sensor.mitemp_bt/issues). From c49b59cbe487b1ef0fff23a16db9d0b2a00c90f6 Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Tue, 11 Feb 2020 00:15:43 +0300 Subject: [PATCH 03/14] Battery sensor entity _in addition_ to attribute. --- README.md | 2 +- custom_components/mitemp_bt/sensor.py | 11 ++++++----- info.md | 2 +- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e700309da..c8dd6ff97 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ sensor: #### batt_entities - (boolean)(Optional) If you set this parameter to `True`, then the battery information will be presented as a separate entity as `sensor.mi_batt_ `. By default, the battery information will be presented as a sensor attribute called `battery level` Default value: False + (boolean)(Optional) By default, the battery information will be presented only as a sensor attribute called `battery level`. If you set this parameter to `True`, then the battery sensor entity will be additionally created - `sensor.mi_batt_ `. Default value: False ## Frequently asked questions diff --git a/custom_components/mitemp_bt/sensor.py b/custom_components/mitemp_bt/sensor.py index c332932e2..4bc475665 100644 --- a/custom_components/mitemp_bt/sensor.py +++ b/custom_components/mitemp_bt/sensor.py @@ -458,11 +458,12 @@ def discover_ble_devices(config): sensors[b_i].async_schedule_update_ha_state() except AttributeError: _LOGGER.debug("BatterySensor %s not yet ready for update", mac) - else: - for sensor in sensors: - getattr(sensor, "_device_state_attributes")[ - ATTR_BATTERY_LEVEL - ] = batt[mac] + for sensor in sensors: + if isinstance(sensor, BatterySensor): + continue + getattr(sensor, "_device_state_attributes")[ + ATTR_BATTERY_LEVEL + ] = batt[mac] # averaging and states updating if mac in temp_m_data: success, error = calc_update_state( diff --git a/info.md b/info.md index c5cbf1f09..61d906fb2 100644 --- a/info.md +++ b/info.md @@ -156,7 +156,7 @@ sensor: #### batt_entities - (boolean)(Optional) If you set this parameter to `True`, then the battery information will be presented as a separate entity as `sensor.mi_batt_ `. By default, the battery information will be presented as a sensor attribute called `battery level` Default value: False + (boolean)(Optional) By default, the battery information will be presented only as a sensor attribute called `battery level`. If you set this parameter to `True`, then the battery sensor entity will be additionally created - `sensor.mi_batt_ `. Default value: False ## Frequently asked questions From 034a87e763954cf874f31e2bb70c2baab314178d Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Tue, 11 Feb 2020 00:23:46 +0300 Subject: [PATCH 04/14] info.md update --- info.md | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/info.md b/info.md index 61d906fb2..c0e512e51 100644 --- a/info.md +++ b/info.md @@ -5,24 +5,13 @@ ### NB!: This is a Beta version! {% endif %} {% if installed %} -# Changes since 0.4 - -The component does not use external utilities anymore, we get access to data directly from python, from a separate tread. +# Changes since 0.5.3 New configuration options: -- active_scan: False -- hci_interface: 0 (integer number, 0 as default for hci0, 1 for hci1 and so on) - -Deprecated configuration options: - -- hcidump_active is deprecated and __must be removed__ from `configuration.yaml`) - -NB: - -Since the component now uses direct access to the HCI interface, python must have the appropriate rights, see paragraph 1 of the HOW TO INSTALL section [below](#how-to-install). +- batt_entities: False -In addition, we began to collect [Frequently Asked Questions](https://github.com/custom-components/sensor.mitemp_bt/blob/master/faq.md). Please read it before creating a new issue. +(boolean)(Optional) By default, the battery information will be presented only as a sensor attribute called `battery level`. If you set this parameter to `True`, then the battery sensor entity will be additionally created - `sensor.mi_batt_ `. Default value: False --- {% endif %} From 8f7514972083bee610169feb49cf17833945a99e Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Tue, 11 Feb 2020 03:48:25 +0300 Subject: [PATCH 05/14] Work with several interfaces simultaneously --- README.md | 12 +++++++- custom_components/mitemp_bt/sensor.py | 43 +++++++++++++++++---------- info.md | 24 ++++++++++++++- 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index c8dd6ff97..6fbea7797 100644 --- a/README.md +++ b/README.md @@ -125,7 +125,17 @@ sensor: #### hci_interface - (positive integer)(Optional) This parameter is used to select the bt-interface used. 0 for hci0, 1 for hci1 and so on. On most systems, the interface is hci0. Default value: 0 + (positive integer or list of positive integers)(Optional) This parameter is used to select the bt-interface used. 0 for hci0, 1 for hci1 and so on. On most systems, the interface is hci0. In addition, if you need to collect data from several interfaces, you can specify a list of interfaces: + + ```yaml + sensor: + - platform: mitemp_bt + hci_interface: + - 0 + - 1 + ``` + + Default value: 0 #### batt_entities diff --git a/custom_components/mitemp_bt/sensor.py b/custom_components/mitemp_bt/sensor.py index 4bc475665..fa4837a86 100644 --- a/custom_components/mitemp_bt/sensor.py +++ b/custom_components/mitemp_bt/sensor.py @@ -57,9 +57,12 @@ vol.Optional(CONF_LOG_SPIKES, default=DEFAULT_LOG_SPIKES): cv.boolean, vol.Optional(CONF_USE_MEDIAN, default=DEFAULT_USE_MEDIAN): cv.boolean, vol.Optional(CONF_ACTIVE_SCAN, default=DEFAULT_ACTIVE_SCAN): cv.boolean, + #vol.Optional( + # CONF_HCI_INTERFACE, default=DEFAULT_HCI_INTERFACE + #): cv.positive_int, vol.Optional( - CONF_HCI_INTERFACE, default=DEFAULT_HCI_INTERFACE - ): cv.positive_int, + CONF_HCI_INTERFACE, default=[DEFAULT_HCI_INTERFACE] + ): vol.All(cv.ensure_list, [cv.positive_int]), vol.Optional(CONF_BATT_ENTITIES, default=DEFAULT_BATT_ENTITIES): cv.boolean, } ) @@ -224,7 +227,7 @@ def parse_raw_message(data): } # loop through xiaomi payload - # assume that the data may have several values ​​of different types, + # assume that the data may have several values of different types, # although I did not notice this behavior with my LYWSDCGQ sensors while True: xvalue_typecode = data[xdata_point:xdata_point + 2] @@ -248,31 +251,37 @@ def parse_raw_message(data): class BLEScanner: """BLE scanner.""" - dumpthread = None + dumpthreads = [] hcidump_data = [] def start(self, config): """Start receiving broadcasts.""" active_scan = config[CONF_ACTIVE_SCAN] - hci_interface = config[CONF_HCI_INTERFACE] + hci_interfaces = config[CONF_HCI_INTERFACE] self.hcidump_data.clear() - _LOGGER.debug("Spawning HCIdump thread.") - self.dumpthread = HCIdump( - dumplist=self.hcidump_data, - interface=hci_interface, - active=int(active_scan is True), - ) - _LOGGER.debug("Starting HCIdump thread.") - self.dumpthread.start() + _LOGGER.debug("Spawning HCIdump thread(s).") + for hci_int in hci_interfaces: + dumpthread = HCIdump( + dumplist=self.hcidump_data, + interface=hci_int, + active=int(active_scan is True), + ) + self.dumpthreads.append(dumpthread) + _LOGGER.debug("Starting HCIdump thread for hci%s", hci_int) + dumpthread.start() + _LOGGER.debug("HCIdump threads count = %s", len(self.dumpthreads)) + def stop(self): - """Stop HCIdump thread.""" - self.dumpthread.join() + """Stop HCIdump thread(s).""" + for dumpthread in self.dumpthreads: + dumpthread.join() + self.dumpthreads = [] def shutdown_handler(self, event): """Run homeassistant_stop event handler.""" _LOGGER.debug("Running homeassistant_stop event handler: %s", event) - self.dumpthread.join() + self.stop() def setup_platform(hass, config, add_entities, discovery_info=None): @@ -352,7 +361,9 @@ def discover_ble_devices(config): else: prev_packet = None if prev_packet == packet: +# _LOGGER.debug("DUPLICATE: %s, IGNORING!", data) continue +# _LOGGER.debug("NEW DATA: %s", data) lpacket[data["mac"]] = packet # store found readings per device if "temperature" in data: diff --git a/info.md b/info.md index c0e512e51..000637417 100644 --- a/info.md +++ b/info.md @@ -13,6 +13,18 @@ New configuration options: (boolean)(Optional) By default, the battery information will be presented only as a sensor attribute called `battery level`. If you set this parameter to `True`, then the battery sensor entity will be additionally created - `sensor.mi_batt_ `. Default value: False +Changed: + +Added the ability to specify a list for a `hci_interface` option, that is, now it is possible to collect data from several interfaces simultaneously: + +```yaml + sensor: + - platform: mitemp_bt + hci_interface: + - 0 + - 1 + ``` + --- {% endif %} @@ -141,7 +153,17 @@ sensor: #### hci_interface - (positive integer)(Optional) This parameter is used to select the bt-interface used. 0 for hci0, 1 for hci1 and so on. On most systems, the interface is hci0. Default value: 0 + (positive integer or list of positive integers)(Optional) This parameter is used to select the bt-interface used. 0 for hci0, 1 for hci1 and so on. On most systems, the interface is hci0. In addition, if you need to collect data from multiple interfaces simultaneously, you can specify a list of interfaces: + + ```yaml + sensor: + - platform: mitemp_bt + hci_interface: + - 0 + - 1 + ``` + + Default value: 0 #### batt_entities From 1c34bde35d28d44c45e3ed17ed1d7e76058fdf1d Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Tue, 11 Feb 2020 04:16:03 +0300 Subject: [PATCH 06/14] Small fix --- custom_components/mitemp_bt/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/mitemp_bt/sensor.py b/custom_components/mitemp_bt/sensor.py index fa4837a86..4d01785d6 100644 --- a/custom_components/mitemp_bt/sensor.py +++ b/custom_components/mitemp_bt/sensor.py @@ -276,7 +276,7 @@ def stop(self): """Stop HCIdump thread(s).""" for dumpthread in self.dumpthreads: dumpthread.join() - self.dumpthreads = [] + self.dumpthreads.clear() def shutdown_handler(self, event): """Run homeassistant_stop event handler.""" From 06cd940ddf6969ec4f995724de98eb0d40e35bb9 Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Tue, 11 Feb 2020 04:50:11 +0300 Subject: [PATCH 07/14] FAQ updated with 0.5.4 and 0.5.5 changes --- faq.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/faq.md b/faq.md index 0b3041bd3..e4ac55f78 100644 --- a/faq.md +++ b/faq.md @@ -85,6 +85,8 @@ sensor: device_class: "battery" ``` +Or (since v0.5.4) you can set option `batt_entities` to `True` - the battery sensor entity will be created automatically for each device reporting battery status. + ## RECEPTION ISSUES ### My sensor doesn't receive any readings from my sensors anymore or only occasionally @@ -104,6 +106,7 @@ Especially SSD devices are known to affect the Bluetooth reception, try to place - The quality of your Bluetooth transceiver. The range of the built-in Bluetooth tranceiver of a Raspberry Pi is known to be limited. Try using an external Bluetooth transceiver to increase the range, e.g. with an external antenna. +It is also worth noting that starting from v0.5.5, a component can receive data from multiple interfaces simultaneously (see the `hci_interface` option). ## OTHER ISSUES From b2d572316aeddc57acd2952b8ca530457f07c6d4 Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Tue, 11 Feb 2020 05:33:46 +0300 Subject: [PATCH 08/14] Some fixes --- custom_components/mitemp_bt/sensor.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/custom_components/mitemp_bt/sensor.py b/custom_components/mitemp_bt/sensor.py index 4d01785d6..4f31063f9 100644 --- a/custom_components/mitemp_bt/sensor.py +++ b/custom_components/mitemp_bt/sensor.py @@ -114,12 +114,14 @@ def run(self): ) btctrl.send_scan_request() _LOGGER.debug("HCIdump thread: start main event_loop") - self._event_loop.run_forever() - _LOGGER.debug("HCIdump thread: main event_loop stopped, finishing") - btctrl.stop_scan_request() - conn.close() - self._event_loop.close() - _LOGGER.debug("HCIdump thread: Run finished") + try: + self._event_loop.run_forever() + finally: + _LOGGER.debug("HCIdump thread: main event_loop stopped, finishing") + btctrl.stop_scan_request() + conn.close() + self._event_loop.close() + _LOGGER.debug("HCIdump thread: Run finished") def join(self, timeout=3): """Join HCIdump thread.""" @@ -128,8 +130,9 @@ def join(self, timeout=3): self._event_loop.call_soon_threadsafe(self._event_loop.stop) except AttributeError as error: _LOGGER.debug("%s", error) - Thread.join(self, timeout) - _LOGGER.debug("HCIdump thread: joined") + finally: + Thread.join(self, timeout) + _LOGGER.debug("HCIdump thread: joined") def reverse_mac(rmac): From 5334c9986c4973f1f35849eafed4211cbcc7d10a Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Tue, 11 Feb 2020 07:02:27 +0300 Subject: [PATCH 09/14] No more unclosed connections --- custom_components/mitemp_bt/sensor.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/custom_components/mitemp_bt/sensor.py b/custom_components/mitemp_bt/sensor.py index 4f31063f9..3c9ae84db 100644 --- a/custom_components/mitemp_bt/sensor.py +++ b/custom_components/mitemp_bt/sensor.py @@ -5,6 +5,7 @@ import statistics as sts import struct from threading import Thread +from time import sleep import aioblescan as aiobs import voluptuous as vol @@ -120,6 +121,7 @@ def run(self): _LOGGER.debug("HCIdump thread: main event_loop stopped, finishing") btctrl.stop_scan_request() conn.close() + self._event_loop.run_until_complete(asyncio.sleep(0)) self._event_loop.close() _LOGGER.debug("HCIdump thread: Run finished") @@ -294,6 +296,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): hass.bus.listen("homeassistant_stop", scanner.shutdown_handler) scanner.start(config) sensors_by_mac = {} + sleep(1) def calc_update_state(entity_to_update, sensor_mac, config, measurements_list): """Averages according to options and updates the entity state.""" From 7641c8d7a39a971578c589ffc7301a22202354da Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Wed, 12 Feb 2020 02:48:50 +0300 Subject: [PATCH 10/14] README.md and info.md updated --- README.md | 2 +- info.md | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6fbea7797..a1c5d31d4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Xiaomi BLE Monitor sensor platform -This custom component is an alternative for the standard build in [mitemp_bt](https://www.home-assistant.io/integrations/mitemp_bt/) integration that is available in Home Assistant. Unlike the original `mitemp_bt` integration, which is getting its data by polling the device with a default five-minute interval, this custom component is parsing the Bluetooth Low Energy packets payload that is constantly emitted by the sensor. The packets payload may contain temperature/humidity/battery and other data. Advantage of this integration is that it doesn't affect the battery as much as the built-in integration. It also solves connection issues some people have with the standard integration. +This custom component is an alternative for the standard build in [mitemp_bt](https://www.home-assistant.io/integrations/mitemp_bt/) integration that is available in Home Assistant. Unlike the original `mitemp_bt` integration, which is getting its data by polling the device with a default five-minute interval, this custom component is parsing the Bluetooth Low Energy packets payload that is constantly emitted by the sensor. The packets payload may contain temperature/humidity/battery and other data. Advantage of this integration is that it doesn't affect the battery as much as the built-in integration. It also solves connection issues some people have with the standard integration (due to passivity and the ability to collect data from multiple bt-interfaces simultaneously). Supported sensors: diff --git a/info.md b/info.md index 000637417..31e08f3d0 100644 --- a/info.md +++ b/info.md @@ -2,9 +2,12 @@ [![hacs_badge](https://img.shields.io/badge/HACS-Custom-orange.svg)](https://github.com/custom-components/hacs) {% if prerelease %} + ### NB!: This is a Beta version! + {% endif %} -{% if installed %} +{% if pending_update %} + # Changes since 0.5.3 New configuration options: @@ -15,7 +18,7 @@ New configuration options: Changed: -Added the ability to specify a list for a `hci_interface` option, that is, now it is possible to collect data from several interfaces simultaneously: +Added the ability to specify a list for a `hci_interface` option, that is, now it is possible to collect data from multiple interfaces simultaneously: ```yaml sensor: @@ -30,7 +33,7 @@ Added the ability to specify a list for a `hci_interface` option, that is, now i # Xiaomi BLE Monitor sensor platform -This custom component is an alternative for the standard build in [mitemp_bt](https://www.home-assistant.io/integrations/mitemp_bt/) integration that is available in Home Assistant. Unlike the original `mitemp_bt` integration, which is getting its data by polling the device with a default five-minute interval, this custom component is parsing the Bluetooth Low Energy packets payload that is constantly emitted by the sensor. The packets payload may contain temperature/humidity/battery and other data. Advantage of this integration is that it doesn't affect the battery as much as the built-in integration. It also solves connection issues some people have with the standard integration. +This custom component is an alternative for the standard build in [mitemp_bt](https://www.home-assistant.io/integrations/mitemp_bt/) integration that is available in Home Assistant. Unlike the original `mitemp_bt` integration, which is getting its data by polling the device with a default five-minute interval, this custom component is parsing the Bluetooth Low Energy packets payload that is constantly emitted by the sensor. The packets payload may contain temperature/humidity/battery and other data. Advantage of this integration is that it doesn't affect the battery as much as the built-in integration. It also solves connection issues some people have with the standard integration (due to passivity and the ability to collect data from multiple bt-interfaces simultaneously). ![supported sensors](https://raw.github.com/custom-components/sensor.mitemp_bt/master/sensors.jpg) From 166bed6bdfa644bea2662b9ca61eca31b8fcb8fa Mon Sep 17 00:00:00 2001 From: Aleksey Makarenko Date: Wed, 12 Feb 2020 02:55:41 +0300 Subject: [PATCH 11/14] info.md Jinja template test --- info.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/info.md b/info.md index 31e08f3d0..866991426 100644 --- a/info.md +++ b/info.md @@ -6,7 +6,7 @@ ### NB!: This is a Beta version! {% endif %} -{% if pending_update %} +{% if installed or pending_update %} # Changes since 0.5.3 From 96b61bcfc24e7a4edf6790aed755c1fe926d5612 Mon Sep 17 00:00:00 2001 From: Ernst Klamer Date: Fri, 14 Feb 2020 11:45:13 +0100 Subject: [PATCH 12/14] Rephrased "changed" --- info.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/info.md b/info.md index 866991426..97bce5b3b 100644 --- a/info.md +++ b/info.md @@ -18,7 +18,7 @@ New configuration options: Changed: -Added the ability to specify a list for a `hci_interface` option, that is, now it is possible to collect data from multiple interfaces simultaneously: +Added the ability to specify a list for the `hci_interface` configuration option. This makes it possible to collect data from multiple interfaces simultaneously: ```yaml sensor: From 1c996b5dbab7282c4a6ba5aaeb52a4151310268f Mon Sep 17 00:00:00 2001 From: Ernst Klamer Date: Fri, 14 Feb 2020 11:50:32 +0100 Subject: [PATCH 13/14] Remove commented out code --- custom_components/mitemp_bt/sensor.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/custom_components/mitemp_bt/sensor.py b/custom_components/mitemp_bt/sensor.py index 3c9ae84db..924509b4c 100644 --- a/custom_components/mitemp_bt/sensor.py +++ b/custom_components/mitemp_bt/sensor.py @@ -58,9 +58,6 @@ vol.Optional(CONF_LOG_SPIKES, default=DEFAULT_LOG_SPIKES): cv.boolean, vol.Optional(CONF_USE_MEDIAN, default=DEFAULT_USE_MEDIAN): cv.boolean, vol.Optional(CONF_ACTIVE_SCAN, default=DEFAULT_ACTIVE_SCAN): cv.boolean, - #vol.Optional( - # CONF_HCI_INTERFACE, default=DEFAULT_HCI_INTERFACE - #): cv.positive_int, vol.Optional( CONF_HCI_INTERFACE, default=[DEFAULT_HCI_INTERFACE] ): vol.All(cv.ensure_list, [cv.positive_int]), @@ -464,10 +461,6 @@ def discover_ble_devices(config): sts.mean(rssi[mac]) ) getattr(sensor, "_device_state_attributes")["sensor type"] = stype[mac] - #if mac in batt: - # getattr(sensor, "_device_state_attributes")[ - # ATTR_BATTERY_LEVEL - # ] = batt[mac] if mac in batt: if config[CONF_BATT_ENTITIES]: try: From e02f9894d685e776b541e51fb3f776ae0613f9b7 Mon Sep 17 00:00:00 2001 From: Ernst Klamer Date: Fri, 14 Feb 2020 11:52:51 +0100 Subject: [PATCH 14/14] Indentation fix (commented out lines) --- custom_components/mitemp_bt/sensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/custom_components/mitemp_bt/sensor.py b/custom_components/mitemp_bt/sensor.py index 924509b4c..199b5fa2b 100644 --- a/custom_components/mitemp_bt/sensor.py +++ b/custom_components/mitemp_bt/sensor.py @@ -364,9 +364,9 @@ def discover_ble_devices(config): else: prev_packet = None if prev_packet == packet: -# _LOGGER.debug("DUPLICATE: %s, IGNORING!", data) + # _LOGGER.debug("DUPLICATE: %s, IGNORING!", data) continue -# _LOGGER.debug("NEW DATA: %s", data) + # _LOGGER.debug("NEW DATA: %s", data) lpacket[data["mac"]] = packet # store found readings per device if "temperature" in data: