mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	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 <glm.net@gmail.com>
This commit is contained in:
		
							
								
								
									
										0
									
								
								esphome/components/ble_scanner/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								esphome/components/ble_scanner/__init__.py
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										16
									
								
								esphome/components/ble_scanner/ble_scanner.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								esphome/components/ble_scanner/ble_scanner.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||||
							
								
								
									
										38
									
								
								esphome/components/ble_scanner/ble_scanner.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								esphome/components/ble_scanner/ble_scanner.h
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | |||||||
|  | #pragma once | ||||||
|  |  | ||||||
|  | #include <ctime> | ||||||
|  | #include <string> | ||||||
|  |  | ||||||
|  | #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 | ||||||
							
								
								
									
										22
									
								
								esphome/components/ble_scanner/text_sensor.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										22
									
								
								esphome/components/ble_scanner/text_sensor.py
									
									
									
									
									
										Normal file
									
								
							| @@ -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) | ||||||
| @@ -100,62 +100,6 @@ sensor: | |||||||
|       name: "Xiaomi CGG1 Humidity" |       name: "Xiaomi CGG1 Humidity" | ||||||
|     battery_level: |     battery_level: | ||||||
|       name: "Xiaomi CGG1 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 |   - platform: ruuvitag | ||||||
|     mac_address: FF:56:D3:2F:7D:E8 |     mac_address: FF:56:D3:2F:7D:E8 | ||||||
|     humidity: |     humidity: | ||||||
| @@ -178,6 +122,14 @@ sensor: | |||||||
|       name: "RuuviTag Movement Counter" |       name: "RuuviTag Movement Counter" | ||||||
|     measurement_sequence_number: |     measurement_sequence_number: | ||||||
|       name: "RuuviTag 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: | time: | ||||||
| - platform: homeassistant | - platform: homeassistant | ||||||
| @@ -186,10 +138,6 @@ time: | |||||||
|       then: |       then: | ||||||
|         - logger.log: It's 16:00 |         - logger.log: It's 16:00 | ||||||
|  |  | ||||||
| apds9960: |  | ||||||
|   address: 0x20 |  | ||||||
|   update_interval: 60s |  | ||||||
|  |  | ||||||
| esp32_touch: | esp32_touch: | ||||||
|   setup_mode: True |   setup_mode: True | ||||||
|  |  | ||||||
| @@ -210,18 +158,6 @@ binary_sensor: | |||||||
|     name: "ESP32 Touch Pad GPIO27" |     name: "ESP32 Touch Pad GPIO27" | ||||||
|     pin: GPIO27 |     pin: GPIO27 | ||||||
|     threshold: 1000 |     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 |   - platform: homeassistant | ||||||
|     entity_id: binary_sensor.hello_world |     entity_id: binary_sensor.hello_world | ||||||
|     id: ha_hello_world_binary |     id: ha_hello_world_binary | ||||||
| @@ -291,6 +227,8 @@ text_sensor: | |||||||
|   - platform: homeassistant |   - platform: homeassistant | ||||||
|     entity_id: sensor.hello_world2 |     entity_id: sensor.hello_world2 | ||||||
|     id: ha_hello_world2 |     id: ha_hello_world2 | ||||||
|  |   - platform: ble_scanner | ||||||
|  |     name: Scanner | ||||||
|  |  | ||||||
| script: | script: | ||||||
|   - id: my_script |   - id: my_script | ||||||
|   | |||||||
| @@ -360,6 +360,39 @@ sensor: | |||||||
|       name: "PM2.5" |       name: "PM2.5" | ||||||
|     pm_10_0: |     pm_10_0: | ||||||
|       name: "PM10.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: | time: | ||||||
| - platform: homeassistant | - platform: homeassistant | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user