Merge branch 'dev' into optolink

This commit is contained in:
j0ta29 2023-05-12 21:04:35 +02:00 committed by GitHub
commit 746dea4945
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 113 additions and 72 deletions

View File

@ -4,7 +4,6 @@ from esphome.components import i2c
from esphome.const import CONF_ID from esphome.const import CONF_ID
DEPENDENCIES = ["i2c"] DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["sensor", "binary_sensor"]
MULTI_CONF = True MULTI_CONF = True
CONF_APDS9960_ID = "apds9960_id" CONF_APDS9960_ID = "apds9960_id"

View File

@ -116,8 +116,12 @@ void APDS9960::setup() {
APDS9960_WRITE_BYTE(0x80, val); APDS9960_WRITE_BYTE(0x80, val);
} }
bool APDS9960::is_color_enabled_() const { bool APDS9960::is_color_enabled_() const {
return this->red_channel_ != nullptr || this->green_channel_ != nullptr || this->blue_channel_ != nullptr || #ifdef USE_SENSOR
this->clear_channel_ != nullptr; 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() { void APDS9960::dump_config() {
@ -125,6 +129,15 @@ void APDS9960::dump_config() {
LOG_I2C_DEVICE(this); LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(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()) { if (this->is_failed()) {
switch (this->error_code_) { switch (this->error_code_) {
case COMMUNICATION_FAILED: 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; 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); 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) #ifdef USE_SENSOR
this->clear_channel_->publish_state(clear_perc); if (this->clear_sensor_ != nullptr)
if (this->red_channel_ != nullptr) this->clear_sensor_->publish_state(clear_perc);
this->red_channel_->publish_state(red_perc); if (this->red_sensor_ != nullptr)
if (this->green_channel_ != nullptr) this->red_sensor_->publish_state(red_perc);
this->green_channel_->publish_state(green_perc); if (this->green_sensor_ != nullptr)
if (this->blue_channel_ != nullptr) this->green_sensor_->publish_state(green_perc);
this->blue_channel_->publish_state(blue_perc); if (this->blue_sensor_ != nullptr)
this->blue_sensor_->publish_state(blue_perc);
#endif
} }
void APDS9960::read_proximity_data_(uint8_t status) { void APDS9960::read_proximity_data_(uint8_t status) {
if (this->proximity_ == nullptr) #ifndef USE_SENSOR
return;
#else
if (this->proximity_sensor_ == nullptr)
return; return;
if ((status & 0b10) == 0x00) { 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; float prox_perc = (prox / float(UINT8_MAX)) * 100.0f;
ESP_LOGD(TAG, "Got proximity=%.1f%%", prox_perc); 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_() { void APDS9960::read_gesture_data_() {
if (!this->is_gesture_enabled_()) if (!this->is_gesture_enabled_())
@ -256,28 +275,29 @@ void APDS9960::read_gesture_data_() {
} }
} }
void APDS9960::report_gesture_(int gesture) { void APDS9960::report_gesture_(int gesture) {
#ifdef USE_BINARY_SENSOR
binary_sensor::BinarySensor *bin; binary_sensor::BinarySensor *bin;
switch (gesture) { switch (gesture) {
case 1: case 1:
bin = this->up_direction_; bin = this->up_direction_binary_sensor_;
this->gesture_up_started_ = false; this->gesture_up_started_ = false;
this->gesture_down_started_ = false; this->gesture_down_started_ = false;
ESP_LOGD(TAG, "Got gesture UP"); ESP_LOGD(TAG, "Got gesture UP");
break; break;
case 2: case 2:
bin = this->down_direction_; bin = this->down_direction_binary_sensor_;
this->gesture_up_started_ = false; this->gesture_up_started_ = false;
this->gesture_down_started_ = false; this->gesture_down_started_ = false;
ESP_LOGD(TAG, "Got gesture DOWN"); ESP_LOGD(TAG, "Got gesture DOWN");
break; break;
case 3: case 3:
bin = this->left_direction_; bin = this->left_direction_binary_sensor_;
this->gesture_left_started_ = false; this->gesture_left_started_ = false;
this->gesture_right_started_ = false; this->gesture_right_started_ = false;
ESP_LOGD(TAG, "Got gesture LEFT"); ESP_LOGD(TAG, "Got gesture LEFT");
break; break;
case 4: case 4:
bin = this->right_direction_; bin = this->right_direction_binary_sensor_;
this->gesture_left_started_ = false; this->gesture_left_started_ = false;
this->gesture_right_started_ = false; this->gesture_right_started_ = false;
ESP_LOGD(TAG, "Got gesture RIGHT"); ESP_LOGD(TAG, "Got gesture RIGHT");
@ -290,6 +310,7 @@ void APDS9960::report_gesture_(int gesture) {
bin->publish_state(true); bin->publish_state(true);
bin->publish_state(false); bin->publish_state(false);
} }
#endif
} }
void APDS9960::process_dataset_(int up, int down, int left, int right) { void APDS9960::process_dataset_(int up, int down, int left, int right) {
/* Algorithm: (see Figure 11 in datasheet) /* 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; } 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 { bool APDS9960::is_gesture_enabled_() const {
return this->up_direction_ != nullptr || this->left_direction_ != nullptr || this->down_direction_ != nullptr || #ifdef USE_BINARY_SENSOR
this->right_direction_ != nullptr; 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 } // namespace apds9960

View File

@ -1,14 +1,34 @@
#pragma once #pragma once
#include "esphome/core/component.h"
#include "esphome/components/i2c/i2c.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" #include "esphome/components/sensor/sensor.h"
#endif
#ifdef USE_BINARY_SENSOR
#include "esphome/components/binary_sensor/binary_sensor.h" #include "esphome/components/binary_sensor/binary_sensor.h"
#endif
namespace esphome { namespace esphome {
namespace apds9960 { namespace apds9960 {
class APDS9960 : public PollingComponent, public i2c::I2CDevice { 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: public:
void setup() override; void setup() override;
void dump_config() 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_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_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: protected:
bool is_color_enabled_() const; bool is_color_enabled_() const;
bool is_proximity_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_gain_;
uint8_t gesture_wait_time_; 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 { enum ErrorCode {
NONE = 0, NONE = 0,
COMMUNICATION_FAILED, COMMUNICATION_FAILED,

View File

@ -6,19 +6,14 @@ from . import APDS9960, CONF_APDS9960_ID
DEPENDENCIES = ["apds9960"] DEPENDENCIES = ["apds9960"]
DIRECTIONS = { DIRECTIONS = ["up", "down", "left", "right"]
"UP": "set_up_direction",
"DOWN": "set_down_direction",
"LEFT": "set_left_direction",
"RIGHT": "set_right_direction",
}
CONFIG_SCHEMA = binary_sensor.binary_sensor_schema( CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_MOVING device_class=DEVICE_CLASS_MOVING
).extend( ).extend(
{ {
cv.GenerateID(CONF_APDS9960_ID): cv.use_id(APDS9960), 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): async def to_code(config):
hub = await cg.get_variable(config[CONF_APDS9960_ID]) hub = await cg.get_variable(config[CONF_APDS9960_ID])
var = await binary_sensor.new_binary_sensor(config) 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)) cg.add(func(var))

View File

@ -11,13 +11,7 @@ from . import APDS9960, CONF_APDS9960_ID
DEPENDENCIES = ["apds9960"] DEPENDENCIES = ["apds9960"]
TYPES = { TYPES = ["clear", "red", "green", "blue", "proximity"]
"CLEAR": "set_clear_channel",
"RED": "set_red_channel",
"GREEN": "set_green_channel",
"BLUE": "set_blue_channel",
"PROXIMITY": "set_proximity",
}
CONFIG_SCHEMA = sensor.sensor_schema( CONFIG_SCHEMA = sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT, unit_of_measurement=UNIT_PERCENT,
@ -26,7 +20,7 @@ CONFIG_SCHEMA = sensor.sensor_schema(
state_class=STATE_CLASS_MEASUREMENT, state_class=STATE_CLASS_MEASUREMENT,
).extend( ).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), cv.GenerateID(CONF_APDS9960_ID): cv.use_id(APDS9960),
} }
) )
@ -35,5 +29,5 @@ CONFIG_SCHEMA = sensor.sensor_schema(
async def to_code(config): async def to_code(config):
hub = await cg.get_variable(config[CONF_APDS9960_ID]) hub = await cg.get_variable(config[CONF_APDS9960_ID])
var = await sensor.new_sensor(config) 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)) cg.add(func(var))

View File

@ -7,6 +7,7 @@ from esphome.const import (
CONF_IBEACON_MAJOR, CONF_IBEACON_MAJOR,
CONF_IBEACON_MINOR, CONF_IBEACON_MINOR,
CONF_IBEACON_UUID, CONF_IBEACON_UUID,
CONF_MIN_RSSI,
) )
DEPENDENCIES = ["esp32_ble_tracker"] DEPENDENCIES = ["esp32_ble_tracker"]
@ -37,6 +38,9 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_IBEACON_MAJOR): cv.uint16_t, cv.Optional(CONF_IBEACON_MAJOR): cv.uint16_t,
cv.Optional(CONF_IBEACON_MINOR): cv.uint16_t, cv.Optional(CONF_IBEACON_MINOR): cv.uint16_t,
cv.Optional(CONF_IBEACON_UUID): cv.uuid, 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) .extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
@ -51,6 +55,9 @@ async def to_code(config):
await cg.register_component(var, config) await cg.register_component(var, config)
await esp32_ble_tracker.register_ble_device(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: if CONF_MAC_ADDRESS in config:
cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex)) cg.add(var.set_address(config[CONF_MAC_ADDRESS].as_hex))

View File

@ -41,12 +41,19 @@ class BLEPresenceDevice : public binary_sensor::BinarySensorInitiallyOff,
this->check_ibeacon_minor_ = true; this->check_ibeacon_minor_ = true;
this->ibeacon_minor_ = minor; this->ibeacon_minor_ = minor;
} }
void set_minimum_rssi(int rssi) {
this->check_minimum_rssi_ = true;
this->minimum_rssi_ = rssi;
}
void on_scan_end() override { void on_scan_end() override {
if (!this->found_) if (!this->found_)
this->publish_state(false); this->publish_state(false);
this->found_ = false; this->found_ = false;
} }
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override { 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_) { switch (this->match_by_) {
case MATCH_BY_MAC_ADDRESS: case MATCH_BY_MAC_ADDRESS:
if (device.address_uint64() == this->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 }; enum MatchType { MATCH_BY_MAC_ADDRESS, MATCH_BY_SERVICE_UUID, MATCH_BY_IBEACON_UUID };
MatchType match_by_; MatchType match_by_;
bool found_{false};
uint64_t address_; uint64_t address_;
esp32_ble_tracker::ESPBTUUID uuid_; esp32_ble_tracker::ESPBTUUID uuid_;
esp32_ble_tracker::ESPBTUUID ibeacon_uuid_; esp32_ble_tracker::ESPBTUUID ibeacon_uuid_;
uint16_t ibeacon_major_; uint16_t ibeacon_major_{0};
bool check_ibeacon_major_; uint16_t ibeacon_minor_{0};
uint16_t ibeacon_minor_;
bool check_ibeacon_minor_; 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 } // namespace ble_presence

View File

@ -102,8 +102,9 @@ class BLERSSISensor : public sensor::Sensor, public esp32_ble_tracker::ESPBTDevi
esp32_ble_tracker::ESPBTUUID ibeacon_uuid_; esp32_ble_tracker::ESPBTUUID ibeacon_uuid_;
uint16_t ibeacon_major_; uint16_t ibeacon_major_;
bool check_ibeacon_major_;
uint16_t ibeacon_minor_; uint16_t ibeacon_minor_;
bool check_ibeacon_major_;
bool check_ibeacon_minor_; bool check_ibeacon_minor_;
}; };

View File

@ -252,7 +252,7 @@ void SEN5XComponent::dump_config() {
ESP_LOGCONFIG(TAG, " Firmware version: %d", this->firmware_version_); 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]); ESP_LOGCONFIG(TAG, " Serial number %02d.%02d.%02d", serial_number_[0], serial_number_[1], serial_number_[2]);
if (this->auto_cleaning_interval_.has_value()) { 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()) { if (this->acceleration_mode_.has_value()) {
switch (this->acceleration_mode_.value()) { switch (this->acceleration_mode_.value()) {

View File

@ -275,7 +275,7 @@ SPRINKLER_ACTION_SET_RUN_DURATION_SCHEMA = cv.Schema(
SPRINKLER_ACTION_QUEUE_VALVE_SCHEMA = cv.Schema( SPRINKLER_ACTION_QUEUE_VALVE_SCHEMA = cv.Schema(
{ {
cv.Required(CONF_ID): cv.use_id(Sprinkler), 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.positive_time_period_seconds
), ),
cv.Required(CONF_VALVE_NUMBER): cv.templatable(cv.positive_int), cv.Required(CONF_VALVE_NUMBER): cv.templatable(cv.positive_int),

View File

@ -767,11 +767,10 @@ bool WiFiComponent::wifi_ap_ip_config_(optional<ManualIP> manual_ip) {
info.gw.addr = static_cast<uint32_t>(network::IPAddress(192, 168, 4, 1)); info.gw.addr = static_cast<uint32_t>(network::IPAddress(192, 168, 4, 1));
info.netmask.addr = static_cast<uint32_t>(network::IPAddress(255, 255, 255, 0)); info.netmask.addr = static_cast<uint32_t>(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_dhcpc_stop(s_sta_netif);
err = esp_netif_dhcps_stop(s_sta_netif); if (err != ESP_OK && err != ESP_ERR_ESP_NETIF_DHCP_ALREADY_STOPPED) {
if (err != ESP_OK) { ESP_LOGV(TAG, "esp_netif_dhcpc_stop failed: %s", esp_err_to_name(err));
ESP_LOGV(TAG, "esp_netif_dhcps_stop failed! %d", err);
return false; return false;
} }

View File

@ -1,6 +1,6 @@
"""Constants used by esphome.""" """Constants used by esphome."""
__version__ = "2023.5.0-dev" __version__ = "2023.6.0-dev"
ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_" ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_"
@ -408,6 +408,7 @@ CONF_MIN_LENGTH = "min_length"
CONF_MIN_LEVEL = "min_level" CONF_MIN_LEVEL = "min_level"
CONF_MIN_POWER = "min_power" CONF_MIN_POWER = "min_power"
CONF_MIN_RANGE = "min_range" CONF_MIN_RANGE = "min_range"
CONF_MIN_RSSI = "min_rssi"
CONF_MIN_TEMPERATURE = "min_temperature" CONF_MIN_TEMPERATURE = "min_temperature"
CONF_MIN_VALUE = "min_value" CONF_MIN_VALUE = "min_value"
CONF_MIN_VERSION = "min_version" CONF_MIN_VERSION = "min_version"