diff --git a/esphome/components/apds9960/__init__.py b/esphome/components/apds9960/__init__.py index 37dc4c0b28..06b3c85aee 100644 --- a/esphome/components/apds9960/__init__.py +++ b/esphome/components/apds9960/__init__.py @@ -4,7 +4,6 @@ from esphome.components import i2c from esphome.const import CONF_ID DEPENDENCIES = ["i2c"] -AUTO_LOAD = ["sensor", "binary_sensor"] MULTI_CONF = True CONF_APDS9960_ID = "apds9960_id" diff --git a/esphome/components/apds9960/apds9960.cpp b/esphome/components/apds9960/apds9960.cpp index 1c6ec5c14a..d91378afee 100644 --- a/esphome/components/apds9960/apds9960.cpp +++ b/esphome/components/apds9960/apds9960.cpp @@ -116,8 +116,12 @@ void APDS9960::setup() { APDS9960_WRITE_BYTE(0x80, val); } bool APDS9960::is_color_enabled_() const { - return this->red_channel_ != nullptr || this->green_channel_ != nullptr || this->blue_channel_ != nullptr || - this->clear_channel_ != nullptr; +#ifdef USE_SENSOR + return this->red_sensor_ != nullptr || this->green_sensor_ != nullptr || this->blue_sensor_ != nullptr || + this->clear_sensor_ != nullptr; +#else + return false; +#endif } void APDS9960::dump_config() { @@ -125,6 +129,15 @@ void APDS9960::dump_config() { LOG_I2C_DEVICE(this); LOG_UPDATE_INTERVAL(this); + +#ifdef USE_SENSOR + LOG_SENSOR(" ", "Red channel", this->red_sensor_); + LOG_SENSOR(" ", "Green channel", this->green_sensor_); + LOG_SENSOR(" ", "Blue channel", this->blue_sensor_); + LOG_SENSOR(" ", "Clear channel", this->clear_sensor_); + LOG_SENSOR(" ", "Proximity", this->proximity_sensor_); +#endif + if (this->is_failed()) { switch (this->error_code_) { case COMMUNICATION_FAILED: @@ -181,17 +194,22 @@ void APDS9960::read_color_data_(uint8_t status) { float blue_perc = (uint_blue / float(UINT16_MAX)) * 100.0f; ESP_LOGD(TAG, "Got clear=%.1f%% red=%.1f%% green=%.1f%% blue=%.1f%%", clear_perc, red_perc, green_perc, blue_perc); - if (this->clear_channel_ != nullptr) - this->clear_channel_->publish_state(clear_perc); - if (this->red_channel_ != nullptr) - this->red_channel_->publish_state(red_perc); - if (this->green_channel_ != nullptr) - this->green_channel_->publish_state(green_perc); - if (this->blue_channel_ != nullptr) - this->blue_channel_->publish_state(blue_perc); +#ifdef USE_SENSOR + if (this->clear_sensor_ != nullptr) + this->clear_sensor_->publish_state(clear_perc); + if (this->red_sensor_ != nullptr) + this->red_sensor_->publish_state(red_perc); + if (this->green_sensor_ != nullptr) + this->green_sensor_->publish_state(green_perc); + if (this->blue_sensor_ != nullptr) + this->blue_sensor_->publish_state(blue_perc); +#endif } void APDS9960::read_proximity_data_(uint8_t status) { - if (this->proximity_ == nullptr) +#ifndef USE_SENSOR + return; +#else + if (this->proximity_sensor_ == nullptr) return; if ((status & 0b10) == 0x00) { @@ -204,7 +222,8 @@ void APDS9960::read_proximity_data_(uint8_t status) { float prox_perc = (prox / float(UINT8_MAX)) * 100.0f; ESP_LOGD(TAG, "Got proximity=%.1f%%", prox_perc); - this->proximity_->publish_state(prox_perc); + this->proximity_sensor_->publish_state(prox_perc); +#endif } void APDS9960::read_gesture_data_() { if (!this->is_gesture_enabled_()) @@ -256,28 +275,29 @@ void APDS9960::read_gesture_data_() { } } void APDS9960::report_gesture_(int gesture) { +#ifdef USE_BINARY_SENSOR binary_sensor::BinarySensor *bin; switch (gesture) { case 1: - bin = this->up_direction_; + bin = this->up_direction_binary_sensor_; this->gesture_up_started_ = false; this->gesture_down_started_ = false; ESP_LOGD(TAG, "Got gesture UP"); break; case 2: - bin = this->down_direction_; + bin = this->down_direction_binary_sensor_; this->gesture_up_started_ = false; this->gesture_down_started_ = false; ESP_LOGD(TAG, "Got gesture DOWN"); break; case 3: - bin = this->left_direction_; + bin = this->left_direction_binary_sensor_; this->gesture_left_started_ = false; this->gesture_right_started_ = false; ESP_LOGD(TAG, "Got gesture LEFT"); break; case 4: - bin = this->right_direction_; + bin = this->right_direction_binary_sensor_; this->gesture_left_started_ = false; this->gesture_right_started_ = false; ESP_LOGD(TAG, "Got gesture RIGHT"); @@ -290,6 +310,7 @@ void APDS9960::report_gesture_(int gesture) { bin->publish_state(true); bin->publish_state(false); } +#endif } void APDS9960::process_dataset_(int up, int down, int left, int right) { /* Algorithm: (see Figure 11 in datasheet) @@ -365,10 +386,22 @@ void APDS9960::process_dataset_(int up, int down, int left, int right) { } } float APDS9960::get_setup_priority() const { return setup_priority::DATA; } -bool APDS9960::is_proximity_enabled_() const { return this->proximity_ != nullptr || this->is_gesture_enabled_(); } +bool APDS9960::is_proximity_enabled_() const { + return +#ifdef USE_SENSOR + this->proximity_sensor_ != nullptr +#else + false +#endif + || this->is_gesture_enabled_(); +} bool APDS9960::is_gesture_enabled_() const { - return this->up_direction_ != nullptr || this->left_direction_ != nullptr || this->down_direction_ != nullptr || - this->right_direction_ != nullptr; +#ifdef USE_BINARY_SENSOR + return this->up_direction_binary_sensor_ != nullptr || this->left_direction_binary_sensor_ != nullptr || + this->down_direction_binary_sensor_ != nullptr || this->right_direction_binary_sensor_ != nullptr; +#else + return false; +#endif } } // namespace apds9960 diff --git a/esphome/components/apds9960/apds9960.h b/esphome/components/apds9960/apds9960.h index 23d9835640..2a0fbb5c19 100644 --- a/esphome/components/apds9960/apds9960.h +++ b/esphome/components/apds9960/apds9960.h @@ -1,14 +1,34 @@ #pragma once -#include "esphome/core/component.h" #include "esphome/components/i2c/i2c.h" +#include "esphome/core/component.h" +#include "esphome/core/defines.h" +#ifdef USE_SENSOR #include "esphome/components/sensor/sensor.h" +#endif +#ifdef USE_BINARY_SENSOR #include "esphome/components/binary_sensor/binary_sensor.h" +#endif namespace esphome { namespace apds9960 { class APDS9960 : public PollingComponent, public i2c::I2CDevice { +#ifdef USE_SENSOR + SUB_SENSOR(red) + SUB_SENSOR(green) + SUB_SENSOR(blue) + SUB_SENSOR(clear) + SUB_SENSOR(proximity) +#endif + +#ifdef USE_BINARY_SENSOR + SUB_BINARY_SENSOR(up_direction) + SUB_BINARY_SENSOR(right_direction) + SUB_BINARY_SENSOR(down_direction) + SUB_BINARY_SENSOR(left_direction) +#endif + public: void setup() override; void dump_config() override; @@ -23,16 +43,6 @@ class APDS9960 : public PollingComponent, public i2c::I2CDevice { void set_gesture_gain(uint8_t gain) { this->gesture_gain_ = gain; } void set_gesture_wait_time(uint8_t wait_time) { this->gesture_wait_time_ = wait_time; } - void set_red_channel(sensor::Sensor *red_channel) { red_channel_ = red_channel; } - void set_green_channel(sensor::Sensor *green_channel) { green_channel_ = green_channel; } - void set_blue_channel(sensor::Sensor *blue_channel) { blue_channel_ = blue_channel; } - void set_clear_channel(sensor::Sensor *clear_channel) { clear_channel_ = clear_channel; } - void set_up_direction(binary_sensor::BinarySensor *up_direction) { up_direction_ = up_direction; } - void set_right_direction(binary_sensor::BinarySensor *right_direction) { right_direction_ = right_direction; } - void set_down_direction(binary_sensor::BinarySensor *down_direction) { down_direction_ = down_direction; } - void set_left_direction(binary_sensor::BinarySensor *left_direction) { left_direction_ = left_direction; } - void set_proximity(sensor::Sensor *proximity) { proximity_ = proximity; } - protected: bool is_color_enabled_() const; bool is_proximity_enabled_() const; @@ -50,15 +60,6 @@ class APDS9960 : public PollingComponent, public i2c::I2CDevice { uint8_t gesture_gain_; uint8_t gesture_wait_time_; - sensor::Sensor *red_channel_{nullptr}; - sensor::Sensor *green_channel_{nullptr}; - sensor::Sensor *blue_channel_{nullptr}; - sensor::Sensor *clear_channel_{nullptr}; - binary_sensor::BinarySensor *up_direction_{nullptr}; - binary_sensor::BinarySensor *right_direction_{nullptr}; - binary_sensor::BinarySensor *down_direction_{nullptr}; - binary_sensor::BinarySensor *left_direction_{nullptr}; - sensor::Sensor *proximity_{nullptr}; enum ErrorCode { NONE = 0, COMMUNICATION_FAILED, diff --git a/esphome/components/apds9960/binary_sensor.py b/esphome/components/apds9960/binary_sensor.py index 04dc6f4d5d..46b08d8d69 100644 --- a/esphome/components/apds9960/binary_sensor.py +++ b/esphome/components/apds9960/binary_sensor.py @@ -6,19 +6,14 @@ from . import APDS9960, CONF_APDS9960_ID DEPENDENCIES = ["apds9960"] -DIRECTIONS = { - "UP": "set_up_direction", - "DOWN": "set_down_direction", - "LEFT": "set_left_direction", - "RIGHT": "set_right_direction", -} +DIRECTIONS = ["up", "down", "left", "right"] CONFIG_SCHEMA = binary_sensor.binary_sensor_schema( device_class=DEVICE_CLASS_MOVING ).extend( { cv.GenerateID(CONF_APDS9960_ID): cv.use_id(APDS9960), - cv.Required(CONF_DIRECTION): cv.one_of(*DIRECTIONS, upper=True), + cv.Required(CONF_DIRECTION): cv.one_of(*DIRECTIONS, lower=True), } ) @@ -26,5 +21,5 @@ CONFIG_SCHEMA = binary_sensor.binary_sensor_schema( async def to_code(config): hub = await cg.get_variable(config[CONF_APDS9960_ID]) var = await binary_sensor.new_binary_sensor(config) - func = getattr(hub, DIRECTIONS[config[CONF_DIRECTION]]) + func = getattr(hub, f"set_{config[CONF_DIRECTION]}_direction_binary_sensor") cg.add(func(var)) diff --git a/esphome/components/apds9960/sensor.py b/esphome/components/apds9960/sensor.py index e1990ec26e..c9865f8687 100644 --- a/esphome/components/apds9960/sensor.py +++ b/esphome/components/apds9960/sensor.py @@ -11,13 +11,7 @@ from . import APDS9960, CONF_APDS9960_ID DEPENDENCIES = ["apds9960"] -TYPES = { - "CLEAR": "set_clear_channel", - "RED": "set_red_channel", - "GREEN": "set_green_channel", - "BLUE": "set_blue_channel", - "PROXIMITY": "set_proximity", -} +TYPES = ["clear", "red", "green", "blue", "proximity"] CONFIG_SCHEMA = sensor.sensor_schema( unit_of_measurement=UNIT_PERCENT, @@ -26,7 +20,7 @@ CONFIG_SCHEMA = sensor.sensor_schema( state_class=STATE_CLASS_MEASUREMENT, ).extend( { - cv.Required(CONF_TYPE): cv.one_of(*TYPES, upper=True), + cv.Required(CONF_TYPE): cv.one_of(*TYPES, lower=True), cv.GenerateID(CONF_APDS9960_ID): cv.use_id(APDS9960), } ) @@ -35,5 +29,5 @@ CONFIG_SCHEMA = sensor.sensor_schema( async def to_code(config): hub = await cg.get_variable(config[CONF_APDS9960_ID]) var = await sensor.new_sensor(config) - func = getattr(hub, TYPES[config[CONF_TYPE]]) + func = getattr(hub, f"set_{config[CONF_TYPE]}_sensor") cg.add(func(var)) diff --git a/esphome/components/ble_presence/binary_sensor.py b/esphome/components/ble_presence/binary_sensor.py index 67f2c3516f..d54b7678e1 100644 --- a/esphome/components/ble_presence/binary_sensor.py +++ b/esphome/components/ble_presence/binary_sensor.py @@ -7,6 +7,7 @@ from esphome.const import ( CONF_IBEACON_MAJOR, CONF_IBEACON_MINOR, CONF_IBEACON_UUID, + CONF_MIN_RSSI, ) DEPENDENCIES = ["esp32_ble_tracker"] @@ -37,6 +38,9 @@ CONFIG_SCHEMA = cv.All( cv.Optional(CONF_IBEACON_MAJOR): cv.uint16_t, cv.Optional(CONF_IBEACON_MINOR): cv.uint16_t, cv.Optional(CONF_IBEACON_UUID): cv.uuid, + cv.Optional(CONF_MIN_RSSI): cv.All( + cv.decibel, cv.int_range(min=-90, max=-30) + ), } ) .extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA) @@ -51,6 +55,9 @@ async def to_code(config): await cg.register_component(var, config) await esp32_ble_tracker.register_ble_device(var, config) + if CONF_MIN_RSSI in config: + cg.add(var.set_minimum_rssi(config[CONF_MIN_RSSI])) + if CONF_MAC_ADDRESS in config: cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) diff --git a/esphome/components/ble_presence/ble_presence_device.h b/esphome/components/ble_presence/ble_presence_device.h index 1689c9ba3f..953ea460a8 100644 --- a/esphome/components/ble_presence/ble_presence_device.h +++ b/esphome/components/ble_presence/ble_presence_device.h @@ -41,12 +41,19 @@ class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff, this->check_ibeacon_minor_ = true; this->ibeacon_minor_ = minor; } + void set_minimum_rssi(int rssi) { + this->check_minimum_rssi_ = true; + this->minimum_rssi_ = rssi; + } void on_scan_end() override { if (!this->found_) this->publish_state(false); this->found_ = false; } bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override { + if (this->check_minimum_rssi_ && this->minimum_rssi_ <= device.get_rssi()) { + return false; + } switch (this->match_by_) { case MATCH_BY_MAC_ADDRESS: if (device.address_uint64() == this->address_) { @@ -96,17 +103,21 @@ class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff, enum MatchType { MATCH_BY_MAC_ADDRESS, MATCH_BY_SERVICE_UUID, MATCH_BY_IBEACON_UUID }; MatchType match_by_; - bool found_{false}; - uint64_t address_; esp32_ble_tracker::ESPBTUUID uuid_; esp32_ble_tracker::ESPBTUUID ibeacon_uuid_; - uint16_t ibeacon_major_; - bool check_ibeacon_major_; - uint16_t ibeacon_minor_; - bool check_ibeacon_minor_; + uint16_t ibeacon_major_{0}; + uint16_t ibeacon_minor_{0}; + + int minimum_rssi_{0}; + + bool check_ibeacon_major_{false}; + bool check_ibeacon_minor_{false}; + bool check_minimum_rssi_{false}; + + bool found_{false}; }; } // namespace ble_presence diff --git a/esphome/components/ble_rssi/ble_rssi_sensor.h b/esphome/components/ble_rssi/ble_rssi_sensor.h index 0cb511de3b..79aebce7d3 100644 --- a/esphome/components/ble_rssi/ble_rssi_sensor.h +++ b/esphome/components/ble_rssi/ble_rssi_sensor.h @@ -102,8 +102,9 @@ class BLERSSISensor : public sensor::Sensor, public esp32_ble_tracker::ESPBTDevi esp32_ble_tracker::ESPBTUUID ibeacon_uuid_; uint16_t ibeacon_major_; - bool check_ibeacon_major_; uint16_t ibeacon_minor_; + + bool check_ibeacon_major_; bool check_ibeacon_minor_; }; diff --git a/esphome/components/sen5x/sen5x.cpp b/esphome/components/sen5x/sen5x.cpp index 865fae373b..ddce568c97 100644 --- a/esphome/components/sen5x/sen5x.cpp +++ b/esphome/components/sen5x/sen5x.cpp @@ -252,7 +252,7 @@ void SEN5XComponent::dump_config() { ESP_LOGCONFIG(TAG, " Firmware version: %d", this->firmware_version_); ESP_LOGCONFIG(TAG, " Serial number %02d.%02d.%02d", serial_number_[0], serial_number_[1], serial_number_[2]); if (this->auto_cleaning_interval_.has_value()) { - ESP_LOGCONFIG(TAG, " Auto auto cleaning interval %d seconds", auto_cleaning_interval_.value()); + ESP_LOGCONFIG(TAG, " Auto cleaning interval %d seconds", auto_cleaning_interval_.value()); } if (this->acceleration_mode_.has_value()) { switch (this->acceleration_mode_.value()) { diff --git a/esphome/components/sprinkler/__init__.py b/esphome/components/sprinkler/__init__.py index 6aa76dcd2f..5097abc7e7 100644 --- a/esphome/components/sprinkler/__init__.py +++ b/esphome/components/sprinkler/__init__.py @@ -275,7 +275,7 @@ SPRINKLER_ACTION_SET_RUN_DURATION_SCHEMA = cv.Schema( SPRINKLER_ACTION_QUEUE_VALVE_SCHEMA = cv.Schema( { cv.Required(CONF_ID): cv.use_id(Sprinkler), - cv.Optional(CONF_RUN_DURATION, default=0): cv.templatable( + cv.Optional(CONF_RUN_DURATION, default="0s"): cv.templatable( cv.positive_time_period_seconds ), cv.Required(CONF_VALVE_NUMBER): cv.templatable(cv.positive_int), diff --git a/esphome/components/wifi/wifi_component_esp_idf.cpp b/esphome/components/wifi/wifi_component_esp_idf.cpp index 1c70f33040..e18d3cc043 100644 --- a/esphome/components/wifi/wifi_component_esp_idf.cpp +++ b/esphome/components/wifi/wifi_component_esp_idf.cpp @@ -767,11 +767,10 @@ bool WiFiComponent::wifi_ap_ip_config_(optional manual_ip) { info.gw.addr = static_cast(network::IPAddress(192, 168, 4, 1)); info.netmask.addr = static_cast(network::IPAddress(255, 255, 255, 0)); } - esp_netif_dhcp_status_t dhcp_status; - esp_netif_dhcps_get_status(s_sta_netif, &dhcp_status); - err = esp_netif_dhcps_stop(s_sta_netif); - if (err != ESP_OK) { - ESP_LOGV(TAG, "esp_netif_dhcps_stop failed! %d", err); + + err = esp_netif_dhcpc_stop(s_sta_netif); + if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) { + ESP_LOGV(TAG, "esp_netif_dhcpc_stop failed: %s", esp_err_to_name(err)); return false; } diff --git a/esphome/const.py b/esphome/const.py index 46e5545078..b9d8ee81e8 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -1,6 +1,6 @@ """Constants used by esphome.""" -__version__ = "2023.5.0-dev" +__version__ = "2023.6.0-dev" ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_" @@ -408,6 +408,7 @@ CONF_MIN_LENGTH = "min_length" CONF_MIN_LEVEL = "min_level" CONF_MIN_POWER = "min_power" CONF_MIN_RANGE = "min_range" +CONF_MIN_RSSI = "min_rssi" CONF_MIN_TEMPERATURE = "min_temperature" CONF_MIN_VALUE = "min_value" CONF_MIN_VERSION = "min_version"