Compare commits

..

3 Commits

Author SHA1 Message Date
Jesse Hills
a85b7b3f84 Code tidy 2021-12-22 15:58:02 +13:00
Jesse Hills
a207ed08a9 Merge branch 'dev' into pr/ciB89/1424-1 2021-12-22 15:49:07 +13:00
Ian
90c3cb62b3 Add tracker for OralB toothbrushes
The OralB toothbrushes expose some of their information in their bluetooth advertisement data.

This data lets us see the state (idle, running), brush mode (daily clean, tongue, whitening, etc.), pressure and some other bits of data.

This component lets you expose that data with config as follows:

```
esp32_ble_tracker:

sensor:
  - platform: oralb_brush
    mac_address: 00:00:00:00:00:00
    state:
      name: "Toothbrush State"
```

Checkout https://github.com/zewelor/bt-mqtt-gateway/blob/master/workers/toothbrush_homeassistant.py and https://esphome.io/components/esp32_ble_tracker.html for more information.
2020-05-03 15:23:21 +01:00
211 changed files with 1421 additions and 5674 deletions

View File

@@ -51,26 +51,26 @@ jobs:
name: Run script/clang-format
- id: clang-tidy
name: Run script/clang-tidy for ESP8266
options: --environment esp8266-arduino-tidy --grep USE_ESP8266
options: --environment esp8266-tidy --grep USE_ESP8266
pio_cache_key: tidyesp8266
- id: clang-tidy
name: Run script/clang-tidy for ESP32 Arduino 1/4
options: --environment esp32-arduino-tidy --split-num 4 --split-at 1
name: Run script/clang-tidy for ESP32 1/4
options: --environment esp32-tidy --split-num 4 --split-at 1
pio_cache_key: tidyesp32
- id: clang-tidy
name: Run script/clang-tidy for ESP32 Arduino 2/4
options: --environment esp32-arduino-tidy --split-num 4 --split-at 2
name: Run script/clang-tidy for ESP32 2/4
options: --environment esp32-tidy --split-num 4 --split-at 2
pio_cache_key: tidyesp32
- id: clang-tidy
name: Run script/clang-tidy for ESP32 Arduino 3/4
options: --environment esp32-arduino-tidy --split-num 4 --split-at 3
name: Run script/clang-tidy for ESP32 3/4
options: --environment esp32-tidy --split-num 4 --split-at 3
pio_cache_key: tidyesp32
- id: clang-tidy
name: Run script/clang-tidy for ESP32 Arduino 4/4
options: --environment esp32-arduino-tidy --split-num 4 --split-at 4
name: Run script/clang-tidy for ESP32 4/4
options: --environment esp32-tidy --split-num 4 --split-at 4
pio_cache_key: tidyesp32
- id: clang-tidy
name: Run script/clang-tidy for ESP32 IDF
name: Run script/clang-tidy for ESP32 esp-idf
options: --environment esp32-idf-tidy --grep USE_ESP_IDF
pio_cache_key: tidyesp32-idf

View File

@@ -3,4 +3,4 @@ ports:
onOpen: open-preview
tasks:
- before: pyenv local $(pyenv version | grep '^3\.' | cut -d ' ' -f 1) && script/setup
command: python -m esphome dashboard config
command: python -m esphome config dashboard

View File

@@ -28,16 +28,13 @@ esphome/components/b_parasite/* @rbaron
esphome/components/ballu/* @bazuchan
esphome/components/bang_bang/* @OttoWinter
esphome/components/binary_sensor/* @esphome/core
esphome/components/bl0940/* @tobias-
esphome/components/ble_client/* @buxtronix
esphome/components/bme680_bsec/* @trvrnrth
esphome/components/bmp3xx/* @martgras
esphome/components/button/* @esphome/core
esphome/components/canbus/* @danielschramm @mvturnho
esphome/components/cap1188/* @MrEditor97
esphome/components/captive_portal/* @OttoWinter
esphome/components/ccs811/* @habbie
esphome/components/cd74hc4067/* @asoehlke
esphome/components/climate/* @esphome/core
esphome/components/climate_ir/* @glmnet
esphome/components/color_temperature/* @jesserockz
@@ -56,10 +53,8 @@ esphome/components/ds1307/* @badbadc0ffee
esphome/components/dsmr/* @glmnet @zuidwijk
esphome/components/esp32/* @esphome/core
esphome/components/esp32_ble/* @jesserockz
esphome/components/esp32_ble_controller/* @jesserockz
esphome/components/esp32_ble_server/* @jesserockz
esphome/components/esp32_camera_web_server/* @ayufan
esphome/components/esp32_can/* @Sympatron
esphome/components/esp32_improv/* @jesserockz
esphome/components/esp8266/* @esphome/core
esphome/components/exposure_notifications/* @OttoWinter
@@ -80,13 +75,11 @@ esphome/components/homeassistant/* @OttoWinter
esphome/components/hrxl_maxsonar_wr/* @netmikey
esphome/components/i2c/* @esphome/core
esphome/components/improv_serial/* @esphome/core
esphome/components/ina260/* @MrEditor97
esphome/components/inkbird_ibsth1_mini/* @fkirill
esphome/components/inkplate6/* @jesserockz
esphome/components/integration/* @OttoWinter
esphome/components/interval/* @esphome/core
esphome/components/json/* @OttoWinter
esphome/components/kalman_combinator/* @Cat-Ion
esphome/components/ledc/* @OttoWinter
esphome/components/light/* @esphome/core
esphome/components/logger/* @esphome/core
@@ -100,13 +93,10 @@ esphome/components/mcp23x08_base/* @jesserockz
esphome/components/mcp23x17_base/* @jesserockz
esphome/components/mcp23xxx_base/* @jesserockz
esphome/components/mcp2515/* @danielschramm @mvturnho
esphome/components/mcp3204/* @rsumner
esphome/components/mcp47a1/* @jesserockz
esphome/components/mcp9808/* @k7hpn
esphome/components/md5/* @esphome/core
esphome/components/mdns/* @esphome/core
esphome/components/midea/* @dudanov
esphome/components/midea_ir/* @dudanov
esphome/components/mitsubishi/* @RubyBailey
esphome/components/modbus_controller/* @martgras
esphome/components/modbus_controller/binary_sensor/* @martgras
@@ -134,7 +124,6 @@ esphome/components/pn532_i2c/* @OttoWinter @jesserockz
esphome/components/pn532_spi/* @OttoWinter @jesserockz
esphome/components/power_supply/* @esphome/core
esphome/components/preferences/* @esphome/core
esphome/components/psram/* @esphome/core
esphome/components/pulse_meter/* @stevebaxter
esphome/components/pvvx_mithermometer/* @pasiz
esphome/components/rc522/* @glmnet

View File

@@ -5,12 +5,12 @@
# One of "docker", "hassio"
ARG BASEIMGTYPE=docker
FROM ghcr.io/hassio-addons/debian-base/amd64:5.2.3 AS base-hassio-amd64
FROM ghcr.io/hassio-addons/debian-base/aarch64:5.2.3 AS base-hassio-arm64
FROM ghcr.io/hassio-addons/debian-base/armv7:5.2.3 AS base-hassio-armv7
FROM debian:bullseye-20211220-slim AS base-docker-amd64
FROM debian:bullseye-20211220-slim AS base-docker-arm64
FROM debian:bullseye-20211220-slim AS base-docker-armv7
FROM ghcr.io/hassio-addons/debian-base/amd64:5.1.1 AS base-hassio-amd64
FROM ghcr.io/hassio-addons/debian-base/aarch64:5.1.1 AS base-hassio-arm64
FROM ghcr.io/hassio-addons/debian-base/armv7:5.1.1 AS base-hassio-armv7
FROM debian:bullseye-20211011-slim AS base-docker-amd64
FROM debian:bullseye-20211011-slim AS base-docker-arm64
FROM debian:bullseye-20211011-slim AS base-docker-armv7
# Use TARGETARCH/TARGETVARIANT defined by docker
# https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope
@@ -27,7 +27,7 @@ RUN \
python3-cryptography=3.3.2-1 \
iputils-ping=3:20210202-1 \
git=1:2.30.2-1 \
curl=7.74.0-1.3+deb11u1 \
curl=7.74.0-1.3+b1 \
&& rm -rf \
/tmp/* \
/var/{cache,log}/* \
@@ -42,8 +42,8 @@ ENV \
RUN \
# Ubuntu python3-pip is missing wheel
pip3 install --no-cache-dir \
wheel==0.37.1 \
platformio==5.2.4 \
wheel==0.36.2 \
platformio==5.2.2 \
# Change some platformio settings
&& platformio settings set enable_telemetry No \
&& platformio settings set check_libraries_interval 1000000 \
@@ -64,7 +64,7 @@ RUN \
# Copy esphome and install
COPY . /esphome
RUN pip3 install --no-cache-dir --no-use-pep517 -e /esphome
RUN pip3 install --no-cache-dir -e /esphome
# Settings for dashboard
ENV USERNAME="" PASSWORD=""
@@ -112,7 +112,7 @@ RUN \
# Copy esphome and install
COPY . /esphome
RUN pip3 install --no-cache-dir --no-use-pep517 -e /esphome
RUN pip3 install --no-cache-dir -e /esphome
# Labels
LABEL \

View File

@@ -32,7 +32,6 @@ parser.add_argument("--dry-run", action="store_true", help="Don't run any comman
subparsers = parser.add_subparsers(help="Action to perform", dest="command", required=True)
build_parser = subparsers.add_parser("build", help="Build the image")
build_parser.add_argument("--push", help="Also push the images", action="store_true")
build_parser.add_argument("--load", help="Load the docker image locally", action="store_true")
manifest_parser = subparsers.add_parser("manifest", help="Create a manifest from already pushed images")
@@ -133,8 +132,6 @@ def main():
cmd += ["--tag", img]
if args.push:
cmd += ["--push", "--cache-to", f"type=registry,ref={cache_img},mode=max"]
if args.load:
cmd += ["--load"]
run_command(*cmd, ".")
elif args.command == "manifest":

View File

@@ -75,7 +75,8 @@ from esphome.cpp_types import ( # noqa
optional,
arduino_json_ns,
JsonObject,
JsonObjectConst,
JsonObjectRef,
JsonObjectConstRef,
Controller,
GPIOPin,
InternalGPIOPin,

View File

@@ -20,7 +20,6 @@ namespace esphome {
namespace api {
static const char *const TAG = "api.connection";
static const int ESP32_CAMERA_STOP_STREAM = 5000;
APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
: parent_(parent), initial_state_iterator_(parent, this), list_entities_iterator_(parent, this) {
@@ -705,9 +704,7 @@ void APIConnection::send_camera_state(std::shared_ptr<esp32_camera::CameraImage>
return;
if (this->image_reader_.available())
return;
if (image->was_requested_by(esphome::esp32_camera::API_REQUESTER) ||
image->was_requested_by(esphome::esp32_camera::IDLE))
this->image_reader_.set_image(std::move(image));
this->image_reader_.set_image(std::move(image));
}
bool APIConnection::send_camera_info(esp32_camera::ESP32Camera *camera) {
ListEntitiesCameraResponse msg;
@@ -725,14 +722,9 @@ void APIConnection::camera_image(const CameraImageRequest &msg) {
return;
if (msg.single)
esp32_camera::global_esp32_camera->request_image(esphome::esp32_camera::API_REQUESTER);
if (msg.stream) {
esp32_camera::global_esp32_camera->start_stream(esphome::esp32_camera::API_REQUESTER);
App.scheduler.set_timeout(this->parent_, "api_esp32_camera_stop_stream", ESP32_CAMERA_STOP_STREAM, []() {
esp32_camera::global_esp32_camera->stop_stream(esphome::esp32_camera::API_REQUESTER);
});
}
esp32_camera::global_esp32_camera->request_image();
if (msg.stream)
esp32_camera::global_esp32_camera->request_stream();
}
#endif

View File

@@ -1,7 +1,6 @@
#include "api_frame_helper.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
#include "proto.h"
#include <cstring>
@@ -722,12 +721,7 @@ APIError APINoiseFrameHelper::shutdown(int how) {
}
extern "C" {
// declare how noise generates random bytes (here with a good HWRNG based on the RF system)
void noise_rand_bytes(void *output, size_t len) {
if (!esphome::random_bytes(reinterpret_cast<uint8_t *>(output), len)) {
ESP_LOGE(TAG, "Failed to acquire random bytes, rebooting!");
arch_restart();
}
}
void noise_rand_bytes(void *output, size_t len) { esphome::fill_random(reinterpret_cast<uint8_t *>(output), len); }
}
#endif // USE_API_NOISE

View File

@@ -12,10 +12,10 @@ template<typename... X> class TemplatableStringValue : public TemplatableValue<s
public:
TemplatableStringValue() : TemplatableValue<std::string, X...>() {}
template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0>
template<typename F, enable_if_t<!is_callable<F, X...>::value, int> = 0>
TemplatableStringValue(F value) : TemplatableValue<std::string, X...>(value) {}
template<typename F, enable_if_t<is_invocable<F, X...>::value, int> = 0>
template<typename F, enable_if_t<is_callable<F, X...>::value, int> = 0>
TemplatableStringValue(F f)
: TemplatableValue<std::string, X...>([f](X... x) -> std::string { return to_string(f(x...)); }) {}
};

View File

@@ -45,8 +45,6 @@ bool ATCMiThermometer::parse_device(const esp32_ble_tracker::ESPBTDevice &device
this->battery_voltage_->publish_state(*res->battery_voltage);
success = true;
}
if (this->signal_strength_ != nullptr)
this->signal_strength_->publish_state(device.get_rssi());
return success;
}

View File

@@ -28,7 +28,6 @@ class ATCMiThermometer : public Component, public esp32_ble_tracker::ESPBTDevice
void set_humidity(sensor::Sensor *humidity) { humidity_ = humidity; }
void set_battery_level(sensor::Sensor *battery_level) { battery_level_ = battery_level; }
void set_battery_voltage(sensor::Sensor *battery_voltage) { battery_voltage_ = battery_voltage; }
void set_signal_strength(sensor::Sensor *signal_strength) { signal_strength_ = signal_strength; }
protected:
uint64_t address_;
@@ -36,7 +35,6 @@ class ATCMiThermometer : public Component, public esp32_ble_tracker::ESPBTDevice
sensor::Sensor *humidity_{nullptr};
sensor::Sensor *battery_level_{nullptr};
sensor::Sensor *battery_voltage_{nullptr};
sensor::Sensor *signal_strength_{nullptr};
optional<ParseResult> parse_header_(const esp32_ble_tracker::ServiceData &service_data);
bool parse_message_(const std::vector<uint8_t> &message, ParseResult &result);

View File

@@ -6,18 +6,15 @@ from esphome.const import (
CONF_BATTERY_VOLTAGE,
CONF_MAC_ADDRESS,
CONF_HUMIDITY,
CONF_SIGNAL_STRENGTH,
CONF_TEMPERATURE,
CONF_ID,
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_HUMIDITY,
DEVICE_CLASS_SIGNAL_STRENGTH,
DEVICE_CLASS_TEMPERATURE,
DEVICE_CLASS_VOLTAGE,
ENTITY_CATEGORY_DIAGNOSTIC,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_DECIBEL_MILLIWATT,
UNIT_PERCENT,
UNIT_VOLT,
)
@@ -62,13 +59,6 @@ CONFIG_SCHEMA = (
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_SIGNAL_STRENGTH): sensor.sensor_schema(
unit_of_measurement=UNIT_DECIBEL_MILLIWATT,
accuracy_decimals=0,
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
@@ -95,6 +85,3 @@ async def to_code(config):
if CONF_BATTERY_VOLTAGE in config:
sens = await sensor.new_sensor(config[CONF_BATTERY_VOLTAGE])
cg.add(var.set_battery_voltage(sens))
if CONF_SIGNAL_STRENGTH in config:
sens = await sensor.new_sensor(config[CONF_SIGNAL_STRENGTH])
cg.add(var.set_signal_strength(sens))

View File

@@ -80,23 +80,21 @@ void BangBangClimate::compute_state_() {
climate::ClimateAction target_action;
if (too_cold) {
// too cold -> enable heating if possible and enabled, else idle
if (this->supports_heat_ &&
(this->mode == climate::CLIMATE_MODE_HEAT_COOL || this->mode == climate::CLIMATE_MODE_HEAT))
// too cold -> enable heating if possible, else idle
if (this->supports_heat_)
target_action = climate::CLIMATE_ACTION_HEATING;
else
target_action = climate::CLIMATE_ACTION_IDLE;
} else if (too_hot) {
// too hot -> enable cooling if possible and enabled, else idle
if (this->supports_cool_ &&
(this->mode == climate::CLIMATE_MODE_HEAT_COOL || this->mode == climate::CLIMATE_MODE_COOL))
// too hot -> enable cooling if possible, else idle
if (this->supports_cool_)
target_action = climate::CLIMATE_ACTION_COOLING;
else
target_action = climate::CLIMATE_ACTION_IDLE;
} else {
// neither too hot nor too cold -> in range
if (this->supports_cool_ && this->supports_heat_ && this->mode == climate::CLIMATE_MODE_HEAT_COOL) {
// if supports both ends and both cooling and heating enabled, go to idle action
if (this->supports_cool_ && this->supports_heat_) {
// if supports both ends, go to idle action
target_action = climate::CLIMATE_ACTION_IDLE;
} else {
// else use current mode and don't change (hysteresis)

View File

@@ -48,10 +48,7 @@ void BinarySensor::set_device_class(const std::string &device_class) { this->dev
std::string BinarySensor::get_device_class() {
if (this->device_class_.has_value())
return *this->device_class_;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return this->device_class();
#pragma GCC diagnostic pop
}
void BinarySensor::add_filter(Filter *filter) {
filter->parent_ = this;

View File

@@ -74,10 +74,7 @@ class BinarySensor : public EntityBase {
// ========== OVERRIDE METHODS ==========
// (You'll only need this when creating your own custom binary sensor)
/** Override this to set the default device class.
*
* @deprecated This method is deprecated, set the property during config validation instead. (2022.1)
*/
/// Get the default device class for this sensor, or empty string for no default.
virtual std::string device_class();
protected:

View File

@@ -1 +0,0 @@
CODEOWNERS = ["@tobias-"]

View File

@@ -1,137 +0,0 @@
#include "bl0940.h"
#include "esphome/core/log.h"
namespace esphome {
namespace bl0940 {
static const char *const TAG = "bl0940";
static const uint8_t BL0940_READ_COMMAND = 0x50; // 0x58 according to documentation
static const uint8_t BL0940_FULL_PACKET = 0xAA;
static const uint8_t BL0940_PACKET_HEADER = 0x55; // 0x58 according to documentation
static const uint8_t BL0940_WRITE_COMMAND = 0xA0; // 0xA8 according to documentation
static const uint8_t BL0940_REG_I_FAST_RMS_CTRL = 0x10;
static const uint8_t BL0940_REG_MODE = 0x18;
static const uint8_t BL0940_REG_SOFT_RESET = 0x19;
static const uint8_t BL0940_REG_USR_WRPROT = 0x1A;
static const uint8_t BL0940_REG_TPS_CTRL = 0x1B;
const uint8_t BL0940_INIT[5][6] = {
// Reset to default
{BL0940_WRITE_COMMAND, BL0940_REG_SOFT_RESET, 0x5A, 0x5A, 0x5A, 0x38},
// Enable User Operation Write
{BL0940_WRITE_COMMAND, BL0940_REG_USR_WRPROT, 0x55, 0x00, 0x00, 0xF0},
// 0x0100 = CF_UNABLE energy pulse, AC_FREQ_SEL 50Hz, RMS_UPDATE_SEL 800mS
{BL0940_WRITE_COMMAND, BL0940_REG_MODE, 0x00, 0x10, 0x00, 0x37},
// 0x47FF = Over-current and leakage alarm on, Automatic temperature measurement, Interval 100mS
{BL0940_WRITE_COMMAND, BL0940_REG_TPS_CTRL, 0xFF, 0x47, 0x00, 0xFE},
// 0x181C = Half cycle, Fast RMS threshold 6172
{BL0940_WRITE_COMMAND, BL0940_REG_I_FAST_RMS_CTRL, 0x1C, 0x18, 0x00, 0x1B}};
void BL0940::loop() {
DataPacket buffer;
if (!this->available()) {
return;
}
if (read_array((uint8_t *) &buffer, sizeof(buffer))) {
if (validate_checksum(&buffer)) {
received_package_(&buffer);
}
} else {
ESP_LOGW(TAG, "Junk on wire. Throwing away partial message");
while (read() >= 0)
;
}
}
bool BL0940::validate_checksum(const DataPacket *data) {
uint8_t checksum = BL0940_READ_COMMAND;
// Whole package but checksum
for (uint32_t i = 0; i < sizeof(data->raw) - 1; i++) {
checksum += data->raw[i];
}
checksum ^= 0xFF;
if (checksum != data->checksum) {
ESP_LOGW(TAG, "BL0940 invalid checksum! 0x%02X != 0x%02X", checksum, data->checksum);
}
return checksum == data->checksum;
}
void BL0940::update() {
this->flush();
this->write_byte(BL0940_READ_COMMAND);
this->write_byte(BL0940_FULL_PACKET);
}
void BL0940::setup() {
for (auto i : BL0940_INIT) {
this->write_array(i, 6);
delay(1);
}
this->flush();
}
float BL0940::update_temp_(sensor::Sensor *sensor, ube16_t temperature) const {
auto tb = (float) (temperature.h << 8 | temperature.l);
float converted_temp = ((float) 170 / 448) * (tb / 2 - 32) - 45;
if (sensor != nullptr) {
if (sensor->has_state() && std::abs(converted_temp - sensor->get_state()) > max_temperature_diff_) {
ESP_LOGD("bl0940", "Invalid temperature change. Sensor: '%s', Old temperature: %f, New temperature: %f",
sensor->get_name().c_str(), sensor->get_state(), converted_temp);
return 0.0f;
}
sensor->publish_state(converted_temp);
}
return converted_temp;
}
void BL0940::received_package_(const DataPacket *data) const {
// Bad header
if (data->frame_header != BL0940_PACKET_HEADER) {
ESP_LOGI("bl0940", "Invalid data. Header mismatch: %d", data->frame_header);
return;
}
float v_rms = (float) to_uint32_t(data->v_rms) / voltage_reference_;
float i_rms = (float) to_uint32_t(data->i_rms) / current_reference_;
float watt = (float) to_int32_t(data->watt) / power_reference_;
uint32_t cf_cnt = to_uint32_t(data->cf_cnt);
float total_energy_consumption = (float) cf_cnt / energy_reference_;
float tps1 = update_temp_(internal_temperature_sensor_, data->tps1);
float tps2 = update_temp_(external_temperature_sensor_, data->tps2);
if (voltage_sensor_ != nullptr) {
voltage_sensor_->publish_state(v_rms);
}
if (current_sensor_ != nullptr) {
current_sensor_->publish_state(i_rms);
}
if (power_sensor_ != nullptr) {
power_sensor_->publish_state(watt);
}
if (energy_sensor_ != nullptr) {
energy_sensor_->publish_state(total_energy_consumption);
}
ESP_LOGV("bl0940", "BL0940: U %fV, I %fA, P %fW, Cnt %d, ∫P %fkWh, T1 %f°C, T2 %f°C", v_rms, i_rms, watt, cf_cnt,
total_energy_consumption, tps1, tps2);
}
void BL0940::dump_config() { // NOLINT(readability-function-cognitive-complexity)
ESP_LOGCONFIG(TAG, "BL0940:");
LOG_SENSOR("", "Voltage", this->voltage_sensor_);
LOG_SENSOR("", "Current", this->current_sensor_);
LOG_SENSOR("", "Power", this->power_sensor_);
LOG_SENSOR("", "Energy", this->energy_sensor_);
LOG_SENSOR("", "Internal temperature", this->internal_temperature_sensor_);
LOG_SENSOR("", "External temperature", this->external_temperature_sensor_);
}
uint32_t BL0940::to_uint32_t(ube24_t input) { return input.h << 16 | input.m << 8 | input.l; }
int32_t BL0940::to_int32_t(sbe24_t input) { return input.h << 16 | input.m << 8 | input.l; }
} // namespace bl0940
} // namespace esphome

View File

@@ -1,109 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/uart/uart.h"
#include "esphome/components/sensor/sensor.h"
namespace esphome {
namespace bl0940 {
static const float BL0940_PREF = 1430;
static const float BL0940_UREF = 33000;
static const float BL0940_IREF = 275000; // 2750 from tasmota. Seems to generate values 100 times too high
// Measured to 297J per click according to power consumption of 5 minutes
// Converted to kWh (3.6MJ per kwH). Used to be 256 * 1638.4
static const float BL0940_EREF = 3.6e6 / 297;
struct ube24_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
uint8_t l;
uint8_t m;
uint8_t h;
} __attribute__((packed));
struct ube16_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
uint8_t l;
uint8_t h;
} __attribute__((packed));
struct sbe24_t { // NOLINT(readability-identifier-naming,altera-struct-pack-align)
uint8_t l;
uint8_t m;
int8_t h;
} __attribute__((packed));
// Caveat: All these values are big endian (low - middle - high)
union DataPacket { // NOLINT(altera-struct-pack-align)
uint8_t raw[35];
struct {
uint8_t frame_header; // value of 0x58 according to docs. 0x55 according to Tasmota real world tests. Reality wins.
ube24_t i_fast_rms; // 0x00
ube24_t i_rms; // 0x04
ube24_t RESERVED0; // reserved
ube24_t v_rms; // 0x06
ube24_t RESERVED1; // reserved
sbe24_t watt; // 0x08
ube24_t RESERVED2; // reserved
ube24_t cf_cnt; // 0x0A
ube24_t RESERVED3; // reserved
ube16_t tps1; // 0x0c
uint8_t RESERVED4; // value of 0x00
ube16_t tps2; // 0x0c
uint8_t RESERVED5; // value of 0x00
uint8_t checksum; // checksum
};
} __attribute__((packed));
class BL0940 : public PollingComponent, public uart::UARTDevice {
public:
void set_voltage_sensor(sensor::Sensor *voltage_sensor) { voltage_sensor_ = voltage_sensor; }
void set_current_sensor(sensor::Sensor *current_sensor) { current_sensor_ = current_sensor; }
void set_power_sensor(sensor::Sensor *power_sensor) { power_sensor_ = power_sensor; }
void set_energy_sensor(sensor::Sensor *energy_sensor) { energy_sensor_ = energy_sensor; }
void set_internal_temperature_sensor(sensor::Sensor *internal_temperature_sensor) {
internal_temperature_sensor_ = internal_temperature_sensor;
}
void set_external_temperature_sensor(sensor::Sensor *external_temperature_sensor) {
external_temperature_sensor_ = external_temperature_sensor;
}
void loop() override;
void update() override;
void setup() override;
void dump_config() override;
protected:
sensor::Sensor *voltage_sensor_;
sensor::Sensor *current_sensor_;
// NB This may be negative as the circuits is seemingly able to measure
// power in both directions
sensor::Sensor *power_sensor_;
sensor::Sensor *energy_sensor_;
sensor::Sensor *internal_temperature_sensor_;
sensor::Sensor *external_temperature_sensor_;
// Max difference between two measurements of the temperature. Used to avoid noise.
float max_temperature_diff_{0};
// Divide by this to turn into Watt
float power_reference_ = BL0940_PREF;
// Divide by this to turn into Volt
float voltage_reference_ = BL0940_UREF;
// Divide by this to turn into Ampere
float current_reference_ = BL0940_IREF;
// Divide by this to turn into kWh
float energy_reference_ = BL0940_EREF;
float update_temp_(sensor::Sensor *sensor, ube16_t packed_temperature) const;
static uint32_t to_uint32_t(ube24_t input);
static int32_t to_int32_t(sbe24_t input);
static bool validate_checksum(const DataPacket *data);
void received_package_(const DataPacket *data) const;
};
} // namespace bl0940
} // namespace esphome

View File

@@ -1,106 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, uart
from esphome.const import (
CONF_CURRENT,
CONF_ENERGY,
CONF_ID,
CONF_POWER,
CONF_VOLTAGE,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_ENERGY,
DEVICE_CLASS_POWER,
DEVICE_CLASS_VOLTAGE,
DEVICE_CLASS_TEMPERATURE,
ICON_EMPTY,
STATE_CLASS_MEASUREMENT,
STATE_CLASS_NONE,
UNIT_AMPERE,
UNIT_CELSIUS,
UNIT_KILOWATT_HOURS,
UNIT_VOLT,
UNIT_WATT,
)
DEPENDENCIES = ["uart"]
CONF_INTERNAL_TEMPERATURE = "internal_temperature"
CONF_EXTERNAL_TEMPERATURE = "external_temperature"
bl0940_ns = cg.esphome_ns.namespace("bl0940")
BL0940 = bl0940_ns.class_("BL0940", cg.PollingComponent, uart.UARTDevice)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(BL0940),
cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(
UNIT_VOLT, ICON_EMPTY, 1, DEVICE_CLASS_VOLTAGE, STATE_CLASS_MEASUREMENT
),
cv.Optional(CONF_CURRENT): sensor.sensor_schema(
UNIT_AMPERE,
ICON_EMPTY,
2,
DEVICE_CLASS_CURRENT,
STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_POWER): sensor.sensor_schema(
UNIT_WATT, ICON_EMPTY, 0, DEVICE_CLASS_POWER, STATE_CLASS_MEASUREMENT
),
cv.Optional(CONF_ENERGY): sensor.sensor_schema(
UNIT_KILOWATT_HOURS,
ICON_EMPTY,
0,
DEVICE_CLASS_ENERGY,
STATE_CLASS_NONE,
),
cv.Optional(CONF_INTERNAL_TEMPERATURE): sensor.sensor_schema(
UNIT_CELSIUS,
ICON_EMPTY,
0,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_NONE,
),
cv.Optional(CONF_EXTERNAL_TEMPERATURE): sensor.sensor_schema(
UNIT_CELSIUS,
ICON_EMPTY,
0,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_NONE,
),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(uart.UART_DEVICE_SCHEMA)
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
if CONF_VOLTAGE in config:
conf = config[CONF_VOLTAGE]
sens = await sensor.new_sensor(conf)
cg.add(var.set_voltage_sensor(sens))
if CONF_CURRENT in config:
conf = config[CONF_CURRENT]
sens = await sensor.new_sensor(conf)
cg.add(var.set_current_sensor(sens))
if CONF_POWER in config:
conf = config[CONF_POWER]
sens = await sensor.new_sensor(conf)
cg.add(var.set_power_sensor(sens))
if CONF_ENERGY in config:
conf = config[CONF_ENERGY]
sens = await sensor.new_sensor(conf)
cg.add(var.set_energy_sensor(sens))
if CONF_INTERNAL_TEMPERATURE in config:
conf = config[CONF_INTERNAL_TEMPERATURE]
sens = await sensor.new_sensor(conf)
cg.add(var.set_internal_temperature_sensor(sens))
if CONF_EXTERNAL_TEMPERATURE in config:
conf = config[CONF_EXTERNAL_TEMPERATURE]
sens = await sensor.new_sensor(conf)
cg.add(var.set_external_temperature_sensor(sens))

View File

@@ -1,388 +0,0 @@
/*
based on BMP388_DEV by Martin Lindupp
under MIT License (MIT)
Copyright (C) Martin Lindupp 2020
http://github.com/MartinL1/BMP388_DEV
*/
#include "bmp3xx.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace bmp3xx {
static const char *const TAG = "bmp3xx.sensor";
static const LogString *chip_type_to_str(uint8_t chip_type) {
switch (chip_type) {
case BMP388_ID:
return LOG_STR("BMP 388");
case BMP390_ID:
return LOG_STR("BMP 390");
default:
return LOG_STR("Unknown Chip Type");
}
}
static const LogString *oversampling_to_str(Oversampling oversampling) {
switch (oversampling) {
case Oversampling::OVERSAMPLING_NONE:
return LOG_STR("None");
case Oversampling::OVERSAMPLING_X2:
return LOG_STR("2x");
case Oversampling::OVERSAMPLING_X4:
return LOG_STR("4x");
case Oversampling::OVERSAMPLING_X8:
return LOG_STR("8x");
case Oversampling::OVERSAMPLING_X16:
return LOG_STR("16x");
case Oversampling::OVERSAMPLING_X32:
return LOG_STR("32x");
default:
return LOG_STR("");
}
}
static const LogString *iir_filter_to_str(IIRFilter filter) {
switch (filter) {
case IIRFilter::IIR_FILTER_OFF:
return LOG_STR("OFF");
case IIRFilter::IIR_FILTER_2:
return LOG_STR("2x");
case IIRFilter::IIR_FILTER_4:
return LOG_STR("4x");
case IIRFilter::IIR_FILTER_8:
return LOG_STR("8x");
case IIRFilter::IIR_FILTER_16:
return LOG_STR("16x");
case IIRFilter::IIR_FILTER_32:
return LOG_STR("32x");
case IIRFilter::IIR_FILTER_64:
return LOG_STR("64x");
case IIRFilter::IIR_FILTER_128:
return LOG_STR("128x");
default:
return LOG_STR("");
}
}
void BMP3XXComponent::setup() {
this->error_code_ = NONE;
ESP_LOGCONFIG(TAG, "Setting up BMP3XX...");
// Call the Device base class "initialise" function
if (!reset()) {
ESP_LOGE(TAG, "Failed to reset BMP3XX...");
this->error_code_ = ERROR_SENSOR_RESET;
this->mark_failed();
}
if (!read_byte(BMP388_CHIP_ID, &this->chip_id_.reg)) {
ESP_LOGE(TAG, "Can't read chip id");
this->error_code_ = ERROR_COMMUNICATION_FAILED;
this->mark_failed();
return;
}
ESP_LOGCONFIG(TAG, "Chip %s Id 0x%X", LOG_STR_ARG(chip_type_to_str(this->chip_id_.reg)), this->chip_id_.reg);
if (chip_id_.reg != BMP388_ID && chip_id_.reg != BMP390_ID) {
ESP_LOGE(TAG, "Unknown chip id - is this really a BMP388 or BMP390?");
this->error_code_ = ERROR_WRONG_CHIP_ID;
this->mark_failed();
return;
}
// set sensor in sleep mode
stop_conversion();
// Read the calibration parameters into the params structure
if (!read_bytes(BMP388_TRIM_PARAMS, (uint8_t *) &compensation_params_, sizeof(compensation_params_))) {
ESP_LOGE(TAG, "Can't read calibration data");
this->error_code_ = ERROR_COMMUNICATION_FAILED;
this->mark_failed();
return;
}
compensation_float_params_.param_T1 =
(float) compensation_params_.param_T1 / powf(2.0f, -8.0f); // Calculate the floating point trim parameters
compensation_float_params_.param_T2 = (float) compensation_params_.param_T2 / powf(2.0f, 30.0f);
compensation_float_params_.param_T3 = (float) compensation_params_.param_T3 / powf(2.0f, 48.0f);
compensation_float_params_.param_P1 = ((float) compensation_params_.param_P1 - powf(2.0f, 14.0f)) / powf(2.0f, 20.0f);
compensation_float_params_.param_P2 = ((float) compensation_params_.param_P2 - powf(2.0f, 14.0f)) / powf(2.0f, 29.0f);
compensation_float_params_.param_P3 = (float) compensation_params_.param_P3 / powf(2.0f, 32.0f);
compensation_float_params_.param_P4 = (float) compensation_params_.param_P4 / powf(2.0f, 37.0f);
compensation_float_params_.param_P5 = (float) compensation_params_.param_P5 / powf(2.0f, -3.0f);
compensation_float_params_.param_P6 = (float) compensation_params_.param_P6 / powf(2.0f, 6.0f);
compensation_float_params_.param_P7 = (float) compensation_params_.param_P7 / powf(2.0f, 8.0f);
compensation_float_params_.param_P8 = (float) compensation_params_.param_P8 / powf(2.0f, 15.0f);
compensation_float_params_.param_P9 = (float) compensation_params_.param_P9 / powf(2.0f, 48.0f);
compensation_float_params_.param_P10 = (float) compensation_params_.param_P10 / powf(2.0f, 48.0f);
compensation_float_params_.param_P11 = (float) compensation_params_.param_P11 / powf(2.0f, 65.0f);
// Initialise the BMP388 IIR filter register
if (!set_iir_filter(this->iir_filter_)) {
ESP_LOGE(TAG, "Failed to set IIR filter");
this->error_code_ = ERROR_COMMUNICATION_FAILED;
this->mark_failed();
return;
}
// Set power control registers
pwr_ctrl_.bit.press_en = 1;
pwr_ctrl_.bit.temp_en = 1;
// Disable pressure if no sensor defined
// keep temperature enabled since it's needed for compensation
if (this->pressure_sensor_ == nullptr) {
pwr_ctrl_.bit.press_en = 0;
this->pressure_oversampling_ = OVERSAMPLING_NONE;
}
// just disable oeversampling for temp if not used
if (this->temperature_sensor_ == nullptr) {
this->temperature_oversampling_ = OVERSAMPLING_NONE;
}
// Initialise the BMP388 oversampling register
if (!set_oversampling_register(this->pressure_oversampling_, this->temperature_oversampling_)) {
ESP_LOGE(TAG, "Failed to set oversampling register");
this->error_code_ = ERROR_COMMUNICATION_FAILED;
this->mark_failed();
return;
}
}
void BMP3XXComponent::dump_config() {
ESP_LOGCONFIG(TAG, "BMP3XX:");
ESP_LOGCONFIG(TAG, " Type: %s (0x%X)", LOG_STR_ARG(chip_type_to_str(this->chip_id_.reg)), this->chip_id_.reg);
LOG_I2C_DEVICE(this);
switch (this->error_code_) {
case NONE:
break;
case ERROR_COMMUNICATION_FAILED:
ESP_LOGE(TAG, "Communication with BMP3XX failed!");
break;
case ERROR_WRONG_CHIP_ID:
ESP_LOGE(
TAG,
"BMP3XX has wrong chip ID (reported id: 0x%X) - please check if you are really using a BMP 388 or BMP 390",
this->chip_id_.reg);
break;
case ERROR_SENSOR_RESET:
ESP_LOGE(TAG, "BMP3XX failed to reset");
break;
default:
ESP_LOGE(TAG, "BMP3XX error code %d", (int) this->error_code_);
break;
}
ESP_LOGCONFIG(TAG, " IIR Filter: %s", LOG_STR_ARG(iir_filter_to_str(this->iir_filter_)));
LOG_UPDATE_INTERVAL(this);
if (this->temperature_sensor_) {
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->temperature_oversampling_)));
}
if (this->pressure_sensor_) {
LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
ESP_LOGCONFIG(TAG, " Oversampling: %s", LOG_STR_ARG(oversampling_to_str(this->pressure_oversampling_)));
}
}
float BMP3XXComponent::get_setup_priority() const { return setup_priority::DATA; }
inline uint8_t oversampling_to_time(Oversampling over_sampling) { return (1 << uint8_t(over_sampling)); }
void BMP3XXComponent::update() {
// Enable sensor
ESP_LOGV(TAG, "Sending conversion request...");
float meas_time = 1.0f;
// Ref: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmp390-ds002.pdf 3.9.2
meas_time += 2.02f * oversampling_to_time(this->temperature_oversampling_) + 0.163f;
meas_time += 2.02f * oversampling_to_time(this->pressure_oversampling_) + 0.392f;
meas_time += 0.234f;
if (!set_mode(FORCED_MODE)) {
ESP_LOGE(TAG, "Failed start forced mode");
this->mark_failed();
return;
}
ESP_LOGVV(TAG, "measurement time %d", uint32_t(ceilf(meas_time)));
this->set_timeout("data", uint32_t(ceilf(meas_time)), [this]() {
float temperature = 0.0f;
float pressure = 0.0f;
if (this->pressure_sensor_ != nullptr) {
if (!get_measurements(temperature, pressure)) {
ESP_LOGW(TAG, "Failed to read pressure and temperature - skipping update");
this->status_set_warning();
return;
}
ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa", temperature, pressure);
} else {
if (!get_temperature(temperature)) {
ESP_LOGW(TAG, "Failed to read temperature - skipping update");
this->status_set_warning();
return;
}
ESP_LOGD(TAG, "Got temperature=%.1f°C", temperature);
}
if (this->temperature_sensor_ != nullptr)
this->temperature_sensor_->publish_state(temperature);
if (this->pressure_sensor_ != nullptr)
this->pressure_sensor_->publish_state(pressure);
this->status_clear_warning();
set_mode(SLEEP_MODE);
});
}
// Reset the BMP3XX
uint8_t BMP3XXComponent::reset() {
write_byte(BMP388_CMD, RESET_CODE); // Write the reset code to the command register
// Wait for 10ms
delay(10);
this->read_byte(BMP388_EVENT, &event_.reg); // Read the BMP388's event register
return event_.bit.por_detected; // Return if device reset is complete
}
// Start a one shot measurement in FORCED_MODE
bool BMP3XXComponent::start_forced_conversion() {
// Only set FORCED_MODE if we're already in SLEEP_MODE
if (pwr_ctrl_.bit.mode == SLEEP_MODE) {
return set_mode(FORCED_MODE);
}
return true;
}
// Stop the conversion and return to SLEEP_MODE
bool BMP3XXComponent::stop_conversion() { return set_mode(SLEEP_MODE); }
// Set the pressure oversampling rate
bool BMP3XXComponent::set_pressure_oversampling(Oversampling oversampling) {
osr_.bit.osr_p = oversampling;
return this->write_byte(BMP388_OSR, osr_.reg);
}
// Set the temperature oversampling rate
bool BMP3XXComponent::set_temperature_oversampling(Oversampling oversampling) {
osr_.bit.osr_t = oversampling;
return this->write_byte(BMP388_OSR, osr_.reg);
}
// Set the IIR filter setting
bool BMP3XXComponent::set_iir_filter(IIRFilter iir_filter) {
config_.bit.iir_filter = iir_filter;
return this->write_byte(BMP388_CONFIG, config_.reg);
}
// Get temperature
bool BMP3XXComponent::get_temperature(float &temperature) {
// Check if a measurement is ready
if (!data_ready()) {
return false;
}
uint8_t data[3];
// Read the temperature
if (!this->read_bytes(BMP388_DATA_3, &data[0], 3)) {
ESP_LOGE(TAG, "Failed to read temperature");
return false;
}
// Copy the temperature data into the adc variables
int32_t adc_temp = (int32_t) data[2] << 16 | (int32_t) data[1] << 8 | (int32_t) data[0];
// Temperature compensation (function from BMP388 datasheet)
temperature = bmp388_compensate_temperature_((float) adc_temp);
return true;
}
// Get the pressure
bool BMP3XXComponent::get_pressure(float &pressure) {
float temperature;
return get_measurements(temperature, pressure);
}
// Get temperature and pressure
bool BMP3XXComponent::get_measurements(float &temperature, float &pressure) {
// Check if a measurement is ready
if (!data_ready()) {
ESP_LOGD(TAG, "BMP3XX Get measurement - data not ready skipping update");
return false;
}
uint8_t data[6];
// Read the temperature and pressure data
if (!this->read_bytes(BMP388_DATA_0, &data[0], 6)) {
ESP_LOGE(TAG, "Failed to read measurements");
return false;
}
// Copy the temperature and pressure data into the adc variables
int32_t adc_pres = (int32_t) data[2] << 16 | (int32_t) data[1] << 8 | (int32_t) data[0];
int32_t adc_temp = (int32_t) data[5] << 16 | (int32_t) data[4] << 8 | (int32_t) data[3];
// Temperature compensation (function from BMP388 datasheet)
temperature = bmp388_compensate_temperature_((float) adc_temp);
// Pressure compensation (function from BMP388 datasheet)
pressure = bmp388_compensate_pressure_((float) adc_pres, temperature);
// Calculate the pressure in millibar/hPa
pressure /= 100.0f;
return true;
}
// Set the BMP388's mode in the power control register
bool BMP3XXComponent::set_mode(OperationMode mode) {
pwr_ctrl_.bit.mode = mode;
return this->write_byte(BMP388_PWR_CTRL, pwr_ctrl_.reg);
}
// Set the BMP388 oversampling register
bool BMP3XXComponent::set_oversampling_register(Oversampling pressure_oversampling,
Oversampling temperature_oversampling) {
osr_.reg = temperature_oversampling << 3 | pressure_oversampling;
return this->write_byte(BMP388_OSR, osr_.reg);
}
// Check if measurement data is ready
bool BMP3XXComponent::data_ready() {
// If we're in SLEEP_MODE return immediately
if (pwr_ctrl_.bit.mode == SLEEP_MODE) {
ESP_LOGD(TAG, "Not ready - sensor is in sleep mode");
return false;
}
// Read the interrupt status register
uint8_t status;
if (!this->read_byte(BMP388_INT_STATUS, &status)) {
ESP_LOGE(TAG, "Failed to read status register");
return false;
}
int_status_.reg = status;
ESP_LOGVV(TAG, "data ready status %d", status);
// If we're in FORCED_MODE switch back to SLEEP_MODE
if (int_status_.bit.drdy) {
if (pwr_ctrl_.bit.mode == FORCED_MODE) {
pwr_ctrl_.bit.mode = SLEEP_MODE;
}
return true; // The measurement is ready
}
return false; // The measurement is still pending
}
////////////////////////////////////////////////////////////////////////////////
// Bosch BMP3XXComponent (Private) Member Functions
////////////////////////////////////////////////////////////////////////////////
float BMP3XXComponent::bmp388_compensate_temperature_(float uncomp_temp) {
float partial_data1 = uncomp_temp - compensation_float_params_.param_T1;
float partial_data2 = partial_data1 * compensation_float_params_.param_T2;
return partial_data2 + partial_data1 * partial_data1 * compensation_float_params_.param_T3;
}
float BMP3XXComponent::bmp388_compensate_pressure_(float uncomp_press, float t_lin) {
float partial_data1 = compensation_float_params_.param_P6 * t_lin;
float partial_data2 = compensation_float_params_.param_P7 * t_lin * t_lin;
float partial_data3 = compensation_float_params_.param_P8 * t_lin * t_lin * t_lin;
float partial_out1 = compensation_float_params_.param_P5 + partial_data1 + partial_data2 + partial_data3;
partial_data1 = compensation_float_params_.param_P2 * t_lin;
partial_data2 = compensation_float_params_.param_P3 * t_lin * t_lin;
partial_data3 = compensation_float_params_.param_P4 * t_lin * t_lin * t_lin;
float partial_out2 =
uncomp_press * (compensation_float_params_.param_P1 + partial_data1 + partial_data2 + partial_data3);
partial_data1 = uncomp_press * uncomp_press;
partial_data2 = compensation_float_params_.param_P9 + compensation_float_params_.param_P10 * t_lin;
partial_data3 = partial_data1 * partial_data2;
float partial_data4 =
partial_data3 + uncomp_press * uncomp_press * uncomp_press * compensation_float_params_.param_P11;
return partial_out1 + partial_out2 + partial_data4;
}
} // namespace bmp3xx
} // namespace esphome

View File

@@ -1,237 +0,0 @@
/*
based on BMP388_DEV by Martin Lindupp
under MIT License (MIT)
Copyright (C) Martin Lindupp 2020
http://github.com/MartinL1/BMP388_DEV
*/
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace bmp3xx {
static const uint8_t BMP388_ID = 0x50; // The BMP388 device ID
static const uint8_t BMP390_ID = 0x60; // The BMP390 device ID
static const uint8_t RESET_CODE = 0xB6; // The BMP388 reset code
/// BMP388_DEV Registers
enum {
BMP388_CHIP_ID = 0x00, // Chip ID register sub-address
BMP388_ERR_REG = 0x02, // Error register sub-address
BMP388_STATUS = 0x03, // Status register sub-address
BMP388_DATA_0 = 0x04, // Pressure eXtended Least Significant Byte (XLSB) register sub-address
BMP388_DATA_1 = 0x05, // Pressure Least Significant Byte (LSB) register sub-address
BMP388_DATA_2 = 0x06, // Pressure Most Significant Byte (MSB) register sub-address
BMP388_DATA_3 = 0x07, // Temperature eXtended Least Significant Byte (XLSB) register sub-address
BMP388_DATA_4 = 0x08, // Temperature Least Significant Byte (LSB) register sub-address
BMP388_DATA_5 = 0x09, // Temperature Most Significant Byte (MSB) register sub-address
BMP388_SENSORTIME_0 = 0x0C, // Sensor time register 0 sub-address
BMP388_SENSORTIME_1 = 0x0D, // Sensor time register 1 sub-address
BMP388_SENSORTIME_2 = 0x0E, // Sensor time register 2 sub-address
BMP388_EVENT = 0x10, // Event register sub-address
BMP388_INT_STATUS = 0x11, // Interrupt Status register sub-address
BMP388_INT_CTRL = 0x19, // Interrupt Control register sub-address
BMP388_IF_CONFIG = 0x1A, // Interface Configuration register sub-address
BMP388_PWR_CTRL = 0x1B, // Power Control register sub-address
BMP388_OSR = 0x1C, // Oversampling register sub-address
BMP388_ODR = 0x1D, // Output Data Rate register sub-address
BMP388_CONFIG = 0x1F, // Configuration register sub-address
BMP388_TRIM_PARAMS = 0x31, // Trim parameter registers' base sub-address
BMP388_CMD = 0x7E // Command register sub-address
};
/// Device mode bitfield in the control and measurement register
enum OperationMode { SLEEP_MODE = 0x00, FORCED_MODE = 0x01, NORMAL_MODE = 0x03 };
/// Oversampling bit fields in the control and measurement register
enum Oversampling {
OVERSAMPLING_NONE = 0x00,
OVERSAMPLING_X2 = 0x01,
OVERSAMPLING_X4 = 0x02,
OVERSAMPLING_X8 = 0x03,
OVERSAMPLING_X16 = 0x04,
OVERSAMPLING_X32 = 0x05
};
/// Infinite Impulse Response (IIR) filter bit field in the configuration register
enum IIRFilter {
IIR_FILTER_OFF = 0x00,
IIR_FILTER_2 = 0x01,
IIR_FILTER_4 = 0x02,
IIR_FILTER_8 = 0x03,
IIR_FILTER_16 = 0x04,
IIR_FILTER_32 = 0x05,
IIR_FILTER_64 = 0x06,
IIR_FILTER_128 = 0x07
};
/// This class implements support for the BMP3XX Temperature+Pressure i2c sensor.
class BMP3XXComponent : public PollingComponent, public i2c::I2CDevice {
public:
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
void update() override;
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { temperature_sensor_ = temperature_sensor; }
void set_pressure_sensor(sensor::Sensor *pressure_sensor) { pressure_sensor_ = pressure_sensor; }
/// Set the oversampling value for the temperature sensor. Default is 16x.
void set_temperature_oversampling_config(Oversampling temperature_oversampling) {
this->temperature_oversampling_ = temperature_oversampling;
}
/// Set the oversampling value for the pressure sensor. Default is 16x.
void set_pressure_oversampling_config(Oversampling pressure_oversampling) {
this->pressure_oversampling_ = pressure_oversampling;
}
/// Set the IIR Filter used to increase accuracy, defaults to no IIR Filter.
void set_iir_filter_config(IIRFilter iir_filter) { this->iir_filter_ = iir_filter; }
/// Soft reset the sensor
uint8_t reset();
/// Start continuous measurement in NORMAL_MODE
bool start_normal_conversion();
/// Start a one shot measurement in FORCED_MODE
bool start_forced_conversion();
/// Stop the conversion and return to SLEEP_MODE
bool stop_conversion();
/// Set the pressure oversampling: OFF, X1, X2, X4, X8, X16, X32
bool set_pressure_oversampling(Oversampling pressure_oversampling);
/// Set the temperature oversampling: OFF, X1, X2, X4, X8, X16, X32
bool set_temperature_oversampling(Oversampling temperature_oversampling);
/// Set the IIR filter setting: OFF, 2, 3, 8, 16, 32
bool set_iir_filter(IIRFilter iir_filter);
/// Get a temperature measurement
bool get_temperature(float &temperature);
/// Get a pressure measurement
bool get_pressure(float &pressure);
/// Get a temperature and pressure measurement
bool get_measurements(float &temperature, float &pressure);
/// Get a temperature and pressure measurement
bool get_measurement();
/// Set the barometer mode
bool set_mode(OperationMode mode);
/// Set the BMP388 oversampling register
bool set_oversampling_register(Oversampling pressure_oversampling, Oversampling temperature_oversampling);
/// Checks if a measurement is ready
bool data_ready();
protected:
Oversampling temperature_oversampling_{OVERSAMPLING_X16};
Oversampling pressure_oversampling_{OVERSAMPLING_X16};
IIRFilter iir_filter_{IIR_FILTER_OFF};
OperationMode operation_mode_{FORCED_MODE};
sensor::Sensor *temperature_sensor_;
sensor::Sensor *pressure_sensor_;
enum ErrorCode {
NONE = 0,
ERROR_COMMUNICATION_FAILED,
ERROR_WRONG_CHIP_ID,
ERROR_SENSOR_STATUS,
ERROR_SENSOR_RESET,
} error_code_{NONE};
struct { // The BMP388 compensation trim parameters (coefficients)
uint16_t param_T1;
uint16_t param_T2;
int8_t param_T3;
int16_t param_P1;
int16_t param_P2;
int8_t param_P3;
int8_t param_P4;
uint16_t param_P5;
uint16_t param_P6;
int8_t param_P7;
int8_t param_P8;
int16_t param_P9;
int8_t param_P10;
int8_t param_P11;
} __attribute__((packed)) compensation_params_;
struct FloatParams { // The BMP388 float point compensation trim parameters
float param_T1;
float param_T2;
float param_T3;
float param_P1;
float param_P2;
float param_P3;
float param_P4;
float param_P5;
float param_P6;
float param_P7;
float param_P8;
float param_P9;
float param_P10;
float param_P11;
} compensation_float_params_;
union { // Copy of the BMP388's chip id register
struct {
uint8_t chip_id_nvm : 4;
uint8_t chip_id_fixed : 4;
} bit;
uint8_t reg;
} chip_id_ = {.reg = 0};
union { // Copy of the BMP388's event register
struct {
uint8_t por_detected : 1;
} bit;
uint8_t reg;
} event_ = {.reg = 0};
union { // Copy of the BMP388's interrupt status register
struct {
uint8_t fwm_int : 1;
uint8_t ffull_int : 1;
uint8_t : 1;
uint8_t drdy : 1;
} bit;
uint8_t reg;
} int_status_ = {.reg = 0};
union { // Copy of the BMP388's power control register
struct {
uint8_t press_en : 1;
uint8_t temp_en : 1;
uint8_t : 2;
uint8_t mode : 2;
} bit;
uint8_t reg;
} pwr_ctrl_ = {.reg = 0};
union { // Copy of the BMP388's oversampling register
struct {
uint8_t osr_p : 3;
uint8_t osr_t : 3;
} bit;
uint8_t reg;
} osr_ = {.reg = 0};
union { // Copy of the BMP388's output data rate register
struct {
uint8_t odr_sel : 5;
} bit;
uint8_t reg;
} odr_ = {.reg = 0};
union { // Copy of the BMP388's configuration register
struct {
uint8_t : 1;
uint8_t iir_filter : 3;
} bit;
uint8_t reg;
} config_ = {.reg = 0};
// Bosch temperature compensation function
float bmp388_compensate_temperature_(float uncomp_temp);
// Bosch pressure compensation function
float bmp388_compensate_pressure_(float uncomp_press, float t_lin);
};
} // namespace bmp3xx
} // namespace esphome

View File

@@ -1,100 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
CONF_ID,
CONF_IIR_FILTER,
CONF_OVERSAMPLING,
CONF_PRESSURE,
CONF_TEMPERATURE,
DEVICE_CLASS_PRESSURE,
DEVICE_CLASS_TEMPERATURE,
STATE_CLASS_MEASUREMENT,
UNIT_CELSIUS,
UNIT_HECTOPASCAL,
)
CODEOWNERS = ["@martgras"]
DEPENDENCIES = ["i2c"]
bmp3xx_ns = cg.esphome_ns.namespace("bmp3xx")
Oversampling = bmp3xx_ns.enum("Oversampling")
OVERSAMPLING_OPTIONS = {
"NONE": Oversampling.OVERSAMPLING_NONE,
"2X": Oversampling.OVERSAMPLING_X2,
"4X": Oversampling.OVERSAMPLING_X4,
"8X": Oversampling.OVERSAMPLING_X8,
"16X": Oversampling.OVERSAMPLING_X16,
"32x": Oversampling.OVERSAMPLING_X32,
}
IIRFilter = bmp3xx_ns.enum("IIRFilter")
IIR_FILTER_OPTIONS = {
"OFF": IIRFilter.IIR_FILTER_OFF,
"2X": IIRFilter.IIR_FILTER_2,
"4X": IIRFilter.IIR_FILTER_4,
"8X": IIRFilter.IIR_FILTER_8,
"16X": IIRFilter.IIR_FILTER_16,
"32X": IIRFilter.IIR_FILTER_32,
"64X": IIRFilter.IIR_FILTER_64,
"128X": IIRFilter.IIR_FILTER_128,
}
BMP3XXComponent = bmp3xx_ns.class_(
"BMP3XXComponent", cg.PollingComponent, i2c.I2CDevice
)
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(BMP3XXComponent),
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,
device_class=DEVICE_CLASS_TEMPERATURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="2X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
unit_of_measurement=UNIT_HECTOPASCAL,
accuracy_decimals=1,
device_class=DEVICE_CLASS_PRESSURE,
state_class=STATE_CLASS_MEASUREMENT,
).extend(
{
cv.Optional(CONF_OVERSAMPLING, default="16X"): cv.enum(
OVERSAMPLING_OPTIONS, upper=True
),
}
),
cv.Optional(CONF_IIR_FILTER, default="OFF"): cv.enum(
IIR_FILTER_OPTIONS, upper=True
),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x77))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
cg.add(var.set_iir_filter_config(config[CONF_IIR_FILTER]))
if CONF_TEMPERATURE in config:
conf = config[CONF_TEMPERATURE]
sens = await sensor.new_sensor(conf)
cg.add(var.set_temperature_sensor(sens))
cg.add(var.set_temperature_oversampling_config(conf[CONF_OVERSAMPLING]))
if CONF_PRESSURE in config:
conf = config[CONF_PRESSURE]
sens = await sensor.new_sensor(conf)
cg.add(var.set_pressure_sensor(sens))
cg.add(var.set_pressure_oversampling_config(conf[CONF_OVERSAMPLING]))

View File

@@ -1,53 +0,0 @@
import esphome.codegen as cg
from esphome import pins
import esphome.config_validation as cv
from esphome.const import (
CONF_DELAY,
CONF_ID,
)
CODEOWNERS = ["@asoehlke"]
AUTO_LOAD = ["sensor", "voltage_sampler"]
cd74hc4067_ns = cg.esphome_ns.namespace("cd74hc4067")
CD74HC4067Component = cd74hc4067_ns.class_(
"CD74HC4067Component", cg.Component, cg.PollingComponent
)
CONF_PIN_S0 = "pin_s0"
CONF_PIN_S1 = "pin_s1"
CONF_PIN_S2 = "pin_s2"
CONF_PIN_S3 = "pin_s3"
DEFAULT_DELAY = "2ms"
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(CD74HC4067Component),
cv.Required(CONF_PIN_S0): pins.internal_gpio_output_pin_schema,
cv.Required(CONF_PIN_S1): pins.internal_gpio_output_pin_schema,
cv.Required(CONF_PIN_S2): pins.internal_gpio_output_pin_schema,
cv.Required(CONF_PIN_S3): pins.internal_gpio_output_pin_schema,
cv.Optional(
CONF_DELAY, default=DEFAULT_DELAY
): cv.positive_time_period_milliseconds,
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
pin_s0 = await cg.gpio_pin_expression(config[CONF_PIN_S0])
cg.add(var.set_pin_s0(pin_s0))
pin_s1 = await cg.gpio_pin_expression(config[CONF_PIN_S1])
cg.add(var.set_pin_s1(pin_s1))
pin_s2 = await cg.gpio_pin_expression(config[CONF_PIN_S2])
cg.add(var.set_pin_s2(pin_s2))
pin_s3 = await cg.gpio_pin_expression(config[CONF_PIN_S3])
cg.add(var.set_pin_s3(pin_s3))
cg.add(var.set_switch_delay(config[CONF_DELAY]))

View File

@@ -1,86 +0,0 @@
#include "cd74hc4067.h"
#include "esphome/core/log.h"
namespace esphome {
namespace cd74hc4067 {
static const char *const TAG = "cd74hc4067";
float CD74HC4067Component::get_setup_priority() const { return setup_priority::DATA; }
void CD74HC4067Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up CD74HC4067...");
this->pin_s0_->setup();
this->pin_s1_->setup();
this->pin_s2_->setup();
this->pin_s3_->setup();
// set other pin, so that activate_pin will really switch
this->active_pin_ = 1;
this->activate_pin(0);
}
void CD74HC4067Component::dump_config() {
ESP_LOGCONFIG(TAG, "CD74HC4067 Multiplexer:");
LOG_PIN(" S0 Pin: ", this->pin_s0_);
LOG_PIN(" S1 Pin: ", this->pin_s1_);
LOG_PIN(" S2 Pin: ", this->pin_s2_);
LOG_PIN(" S3 Pin: ", this->pin_s3_);
ESP_LOGCONFIG(TAG, "switch delay: %d", this->switch_delay_);
}
void CD74HC4067Component::activate_pin(uint8_t pin) {
if (this->active_pin_ != pin) {
ESP_LOGD(TAG, "switch to input %d", pin);
static int mux_channel[16][4] = {
{0, 0, 0, 0}, // channel 0
{1, 0, 0, 0}, // channel 1
{0, 1, 0, 0}, // channel 2
{1, 1, 0, 0}, // channel 3
{0, 0, 1, 0}, // channel 4
{1, 0, 1, 0}, // channel 5
{0, 1, 1, 0}, // channel 6
{1, 1, 1, 0}, // channel 7
{0, 0, 0, 1}, // channel 8
{1, 0, 0, 1}, // channel 9
{0, 1, 0, 1}, // channel 10
{1, 1, 0, 1}, // channel 11
{0, 0, 1, 1}, // channel 12
{1, 0, 1, 1}, // channel 13
{0, 1, 1, 1}, // channel 14
{1, 1, 1, 1} // channel 15
};
this->pin_s0_->digital_write(mux_channel[pin][0]);
this->pin_s1_->digital_write(mux_channel[pin][1]);
this->pin_s2_->digital_write(mux_channel[pin][2]);
this->pin_s3_->digital_write(mux_channel[pin][3]);
// small delay is needed to let the multiplexer switch
delay(this->switch_delay_);
this->active_pin_ = pin;
}
}
CD74HC4067Sensor::CD74HC4067Sensor(CD74HC4067Component *parent) : parent_(parent) {}
void CD74HC4067Sensor::update() {
float value_v = this->sample();
this->publish_state(value_v);
}
float CD74HC4067Sensor::get_setup_priority() const { return this->parent_->get_setup_priority() - 1.0f; }
float CD74HC4067Sensor::sample() {
this->parent_->activate_pin(this->pin_);
return this->source_->sample();
}
void CD74HC4067Sensor::dump_config() {
LOG_SENSOR(TAG, "CD74HC4067 Sensor", this);
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
LOG_UPDATE_INTERVAL(this);
}
} // namespace cd74hc4067
} // namespace esphome

View File

@@ -1,65 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/voltage_sampler/voltage_sampler.h"
namespace esphome {
namespace cd74hc4067 {
class CD74HC4067Component : public Component {
public:
/// Set up the internal sensor array.
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
/// setting pin active by setting the right combination of the four multiplexer input pins
void activate_pin(uint8_t pin);
/// set the pin connected to multiplexer control pin 0
void set_pin_s0(InternalGPIOPin *pin) { this->pin_s0_ = pin; }
/// set the pin connected to multiplexer control pin 1
void set_pin_s1(InternalGPIOPin *pin) { this->pin_s1_ = pin; }
/// set the pin connected to multiplexer control pin 2
void set_pin_s2(InternalGPIOPin *pin) { this->pin_s2_ = pin; }
/// set the pin connected to multiplexer control pin 3
void set_pin_s3(InternalGPIOPin *pin) { this->pin_s3_ = pin; }
/// set the delay needed after an input switch
void set_switch_delay(uint32_t switch_delay) { this->switch_delay_ = switch_delay; }
private:
InternalGPIOPin *pin_s0_;
InternalGPIOPin *pin_s1_;
InternalGPIOPin *pin_s2_;
InternalGPIOPin *pin_s3_;
/// the currently active pin
uint8_t active_pin_;
uint32_t switch_delay_;
};
class CD74HC4067Sensor : public sensor::Sensor, public PollingComponent, public voltage_sampler::VoltageSampler {
public:
CD74HC4067Sensor(CD74HC4067Component *parent);
void update() override;
void dump_config() override;
/// `HARDWARE_LATE` setup priority.
float get_setup_priority() const override;
void set_pin(uint8_t pin) { this->pin_ = pin; }
void set_source(voltage_sampler::VoltageSampler *source) { this->source_ = source; }
float sample() override;
protected:
CD74HC4067Component *parent_;
/// The sampling source to read values from.
voltage_sampler::VoltageSampler *source_;
uint8_t pin_;
};
} // namespace cd74hc4067
} // namespace esphome

View File

@@ -1,55 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, voltage_sampler
from esphome.const import (
CONF_ID,
CONF_SENSOR,
CONF_NUMBER,
ICON_FLASH,
UNIT_VOLT,
STATE_CLASS_MEASUREMENT,
DEVICE_CLASS_VOLTAGE,
)
from . import cd74hc4067_ns, CD74HC4067Component
DEPENDENCIES = ["cd74hc4067"]
CD74HC4067Sensor = cd74hc4067_ns.class_(
"CD74HC4067Sensor",
sensor.Sensor,
cg.PollingComponent,
voltage_sampler.VoltageSampler,
)
CONF_CD74HC4067_ID = "cd74hc4067_id"
CONFIG_SCHEMA = (
sensor.sensor_schema(
unit_of_measurement=UNIT_VOLT,
accuracy_decimals=3,
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
icon=ICON_FLASH,
)
.extend(
{
cv.GenerateID(): cv.declare_id(CD74HC4067Sensor),
cv.GenerateID(CONF_CD74HC4067_ID): cv.use_id(CD74HC4067Component),
cv.Required(CONF_NUMBER): cv.int_range(0, 15),
cv.Required(CONF_SENSOR): cv.use_id(voltage_sampler.VoltageSampler),
}
)
.extend(cv.polling_component_schema("60s"))
)
async def to_code(config):
parent = await cg.get_variable(config[CONF_CD74HC4067_ID])
var = cg.new_Pvariable(config[CONF_ID], parent)
await cg.register_component(var, config)
await sensor.register_sensor(var, config)
cg.add(var.set_pin(config[CONF_NUMBER]))
sens = await cg.get_variable(config[CONF_SENSOR])
cg.add(var.set_source(sens))

View File

@@ -10,22 +10,21 @@ climate::ClimateTraits ClimateIR::traits() {
auto traits = climate::ClimateTraits();
traits.set_supports_current_temperature(this->sensor_ != nullptr);
traits.set_supported_modes({climate::CLIMATE_MODE_OFF, climate::CLIMATE_MODE_HEAT_COOL});
if (this->supports_cool_)
if (supports_cool_)
traits.add_supported_mode(climate::CLIMATE_MODE_COOL);
if (this->supports_heat_)
if (supports_heat_)
traits.add_supported_mode(climate::CLIMATE_MODE_HEAT);
if (this->supports_dry_)
if (supports_dry_)
traits.add_supported_mode(climate::CLIMATE_MODE_DRY);
if (this->supports_fan_only_)
if (supports_fan_only_)
traits.add_supported_mode(climate::CLIMATE_MODE_FAN_ONLY);
traits.set_supports_two_point_target_temperature(false);
traits.set_visual_min_temperature(this->minimum_temperature_);
traits.set_visual_max_temperature(this->maximum_temperature_);
traits.set_visual_temperature_step(this->temperature_step_);
traits.set_supported_fan_modes(this->fan_modes_);
traits.set_supported_swing_modes(this->swing_modes_);
traits.set_supported_presets(this->presets_);
traits.set_supported_fan_modes(fan_modes_);
traits.set_supported_swing_modes(swing_modes_);
return traits;
}
@@ -51,7 +50,6 @@ void ClimateIR::setup() {
roundf(clamp(this->current_temperature, this->minimum_temperature_, this->maximum_temperature_));
this->fan_mode = climate::CLIMATE_FAN_AUTO;
this->swing_mode = climate::CLIMATE_SWING_OFF;
this->preset = climate::CLIMATE_PRESET_NONE;
}
// Never send nan to HA
if (std::isnan(this->target_temperature))
@@ -67,8 +65,6 @@ void ClimateIR::control(const climate::ClimateCall &call) {
this->fan_mode = *call.get_fan_mode();
if (call.get_swing_mode().has_value())
this->swing_mode = *call.get_swing_mode();
if (call.get_preset().has_value())
this->preset = *call.get_preset();
this->transmit_state();
this->publish_state();
}

View File

@@ -22,7 +22,7 @@ class ClimateIR : public climate::Climate, public Component, public remote_base:
public:
ClimateIR(float minimum_temperature, float maximum_temperature, float temperature_step = 1.0f,
bool supports_dry = false, bool supports_fan_only = false, std::set<climate::ClimateFanMode> fan_modes = {},
std::set<climate::ClimateSwingMode> swing_modes = {}, std::set<climate::ClimatePreset> presets = {}) {
std::set<climate::ClimateSwingMode> swing_modes = {}) {
this->minimum_temperature_ = minimum_temperature;
this->maximum_temperature_ = maximum_temperature;
this->temperature_step_ = temperature_step;
@@ -30,7 +30,6 @@ class ClimateIR : public climate::Climate, public Component, public remote_base:
this->supports_fan_only_ = supports_fan_only;
this->fan_modes_ = std::move(fan_modes);
this->swing_modes_ = std::move(swing_modes);
this->presets_ = std::move(presets);
}
void setup() override;
@@ -62,7 +61,6 @@ class ClimateIR : public climate::Climate, public Component, public remote_base:
bool supports_fan_only_{false};
std::set<climate::ClimateFanMode> fan_modes_ = {};
std::set<climate::ClimateSwingMode> swing_modes_ = {};
std::set<climate::ClimatePreset> presets_ = {};
remote_transmitter::RemoteTransmitterComponent *transmitter_;
sensor::Sensor *sensor_{nullptr};

View File

@@ -1,5 +1,4 @@
#include "coolix.h"
#include "esphome/components/remote_base/coolix_protocol.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -7,29 +6,29 @@ namespace coolix {
static const char *const TAG = "coolix.climate";
static const uint32_t COOLIX_OFF = 0xB27BE0;
static const uint32_t COOLIX_SWING = 0xB26BE0;
static const uint32_t COOLIX_LED = 0xB5F5A5;
static const uint32_t COOLIX_SILENCE_FP = 0xB5F5B6;
const uint32_t COOLIX_OFF = 0xB27BE0;
const uint32_t COOLIX_SWING = 0xB26BE0;
const uint32_t COOLIX_LED = 0xB5F5A5;
const uint32_t COOLIX_SILENCE_FP = 0xB5F5B6;
// On, 25C, Mode: Auto, Fan: Auto, Zone Follow: Off, Sensor Temp: Ignore.
static const uint8_t COOLIX_COOL = 0b0000;
static const uint8_t COOLIX_DRY_FAN = 0b0100;
static const uint8_t COOLIX_AUTO = 0b1000;
static const uint8_t COOLIX_HEAT = 0b1100;
static const uint32_t COOLIX_MODE_MASK = 0b1100;
static const uint32_t COOLIX_FAN_MASK = 0xF000;
static const uint32_t COOLIX_FAN_MODE_AUTO_DRY = 0x1000;
static const uint32_t COOLIX_FAN_AUTO = 0xB000;
static const uint32_t COOLIX_FAN_MIN = 0x9000;
static const uint32_t COOLIX_FAN_MED = 0x5000;
static const uint32_t COOLIX_FAN_MAX = 0x3000;
const uint8_t COOLIX_COOL = 0b0000;
const uint8_t COOLIX_DRY_FAN = 0b0100;
const uint8_t COOLIX_AUTO = 0b1000;
const uint8_t COOLIX_HEAT = 0b1100;
const uint32_t COOLIX_MODE_MASK = 0b1100;
const uint32_t COOLIX_FAN_MASK = 0xF000;
const uint32_t COOLIX_FAN_MODE_AUTO_DRY = 0x1000;
const uint32_t COOLIX_FAN_AUTO = 0xB000;
const uint32_t COOLIX_FAN_MIN = 0x9000;
const uint32_t COOLIX_FAN_MED = 0x5000;
const uint32_t COOLIX_FAN_MAX = 0x3000;
// Temperature
static const uint8_t COOLIX_TEMP_RANGE = COOLIX_TEMP_MAX - COOLIX_TEMP_MIN + 1;
static const uint8_t COOLIX_FAN_TEMP_CODE = 0b11100000; // Part of Fan Mode.
static const uint32_t COOLIX_TEMP_MASK = 0b11110000;
static const uint8_t COOLIX_TEMP_MAP[COOLIX_TEMP_RANGE] = {
const uint8_t COOLIX_TEMP_RANGE = COOLIX_TEMP_MAX - COOLIX_TEMP_MIN + 1;
const uint8_t COOLIX_FAN_TEMP_CODE = 0b11100000; // Part of Fan Mode.
const uint32_t COOLIX_TEMP_MASK = 0b11110000;
const uint8_t COOLIX_TEMP_MAP[COOLIX_TEMP_RANGE] = {
0b00000000, // 17C
0b00010000, // 18c
0b00110000, // 19C
@@ -46,6 +45,17 @@ static const uint8_t COOLIX_TEMP_MAP[COOLIX_TEMP_RANGE] = {
0b10110000 // 30C
};
// Constants
static const uint32_t BIT_MARK_US = 660;
static const uint32_t HEADER_MARK_US = 560 * 8;
static const uint32_t HEADER_SPACE_US = 560 * 8;
static const uint32_t BIT_ONE_SPACE_US = 1500;
static const uint32_t BIT_ZERO_SPACE_US = 450;
static const uint32_t FOOTER_MARK_US = BIT_MARK_US;
static const uint32_t FOOTER_SPACE_US = HEADER_SPACE_US;
const uint16_t COOLIX_BITS = 24;
void CoolixClimate::transmit_state() {
uint32_t remote_state = 0xB20F00;
@@ -101,60 +111,119 @@ void CoolixClimate::transmit_state() {
}
}
}
ESP_LOGV(TAG, "Sending coolix code: 0x%06X", remote_state);
ESP_LOGV(TAG, "Sending coolix code: 0x%02X", remote_state);
auto transmit = this->transmitter_->transmit();
auto data = transmit.get_data();
remote_base::CoolixProtocol().encode(data, remote_state);
data->set_carrier_frequency(38000);
uint16_t repeat = 1;
for (uint16_t r = 0; r <= repeat; r++) {
// Header
data->mark(HEADER_MARK_US);
data->space(HEADER_SPACE_US);
// Data
// Break data into bytes, starting at the Most Significant
// Byte. Each byte then being sent normal, then followed inverted.
for (uint16_t i = 8; i <= COOLIX_BITS; i += 8) {
// Grab a bytes worth of data.
uint8_t byte = (remote_state >> (COOLIX_BITS - i)) & 0xFF;
// Normal
for (uint64_t mask = 1ULL << 7; mask; mask >>= 1) {
data->mark(BIT_MARK_US);
data->space((byte & mask) ? BIT_ONE_SPACE_US : BIT_ZERO_SPACE_US);
}
// Inverted
for (uint64_t mask = 1ULL << 7; mask; mask >>= 1) {
data->mark(BIT_MARK_US);
data->space(!(byte & mask) ? BIT_ONE_SPACE_US : BIT_ZERO_SPACE_US);
}
}
// Footer
data->mark(BIT_MARK_US);
data->space(FOOTER_SPACE_US); // Pause before repeating
}
transmit.perform();
}
bool CoolixClimate::on_coolix(climate::Climate *parent, remote_base::RemoteReceiveData data) {
auto decoded = remote_base::CoolixProtocol().decode(data);
if (!decoded.has_value())
return false;
bool CoolixClimate::on_receive(remote_base::RemoteReceiveData data) {
// Decoded remote state y 3 bytes long code.
uint32_t remote_state = *decoded;
ESP_LOGV(TAG, "Decoded 0x%06X", remote_state);
if ((remote_state & 0xFF0000) != 0xB20000)
uint32_t remote_state = 0;
// The protocol sends the data twice, read here
uint32_t loop_read;
for (uint16_t loop = 1; loop <= 2; loop++) {
if (!data.expect_item(HEADER_MARK_US, HEADER_SPACE_US))
return false;
loop_read = 0;
for (uint8_t a_byte = 0; a_byte < 3; a_byte++) {
uint8_t byte = 0;
for (int8_t a_bit = 7; a_bit >= 0; a_bit--) {
if (data.expect_item(BIT_MARK_US, BIT_ONE_SPACE_US))
byte |= 1 << a_bit;
else if (!data.expect_item(BIT_MARK_US, BIT_ZERO_SPACE_US))
return false;
}
// Need to see this segment inverted
for (int8_t a_bit = 7; a_bit >= 0; a_bit--) {
bool bit = byte & (1 << a_bit);
if (!data.expect_item(BIT_MARK_US, bit ? BIT_ZERO_SPACE_US : BIT_ONE_SPACE_US))
return false;
}
// Receiving MSB first: reorder bytes
loop_read |= byte << ((2 - a_byte) * 8);
}
// Footer Mark
if (!data.expect_mark(BIT_MARK_US))
return false;
if (loop == 1) {
// Back up state on first loop
remote_state = loop_read;
if (!data.expect_space(FOOTER_SPACE_US))
return false;
}
}
ESP_LOGV(TAG, "Decoded 0x%02X", remote_state);
if (remote_state != loop_read || (remote_state & 0xFF0000) != 0xB20000)
return false;
if (remote_state == COOLIX_OFF) {
parent->mode = climate::CLIMATE_MODE_OFF;
this->mode = climate::CLIMATE_MODE_OFF;
} else if (remote_state == COOLIX_SWING) {
parent->swing_mode =
parent->swing_mode == climate::CLIMATE_SWING_OFF ? climate::CLIMATE_SWING_VERTICAL : climate::CLIMATE_SWING_OFF;
this->swing_mode =
this->swing_mode == climate::CLIMATE_SWING_OFF ? climate::CLIMATE_SWING_VERTICAL : climate::CLIMATE_SWING_OFF;
} else {
if ((remote_state & COOLIX_MODE_MASK) == COOLIX_HEAT)
parent->mode = climate::CLIMATE_MODE_HEAT;
this->mode = climate::CLIMATE_MODE_HEAT;
else if ((remote_state & COOLIX_MODE_MASK) == COOLIX_AUTO)
parent->mode = climate::CLIMATE_MODE_HEAT_COOL;
this->mode = climate::CLIMATE_MODE_HEAT_COOL;
else if ((remote_state & COOLIX_MODE_MASK) == COOLIX_DRY_FAN) {
if ((remote_state & COOLIX_FAN_MASK) == COOLIX_FAN_MODE_AUTO_DRY)
parent->mode = climate::CLIMATE_MODE_DRY;
this->mode = climate::CLIMATE_MODE_DRY;
else
parent->mode = climate::CLIMATE_MODE_FAN_ONLY;
this->mode = climate::CLIMATE_MODE_FAN_ONLY;
} else
parent->mode = climate::CLIMATE_MODE_COOL;
this->mode = climate::CLIMATE_MODE_COOL;
// Fan Speed
if ((remote_state & COOLIX_FAN_AUTO) == COOLIX_FAN_AUTO || parent->mode == climate::CLIMATE_MODE_HEAT_COOL ||
parent->mode == climate::CLIMATE_MODE_DRY)
parent->fan_mode = climate::CLIMATE_FAN_AUTO;
if ((remote_state & COOLIX_FAN_AUTO) == COOLIX_FAN_AUTO || this->mode == climate::CLIMATE_MODE_HEAT_COOL ||
this->mode == climate::CLIMATE_MODE_DRY)
this->fan_mode = climate::CLIMATE_FAN_AUTO;
else if ((remote_state & COOLIX_FAN_MIN) == COOLIX_FAN_MIN)
parent->fan_mode = climate::CLIMATE_FAN_LOW;
this->fan_mode = climate::CLIMATE_FAN_LOW;
else if ((remote_state & COOLIX_FAN_MED) == COOLIX_FAN_MED)
parent->fan_mode = climate::CLIMATE_FAN_MEDIUM;
this->fan_mode = climate::CLIMATE_FAN_MEDIUM;
else if ((remote_state & COOLIX_FAN_MAX) == COOLIX_FAN_MAX)
parent->fan_mode = climate::CLIMATE_FAN_HIGH;
this->fan_mode = climate::CLIMATE_FAN_HIGH;
// Temperature
uint8_t temperature_code = remote_state & COOLIX_TEMP_MASK;
for (uint8_t i = 0; i < COOLIX_TEMP_RANGE; i++)
if (COOLIX_TEMP_MAP[i] == temperature_code)
parent->target_temperature = i + COOLIX_TEMP_MIN;
this->target_temperature = i + COOLIX_TEMP_MIN;
}
parent->publish_state();
this->publish_state();
return true;
}

View File

@@ -26,15 +26,11 @@ class CoolixClimate : public climate_ir::ClimateIR {
climate_ir::ClimateIR::control(call);
}
/// This static method can be used in other climate components that accept the Coolix protocol. See midea_ir for
/// example.
static bool on_coolix(climate::Climate *parent, remote_base::RemoteReceiveData data);
protected:
/// Transmit via IR the state of this climate controller.
void transmit_state() override;
/// Handle received IR Buffer
bool on_receive(remote_base::RemoteReceiveData data) override { return CoolixClimate::on_coolix(this, data); }
bool on_receive(remote_base::RemoteReceiveData data) override;
bool send_swing_cmd_{false};
};

View File

@@ -210,10 +210,7 @@ Cover::Cover() : Cover("") {}
std::string Cover::get_device_class() {
if (this->device_class_override_.has_value())
return *this->device_class_override_;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
return this->device_class();
#pragma GCC diagnostic pop
}
bool Cover::is_fully_open() const { return this->position == COVER_OPEN; }
bool Cover::is_fully_closed() const { return this->position == COVER_CLOSED; }

View File

@@ -169,11 +169,6 @@ class Cover : public EntityBase {
friend CoverCall;
virtual void control(const CoverCall &call) = 0;
/** Override this to set the default device class.
*
* @deprecated This method is deprecated, set the property during config validation instead. (2022.1)
*/
virtual std::string device_class();
optional<CoverRestoreState> restore_state_();

View File

@@ -38,9 +38,10 @@ void DallasComponent::setup() {
raw_sensors = this->one_wire_->search_vec();
for (auto &address : raw_sensors) {
std::string s = uint64_to_string(address);
auto *address8 = reinterpret_cast<uint8_t *>(&address);
if (crc8(address8, 7) != address8[7]) {
ESP_LOGW(TAG, "Dallas device 0x%s has invalid CRC.", format_hex(address).c_str());
ESP_LOGW(TAG, "Dallas device 0x%s has invalid CRC.", s.c_str());
continue;
}
if (address8[0] != DALLAS_MODEL_DS18S20 && address8[0] != DALLAS_MODEL_DS1822 &&
@@ -76,7 +77,8 @@ void DallasComponent::dump_config() {
} else {
ESP_LOGD(TAG, " Found sensors:");
for (auto &address : this->found_sensors_) {
ESP_LOGD(TAG, " 0x%s", format_hex(address).c_str());
std::string s = uint64_to_string(address);
ESP_LOGD(TAG, " 0x%s", s.c_str());
}
}
@@ -145,7 +147,7 @@ void DallasTemperatureSensor::set_index(uint8_t index) { this->index_ = index; }
uint8_t *DallasTemperatureSensor::get_address8() { return reinterpret_cast<uint8_t *>(&this->address_); }
const std::string &DallasTemperatureSensor::get_address_name() {
if (this->address_name_.empty()) {
this->address_name_ = std::string("0x") + format_hex(this->address_);
this->address_name_ = std::string("0x") + uint64_to_string(this->address_);
}
return this->address_name_;
@@ -235,7 +237,7 @@ float DallasTemperatureSensor::get_temp_c() {
return temp / 128.0f;
}
std::string DallasTemperatureSensor::unique_id() { return "dallas-" + str_upper_case(format_hex(this->address_)); }
std::string DallasTemperatureSensor::unique_id() { return "dallas-" + uint64_to_string(this->address_); }
} // namespace dallas
} // namespace esphome

View File

@@ -3,7 +3,6 @@ from pathlib import Path
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components.packages import validate_source_shorthand
from esphome.wizard import wizard_file
from esphome.yaml_util import dump
@@ -49,24 +48,12 @@ def import_config(path: str, name: str, project_name: str, import_url: str) -> N
if p.exists():
raise FileExistsError
if project_name == "esphome.web":
p.write_text(
wizard_file(
name=name,
platform="ESP32" if "esp32" in import_url else "ESP8266",
board="esp32dev" if "esp32" in import_url else "esp01_1m",
ssid="!secret wifi_ssid",
psk="!secret wifi_password",
),
encoding="utf8",
)
else:
config = {
"substitutions": {"name": name},
"packages": {project_name: import_url},
"esphome": {"name_add_mac_suffix": False},
}
p.write_text(
dump(config) + WIFI_CONFIG,
encoding="utf8",
)
config = {
"substitutions": {"name": name},
"packages": {project_name: import_url},
"esphome": {"name_add_mac_suffix": False},
}
p.write_text(
dump(config) + WIFI_CONFIG,
encoding="utf8",
)

View File

@@ -101,7 +101,7 @@ void DebugComponent::dump_config() {
info.features &= ~CHIP_FEATURE_BT;
}
if (info.features)
features += "Other:" + format_hex(info.features);
features += "Other:" + uint64_to_string(info.features);
ESP_LOGD(TAG, "Chip: Model=%s, Features=%s Cores=%u, Revision=%u", model, features.c_str(), info.cores,
info.revision);

View File

@@ -33,7 +33,7 @@ DHT = dht_ns.class_("DHT", cg.PollingComponent)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(DHT),
cv.Required(CONF_PIN): pins.internal_gpio_input_pin_schema,
cv.Required(CONF_PIN): pins.gpio_input_pin_schema,
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_CELSIUS,
accuracy_decimals=1,

View File

@@ -5,7 +5,6 @@
#include "esphome/core/color.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
#include "esphome/core/helpers.h"
namespace esphome {
namespace display {
@@ -16,8 +15,7 @@ const Color COLOR_OFF(0, 0, 0, 0);
const Color COLOR_ON(255, 255, 255, 255);
void DisplayBuffer::init_internal_(uint32_t buffer_length) {
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
this->buffer_ = allocator.allocate(buffer_length);
this->buffer_ = new (std::nothrow) uint8_t[buffer_length]; // NOLINT
if (this->buffer_ == nullptr) {
ESP_LOGE(TAG, "Could not allocate buffer for display!");
return;

View File

@@ -2,9 +2,8 @@ from dataclasses import dataclass
from typing import Union
from pathlib import Path
import logging
import os
from esphome.helpers import copy_file_if_changed, write_file_if_changed
from esphome.helpers import write_file_if_changed
from esphome.const import (
CONF_BOARD,
CONF_FRAMEWORK,
@@ -296,8 +295,6 @@ async def to_code(config):
conf = config[CONF_FRAMEWORK]
cg.add_platformio_option("platform", conf[CONF_PLATFORM_VERSION])
cg.add_platformio_option("extra_scripts", ["post:post_build.py"])
if conf[CONF_TYPE] == FRAMEWORK_ESP_IDF:
cg.add_platformio_option("framework", "espidf")
cg.add_build_flag("-DUSE_ESP_IDF")
@@ -415,10 +412,3 @@ def copy_files():
CORE.relative_build_path("partitions.csv"),
IDF_PARTITIONS_CSV,
)
dir = os.path.dirname(__file__)
post_build_file = os.path.join(dir, "post_build.py.script")
copy_file_if_changed(
post_build_file,
CORE.relative_build_path("post_build.py"),
)

View File

@@ -261,37 +261,6 @@ ESP32_BOARD_PINS = {
"SS": 33,
"TX": 17,
},
"featheresp32-s2": {
"SDA": 3,
"SCL": 4,
"SS": 42,
"MOSI": 35,
"SCK": 36,
"MISO": 37,
"A0": 18,
"A1": 17,
"A10": 27,
"A11": 12,
"A12": 13,
"A13": 35,
"A2": 16,
"A3": 15,
"A4": 14,
"A5": 8,
"LED": 13,
"TX": 39,
"RX": 38,
"T5": 5,
"T8": 8,
"T9": 9,
"T10": 10,
"T11": 11,
"T12": 12,
"T13": 13,
"T14": 14,
"DAC1": 17,
"DAC2": 18,
},
"firebeetle32": {"LED": 2},
"fm-devkit": {
"D0": 34,

View File

@@ -1,43 +0,0 @@
# Source https://github.com/letscontrolit/ESPEasy/pull/3845#issuecomment-1005864664
import esptool
# pylint: disable=E0602
Import("env") # noqa
def esp32_create_combined_bin(source, target, env):
print("Generating combined binary for serial flashing")
app_offset = 0x10000
new_file_name = env.subst("$BUILD_DIR/${PROGNAME}-factory.bin")
sections = env.subst(env.get("FLASH_EXTRA_IMAGES"))
firmware_name = env.subst("$BUILD_DIR/${PROGNAME}.bin")
chip = env.get("BOARD_MCU")
flash_size = env.BoardConfig().get("upload.flash_size")
cmd = [
"--chip",
chip,
"merge_bin",
"-o",
new_file_name,
"--flash_size",
flash_size,
]
print(" Offset | File")
for section in sections:
sect_adr, sect_file = section.split(" ", 1)
print(f" - {sect_adr} | {sect_file}")
cmd += [sect_adr, sect_file]
print(f" - {hex(app_offset)} | {firmware_name}")
cmd += [hex(app_offset), firmware_name]
print()
print(f"Using esptool.py arguments: {' '.join(cmd)}")
print()
esptool.main(cmd)
# pylint: disable=E0602
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp32_create_combined_bin) # noqa

View File

@@ -1,60 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import esp32_ble_server, logger
from esphome.const import (
CONF_BLE_SERVER_ID,
CONF_ID,
CONF_LEVEL,
CONF_LOGGER,
ESP_PLATFORM_ESP32,
)
AUTO_LOAD = ["esp32_ble_server"]
ESP_PLATFORMS = [ESP_PLATFORM_ESP32]
CODEOWNERS = ["@jesserockz"]
CONFLICTS_WITH = ["esp32_ble_tracker", "esp32_ble_beacon"]
CONF_LOG_LEVEL = "log_level"
esp32_ble_controller_ns = cg.esphome_ns.namespace("esp32_ble_controller")
BLEController = esp32_ble_controller_ns.class_(
"BLEController",
cg.Component,
cg.Controller,
esp32_ble_server.BLEServiceComponent,
)
def validate(config, item_config):
global_level = config[CONF_LOGGER][CONF_LEVEL]
level = item_config.get(CONF_LOG_LEVEL, "DEBUG")
if logger.LOG_LEVEL_SEVERITY.index(level) > logger.LOG_LEVEL_SEVERITY.index(
global_level
):
raise ValueError(
"The esp32_ble_controller log level {} must be less severe than the "
"global log level {}.".format(level, global_level)
)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(BLEController),
cv.GenerateID(CONF_BLE_SERVER_ID): cv.use_id(esp32_ble_server.BLEServer),
cv.Optional(CONF_LOG_LEVEL): cv.All(
cv.requires_component("logger"),
logger.is_log_level,
),
}
).extend(cv.COMPONENT_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
ble_server = await cg.get_variable(config[CONF_BLE_SERVER_ID])
cg.add(ble_server.register_service_component(var))
if CONF_LOG_LEVEL in config:
cg.add(var.set_log_level(logger.LOG_LEVELS[config[CONF_LOG_LEVEL]]))

View File

@@ -1,387 +0,0 @@
#include "ble_controller.h"
#include "esphome/core/application.h"
#include "esphome/components/esp32_ble_server/ble_2901.h"
#include "esphome/components/esp32_ble_server/ble_2902.h"
#ifdef USE_LOGGER
#include "esphome/components/logger/logger.h"
#endif
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace esp32_ble_controller {
static const char *const ESPHOME_SERVICE_UUID = "03774663-d394-496e-8dcd-000000000000";
static const char *const LOGGER_CHARACTERISTIC_UUID = "03774663-d394-496e-8dcd-000000000001";
static const char *const BINARY_SENSOR_SERVICE_UUID = "03774663-d394-496e-8dcd-000100000000";
static const char *const COVER_SERVICE_UUID = "03774663-d394-496e-8dcd-000200000000";
static const char *const FAN_SERVICE_UUID = "03774663-d394-496e-8dcd-000300000000";
static const char *const LIGHT_SERVICE_UUID = "03774663-d394-496e-8dcd-000400000000";
static const char *const SENSOR_SERVICE_UUID = "03774663-d394-496e-8dcd-000500000000";
static const char *const SWITCH_SERVICE_UUID = "03774663-d394-496e-8dcd-000600000000";
static const char *const TEXT_SENSOR_SERVICE_UUID = "03774663-d394-496e-8dcd-000700000000";
static const char *const CLIMATE_SERVICE_UUID = "03774663-d394-496e-8dcd-000800000000";
static const char *const TAG = "esp32_ble_controller";
void BLEController::setup() {
ESP_LOGD(TAG, "Setting up BLE controller");
this->esphome_service_ = global_ble_server->create_service(ESPHOME_SERVICE_UUID);
#ifdef USE_LOGGER
{
this->logger_characteristic_ = this->esphome_service_->create_characteristic(
LOGGER_CHARACTERISTIC_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor *logger_name = new BLE2901("Logger");
this->logger_characteristic_->add_descriptor(logger_name);
BLEDescriptor *descriptor_2902 = new BLE2902();
this->logger_characteristic_->add_descriptor(descriptor_2902);
}
#endif
#ifdef USE_BINARY_SENSOR
{
auto binary_sensors = App.get_binary_sensors();
if (!binary_sensors.empty()) {
this->binary_sensor_service_ = global_ble_server->create_service(BINARY_SENSOR_SERVICE_UUID);
for (auto *obj : binary_sensors) {
std::string uuid = std::string(BINARY_SENSOR_SERVICE_UUID).substr(0, 28);
uuid += uint32_to_string(obj->get_object_id_hash());
BLECharacteristic *characteristic = this->binary_sensor_service_->create_characteristic(
uuid, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor *name = new BLE2901(obj->get_name());
characteristic->add_descriptor(name);
BLEDescriptor *descriptor = new BLE2902();
characteristic->add_descriptor(descriptor);
this->characteristics_.insert(
std::pair<uint32_t, BLECharacteristic *>(obj->get_object_id_hash(), characteristic));
}
}
}
#endif
#ifdef USE_COVER
if (!App.get_covers().empty()) {
this->cover_service_ = global_ble_server->create_service(COVER_SERVICE_UUID);
}
#endif
#ifdef USE_FAN
if (!App.get_fans().empty()) {
this->fan_service_ = global_ble_server->create_service(FAN_SERVICE_UUID);
}
#endif
#ifdef USE_LIGHT
if (!App.get_lights().empty()) {
this->light_service_ = global_ble_server->create_service(LIGHT_SERVICE_UUID);
}
#endif
#ifdef USE_SENSOR
{
auto sensors = App.get_sensors();
if (!sensors.empty()) {
this->sensor_service_ = global_ble_server->create_service(SENSOR_SERVICE_UUID);
for (auto *obj : sensors) {
std::string uuid = std::string(SENSOR_SERVICE_UUID).substr(0, 28);
uuid += uint32_to_string(obj->get_object_id_hash());
BLECharacteristic *characteristic = this->sensor_service_->create_characteristic(
uuid, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor *name = new BLE2901(obj->get_name());
characteristic->add_descriptor(name);
BLEDescriptor *descriptor = new BLE2902();
characteristic->add_descriptor(descriptor);
this->characteristics_.insert(
std::pair<uint32_t, BLECharacteristic *>(obj->get_object_id_hash(), characteristic));
}
}
}
#endif
#ifdef USE_SWITCH
{
auto switches = App.get_switches();
if (!switches.empty()) {
this->switch_service_ = global_ble_server->create_service(SWITCH_SERVICE_UUID);
for (auto *obj : switches) {
std::string uuid = std::string(SWITCH_SERVICE_UUID).substr(0, 28);
uuid += uint32_to_string(obj->get_object_id_hash());
BLECharacteristic *characteristic = this->switch_service_->create_characteristic(
uuid,
BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY | BLECharacteristic::PROPERTY_WRITE);
BLEDescriptor *name = new BLE2901(obj->get_name());
characteristic->add_descriptor(name);
BLEDescriptor *descriptor = new BLE2902();
characteristic->add_descriptor(descriptor);
this->characteristics_.insert(
std::pair<uint32_t, BLECharacteristic *>(obj->get_object_id_hash(), characteristic));
characteristic->on_write([obj](std::vector<uint8_t> data) {
if (data[0])
obj->turn_on();
else
obj->turn_off();
});
}
}
}
#endif
#ifdef USE_TEXT_SENSOR
{
auto text_sensors = App.get_text_sensors();
if (!text_sensors.empty()) {
this->text_sensor_service_ = global_ble_server->create_service(TEXT_SENSOR_SERVICE_UUID);
for (auto *obj : text_sensors) {
std::string uuid = std::string(TEXT_SENSOR_SERVICE_UUID).substr(0, 28);
uuid += uint32_to_string(obj->get_object_id_hash());
BLECharacteristic *characteristic = this->text_sensor_service_->create_characteristic(
uuid, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor *name = new BLE2901(obj->get_name());
characteristic->add_descriptor(name);
BLEDescriptor *descriptor = new BLE2902();
characteristic->add_descriptor(descriptor);
this->characteristics_.insert(
std::pair<uint32_t, BLECharacteristic *>(obj->get_object_id_hash(), characteristic));
}
}
}
#endif
#ifdef USE_CLIMATE
if (!App.get_climates().empty()) {
this->cover_service_ = global_ble_server->create_service(COVER_SERVICE_UUID);
}
#endif
this->state_ = CREATING;
this->setup_controller();
}
void BLEController::loop() {
switch (this->state_) {
case CREATING: {
bool all_created = true;
all_created &= this->esphome_service_->is_created();
#ifdef USE_BINARY_SENSOR
all_created &= this->binary_sensor_service_ == nullptr || this->binary_sensor_service_->is_created();
#endif
#ifdef USE_COVER
all_created &= this->cover_service_ == nullptr || this->cover_service_->is_created();
#endif
#ifdef USE_FAN
all_created &= this->fan_service_ == nullptr || this->fan_service_->is_created();
#endif
#ifdef USE_LIGHT
all_created &= this->light_service_ == nullptr || this->light_service_->is_created();
#endif
#ifdef USE_SENSOR
all_created &= this->sensor_service_ == nullptr || this->sensor_service_->is_created();
#endif
#ifdef USE_SWITCH
all_created &= this->switch_service_ == nullptr || this->switch_service_->is_created();
#endif
#ifdef USE_TEXT_SENSOR
all_created &= this->text_sensor_service_ == nullptr || this->text_sensor_service_->is_created();
#endif
#ifdef USE_CLIMATE
all_created &= this->climate_service_ == nullptr || this->climate_service_->is_created();
#endif
if (all_created) {
ESP_LOGI(TAG, "All services created");
this->state_ = STARTING;
}
break;
}
case STARTING: {
bool all_running = true;
all_running &= this->esphome_service_->is_running();
#ifdef USE_BINARY_SENSOR
all_running &= this->binary_sensor_service_ == nullptr || this->binary_sensor_service_->is_running();
#endif
#ifdef USE_COVER
all_running &= this->cover_service_ == nullptr || this->cover_service_->is_running();
#endif
#ifdef USE_FAN
all_running &= this->fan_service_ == nullptr || this->fan_service_->is_running();
#endif
#ifdef USE_LIGHT
all_running &= this->light_service_ == nullptr || this->light_service_->is_running();
#endif
#ifdef USE_SENSOR
all_running &= this->sensor_service_ == nullptr || this->sensor_service_->is_running();
#endif
#ifdef USE_SWITCH
all_running &= this->switch_service_ == nullptr || this->switch_service_->is_running();
#endif
#ifdef USE_TEXT_SENSOR
all_running &= this->text_sensor_service_ == nullptr || this->text_sensor_service_->is_running();
#endif
#ifdef USE_CLIMATE
all_running &= this->climate_service_ == nullptr || this->climate_service_->is_running();
#endif
if (all_running) {
ESP_LOGD(TAG, "BLE Controller started");
this->state_ = RUNNING;
#ifdef USE_LOGGER
logger::global_logger->add_on_log_callback([this](int level, const char *tag, const char *message) {
if (level > this->log_level_)
return;
std::string log;
log += "[";
log += tag;
log += "] ";
log += message;
this->logger_characteristic_->set_value(log);
this->logger_characteristic_->notify();
});
#endif
} else {
this->esphome_service_->start();
#ifdef USE_BINARY_SENSOR
this->binary_sensor_service_->start();
#endif
#ifdef USE_COVER
this->cover_service_->start();
#endif
#ifdef USE_FAN
this->fan_service_->start();
#endif
#ifdef USE_LIGHT
this->light_service_->start();
#endif
#ifdef USE_SENSOR
this->sensor_service_->start();
#endif
#ifdef USE_SWITCH
this->switch_service_->start();
#endif
#ifdef USE_TEXT_SENSOR
this->text_sensor_service_->start();
#endif
#ifdef USE_CLIMATE
this->climate_service_->start();
#endif
}
break;
}
case RUNNING:
case INIT:
break;
default:
break;
}
}
void BLEController::start() {
if (this->state_ == RUNNING)
return;
this->state_ = STARTING;
}
void BLEController::stop() {
this->esphome_service_->stop();
#ifdef USE_BINARY_SENSOR
this->binary_sensor_service_->stop();
#endif
#ifdef USE_COVER
this->cover_service_->stop();
#endif
#ifdef USE_FAN
this->fan_service_->stop();
#endif
#ifdef USE_LIGHT
this->light_service_->stop();
#endif
#ifdef USE_SENSOR
this->sensor_service_->stop();
#endif
#ifdef USE_SWITCH
this->switch_service_->stop();
#endif
#ifdef USE_TEXT_SENSOR
this->text_sensor_service_->stop();
#endif
#ifdef USE_CLIMATE
this->climate_service_->stop();
#endif
}
float BLEController::get_setup_priority() const { return setup_priority::AFTER_BLUETOOTH; }
#ifdef USE_BINARY_SENSOR
void BLEController::on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) {
if (obj->is_internal())
return;
auto *characteristic = this->characteristics_[obj->get_object_id_hash()];
characteristic->set_value(state);
characteristic->notify();
}
#endif
#ifdef USE_COVER
void BLEController::on_cover_update(cover::Cover *obj) {
if (obj->is_internal())
return;
}
#endif
#ifdef USE_FAN
void BLEController::on_fan_update(fan::FanState *obj) {
if (obj->is_internal())
return;
}
#endif
#ifdef USE_LIGHT
void BLEController::on_light_update(light::LightState *obj) {
if (obj->is_internal())
return;
}
#endif
#ifdef USE_SENSOR
void BLEController::on_sensor_update(sensor::Sensor *obj, float state) {
if (obj->is_internal())
return;
auto *characteristic = this->characteristics_[obj->get_object_id_hash()];
characteristic->set_value(state);
characteristic->notify();
}
#endif
#ifdef USE_SWITCH
void BLEController::on_switch_update(switch_::Switch *obj, bool state) {
if (obj->is_internal())
return;
auto *characteristic = this->characteristics_[obj->get_object_id_hash()];
characteristic->set_value(state);
characteristic->notify();
}
#endif
#ifdef USE_TEXT_SENSOR
void BLEController::on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) {
if (obj->is_internal())
return;
auto *characteristic = this->characteristics_[obj->get_object_id_hash()];
characteristic->set_value(state);
characteristic->notify();
}
#endif
#ifdef USE_CLIMATE
void BLEController::on_climate_update(climate::Climate *obj) {
if (obj->is_internal())
return;
}
#endif
} // namespace esp32_ble_controller
} // namespace esphome
#endif

View File

@@ -1,106 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/controller.h"
#include "esphome/components/esp32_ble_server/ble_2901.h"
#include "esphome/components/esp32_ble_server/ble_2902.h"
#include "esphome/components/esp32_ble_server/ble_characteristic.h"
#include "esphome/components/esp32_ble_server/ble_server.h"
#include <map>
#ifdef USE_LOGGER
#include "esphome/core/log.h"
#endif
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace esp32_ble_controller {
using namespace esp32_ble_server;
class BLEController : public Component, public Controller, public esp32_ble_server::BLEServiceComponent {
public:
void loop() override;
void setup() override;
void start() override;
void stop() override;
float get_setup_priority() const override;
#ifdef USE_LOGGER
void set_log_level(int level) { this->log_level_ = level; }
#endif
#ifdef USE_BINARY_SENSOR
void on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) override;
#endif
#ifdef USE_COVER
void on_cover_update(cover::Cover *obj) override;
#endif
#ifdef USE_FAN
void on_fan_update(fan::FanState *obj) override;
#endif
#ifdef USE_LIGHT
void on_light_update(light::LightState *obj) override;
#endif
#ifdef USE_SENSOR
void on_sensor_update(sensor::Sensor *obj, float state) override;
#endif
#ifdef USE_SWITCH
void on_switch_update(switch_::Switch *obj, bool state) override;
#endif
#ifdef USE_TEXT_SENSOR
void on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) override;
#endif
#ifdef USE_CLIMATE
void on_climate_update(climate::Climate *obj) override;
#endif
protected:
enum State : uint8_t {
FAILED = 0x00,
INIT,
CREATING,
STARTING,
RUNNING,
} state_{INIT};
std::map<uint32_t, BLECharacteristic *> characteristics_;
BLEService *esphome_service_;
#ifdef USE_LOGGER
BLECharacteristic *logger_characteristic_;
int log_level_{ESPHOME_LOG_LEVEL_DEBUG};
#endif
#ifdef USE_BINARY_SENSOR
BLEService *binary_sensor_service_;
#endif
#ifdef USE_COVER
BLEService *cover_service_;
#endif
#ifdef USE_FAN
BLEService *fan_service_;
#endif
#ifdef USE_LIGHT
BLEService *light_service_;
#endif
#ifdef USE_SENSOR
BLEService *sensor_service_;
#endif
#ifdef USE_SWITCH
BLEService *switch_service_;
#endif
#ifdef USE_TEXT_SENSOR
BLEService *text_sensor_service_;
#endif
#ifdef USE_CLIMATE
BLEService *climate_service_;
#endif
};
} // namespace esp32_ble_controller
} // namespace esphome
#endif

View File

@@ -61,11 +61,14 @@ void BLEServer::loop() {
break;
}
case STARTING_SERVICE: {
if (!this->device_information_service_->is_created()) {
break;
}
if (this->device_information_service_->is_running()) {
this->state_ = RUNNING;
this->can_proceed_ = true;
ESP_LOGD(TAG, "BLE server setup successfully");
} else if (this->device_information_service_->is_created() && !this->device_information_service_->is_starting()) {
} else if (!this->device_information_service_->is_starting()) {
this->device_information_service_->start();
}
break;

View File

@@ -72,14 +72,9 @@ bool BLEService::do_create_characteristics_() {
}
void BLEService::start() {
if (this->running_state_ == RUNNING)
return;
if (this->do_create_characteristics_())
return;
ESP_LOGD(TAG, "Starting BLE service %s", this->uuid_.to_string().c_str());
esp_err_t err = esp_ble_gatts_start_service(this->handle_);
if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_ble_gatts_start_service failed: %d", err);
@@ -117,14 +112,12 @@ void BLEService::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t g
if (this->uuid_ == ESPBTUUID::from_uuid(param->create.service_id.id.uuid) &&
this->inst_id_ == param->create.service_id.id.inst_id) {
this->handle_ = param->create.service_handle;
ESP_LOGI(TAG, "Service %s created", this->uuid_.to_string().c_str());
this->init_state_ = CREATED;
}
break;
}
case ESP_GATTS_START_EVT: {
if (param->start.service_handle == this->handle_) {
ESP_LOGI(TAG, "Service %s started", this->uuid_.to_string().c_str());
this->running_state_ = RUNNING;
}
break;

View File

@@ -19,8 +19,6 @@ from esphome.cpp_helpers import setup_entity
DEPENDENCIES = ["esp32"]
AUTO_LOAD = ["psram"]
esp32_camera_ns = cg.esphome_ns.namespace("esp32_camera")
ESP32Camera = esp32_camera_ns.class_("ESP32Camera", cg.PollingComponent, cg.EntityBase)
ESP32CameraFrameSize = esp32_camera_ns.enum("ESP32CameraFrameSize")
@@ -155,7 +153,9 @@ async def to_code(config):
cg.add(var.set_frame_size(config[CONF_RESOLUTION]))
cg.add_define("USE_ESP32_CAMERA")
cg.add_build_flag("-DBOARD_HAS_PSRAM")
if CORE.using_esp_idf:
cg.add_library("espressif/esp32-camera", "1.0.0")
add_idf_sdkconfig_option("CONFIG_RTCIO_SUPPORT_RTC_GPIO_DESC", True)
add_idf_sdkconfig_option("CONFIG_ESP32_SPIRAM_SUPPORT", True)

View File

@@ -49,6 +49,9 @@ void ESP32Camera::dump_config() {
ESP_LOGCONFIG(TAG, "ESP32 Camera:");
ESP_LOGCONFIG(TAG, " Name: %s", this->name_.c_str());
ESP_LOGCONFIG(TAG, " Internal: %s", YESNO(this->internal_));
#ifdef USE_ARDUINO
ESP_LOGCONFIG(TAG, " Board Has PSRAM: %s", YESNO(psramFound()));
#endif // USE_ARDUINO
ESP_LOGCONFIG(TAG, " Data Pins: D0:%d D1:%d D2:%d D3:%d D4:%d D5:%d D6:%d D7:%d", conf.pin_d0, conf.pin_d1,
conf.pin_d2, conf.pin_d3, conf.pin_d4, conf.pin_d5, conf.pin_d6, conf.pin_d7);
ESP_LOGCONFIG(TAG, " VSYNC Pin: %d", conf.pin_vsync);
@@ -133,13 +136,6 @@ void ESP32Camera::loop() {
this->current_image_.reset();
}
// request idle image every idle_update_interval
const uint32_t now = millis();
if (this->idle_update_interval_ != 0 && now - this->last_idle_request_ > this->idle_update_interval_) {
this->last_idle_request_ = now;
this->request_image(IDLE);
}
// Check if we should fetch a new image
if (!this->has_requested_image_())
return;
@@ -147,6 +143,7 @@ void ESP32Camera::loop() {
// image is still in use
return;
}
const uint32_t now = millis();
if (now - this->last_update_ <= this->max_update_interval_)
return;
@@ -163,12 +160,12 @@ void ESP32Camera::loop() {
xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
return;
}
this->current_image_ = std::make_shared<CameraImage>(fb, this->single_requesters_ | this->stream_requesters_);
this->current_image_ = std::make_shared<CameraImage>(fb);
ESP_LOGD(TAG, "Got Image: len=%u", fb->len);
this->new_image_callback_.call(this->current_image_);
this->last_update_ = now;
this->single_requesters_ = 0;
this->single_requester_ = false;
}
void ESP32Camera::framebuffer_task(void *pv) {
while (true) {
@@ -264,10 +261,24 @@ void ESP32Camera::set_brightness(int brightness) { this->brightness_ = brightnes
void ESP32Camera::set_saturation(int saturation) { this->saturation_ = saturation; }
float ESP32Camera::get_setup_priority() const { return setup_priority::DATA; }
uint32_t ESP32Camera::hash_base() { return 3010542557UL; }
void ESP32Camera::request_image(CameraRequester requester) { this->single_requesters_ |= 1 << requester; }
void ESP32Camera::start_stream(CameraRequester requester) { this->stream_requesters_ |= 1 << requester; }
void ESP32Camera::stop_stream(CameraRequester requester) { this->stream_requesters_ &= ~(1 << requester); }
bool ESP32Camera::has_requested_image_() const { return this->single_requesters_ || this->stream_requesters_; }
void ESP32Camera::request_image() { this->single_requester_ = true; }
void ESP32Camera::request_stream() { this->last_stream_request_ = millis(); }
bool ESP32Camera::has_requested_image_() const {
if (this->single_requester_)
// single request
return true;
uint32_t now = millis();
if (now - this->last_stream_request_ < 5000)
// stream request
return true;
if (this->idle_update_interval_ != 0 && now - this->last_update_ > this->idle_update_interval_)
// idle update
return true;
return false;
}
bool ESP32Camera::can_return_image_() const { return this->current_image_.use_count() == 1; }
void ESP32Camera::set_max_update_interval(uint32_t max_update_interval) {
this->max_update_interval_ = max_update_interval;
@@ -296,10 +307,7 @@ uint8_t *CameraImageReader::peek_data_buffer() { return this->image_->get_data_b
camera_fb_t *CameraImage::get_raw_buffer() { return this->buffer_; }
uint8_t *CameraImage::get_data_buffer() { return this->buffer_->buf; }
size_t CameraImage::get_data_length() { return this->buffer_->len; }
bool CameraImage::was_requested_by(CameraRequester requester) const {
return (this->requesters_ & (1 << requester)) != 0;
}
CameraImage::CameraImage(camera_fb_t *buffer, uint8_t requesters) : buffer_(buffer), requesters_(requesters) {}
CameraImage::CameraImage(camera_fb_t *buffer) : buffer_(buffer) {}
} // namespace esp32_camera
} // namespace esphome

View File

@@ -14,19 +14,15 @@ namespace esp32_camera {
class ESP32Camera;
enum CameraRequester { IDLE, API_REQUESTER, WEB_REQUESTER };
class CameraImage {
public:
CameraImage(camera_fb_t *buffer, uint8_t requester);
CameraImage(camera_fb_t *buffer);
camera_fb_t *get_raw_buffer();
uint8_t *get_data_buffer();
size_t get_data_length();
bool was_requested_by(CameraRequester requester) const;
protected:
camera_fb_t *buffer_;
uint8_t requesters_;
};
class CameraImageReader {
@@ -85,9 +81,8 @@ class ESP32Camera : public Component, public EntityBase {
void dump_config() override;
void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f);
float get_setup_priority() const override;
void start_stream(CameraRequester requester);
void stop_stream(CameraRequester requester);
void request_image(CameraRequester requester);
void request_stream();
void request_image();
protected:
uint32_t hash_base() override;
@@ -109,14 +104,13 @@ class ESP32Camera : public Component, public EntityBase {
esp_err_t init_error_{ESP_OK};
std::shared_ptr<CameraImage> current_image_;
uint8_t single_requesters_{0};
uint8_t stream_requesters_{0};
uint32_t last_stream_request_{0};
bool single_requester_{false};
QueueHandle_t framebuffer_get_queue_;
QueueHandle_t framebuffer_return_queue_;
CallbackManager<void(std::shared_ptr<CameraImage>)> new_image_callback_;
uint32_t max_update_interval_{1000};
uint32_t idle_update_interval_{15000};
uint32_t last_idle_request_{0};
uint32_t last_update_{0};
};

View File

@@ -14,7 +14,7 @@
namespace esphome {
namespace esp32_camera_web_server {
static const int IMAGE_REQUEST_TIMEOUT = 5000;
static const int IMAGE_REQUEST_TIMEOUT = 2000;
static const char *const TAG = "esp32_camera_web_server";
#define PART_BOUNDARY "123456789000000000000987654321"
@@ -68,7 +68,7 @@ void CameraWebServer::setup() {
httpd_register_uri_handler(this->httpd_, &uri);
esp32_camera::global_esp32_camera->add_image_callback([this](std::shared_ptr<esp32_camera::CameraImage> image) {
if (this->running_ && image->was_requested_by(esp32_camera::WEB_REQUESTER)) {
if (this->running_) {
this->image_ = std::move(image);
xSemaphoreGive(this->semaphore_);
}
@@ -169,9 +169,11 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
uint32_t last_frame = millis();
uint32_t frames = 0;
esp32_camera::global_esp32_camera->start_stream(esphome::esp32_camera::WEB_REQUESTER);
while (res == ESP_OK && this->running_) {
if (esp32_camera::global_esp32_camera != nullptr) {
esp32_camera::global_esp32_camera->request_stream();
}
auto image = this->wait_for_image_();
if (!image) {
@@ -202,8 +204,6 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
res = httpd_send_all(req, STREAM_ERROR, strlen(STREAM_ERROR));
}
esp32_camera::global_esp32_camera->stop_stream(esphome::esp32_camera::WEB_REQUESTER);
ESP_LOGI(TAG, "STREAM: closed. Frames: %u", frames);
return res;
@@ -212,7 +212,9 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
esp_err_t CameraWebServer::snapshot_handler_(struct httpd_req *req) {
esp_err_t res = ESP_OK;
esp32_camera::global_esp32_camera->request_image(esphome::esp32_camera::WEB_REQUESTER);
if (esp32_camera::global_esp32_camera != nullptr) {
esp32_camera::global_esp32_camera->request_image();
}
auto image = this->wait_for_image_();
@@ -231,6 +233,9 @@ esp_err_t CameraWebServer::snapshot_handler_(struct httpd_req *req) {
httpd_resp_set_hdr(req, "Content-Disposition", "inline; filename=capture.jpg");
if (res == ESP_OK) {
res = httpd_resp_set_hdr(req, CONTENT_LENGTH, esphome::to_string(image->get_data_length()).c_str());
}
if (res == ESP_OK) {
res = httpd_resp_send(req, (const char *) image->get_data_buffer(), image->get_data_length());
}

View File

@@ -1,39 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import canbus
from esphome.const import CONF_ID, CONF_RX_PIN, CONF_TX_PIN
from esphome.components.canbus import CanbusComponent, CanSpeed, CONF_BIT_RATE
CODEOWNERS = ["@Sympatron"]
DEPENDENCIES = ["esp32"]
esp32_can_ns = cg.esphome_ns.namespace("esp32_can")
esp32_can = esp32_can_ns.class_("ESP32Can", CanbusComponent)
# Currently the driver only supports a subset of the bit rates defined in canbus
CAN_SPEEDS = {
"50KBPS": CanSpeed.CAN_50KBPS,
"100KBPS": CanSpeed.CAN_100KBPS,
"125KBPS": CanSpeed.CAN_125KBPS,
"250KBPS": CanSpeed.CAN_250KBPS,
"500KBPS": CanSpeed.CAN_500KBPS,
"1000KBPS": CanSpeed.CAN_1000KBPS,
}
CONFIG_SCHEMA = canbus.CANBUS_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(esp32_can),
cv.Optional(CONF_BIT_RATE, default="125KBPS"): cv.enum(CAN_SPEEDS, upper=True),
cv.Required(CONF_RX_PIN): pins.internal_gpio_input_pin_number,
cv.Required(CONF_TX_PIN): pins.internal_gpio_output_pin_number,
}
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await canbus.register_canbus(var, config)
cg.add(var.set_rx(config[CONF_RX_PIN]))
cg.add(var.set_tx(config[CONF_TX_PIN]))

View File

@@ -1,123 +0,0 @@
#ifdef USE_ESP32
#include "esp32_can.h"
#include "esphome/core/log.h"
#include <driver/can.h>
// WORKAROUND, because CAN_IO_UNUSED is just defined as (-1) in this version
// of the framework which does not work with -fpermissive
#undef CAN_IO_UNUSED
#define CAN_IO_UNUSED ((gpio_num_t) -1)
namespace esphome {
namespace esp32_can {
static const char *const TAG = "esp32_can";
static bool get_bitrate(canbus::CanSpeed bitrate, can_timing_config_t *t_config) {
switch (bitrate) {
case canbus::CAN_50KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_50KBITS();
return true;
case canbus::CAN_100KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_100KBITS();
return true;
case canbus::CAN_125KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_125KBITS();
return true;
case canbus::CAN_250KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_250KBITS();
return true;
case canbus::CAN_500KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_500KBITS();
return true;
case canbus::CAN_1000KBPS:
*t_config = (can_timing_config_t) CAN_TIMING_CONFIG_1MBITS();
return true;
default:
return false;
}
}
bool ESP32Can::setup_internal() {
can_general_config_t g_config =
CAN_GENERAL_CONFIG_DEFAULT((gpio_num_t) this->tx_, (gpio_num_t) this->rx_, CAN_MODE_NORMAL);
can_filter_config_t f_config = CAN_FILTER_CONFIG_ACCEPT_ALL();
can_timing_config_t t_config;
if (!get_bitrate(this->bit_rate_, &t_config)) {
// invalid bit rate
this->mark_failed();
return false;
}
// Install CAN driver
if (can_driver_install(&g_config, &t_config, &f_config) != ESP_OK) {
// Failed to install driver
this->mark_failed();
return false;
}
// Start CAN driver
if (can_start() != ESP_OK) {
// Failed to start driver
this->mark_failed();
return false;
}
return true;
}
canbus::Error ESP32Can::send_message(struct canbus::CanFrame *frame) {
if (frame->can_data_length_code > canbus::CAN_MAX_DATA_LENGTH) {
return canbus::ERROR_FAILTX;
}
uint32_t flags = CAN_MSG_FLAG_NONE;
if (frame->use_extended_id) {
flags |= CAN_MSG_FLAG_EXTD;
}
if (frame->remote_transmission_request) {
flags |= CAN_MSG_FLAG_RTR;
}
can_message_t message = {
.flags = flags,
.identifier = frame->can_id,
.data_length_code = frame->can_data_length_code,
};
if (!frame->remote_transmission_request) {
memcpy(message.data, frame->data, frame->can_data_length_code);
}
if (can_transmit(&message, pdMS_TO_TICKS(1000)) == ESP_OK) {
return canbus::ERROR_OK;
} else {
return canbus::ERROR_ALLTXBUSY;
}
}
canbus::Error ESP32Can::read_message(struct canbus::CanFrame *frame) {
can_message_t message;
if (can_receive(&message, 0) != ESP_OK) {
return canbus::ERROR_NOMSG;
}
frame->can_id = message.identifier;
frame->use_extended_id = message.flags & CAN_MSG_FLAG_EXTD;
frame->remote_transmission_request = message.flags & CAN_MSG_FLAG_RTR;
frame->can_data_length_code = message.data_length_code;
if (!frame->remote_transmission_request) {
size_t dlc =
message.data_length_code < canbus::CAN_MAX_DATA_LENGTH ? message.data_length_code : canbus::CAN_MAX_DATA_LENGTH;
memcpy(frame->data, message.data, dlc);
}
return canbus::ERROR_OK;
}
} // namespace esp32_can
} // namespace esphome
#endif

View File

@@ -1,29 +0,0 @@
#pragma once
#ifdef USE_ESP32
#include "esphome/components/canbus/canbus.h"
#include "esphome/core/component.h"
namespace esphome {
namespace esp32_can {
class ESP32Can : public canbus::Canbus {
public:
void set_rx(int rx) { rx_ = rx; }
void set_tx(int tx) { tx_ = tx; }
ESP32Can(){};
protected:
bool setup_internal() override;
canbus::Error send_message(struct canbus::CanFrame *frame) override;
canbus::Error read_message(struct canbus::CanFrame *frame) override;
int rx_{-1};
int tx_{-1};
};
} // namespace esp32_can
} // namespace esphome
#endif

View File

@@ -1,7 +1,7 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import binary_sensor, output, esp32_ble_server
from esphome.const import CONF_BLE_SERVER_ID, CONF_ID
from esphome.const import CONF_ID
AUTO_LOAD = ["binary_sensor", "output", "esp32_ble_server"]
@@ -11,6 +11,7 @@ DEPENDENCIES = ["wifi", "esp32"]
CONF_AUTHORIZED_DURATION = "authorized_duration"
CONF_AUTHORIZER = "authorizer"
CONF_BLE_SERVER_ID = "ble_server_id"
CONF_IDENTIFY_DURATION = "identify_duration"
CONF_STATUS_INDICATOR = "status_indicator"
CONF_WIFI_TIMEOUT = "wifi_timeout"

View File

@@ -1,5 +1,4 @@
import logging
import os
from esphome.const import (
CONF_BOARD,
@@ -15,7 +14,6 @@ from esphome.const import (
from esphome.core import CORE, coroutine_with_priority
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.helpers import copy_file_if_changed
from .const import CONF_RESTORE_FROM_FLASH, KEY_BOARD, KEY_ESP8266, esp8266_ns
from .boards import ESP8266_FLASH_SIZES, ESP8266_LD_SCRIPTS
@@ -160,8 +158,6 @@ async def to_code(config):
cg.add_define("ESPHOME_BOARD", config[CONF_BOARD])
cg.add_define("ESPHOME_VARIANT", "ESP8266")
cg.add_platformio_option("extra_scripts", ["post:post_build.py"])
conf = config[CONF_FRAMEWORK]
cg.add_platformio_option("framework", "arduino")
cg.add_build_flag("-DUSE_ARDUINO")
@@ -214,14 +210,3 @@ async def to_code(config):
if ld_script is not None:
cg.add_platformio_option("board_build.ldscript", ld_script)
# Called by writer.py
def copy_files():
dir = os.path.dirname(__file__)
post_build_file = os.path.join(dir, "post_build.py.script")
copy_file_if_changed(
post_build_file,
CORE.relative_build_path("post_build.py"),
)

View File

@@ -1,15 +0,0 @@
import shutil
# pylint: disable=E0602
Import("env") # noqa
def esp8266_copy_factory_bin(source, target, env):
firmware_name = env.subst("$BUILD_DIR/${PROGNAME}.bin")
new_file_name = env.subst("$BUILD_DIR/${PROGNAME}-factory.bin")
shutil.copyfile(firmware_name, new_file_name)
# pylint: disable=E0602
env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp8266_copy_factory_bin) # noqa

View File

@@ -21,9 +21,7 @@ void HDC1080Component::setup() {
};
if (!this->write_bytes(HDC1080_CMD_CONFIGURATION, data, 2)) {
// as instruction is same as powerup defaults (for now), interpret as warning if this fails
ESP_LOGW(TAG, "HDC1080 initial config instruction error");
this->status_set_warning();
this->mark_failed();
return;
}
}

View File

@@ -109,8 +109,8 @@ def to_code(config):
cg.add(var.set_protocol(config[CONF_PROTOCOL]))
cg.add(var.set_horizontal_default(config[CONF_HORIZONTAL_DEFAULT]))
cg.add(var.set_vertical_default(config[CONF_VERTICAL_DEFAULT]))
cg.add(var.set_max_temperature(config[CONF_MAX_TEMPERATURE]))
cg.add(var.set_min_temperature(config[CONF_MIN_TEMPERATURE]))
cg.add(var.set_max_temperature(config[CONF_MIN_TEMPERATURE]))
cg.add(var.set_min_temperature(config[CONF_MAX_TEMPERATURE]))
# PIO isn't updating releases, so referencing the release tag directly. See:
# https://github.com/ToniA/arduino-heatpumpir/commit/0948c619d86407a4e50e8db2f3c193e0576c86fd

View File

@@ -172,7 +172,7 @@ async def http_request_action_to_code(config, action_id, template_arg, args):
if CONF_JSON in config:
json_ = config[CONF_JSON]
if isinstance(json_, Lambda):
args_ = args + [(cg.JsonObject, "root")]
args_ = args + [(cg.JsonObjectRef, "root")]
lambda_ = await cg.process_lambda(json_, args_, return_type=cg.void)
cg.add(var.set_json(lambda_))
else:

View File

@@ -115,11 +115,8 @@ void HttpRequestComponent::close() {
}
const char *HttpRequestComponent::get_string() {
// The static variable is here because HTTPClient::getString() returns a String on ESP32, and we need something to
// to keep a buffer alive.
static std::string str;
str = this->client_.getString().c_str();
return str.c_str();
static const String STR = this->client_.getString();
return STR.c_str();
}
} // namespace http_request

View File

@@ -78,7 +78,7 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
void add_json(const char *key, TemplatableValue<std::string, Ts...> value) { this->json_.insert({key, value}); }
void set_json(std::function<void(Ts..., JsonObject)> json_func) { this->json_func_ = json_func; }
void set_json(std::function<void(Ts..., JsonObject &)> json_func) { this->json_func_ = json_func; }
void register_response_trigger(HttpRequestResponseTrigger *trigger) { this->response_triggers_.push_back(trigger); }
@@ -118,17 +118,17 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
}
protected:
void encode_json_(Ts... x, JsonObject root) {
void encode_json_(Ts... x, JsonObject &root) {
for (const auto &item : this->json_) {
auto val = item.second;
root[item.first] = val.value(x...);
}
}
void encode_json_func_(Ts... x, JsonObject root) { this->json_func_(x..., root); }
void encode_json_func_(Ts... x, JsonObject &root) { this->json_func_(x..., root); }
HttpRequestComponent *parent_;
std::map<const char *, TemplatableValue<const char *, Ts...>> headers_{};
std::map<const char *, TemplatableValue<std::string, Ts...>> json_{};
std::function<void(Ts..., JsonObject)> json_func_{nullptr};
std::function<void(Ts..., JsonObject &)> json_func_{nullptr};
std::vector<HttpRequestResponseTrigger *> response_triggers_;
};

View File

@@ -25,7 +25,7 @@ void ArduinoI2CBus::setup() {
wire_ = &Wire; // NOLINT(cppcoreguidelines-prefer-member-initializer)
#endif
wire_->begin(static_cast<int>(sda_pin_), static_cast<int>(scl_pin_));
wire_->begin(sda_pin_, scl_pin_);
wire_->setClock(frequency_);
initialized_ = true;
if (this->scan_) {

View File

@@ -1,128 +0,0 @@
#include "ina260.h"
#include "esphome/core/log.h"
#include "esphome/core/hal.h"
namespace esphome {
namespace ina260 {
static const char *const TAG = "ina260";
// | A0 | A1 | Address |
// | GND | GND | 0x40 |
// | GND | V_S+ | 0x41 |
// | GND | SDA | 0x42 |
// | GND | SCL | 0x43 |
// | V_S+ | GND | 0x44 |
// | V_S+ | V_S+ | 0x45 |
// | V_S+ | SDA | 0x46 |
// | V_S+ | SCL | 0x47 |
// | SDA | GND | 0x48 |
// | SDA | V_S+ | 0x49 |
// | SDA | SDA | 0x4A |
// | SDA | SCL | 0x4B |
// | SCL | GND | 0x4C |
// | SCL | V_S+ | 0x4D |
// | SCL | SDA | 0x4E |
// | SCL | SCL | 0x4F |
static const uint8_t INA260_REGISTER_CONFIG = 0x00;
static const uint8_t INA260_REGISTER_CURRENT = 0x01;
static const uint8_t INA260_REGISTER_BUS_VOLTAGE = 0x02;
static const uint8_t INA260_REGISTER_POWER = 0x03;
static const uint8_t INA260_REGISTER_MASK_ENABLE = 0x06;
static const uint8_t INA260_REGISTER_ALERT_LIMIT = 0x07;
static const uint8_t INA260_REGISTER_MANUFACTURE_ID = 0xFE;
static const uint8_t INA260_REGISTER_DEVICE_ID = 0xFF;
void INA260Component::setup() {
ESP_LOGCONFIG(TAG, "Setting up INA260...");
// Reset device on setup
if (!this->write_byte_16(INA260_REGISTER_CONFIG, 0x8000)) {
this->error_code_ = DEVICE_RESET_FAILED;
this->mark_failed();
return;
}
delay(2);
this->read_byte_16(INA260_REGISTER_MANUFACTURE_ID, &this->manufacture_id_);
this->read_byte_16(INA260_REGISTER_DEVICE_ID, &this->device_id_);
if (this->manufacture_id_ != (uint16_t) 0x5449 || this->device_id_ != (uint16_t) 0x2270) {
this->error_code_ = COMMUNICATION_FAILED;
this->mark_failed();
return;
}
if (!this->write_byte_16(INA260_REGISTER_CONFIG, (uint16_t) 0b0000001100000111)) {
this->error_code_ = FAILED_TO_UPDATE_CONFIGURATION;
this->mark_failed();
return;
}
}
void INA260Component::dump_config() {
ESP_LOGCONFIG(TAG, "INA260:");
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
ESP_LOGCONFIG(TAG, " Manufacture ID: 0x%x", this->manufacture_id_);
ESP_LOGCONFIG(TAG, " Device ID: 0x%x", this->device_id_);
LOG_SENSOR(" ", "Bus Voltage", this->bus_voltage_sensor_);
LOG_SENSOR(" ", "Current", this->current_sensor_);
LOG_SENSOR(" ", "Power", this->power_sensor_);
switch (this->error_code_) {
case COMMUNICATION_FAILED:
ESP_LOGE(TAG, "Connected device does not match a known INA260 sensor");
break;
case DEVICE_RESET_FAILED:
ESP_LOGE(TAG, "Device reset failed - Is the device connected?");
break;
case FAILED_TO_UPDATE_CONFIGURATION:
ESP_LOGE(TAG, "Failed to update device configuration");
break;
case NONE:
default:
break;
}
}
void INA260Component::update() {
if (this->bus_voltage_sensor_ != nullptr) {
uint16_t raw_bus_voltage;
if (!this->read_byte_16(INA260_REGISTER_BUS_VOLTAGE, &raw_bus_voltage)) {
this->status_set_warning();
return;
}
float bus_voltage_v = int16_t(raw_bus_voltage) * 0.00125f;
this->bus_voltage_sensor_->publish_state(bus_voltage_v);
}
if (this->current_sensor_ != nullptr) {
uint16_t raw_current;
if (!this->read_byte_16(INA260_REGISTER_CURRENT, &raw_current)) {
this->status_set_warning();
return;
}
float current_a = int16_t(raw_current) * 0.00125f;
this->current_sensor_->publish_state(current_a);
}
if (this->power_sensor_ != nullptr) {
uint16_t raw_power;
if (!this->read_byte_16(INA260_REGISTER_POWER, &raw_power)) {
this->status_set_warning();
return;
}
float power_w = ((int16_t(raw_power) * 10.0f) / 1000.0f);
this->power_sensor_->publish_state(power_w);
}
this->status_clear_warning();
}
} // namespace ina260
} // namespace esphome

View File

@@ -1,39 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/i2c/i2c.h"
namespace esphome {
namespace ina260 {
class INA260Component : public PollingComponent, public i2c::I2CDevice {
public:
void setup() override;
void dump_config() override;
void update() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void set_bus_voltage_sensor(sensor::Sensor *bus_voltage_sensor) { this->bus_voltage_sensor_ = bus_voltage_sensor; }
void set_current_sensor(sensor::Sensor *current_sensor) { this->current_sensor_ = current_sensor; }
void set_power_sensor(sensor::Sensor *power_sensor) { this->power_sensor_ = power_sensor; }
protected:
uint16_t manufacture_id_{0};
uint16_t device_id_{0};
sensor::Sensor *bus_voltage_sensor_{nullptr};
sensor::Sensor *current_sensor_{nullptr};
sensor::Sensor *power_sensor_{nullptr};
enum ErrorCode {
NONE,
COMMUNICATION_FAILED,
DEVICE_RESET_FAILED,
FAILED_TO_UPDATE_CONFIGURATION,
} error_code_{NONE};
};
} // namespace ina260
} // namespace esphome

View File

@@ -1,71 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import i2c, sensor
from esphome.const import (
CONF_ID,
CONF_BUS_VOLTAGE,
CONF_CURRENT,
CONF_POWER,
DEVICE_CLASS_VOLTAGE,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_POWER,
STATE_CLASS_MEASUREMENT,
UNIT_VOLT,
UNIT_AMPERE,
UNIT_WATT,
)
DEPENDENCIES = ["i2c"]
CODEOWNERS = ["@MrEditor97"]
ina260_ns = cg.esphome_ns.namespace("ina260")
INA260Component = ina260_ns.class_(
"INA260Component", cg.PollingComponent, i2c.I2CDevice
)
CONFIG_SCHEMA = cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(INA260Component),
cv.Optional(CONF_BUS_VOLTAGE): sensor.sensor_schema(
unit_of_measurement=UNIT_VOLT,
accuracy_decimals=2,
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_CURRENT): sensor.sensor_schema(
unit_of_measurement=UNIT_AMPERE,
accuracy_decimals=3,
device_class=DEVICE_CLASS_CURRENT,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_POWER): sensor.sensor_schema(
unit_of_measurement=UNIT_WATT,
accuracy_decimals=2,
device_class=DEVICE_CLASS_POWER,
state_class=STATE_CLASS_MEASUREMENT,
),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(0x40))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
if CONF_BUS_VOLTAGE in config:
sens = await sensor.new_sensor(config[CONF_BUS_VOLTAGE])
cg.add(var.set_bus_voltage_sensor(sens))
if CONF_CURRENT in config:
sens = await sensor.new_sensor(config[CONF_CURRENT])
cg.add(var.set_current_sensor(sens))
if CONF_POWER in config:
sens = await sensor.new_sensor(config[CONF_POWER])
cg.add(var.set_power_sensor(sens))

View File

@@ -6,13 +6,11 @@ from esphome.const import (
CONF_FULL_UPDATE_EVERY,
CONF_ID,
CONF_LAMBDA,
CONF_MODEL,
CONF_PAGES,
CONF_WAKEUP_PIN,
)
DEPENDENCIES = ["i2c", "esp32"]
AUTO_LOAD = ["psram"]
CONF_DISPLAY_DATA_0_PIN = "display_data_0_pin"
CONF_DISPLAY_DATA_1_PIN = "display_data_1_pin"
@@ -42,13 +40,6 @@ Inkplate6 = inkplate6_ns.class_(
"Inkplate6", cg.PollingComponent, i2c.I2CDevice, display.DisplayBuffer
)
InkplateModel = inkplate6_ns.enum("InkplateModel")
MODELS = {
"inkplate_6": InkplateModel.INKPLATE_6,
"inkplate_10": InkplateModel.INKPLATE_10,
}
CONFIG_SCHEMA = cv.All(
display.FULL_DISPLAY_SCHEMA.extend(
{
@@ -56,9 +47,6 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_GREYSCALE, default=False): cv.boolean,
cv.Optional(CONF_PARTIAL_UPDATING, default=True): cv.boolean,
cv.Optional(CONF_FULL_UPDATE_EVERY, default=10): cv.uint32_t,
cv.Optional(CONF_MODEL, default="inkplate_6"): cv.enum(
MODELS, lower=True, space="_"
),
# Control pins
cv.Required(CONF_CKV_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_GMOD_PIN): pins.gpio_output_pin_schema,
@@ -122,8 +110,6 @@ async def to_code(config):
cg.add(var.set_partial_updating(config[CONF_PARTIAL_UPDATING]))
cg.add(var.set_full_update_every(config[CONF_FULL_UPDATE_EVERY]))
cg.add(var.set_model(config[CONF_MODEL]))
ckv = await cg.gpio_pin_expression(config[CONF_CKV_PIN])
cg.add(var.set_ckv_pin(ckv))
@@ -180,3 +166,5 @@ async def to_code(config):
display_data_7 = await cg.gpio_pin_expression(config[CONF_DISPLAY_DATA_7_PIN])
cg.add(var.set_display_data_7_pin(display_data_7))
cg.add_build_flag("-DBOARD_HAS_PSRAM")

View File

@@ -42,32 +42,32 @@ void Inkplate6::setup() {
this->display();
}
void Inkplate6::initialize_() {
ExternalRAMAllocator<uint8_t> allocator(ExternalRAMAllocator<uint8_t>::ALLOW_FAILURE);
uint32_t buffer_size = this->get_buffer_length_();
if (buffer_size == 0)
return;
if (this->partial_buffer_ != nullptr)
allocator.deallocate(this->partial_buffer_, buffer_size);
if (this->partial_buffer_2_ != nullptr)
allocator.deallocate(this->partial_buffer_2_, buffer_size * 2);
if (this->buffer_ != nullptr)
allocator.deallocate(this->buffer_, buffer_size);
if (this->partial_buffer_ != nullptr) {
free(this->partial_buffer_); // NOLINT
}
if (this->partial_buffer_2_ != nullptr) {
free(this->partial_buffer_2_); // NOLINT
}
if (this->buffer_ != nullptr) {
free(this->buffer_); // NOLINT
}
this->buffer_ = allocator.allocate(buffer_size);
this->buffer_ = (uint8_t *) ps_malloc(buffer_size);
if (this->buffer_ == nullptr) {
ESP_LOGE(TAG, "Could not allocate buffer for display!");
this->mark_failed();
return;
}
if (!this->greyscale_) {
this->partial_buffer_ = allocator.allocate(buffer_size);
this->partial_buffer_ = (uint8_t *) ps_malloc(buffer_size);
if (this->partial_buffer_ == nullptr) {
ESP_LOGE(TAG, "Could not allocate partial buffer for display!");
this->mark_failed();
return;
}
this->partial_buffer_2_ = allocator.allocate(buffer_size * 2);
this->partial_buffer_2_ = (uint8_t *) ps_malloc(buffer_size * 2);
if (this->partial_buffer_2_ == nullptr) {
ESP_LOGE(TAG, "Could not allocate partial buffer 2 for display!");
this->mark_failed();

View File

@@ -10,11 +10,6 @@
namespace esphome {
namespace inkplate6 {
enum InkplateModel : uint8_t {
INKPLATE_6 = 0,
INKPLATE_10 = 1,
};
class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public i2c::I2CDevice {
public:
const uint8_t LUT2[16] = {0b10101010, 0b10101001, 0b10100110, 0b10100101, 0b10011010, 0b10011001,
@@ -48,8 +43,6 @@ class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public
void set_partial_updating(bool partial_updating) { this->partial_updating_ = partial_updating; }
void set_full_update_every(uint32_t full_update_every) { this->full_update_every_ = full_update_every; }
void set_model(InkplateModel model) { this->model_ = model; }
void set_display_data_0_pin(InternalGPIOPin *data) { this->display_data_0_pin_ = data; }
void set_display_data_1_pin(InternalGPIOPin *data) { this->display_data_1_pin_ = data; }
void set_display_data_2_pin(InternalGPIOPin *data) { this->display_data_2_pin_ = data; }
@@ -108,21 +101,9 @@ class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public
void pins_z_state_();
void pins_as_outputs_();
int get_width_internal() override {
if (this->model_ == INKPLATE_6)
return 800;
else if (this->model_ == INKPLATE_10)
return 1200;
return 0;
}
int get_width_internal() override { return 800; }
int get_height_internal() override {
if (this->model_ == INKPLATE_6)
return 600;
else if (this->model_ == INKPLATE_10)
return 825;
return 0;
}
int get_height_internal() override { return 600; }
size_t get_buffer_length_();
@@ -152,8 +133,6 @@ class Inkplate6 : public PollingComponent, public display::DisplayBuffer, public
bool greyscale_;
bool partial_updating_;
InkplateModel model_;
InternalGPIOPin *display_data_0_pin_;
InternalGPIOPin *display_data_1_pin_;
InternalGPIOPin *display_data_2_pin_;

View File

@@ -23,6 +23,31 @@ void IntegrationSensor::setup() {
this->sensor_->add_on_state_callback([this](float state) { this->process_sensor_value_(state); });
}
void IntegrationSensor::dump_config() { LOG_SENSOR("", "Integration Sensor", this); }
std::string IntegrationSensor::unit_of_measurement() {
std::string suffix;
switch (this->time_) {
case INTEGRATION_SENSOR_TIME_MILLISECOND:
suffix = "ms";
break;
case INTEGRATION_SENSOR_TIME_SECOND:
suffix = "s";
break;
case INTEGRATION_SENSOR_TIME_MINUTE:
suffix = "min";
break;
case INTEGRATION_SENSOR_TIME_HOUR:
suffix = "h";
break;
case INTEGRATION_SENSOR_TIME_DAY:
suffix = "d";
break;
}
std::string base = this->sensor_->get_unit_of_measurement();
if (str_endswith(base, "/" + suffix)) {
return base.substr(0, base.size() - suffix.size() - 1);
}
return base + suffix;
}
void IntegrationSensor::process_sensor_value_(float value) {
const uint32_t now = millis();
const double old_value = this->last_value_;

View File

@@ -63,6 +63,8 @@ class IntegrationSensor : public sensor::Sensor, public Component {
this->last_save_ = now;
this->rtc_.save(&result_f);
}
std::string unit_of_measurement() override;
int8_t accuracy_decimals() override { return this->sensor_->get_accuracy_decimals() + 2; }
sensor::Sensor *sensor_;
IntegrationSensorTime time_;

View File

@@ -2,14 +2,7 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.components import sensor
from esphome.const import (
CONF_ICON,
CONF_ID,
CONF_SENSOR,
CONF_RESTORE,
CONF_UNIT_OF_MEASUREMENT,
CONF_ACCURACY_DECIMALS,
)
from esphome.const import CONF_ICON, CONF_ID, CONF_SENSOR, CONF_RESTORE
from esphome.core.entity_helpers import inherit_property_from
integration_ns = cg.esphome_ns.namespace("integration")
@@ -37,18 +30,6 @@ CONF_TIME_UNIT = "time_unit"
CONF_INTEGRATION_METHOD = "integration_method"
CONF_MIN_SAVE_INTERVAL = "min_save_interval"
def inherit_unit_of_measurement(uom, config):
suffix = config[CONF_TIME_UNIT]
if uom.endswith("/" + suffix):
return uom[0 : -len("/" + suffix)]
return uom + suffix
def inherit_accuracy_decimals(decimals, config):
return decimals + 2
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(IntegrationSensor),
@@ -70,19 +51,11 @@ FINAL_VALIDATE_SCHEMA = cv.All(
{
cv.Required(CONF_ID): cv.use_id(IntegrationSensor),
cv.Optional(CONF_ICON): cv.icon,
cv.Optional(CONF_UNIT_OF_MEASUREMENT): sensor.validate_unit_of_measurement,
cv.Optional(CONF_ACCURACY_DECIMALS): sensor.validate_accuracy_decimals,
cv.Required(CONF_SENSOR): cv.use_id(sensor.Sensor),
},
extra=cv.ALLOW_EXTRA,
),
inherit_property_from(CONF_ICON, CONF_SENSOR),
inherit_property_from(
CONF_UNIT_OF_MEASUREMENT, CONF_SENSOR, transform=inherit_unit_of_measurement
),
inherit_property_from(
CONF_ACCURACY_DECIMALS, CONF_SENSOR, transform=inherit_accuracy_decimals
),
)

View File

@@ -7,11 +7,12 @@ json_ns = cg.esphome_ns.namespace("json")
CONFIG_SCHEMA = cv.All(
cv.Schema({}),
cv.only_with_arduino,
)
@coroutine_with_priority(1.0)
async def to_code(config):
cg.add_library("bblanchon/ArduinoJson", "6.18.5")
cg.add_library("ottowinter/ArduinoJson-esphomelib", "5.13.3")
cg.add_define("USE_JSON")
cg.add_global(json_ns.using)

View File

@@ -1,13 +1,8 @@
#ifdef USE_ARDUINO
#include "json_util.h"
#include "esphome/core/log.h"
#ifdef USE_ESP8266
#include <Esp.h>
#endif
#ifdef USE_ESP32
#include <esp_heap_caps.h>
#endif
namespace esphome {
namespace json {
@@ -15,49 +10,110 @@ static const char *const TAG = "json";
static std::vector<char> global_json_build_buffer; // NOLINT
std::string build_json(const json_build_t &f) {
// Here we are allocating as much heap memory as available minus 2kb to be safe
// as we can not have a true dynamic sized document.
// The excess memory is freed below with `shrinkToFit()`
#ifdef USE_ESP8266
const size_t free_heap = ESP.getMaxFreeBlockSize() - 2048; // NOLINT(readability-static-accessed-through-instance)
#elif defined(USE_ESP32)
const size_t free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT) - 2048;
#endif
const char *build_json(const json_build_t &f, size_t *length) {
global_json_buffer.clear();
JsonObject &root = global_json_buffer.createObject();
DynamicJsonDocument json_document(free_heap);
JsonObject root = json_document.to<JsonObject>();
f(root);
json_document.shrinkToFit();
std::string output;
serializeJson(json_document, output);
return output;
// The Json buffer size gives us a good estimate for the required size.
// Usually, it's a bit larger than the actual required string size
// | JSON Buffer Size | String Size |
// Discovery | 388 | 351 |
// Discovery | 372 | 356 |
// Discovery | 336 | 311 |
// Discovery | 408 | 393 |
global_json_build_buffer.reserve(global_json_buffer.size() + 1);
size_t bytes_written = root.printTo(global_json_build_buffer.data(), global_json_build_buffer.capacity());
if (bytes_written >= global_json_build_buffer.capacity() - 1) {
global_json_build_buffer.reserve(root.measureLength() + 1);
bytes_written = root.printTo(global_json_build_buffer.data(), global_json_build_buffer.capacity());
}
*length = bytes_written;
return global_json_build_buffer.data();
}
void parse_json(const std::string &data, const json_parse_t &f) {
// Here we are allocating as much heap memory as available minus 2kb to be safe
// as we can not have a true dynamic sized document.
// The excess memory is freed below with `shrinkToFit()`
#ifdef USE_ESP8266
const size_t free_heap = ESP.getMaxFreeBlockSize() - 2048; // NOLINT(readability-static-accessed-through-instance)
#elif defined(USE_ESP32)
const size_t free_heap = heap_caps_get_largest_free_block(MALLOC_CAP_DEFAULT) - 2048;
#endif
global_json_buffer.clear();
JsonObject &root = global_json_buffer.parseObject(data);
DynamicJsonDocument json_document(free_heap);
DeserializationError err = deserializeJson(json_document, data);
json_document.shrinkToFit();
JsonObject root = json_document.as<JsonObject>();
if (err) {
if (!root.success()) {
ESP_LOGW(TAG, "Parsing JSON failed.");
return;
}
f(root);
}
std::string build_json(const json_build_t &f) {
size_t len;
const char *c_str = build_json(f, &len);
return std::string(c_str, len);
}
VectorJsonBuffer::String::String(VectorJsonBuffer *parent) : parent_(parent), start_(parent->size_) {}
void VectorJsonBuffer::String::append(char c) const {
char *last = static_cast<char *>(this->parent_->do_alloc(1));
*last = c;
}
const char *VectorJsonBuffer::String::c_str() const {
this->append('\0');
return &this->parent_->buffer_[this->start_];
}
void VectorJsonBuffer::clear() {
for (char *block : this->free_blocks_)
free(block); // NOLINT
this->size_ = 0;
this->free_blocks_.clear();
}
VectorJsonBuffer::String VectorJsonBuffer::startString() { return {this}; } // NOLINT
void *VectorJsonBuffer::alloc(size_t bytes) {
// Make sure memory addresses are aligned
uint32_t new_size = round_size_up(this->size_);
this->resize(new_size);
return this->do_alloc(bytes);
}
void *VectorJsonBuffer::do_alloc(size_t bytes) { // NOLINT
const uint32_t begin = this->size_;
this->resize(begin + bytes);
return &this->buffer_[begin];
}
void VectorJsonBuffer::resize(size_t size) { // NOLINT
if (size <= this->size_) {
this->size_ = size;
return;
}
this->reserve(size);
this->size_ = size;
}
void VectorJsonBuffer::reserve(size_t size) { // NOLINT
if (size <= this->capacity_)
return;
uint32_t target_capacity = this->capacity_;
if (this->capacity_ == 0) {
// lazily initialize with a reasonable size
target_capacity = JSON_OBJECT_SIZE(16);
}
while (target_capacity < size)
target_capacity *= 2;
char *old_buffer = this->buffer_;
this->buffer_ = new char[target_capacity]; // NOLINT
if (old_buffer != nullptr && this->capacity_ != 0) {
this->free_blocks_.push_back(old_buffer);
memcpy(this->buffer_, old_buffer, this->capacity_);
}
this->capacity_ = target_capacity;
}
size_t VectorJsonBuffer::size() const { return this->size_; }
VectorJsonBuffer global_json_buffer; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
} // namespace json
} // namespace esphome
#endif // USE_ARDUINO

View File

@@ -1,28 +1,68 @@
#pragma once
#ifdef USE_ARDUINO
#include <vector>
#include "esphome/core/helpers.h"
#undef ARDUINOJSON_ENABLE_STD_STRING
#define ARDUINOJSON_ENABLE_STD_STRING 1 // NOLINT
#include <ArduinoJson.h>
namespace esphome {
namespace json {
/// Callback function typedef for parsing JsonObjects.
using json_parse_t = std::function<void(JsonObject)>;
using json_parse_t = std::function<void(JsonObject &)>;
/// Callback function typedef for building JsonObjects.
using json_build_t = std::function<void(JsonObject)>;
using json_build_t = std::function<void(JsonObject &)>;
/// Build a JSON string with the provided json build function.
const char *build_json(const json_build_t &f, size_t *length);
std::string build_json(const json_build_t &f);
/// Parse a JSON string and run the provided json parse function if it's valid.
void parse_json(const std::string &data, const json_parse_t &f);
class VectorJsonBuffer : public ArduinoJson::Internals::JsonBufferBase<VectorJsonBuffer> {
public:
class String {
public:
String(VectorJsonBuffer *parent);
void append(char c) const;
const char *c_str() const;
protected:
VectorJsonBuffer *parent_;
uint32_t start_;
};
void *alloc(size_t bytes) override;
size_t size() const;
void clear();
String startString(); // NOLINT
protected:
void *do_alloc(size_t bytes); // NOLINT
void resize(size_t size); // NOLINT
void reserve(size_t size); // NOLINT
char *buffer_{nullptr};
size_t size_{0};
size_t capacity_{0};
std::vector<char *> free_blocks_;
};
extern VectorJsonBuffer global_json_buffer; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
} // namespace json
} // namespace esphome
#endif // USE_ARDUINO

View File

@@ -1 +0,0 @@
CODEOWNERS = ["@Cat-Ion"]

View File

@@ -1,82 +0,0 @@
#include "kalman_combinator.h"
#include "esphome/core/hal.h"
#include <cmath>
#include <functional>
namespace esphome {
namespace kalman_combinator {
void KalmanCombinatorComponent::dump_config() {
ESP_LOGCONFIG("kalman_combinator", "Kalman Combinator:");
ESP_LOGCONFIG("kalman_combinator", " Update variance: %f per ms", this->update_variance_value_);
ESP_LOGCONFIG("kalman_combinator", " Sensors:");
for (const auto &sensor : this->sensors_) {
auto &entity = *sensor.first;
ESP_LOGCONFIG("kalman_combinator", " - %s", entity.get_name().c_str());
}
}
void KalmanCombinatorComponent::setup() {
for (const auto &sensor : this->sensors_) {
const auto stddev = sensor.second;
sensor.first->add_on_state_callback([this, stddev](float x) -> void { this->correct_(x, stddev(x)); });
}
}
void KalmanCombinatorComponent::add_source(Sensor *sensor, std::function<float(float)> const &stddev) {
this->sensors_.emplace_back(sensor, stddev);
}
void KalmanCombinatorComponent::add_source(Sensor *sensor, float stddev) {
this->add_source(sensor, std::function<float(float)>{[stddev](float) -> float { return stddev; }});
}
void KalmanCombinatorComponent::update_variance_() {
uint32_t now = millis();
// Variance increases by update_variance_ each millisecond
auto dt = now - this->last_update_;
auto dv = this->update_variance_value_ * dt;
this->variance_ += dv;
this->last_update_ = now;
}
void KalmanCombinatorComponent::correct_(float value, float stddev) {
if (std::isnan(value) || std::isinf(stddev)) {
return;
}
if (std::isnan(this->state_) || std::isinf(this->variance_)) {
this->state_ = value;
this->variance_ = stddev * stddev;
if (this->std_dev_sensor_ != nullptr) {
this->std_dev_sensor_->publish_state(stddev);
}
return;
}
this->update_variance_();
// Combine two gaussian distributions mu1+-var1, mu2+-var2 to a new one around mu
// Use the value with the smaller variance as mu1 to prevent precision errors
const bool this_first = this->variance_ < (stddev * stddev);
const float mu1 = this_first ? this->state_ : value;
const float mu2 = this_first ? value : this->state_;
const float var1 = this_first ? this->variance_ : stddev * stddev;
const float var2 = this_first ? stddev * stddev : this->variance_;
const float mu = mu1 + var1 * (mu2 - mu1) / (var1 + var2);
const float var = var1 - (var1 * var1) / (var1 + var2);
// Update and publish state
this->state_ = mu;
this->variance_ = var;
this->publish_state(mu);
if (this->std_dev_sensor_ != nullptr) {
this->std_dev_sensor_->publish_state(std::sqrt(var));
}
}
} // namespace kalman_combinator
} // namespace esphome

View File

@@ -1,46 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/sensor/sensor.h"
#include <cmath>
#include <vector>
namespace esphome {
namespace kalman_combinator {
class KalmanCombinatorComponent : public Component, public sensor::Sensor {
public:
KalmanCombinatorComponent() = default;
float get_setup_priority() const override { return esphome::setup_priority::DATA; }
void dump_config() override;
void setup() override;
void add_source(Sensor *sensor, std::function<float(float)> const &stddev);
void add_source(Sensor *sensor, float stddev);
void set_process_std_dev(float process_std_dev) {
this->update_variance_value_ = process_std_dev * process_std_dev * 0.001f;
}
void set_std_dev_sensor(Sensor *sensor) { this->std_dev_sensor_ = sensor; }
private:
void update_variance_();
void correct_(float value, float stddev);
// Source sensors and their error functions
std::vector<std::pair<Sensor *, std::function<float(float)>>> sensors_;
// Optional sensor for publishing the current error
sensor::Sensor *std_dev_sensor_{nullptr};
// Tick of the last update
uint32_t last_update_{0};
// Change of the variance, per ms
float update_variance_value_{0.f};
// Best guess for the state and its variance
float state_{NAN};
float variance_{INFINITY};
};
} // namespace kalman_combinator
} // namespace esphome

View File

@@ -1,87 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor
from esphome.const import (
CONF_ID,
CONF_SOURCE,
CONF_ACCURACY_DECIMALS,
CONF_DEVICE_CLASS,
CONF_ENTITY_CATEGORY,
CONF_ICON,
CONF_UNIT_OF_MEASUREMENT,
)
from esphome.core.entity_helpers import inherit_property_from
kalman_combinator_ns = cg.esphome_ns.namespace("kalman_combinator")
KalmanCombinatorComponent = kalman_combinator_ns.class_(
"KalmanCombinatorComponent", cg.Component, sensor.Sensor
)
CONF_ERROR = "error"
CONF_SOURCES = "sources"
CONF_PROCESS_STD_DEV = "process_std_dev"
CONF_STD_DEV = "std_dev"
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(cv.COMPONENT_SCHEMA).extend(
{
cv.GenerateID(): cv.declare_id(KalmanCombinatorComponent),
cv.Required(CONF_PROCESS_STD_DEV): cv.positive_float,
cv.Required(CONF_SOURCES): cv.ensure_list(
cv.Schema(
{
cv.Required(CONF_SOURCE): cv.use_id(sensor.Sensor),
cv.Required(CONF_ERROR): cv.templatable(cv.positive_float),
}
),
),
cv.Optional(CONF_STD_DEV): sensor.SENSOR_SCHEMA,
}
)
# Inherit some sensor values from the first source, for both the state and the error value
properties_to_inherit = [
CONF_ACCURACY_DECIMALS,
CONF_DEVICE_CLASS,
CONF_ENTITY_CATEGORY,
CONF_ICON,
CONF_UNIT_OF_MEASUREMENT,
# CONF_STATE_CLASS could also be inherited, but might lead to unexpected behaviour with "total_increasing"
]
inherit_schema_for_state = [
inherit_property_from(property, [CONF_SOURCES, 0, CONF_SOURCE])
for property in properties_to_inherit
]
inherit_schema_for_std_dev = [
inherit_property_from([CONF_STD_DEV, property], [CONF_SOURCES, 0, CONF_SOURCE])
for property in properties_to_inherit
]
FINAL_VALIDATE_SCHEMA = cv.All(
CONFIG_SCHEMA.extend(
{cv.Required(CONF_ID): cv.use_id(KalmanCombinatorComponent)},
extra=cv.ALLOW_EXTRA,
),
*inherit_schema_for_state,
*inherit_schema_for_std_dev,
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await sensor.register_sensor(var, config)
cg.add(var.set_process_std_dev(config[CONF_PROCESS_STD_DEV]))
for source_conf in config[CONF_SOURCES]:
source = await cg.get_variable(source_conf[CONF_SOURCE])
error = await cg.templatable(
source_conf[CONF_ERROR],
[(float, "x")],
cg.float_,
)
cg.add(var.add_source(source, error))
if CONF_STD_DEV in config:
sens = await sensor.new_sensor(config[CONF_STD_DEV])
cg.add(var.set_std_dev_sensor(sens))

View File

@@ -331,10 +331,9 @@ class AddressableFlickerEffect : public AddressableLightEffect {
return;
this->last_update_ = now;
uint32_t rng_state = random_uint32();
fast_random_set_seed(random_uint32());
for (auto var : it) {
rng_state = (rng_state * 0x9E3779B9) + 0x9E37;
const uint8_t flicker = (rng_state & 0xFF) % intensity;
const uint8_t flicker = fast_random_8() % intensity;
// scale down by random factor
var = var.get() * (255 - flicker);

View File

@@ -8,7 +8,7 @@ namespace light {
// See https://www.home-assistant.io/integrations/light.mqtt/#json-schema for documentation on the schema
void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
void LightJSONSchema::dump_json(LightState &state, JsonObject &root) {
if (state.supports_effects())
root["effect"] = state.get_effect_name();
@@ -52,7 +52,7 @@ void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
if (values.get_color_mode() & ColorCapability::BRIGHTNESS)
root["brightness"] = uint8_t(values.get_brightness() * 255);
JsonObject color = root.createNestedObject("color");
JsonObject &color = root.createNestedObject("color");
if (values.get_color_mode() & ColorCapability::RGB) {
color["r"] = uint8_t(values.get_color_brightness() * values.get_red() * 255);
color["g"] = uint8_t(values.get_color_brightness() * values.get_green() * 255);
@@ -72,7 +72,7 @@ void LightJSONSchema::dump_json(LightState &state, JsonObject root) {
}
}
void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonObject root) {
void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonObject &root) {
if (root.containsKey("state")) {
auto val = parse_on_off(root["state"]);
switch (val) {
@@ -95,7 +95,7 @@ void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonO
}
if (root.containsKey("color")) {
JsonObject color = root["color"];
JsonObject &color = root["color"];
// HA also encodes brightness information in the r, g, b values, so extract that and set it as color brightness.
float max_rgb = 0.0f;
if (color.containsKey("r")) {
@@ -140,7 +140,7 @@ void LightJSONSchema::parse_color_json(LightState &state, LightCall &call, JsonO
}
}
void LightJSONSchema::parse_json(LightState &state, LightCall &call, JsonObject root) {
void LightJSONSchema::parse_json(LightState &state, LightCall &call, JsonObject &root) {
LightJSONSchema::parse_color_json(state, call, root);
if (root.containsKey("flash")) {

View File

@@ -14,12 +14,12 @@ namespace light {
class LightJSONSchema {
public:
/// Dump the state of a light as JSON.
static void dump_json(LightState &state, JsonObject root);
static void dump_json(LightState &state, JsonObject &root);
/// Parse the JSON state of a light to a LightCall.
static void parse_json(LightState &state, LightCall &call, JsonObject root);
static void parse_json(LightState &state, LightCall &call, JsonObject &root);
protected:
static void parse_color_json(LightState &state, LightCall &call, JsonObject root);
static void parse_color_json(LightState &state, LightCall &call, JsonObject &root);
};
} // namespace light

View File

@@ -1,27 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import spi
from esphome.const import CONF_ID
DEPENDENCIES = ["spi"]
MULTI_CONF = True
CODEOWNERS = ["@rsumner"]
mcp3204_ns = cg.esphome_ns.namespace("mcp3204")
MCP3204 = mcp3204_ns.class_("MCP3204", cg.Component, spi.SPIDevice)
CONF_REFERENCE_VOLTAGE = "reference_voltage"
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(MCP3204),
cv.Optional(CONF_REFERENCE_VOLTAGE, default="3.3V"): cv.voltage,
}
).extend(spi.spi_device_schema(cs_pin_required=True))
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
cg.add(var.set_reference_voltage(config[CONF_REFERENCE_VOLTAGE]))
await cg.register_component(var, config)
await spi.register_spi_device(var, config)

View File

@@ -1,35 +0,0 @@
#include "mcp3204.h"
#include "esphome/core/log.h"
namespace esphome {
namespace mcp3204 {
static const char *const TAG = "mcp3204";
float MCP3204::get_setup_priority() const { return setup_priority::HARDWARE; }
void MCP3204::setup() {
ESP_LOGCONFIG(TAG, "Setting up mcp3204");
this->spi_setup();
}
void MCP3204::dump_config() {
ESP_LOGCONFIG(TAG, "MCP3204:");
LOG_PIN(" CS Pin:", this->cs_);
ESP_LOGCONFIG(TAG, " Reference Voltage: %.2fV", this->reference_voltage_);
}
float MCP3204::read_data(uint8_t pin) {
uint8_t adc_primary_config = 0b00000110 & 0b00000111;
uint8_t adc_secondary_config = pin << 6;
this->enable();
this->transfer_byte(adc_primary_config);
uint8_t adc_primary_byte = this->transfer_byte(adc_secondary_config);
uint8_t adc_secondary_byte = this->transfer_byte(0x00);
this->disable();
uint16_t digital_value = (adc_primary_byte << 8 | adc_secondary_byte) & 0b111111111111;
return float(digital_value) / 4096.000 * this->reference_voltage_;
}
} // namespace mcp3204
} // namespace esphome

View File

@@ -1,28 +0,0 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/components/spi/spi.h"
namespace esphome {
namespace mcp3204 {
class MCP3204 : public Component,
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
spi::DATA_RATE_1MHZ> {
public:
MCP3204() = default;
void set_reference_voltage(float reference_voltage) { this->reference_voltage_ = reference_voltage; }
void setup() override;
void dump_config() override;
float get_setup_priority() const override;
float read_data(uint8_t pin);
protected:
float reference_voltage_;
};
} // namespace mcp3204
} // namespace esphome

View File

@@ -1,32 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import sensor, voltage_sampler
from esphome.const import CONF_ID, CONF_NUMBER
from .. import mcp3204_ns, MCP3204
AUTO_LOAD = ["voltage_sampler"]
DEPENDENCIES = ["mcp3204"]
MCP3204Sensor = mcp3204_ns.class_(
"MCP3204Sensor", sensor.Sensor, cg.PollingComponent, voltage_sampler.VoltageSampler
)
CONF_MCP3204_ID = "mcp3204_id"
CONFIG_SCHEMA = sensor.SENSOR_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(MCP3204Sensor),
cv.GenerateID(CONF_MCP3204_ID): cv.use_id(MCP3204),
cv.Required(CONF_NUMBER): cv.int_range(min=0, max=3),
}
).extend(cv.polling_component_schema("60s"))
async def to_code(config):
var = cg.new_Pvariable(
config[CONF_ID],
config[CONF_NUMBER],
)
await cg.register_parented(var, config[CONF_MCP3204_ID])
await cg.register_component(var, config)
await sensor.register_sensor(var, config)

View File

@@ -1,23 +0,0 @@
#include "mcp3204_sensor.h"
#include "esphome/core/log.h"
namespace esphome {
namespace mcp3204 {
static const char *const TAG = "mcp3204.sensor";
MCP3204Sensor::MCP3204Sensor(uint8_t pin) : pin_(pin) {}
float MCP3204Sensor::get_setup_priority() const { return setup_priority::DATA; }
void MCP3204Sensor::dump_config() {
LOG_SENSOR("", "MCP3204 Sensor", this);
ESP_LOGCONFIG(TAG, " Pin: %u", this->pin_);
LOG_UPDATE_INTERVAL(this);
}
float MCP3204Sensor::sample() { return this->parent_->read_data(this->pin_); }
void MCP3204Sensor::update() { this->publish_state(this->sample()); }
} // namespace mcp3204
} // namespace esphome

View File

@@ -1,30 +0,0 @@
#pragma once
#include "esphome/components/sensor/sensor.h"
#include "esphome/components/voltage_sampler/voltage_sampler.h"
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "../mcp3204.h"
namespace esphome {
namespace mcp3204 {
class MCP3204Sensor : public PollingComponent,
public Parented<MCP3204>,
public sensor::Sensor,
public voltage_sampler::VoltageSampler {
public:
MCP3204Sensor(uint8_t pin);
void update() override;
void dump_config() override;
float get_setup_priority() const override;
float sample() override;
protected:
uint8_t pin_;
};
} // namespace mcp3204
} // namespace esphome

View File

@@ -1,21 +0,0 @@
#include "mcp47a1.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
namespace esphome {
namespace mcp47a1 {
static const char *const TAG = "mcp47a1";
void MCP47A1::dump_config() {
ESP_LOGCONFIG(TAG, "MCP47A1 Output:");
LOG_I2C_DEVICE(this);
}
void MCP47A1::write_state(float state) {
const uint8_t value = remap(state, 0.0f, 1.0f, 63, 0);
this->write_byte(0, value);
}
} // namespace mcp47a1
} // namespace esphome

View File

@@ -1,17 +0,0 @@
#pragma once
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/output/float_output.h"
#include "esphome/core/component.h"
namespace esphome {
namespace mcp47a1 {
class MCP47A1 : public Component, public output::FloatOutput, public i2c::I2CDevice {
public:
void dump_config() override;
void write_state(float state) override;
};
} // namespace mcp47a1
} // namespace esphome

View File

@@ -1,27 +0,0 @@
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.components import output, i2c
from esphome.const import CONF_ID
CODEOWNERS = ["@jesserockz"]
DEPENDENCIES = ["i2c"]
mcp47a1_ns = cg.esphome_ns.namespace("mcp47a1")
MCP47A1 = mcp47a1_ns.class_("MCP47A1", output.FloatOutput, cg.Component, i2c.I2CDevice)
CONFIG_SCHEMA = (
output.FLOAT_OUTPUT_SCHEMA.extend(
{
cv.Required(CONF_ID): cv.declare_id(MCP47A1),
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(i2c.i2c_device_schema(0x2E))
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
await output.register_output(var, config)

View File

@@ -23,12 +23,12 @@ class IrFollowMeData : public IrData {
}
/* TEMPERATURE */
uint8_t temp() const { return this->get_value_(4) - 1; }
void set_temp(uint8_t val) { this->set_value_(4, std::min(MAX_TEMP, val) + 1); }
uint8_t temp() const { return this->data_[4] - 1; }
void set_temp(uint8_t val) { this->data_[4] = std::min(MAX_TEMP, val) + 1; }
/* BEEPER */
bool beeper() const { return this->get_value_(3, 128); }
void set_beeper(bool val) { this->set_mask_(3, val, 128); }
bool beeper() const { return this->data_[3] & 128; }
void set_beeper(bool val) { this->set_value_(3, 1, 7, val); }
protected:
static const uint8_t MAX_TEMP = 37;

View File

@@ -1,25 +0,0 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import climate_ir
from esphome.const import CONF_ID
AUTO_LOAD = ["climate_ir", "coolix"]
CODEOWNERS = ["@dudanov"]
midea_ir_ns = cg.esphome_ns.namespace("midea_ir")
MideaIR = midea_ir_ns.class_("MideaIR", climate_ir.ClimateIR)
CONF_USE_FAHRENHEIT = "use_fahrenheit"
CONFIG_SCHEMA = climate_ir.CLIMATE_IR_WITH_RECEIVER_SCHEMA.extend(
{
cv.GenerateID(): cv.declare_id(MideaIR),
cv.Optional(CONF_USE_FAHRENHEIT, default=False): cv.boolean,
}
)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await climate_ir.register_climate_ir(var, config)
cg.add(var.set_fahrenheit(config[CONF_USE_FAHRENHEIT]))

View File

@@ -1,92 +0,0 @@
#pragma once
#include "esphome/components/remote_base/midea_protocol.h"
#include "esphome/components/climate/climate_mode.h"
namespace esphome {
namespace midea_ir {
using climate::ClimateMode;
using climate::ClimateFanMode;
using remote_base::MideaData;
class ControlData : public MideaData {
public:
// Default constructor (power: ON, mode: AUTO, fan: AUTO, temp: 25C)
ControlData() : MideaData({MIDEA_TYPE_CONTROL, 0x82, 0x48, 0xFF, 0xFF}) {}
// Copy from Base
ControlData(const MideaData &data) : MideaData(data) {}
void set_temp(float temp);
float get_temp() const;
void set_mode(ClimateMode mode);
ClimateMode get_mode() const;
void set_fan_mode(ClimateFanMode mode);
ClimateFanMode get_fan_mode() const;
void set_sleep_preset(bool value) { this->set_mask_(1, value, 64); }
bool get_sleep_preset() const { return this->get_value_(1, 64); }
void set_fahrenheit(bool value) { this->set_mask_(2, value, 32); }
bool get_fahrenheit() const { return this->get_value_(2, 32); }
void fix();
protected:
enum Mode : uint8_t {
MODE_COOL,
MODE_DRY,
MODE_AUTO,
MODE_HEAT,
MODE_FAN_ONLY,
};
enum FanMode : uint8_t {
FAN_AUTO,
FAN_LOW,
FAN_MEDIUM,
FAN_HIGH,
};
void set_fan_mode_(FanMode mode) { this->set_value_(1, mode, 3, 3); }
FanMode get_fan_mode_() const { return static_cast<FanMode>(this->get_value_(1, 3, 3)); }
void set_mode_(Mode mode) { this->set_value_(1, mode, 7); }
Mode get_mode_() const { return static_cast<Mode>(this->get_value_(1, 7)); }
void set_power_(bool value) { this->set_mask_(1, value, 128); }
bool get_power_() const { return this->get_value_(1, 128); }
};
class FollowMeData : public MideaData {
public:
// Default constructor (temp: 30C, beeper: off)
FollowMeData() : MideaData({MIDEA_TYPE_FOLLOW_ME, 0x82, 0x48, 0x7F, 0x1F}) {}
// Copy from Base
FollowMeData(const MideaData &data) : MideaData(data) {}
// Direct from temperature and beeper values
FollowMeData(uint8_t temp, bool beeper = false) : FollowMeData() {
this->set_temp(temp);
this->set_beeper(beeper);
}
/* TEMPERATURE */
uint8_t temp() const { return this->get_value_(4) - 1; }
void set_temp(uint8_t val) { this->set_value_(4, std::min(MAX_TEMP, val) + 1); }
/* BEEPER */
bool beeper() const { return this->get_value_(3, 128); }
void set_beeper(bool value) { this->set_mask_(3, value, 128); }
protected:
static const uint8_t MAX_TEMP = 37;
};
class SpecialData : public MideaData {
public:
SpecialData(uint8_t code) : MideaData({MIDEA_TYPE_SPECIAL, code, 0xFF, 0xFF, 0xFF}) {}
static const uint8_t VSWING_STEP = 1;
static const uint8_t VSWING_TOGGLE = 2;
static const uint8_t TURBO_TOGGLE = 9;
};
} // namespace midea_ir
} // namespace esphome

View File

@@ -1,201 +0,0 @@
#include "midea_ir.h"
#include "midea_data.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include "esphome/components/coolix/coolix.h"
namespace esphome {
namespace midea_ir {
static const char *const TAG = "midea_ir.climate";
void ControlData::set_temp(float temp) {
uint8_t min;
if (this->get_fahrenheit()) {
min = MIDEA_TEMPF_MIN;
temp = esphome::clamp<float>(celsius_to_fahrenheit(temp), MIDEA_TEMPF_MIN, MIDEA_TEMPF_MAX);
} else {
min = MIDEA_TEMPC_MIN;
temp = esphome::clamp<float>(temp, MIDEA_TEMPC_MIN, MIDEA_TEMPC_MAX);
}
this->set_value_(2, lroundf(temp) - min, 31);
}
float ControlData::get_temp() const {
const uint8_t temp = this->get_value_(2, 31);
if (this->get_fahrenheit())
return fahrenheit_to_celsius(static_cast<float>(temp + MIDEA_TEMPF_MIN));
return static_cast<float>(temp + MIDEA_TEMPC_MIN);
}
void ControlData::fix() {
// In FAN_AUTO, modes COOL, HEAT and FAN_ONLY bit #5 in byte #1 must be set
const uint8_t value = this->get_value_(1, 31);
if (value == 0 || value == 3 || value == 4)
this->set_mask_(1, true, 32);
// In FAN_ONLY mode we need to set all temperature bits
if (this->get_mode_() == MODE_FAN_ONLY)
this->set_mask_(2, true, 31);
}
void ControlData::set_mode(ClimateMode mode) {
switch (mode) {
case ClimateMode::CLIMATE_MODE_OFF:
this->set_power_(false);
return;
case ClimateMode::CLIMATE_MODE_COOL:
this->set_mode_(MODE_COOL);
break;
case ClimateMode::CLIMATE_MODE_DRY:
this->set_mode_(MODE_DRY);
break;
case ClimateMode::CLIMATE_MODE_FAN_ONLY:
this->set_mode_(MODE_FAN_ONLY);
break;
case ClimateMode::CLIMATE_MODE_HEAT:
this->set_mode_(MODE_HEAT);
break;
default:
this->set_mode_(MODE_AUTO);
break;
}
this->set_power_(true);
}
ClimateMode ControlData::get_mode() const {
if (!this->get_power_())
return ClimateMode::CLIMATE_MODE_OFF;
switch (this->get_mode_()) {
case MODE_COOL:
return ClimateMode::CLIMATE_MODE_COOL;
case MODE_DRY:
return ClimateMode::CLIMATE_MODE_DRY;
case MODE_FAN_ONLY:
return ClimateMode::CLIMATE_MODE_FAN_ONLY;
case MODE_HEAT:
return ClimateMode::CLIMATE_MODE_HEAT;
default:
return ClimateMode::CLIMATE_MODE_HEAT_COOL;
}
}
void ControlData::set_fan_mode(ClimateFanMode mode) {
switch (mode) {
case ClimateFanMode::CLIMATE_FAN_LOW:
this->set_fan_mode_(FAN_LOW);
break;
case ClimateFanMode::CLIMATE_FAN_MEDIUM:
this->set_fan_mode_(FAN_MEDIUM);
break;
case ClimateFanMode::CLIMATE_FAN_HIGH:
this->set_fan_mode_(FAN_HIGH);
break;
default:
this->set_fan_mode_(FAN_AUTO);
break;
}
}
ClimateFanMode ControlData::get_fan_mode() const {
switch (this->get_fan_mode_()) {
case FAN_LOW:
return ClimateFanMode::CLIMATE_FAN_LOW;
case FAN_MEDIUM:
return ClimateFanMode::CLIMATE_FAN_MEDIUM;
case FAN_HIGH:
return ClimateFanMode::CLIMATE_FAN_HIGH;
default:
return ClimateFanMode::CLIMATE_FAN_AUTO;
}
}
void MideaIR::control(const climate::ClimateCall &call) {
// swing and preset resets after unit powered off
if (call.get_mode() == climate::CLIMATE_MODE_OFF) {
this->swing_mode = climate::CLIMATE_SWING_OFF;
this->preset = climate::CLIMATE_PRESET_NONE;
} else if (call.get_swing_mode().has_value() && ((*call.get_swing_mode() == climate::CLIMATE_SWING_OFF &&
this->swing_mode == climate::CLIMATE_SWING_VERTICAL) ||
(*call.get_swing_mode() == climate::CLIMATE_SWING_VERTICAL &&
this->swing_mode == climate::CLIMATE_SWING_OFF))) {
this->swing_ = true;
} else if (call.get_preset().has_value() &&
((*call.get_preset() == climate::CLIMATE_PRESET_NONE && this->preset == climate::CLIMATE_PRESET_BOOST) ||
(*call.get_preset() == climate::CLIMATE_PRESET_BOOST && this->preset == climate::CLIMATE_PRESET_NONE))) {
this->boost_ = true;
}
climate_ir::ClimateIR::control(call);
}
void MideaIR::transmit_(MideaData &data) {
data.finalize();
auto transmit = this->transmitter_->transmit();
remote_base::MideaProtocol().encode(transmit.get_data(), data);
transmit.perform();
}
void MideaIR::transmit_state() {
if (this->swing_) {
SpecialData data(SpecialData::VSWING_TOGGLE);
this->transmit_(data);
this->swing_ = false;
return;
}
if (this->boost_) {
SpecialData data(SpecialData::TURBO_TOGGLE);
this->transmit_(data);
this->boost_ = false;
return;
}
ControlData data;
data.set_fahrenheit(this->fahrenheit_);
data.set_temp(this->target_temperature);
data.set_mode(this->mode);
data.set_fan_mode(this->fan_mode.value_or(ClimateFanMode::CLIMATE_FAN_AUTO));
data.set_sleep_preset(this->preset == climate::CLIMATE_PRESET_SLEEP);
data.fix();
this->transmit_(data);
}
bool MideaIR::on_receive(remote_base::RemoteReceiveData data) {
auto midea = remote_base::MideaProtocol().decode(data);
if (midea.has_value())
return this->on_midea_(*midea);
return coolix::CoolixClimate::on_coolix(this, data);
}
bool MideaIR::on_midea_(const MideaData &data) {
ESP_LOGV(TAG, "Decoded Midea IR data: %s", data.to_string().c_str());
if (data.type() == MideaData::MIDEA_TYPE_CONTROL) {
const ControlData status = data;
if (status.get_mode() != climate::CLIMATE_MODE_FAN_ONLY)
this->target_temperature = status.get_temp();
this->mode = status.get_mode();
this->fan_mode = status.get_fan_mode();
if (status.get_sleep_preset())
this->preset = climate::CLIMATE_PRESET_SLEEP;
else if (this->preset == climate::CLIMATE_PRESET_SLEEP)
this->preset = climate::CLIMATE_PRESET_NONE;
this->publish_state();
return true;
}
if (data.type() == MideaData::MIDEA_TYPE_SPECIAL) {
switch (data[1]) {
case SpecialData::VSWING_TOGGLE:
this->swing_mode = this->swing_mode == climate::CLIMATE_SWING_VERTICAL ? climate::CLIMATE_SWING_OFF
: climate::CLIMATE_SWING_VERTICAL;
break;
case SpecialData::TURBO_TOGGLE:
this->preset = this->preset == climate::CLIMATE_PRESET_BOOST ? climate::CLIMATE_PRESET_NONE
: climate::CLIMATE_PRESET_BOOST;
break;
}
this->publish_state();
return true;
}
return false;
}
} // namespace midea_ir
} // namespace esphome

View File

@@ -1,47 +0,0 @@
#pragma once
#include "esphome/components/climate_ir/climate_ir.h"
#include "midea_data.h"
namespace esphome {
namespace midea_ir {
// Temperature
const uint8_t MIDEA_TEMPC_MIN = 17; // Celsius
const uint8_t MIDEA_TEMPC_MAX = 30; // Celsius
const uint8_t MIDEA_TEMPF_MIN = 62; // Fahrenheit
const uint8_t MIDEA_TEMPF_MAX = 86; // Fahrenheit
class MideaIR : public climate_ir::ClimateIR {
public:
MideaIR()
: climate_ir::ClimateIR(
MIDEA_TEMPC_MIN, MIDEA_TEMPC_MAX, 1.0f, true, true,
{climate::CLIMATE_FAN_AUTO, climate::CLIMATE_FAN_LOW, climate::CLIMATE_FAN_MEDIUM,
climate::CLIMATE_FAN_HIGH},
{climate::CLIMATE_SWING_OFF, climate::CLIMATE_SWING_VERTICAL},
{climate::CLIMATE_PRESET_NONE, climate::CLIMATE_PRESET_SLEEP, climate::CLIMATE_PRESET_BOOST}) {}
/// Override control to change settings of the climate device.
void control(const climate::ClimateCall &call) override;
/// Set use of Fahrenheit units
void set_fahrenheit(bool value) {
this->fahrenheit_ = value;
this->temperature_step_ = value ? 0.5f : 1.0f;
}
protected:
/// Transmit via IR the state of this climate controller.
void transmit_state() override;
void transmit_(MideaData &data);
/// Handle received IR Buffer
bool on_receive(remote_base::RemoteReceiveData data) override;
bool on_midea_(const MideaData &data);
bool fahrenheit_{false};
bool swing_{false};
bool boost_{false};
};
} // namespace midea_ir
} // namespace esphome

View File

@@ -69,7 +69,7 @@ bool Modbus::parse_modbus_byte_(uint8_t byte) {
uint8_t data_len = raw[2];
uint8_t data_offset = 3;
// the response for write command mirrors the requests and data startes at offset 2 instead of 3 for read commands
if (function_code == 0x5 || function_code == 0x06 || function_code == 0xF || function_code == 0x10) {
if (function_code == 0x5 || function_code == 0x06 || function_code == 0x10) {
data_offset = 2;
data_len = 4;
}

View File

@@ -47,16 +47,11 @@ MODBUS_FUNCTION_CODE = {
ModbusRegisterType_ns = modbus_controller_ns.namespace("ModbusRegisterType")
ModbusRegisterType = ModbusRegisterType_ns.enum("ModbusRegisterType")
MODBUS_WRITE_REGISTER_TYPE = {
MODBUS_REGISTER_TYPE = {
"custom": ModbusRegisterType.CUSTOM,
"coil": ModbusRegisterType.COIL,
"holding": ModbusRegisterType.HOLDING,
}
MODBUS_REGISTER_TYPE = {
**MODBUS_WRITE_REGISTER_TYPE,
"discrete_input": ModbusRegisterType.DISCRETE_INPUT,
"holding": ModbusRegisterType.HOLDING,
"read": ModbusRegisterType.READ,
}

Some files were not shown because too many files have changed in this diff Show More