From 153aadadae56ee6f3de8d83f41f99dd8b783f4fe Mon Sep 17 00:00:00 2001 From: Jozef Zuzelka Date: Wed, 20 May 2020 02:13:50 +0200 Subject: [PATCH] Ble scanner (#976) * Added ble_scanner component * ble_scanner: time.h changed to ctime * ble_scanner: Test added. * ble_scanner: fixed code formatting. * Removed duplicate binary sensor tests from test2 and test3 to decrease memory requirements. * Removed another duplicate test from test2.yaml and moved stepper test to yaml3.yaml to reduce memory requirements of test2.yaml. * Reverted the last stepper test change * Moved some sensor tests from test2.yaml to test3.yaml to save memory. * Moved ruuvitag back to test2.yaml as it requires component esp32_ble_tracker. * removed apds9960 as it is duplicated Co-authored-by: Guillermo Ruffino --- esphome/components/ble_scanner/__init__.py | 0 .../components/ble_scanner/ble_scanner.cpp | 16 ++++ esphome/components/ble_scanner/ble_scanner.h | 38 +++++++++ esphome/components/ble_scanner/text_sensor.py | 22 +++++ tests/test2.yaml | 82 +++---------------- tests/test3.yaml | 35 +++++++- 6 files changed, 120 insertions(+), 73 deletions(-) create mode 100644 esphome/components/ble_scanner/__init__.py create mode 100644 esphome/components/ble_scanner/ble_scanner.cpp create mode 100644 esphome/components/ble_scanner/ble_scanner.h create mode 100644 esphome/components/ble_scanner/text_sensor.py diff --git a/esphome/components/ble_scanner/__init__.py b/esphome/components/ble_scanner/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/esphome/components/ble_scanner/ble_scanner.cpp b/esphome/components/ble_scanner/ble_scanner.cpp new file mode 100644 index 0000000000..6735599850 --- /dev/null +++ b/esphome/components/ble_scanner/ble_scanner.cpp @@ -0,0 +1,16 @@ +#include "ble_scanner.h" +#include "esphome/core/log.h" + +#ifdef ARDUINO_ARCH_ESP32 + +namespace esphome { +namespace ble_scanner { + +static const char *TAG = "ble_scanner"; + +void BLEScanner::dump_config() { LOG_TEXT_SENSOR("", "BLE Scanner", this); } + +} // namespace ble_scanner +} // namespace esphome + +#endif diff --git a/esphome/components/ble_scanner/ble_scanner.h b/esphome/components/ble_scanner/ble_scanner.h new file mode 100644 index 0000000000..194494144c --- /dev/null +++ b/esphome/components/ble_scanner/ble_scanner.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +#include "esphome/core/component.h" +#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h" +#include "esphome/components/text_sensor/text_sensor.h" + +#ifdef ARDUINO_ARCH_ESP32 + +namespace esphome { +namespace ble_scanner { + +class BLEScanner : public text_sensor::TextSensor, public esp32_ble_tracker::ESPBTDeviceListener, public Component { + public: + bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override { + this->publish_state("{\"timestamp\":" + to_string(::time(NULL)) + + "," + "\"address\":\"" + + device.address_str() + + "\"," + "\"rssi\":" + + to_string(device.get_rssi()) + + "," + "\"name\":\"" + + device.get_name() + "\"}"); + + return true; + } + void dump_config() override; + float get_setup_priority() const override { return setup_priority::DATA; } +}; + +} // namespace ble_scanner +} // namespace esphome + +#endif diff --git a/esphome/components/ble_scanner/text_sensor.py b/esphome/components/ble_scanner/text_sensor.py new file mode 100644 index 0000000000..1a43ffb68a --- /dev/null +++ b/esphome/components/ble_scanner/text_sensor.py @@ -0,0 +1,22 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import text_sensor, esp32_ble_tracker +from esphome.const import CONF_ID + +DEPENDENCIES = ['esp32_ble_tracker'] + +ble_scanner_ns = cg.esphome_ns.namespace('ble_scanner') +BLEScanner = ble_scanner_ns.class_('BLEScanner', text_sensor.TextSensor, cg.Component, + esp32_ble_tracker.ESPBTDeviceListener) + +CONFIG_SCHEMA = cv.All(text_sensor.TEXT_SENSOR_SCHEMA.extend({ + cv.GenerateID(): cv.declare_id(BLEScanner), +}).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA).extend( + cv.COMPONENT_SCHEMA)) + + +def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + yield cg.register_component(var, config) + yield esp32_ble_tracker.register_ble_device(var, config) + yield text_sensor.register_text_sensor(var, config) diff --git a/tests/test2.yaml b/tests/test2.yaml index d02c093b86..6bc0ac93c3 100644 --- a/tests/test2.yaml +++ b/tests/test2.yaml @@ -100,62 +100,6 @@ sensor: name: "Xiaomi CGG1 Humidity" battery_level: name: "Xiaomi CGG1 Battery Level" - - platform: pmsx003 - type: PMSX003 - pm_1_0: - name: "PM 1.0 Concentration" - pm_2_5: - name: "PM 2.5 Concentration" - pm_10_0: - name: "PM 10.0 Concentration" - - platform: pmsx003 - type: PMS5003T - pm_2_5: - name: "PM 2.5 Concentration" - temperature: - name: "PMS Temperature" - humidity: - name: "PMS Humidity" - - platform: pmsx003 - type: PMS5003ST - pm_2_5: - name: "PM 2.5 Concentration" - temperature: - name: "PMS Temperature" - humidity: - name: "PMS Humidity" - formaldehyde: - name: "PMS Formaldehyde Concentration" - - platform: cse7766 - voltage: - name: "CSE7766 Voltage" - current: - name: "CSE7766 Current" - power: - name: "CSE776 Power" - - platform: apds9960 - type: proximity - name: APDS9960 Proximity - - platform: apds9960 - type: clear - name: APDS9960 Clear - - platform: apds9960 - type: red - name: APDS9960 Red - - platform: apds9960 - type: green - name: APDS9960 Green - - platform: apds9960 - type: blue - name: APDS9960 Blue - - platform: homeassistant - entity_id: sensor.hello_world - id: ha_hello_world - - platform: as3935 - lightning_energy: - name: "Lightning Energy" - distance: - name: "Distance Storm" - platform: ruuvitag mac_address: FF:56:D3:2F:7D:E8 humidity: @@ -178,6 +122,14 @@ sensor: name: "RuuviTag Movement Counter" measurement_sequence_number: name: "RuuviTag Measurement Sequence Number" + - platform: homeassistant + entity_id: sensor.hello_world + id: ha_hello_world + - platform: as3935 + lightning_energy: + name: "Lightning Energy" + distance: + name: "Distance Storm" time: - platform: homeassistant @@ -186,10 +138,6 @@ time: then: - logger.log: It's 16:00 -apds9960: - address: 0x20 - update_interval: 60s - esp32_touch: setup_mode: True @@ -210,18 +158,6 @@ binary_sensor: name: "ESP32 Touch Pad GPIO27" pin: GPIO27 threshold: 1000 - - platform: apds9960 - direction: up - name: APDS9960 Up - - platform: apds9960 - direction: down - name: APDS9960 Down - - platform: apds9960 - direction: left - name: APDS9960 Left - - platform: apds9960 - direction: right - name: APDS9960 Right - platform: homeassistant entity_id: binary_sensor.hello_world id: ha_hello_world_binary @@ -291,6 +227,8 @@ text_sensor: - platform: homeassistant entity_id: sensor.hello_world2 id: ha_hello_world2 + - platform: ble_scanner + name: Scanner script: - id: my_script diff --git a/tests/test3.yaml b/tests/test3.yaml index 2a37095aca..0d8be590cf 100644 --- a/tests/test3.yaml +++ b/tests/test3.yaml @@ -221,7 +221,7 @@ sensor: - platform: homeassistant entity_id: sensor.hello_world id: ha_hello_world - - platform: aht10 + - platform: aht10 temperature: name: "Temperature" humidity: @@ -360,6 +360,39 @@ sensor: name: "PM2.5" pm_10_0: name: "PM10.0" + - platform: pmsx003 + type: PMSX003 + pm_1_0: + name: "PM 1.0 Concentration" + pm_2_5: + name: "PM 2.5 Concentration" + pm_10_0: + name: "PM 10.0 Concentration" + - platform: pmsx003 + type: PMS5003T + pm_2_5: + name: "PM 2.5 Concentration" + temperature: + name: "PMS Temperature" + humidity: + name: "PMS Humidity" + - platform: pmsx003 + type: PMS5003ST + pm_2_5: + name: "PM 2.5 Concentration" + temperature: + name: "PMS Temperature" + humidity: + name: "PMS Humidity" + formaldehyde: + name: "PMS Formaldehyde Concentration" + - platform: cse7766 + voltage: + name: "CSE7766 Voltage" + current: + name: "CSE7766 Current" + power: + name: "CSE776 Power" time: - platform: homeassistant