diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 8d9565ad5f..86f35cc47b 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -31,7 +31,7 @@ "ms-python.python", "ms-python.pylint", "ms-python.flake8", - "ms-python.black-formatter", + "charliermarsh.ruff", "visualstudioexptteam.vscodeintellicode", // yaml "redhat.vscode-yaml", @@ -49,14 +49,11 @@ "flake8.args": [ "--config=${workspaceFolder}/.flake8" ], - "black-formatter.args": [ - "--config", - "${workspaceFolder}/pyproject.toml" - ], + "ruff.configuration": "${workspaceFolder}/pyproject.toml", "[python]": { // VS will say "Value is not accepted" before building the devcontainer, but the warning // should go away after build is completed. - "editor.defaultFormatter": "ms-python.black-formatter" + "editor.defaultFormatter": "charliermarsh.ruff" }, "editor.formatOnPaste": false, "editor.formatOnSave": true, diff --git a/.github/actions/build-image/action.yaml b/.github/actions/build-image/action.yaml index 25ae21fbd0..86a6b3f4d2 100644 --- a/.github/actions/build-image/action.yaml +++ b/.github/actions/build-image/action.yaml @@ -46,7 +46,7 @@ runs: - name: Build and push to ghcr by digest id: build-ghcr - uses: docker/build-push-action@v6.13.0 + uses: docker/build-push-action@v6.15.0 env: DOCKER_BUILD_SUMMARY: false DOCKER_BUILD_RECORD_UPLOAD: false @@ -72,7 +72,7 @@ runs: - name: Build and push to dockerhub by digest id: build-dockerhub - uses: docker/build-push-action@v6.13.0 + uses: docker/build-push-action@v6.15.0 env: DOCKER_BUILD_SUMMARY: false DOCKER_BUILD_RECORD_UPLOAD: false diff --git a/.github/actions/restore-python/action.yml b/.github/actions/restore-python/action.yml index e95eb6331f..6eb557a514 100644 --- a/.github/actions/restore-python/action.yml +++ b/.github/actions/restore-python/action.yml @@ -22,7 +22,7 @@ runs: python-version: ${{ inputs.python-version }} - name: Restore Python virtual environment id: cache-venv - uses: actions/cache/restore@v4.2.0 + uses: actions/cache/restore@v4.2.2 with: path: venv # yamllint disable-line rule:line-length diff --git a/.github/workflows/ci-docker.yml b/.github/workflows/ci-docker.yml index 8de6191205..283b6edaeb 100644 --- a/.github/workflows/ci-docker.yml +++ b/.github/workflows/ci-docker.yml @@ -33,11 +33,11 @@ concurrency: jobs: check-docker: name: Build docker containers - runs-on: ubuntu-latest + runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - arch: [amd64, aarch64] + os: ["ubuntu-latest", "ubuntu-24.04-arm"] build_type: ["ha-addon", "docker", "lint"] steps: - uses: actions/checkout@v4.1.7 @@ -46,9 +46,7 @@ jobs: with: python-version: "3.9" - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3.9.0 - - name: Set up QEMU - uses: docker/setup-qemu-action@v3.4.0 + uses: docker/setup-buildx-action@v3.10.0 - name: Set TAG run: | @@ -58,6 +56,6 @@ jobs: run: | docker/build.py \ --tag "${TAG}" \ - --arch "${{ matrix.arch }}" \ + --arch "${{ matrix.os == 'ubuntu-24.04-arm' && 'aarch64' || 'amd64' }}" \ --build-type "${{ matrix.build_type }}" \ build diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ab77db5ca5..6b7220d011 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,7 +47,7 @@ jobs: python-version: ${{ env.DEFAULT_PYTHON }} - name: Restore Python virtual environment id: cache-venv - uses: actions/cache@v4.2.0 + uses: actions/cache@v4.2.2 with: path: venv # yamllint disable-line rule:line-length @@ -61,8 +61,8 @@ jobs: pip install -r requirements.txt -r requirements_optional.txt -r requirements_test.txt pip install -e . - black: - name: Check black + ruff: + name: Check ruff runs-on: ubuntu-24.04 needs: - common @@ -74,10 +74,10 @@ jobs: with: python-version: ${{ env.DEFAULT_PYTHON }} cache-key: ${{ needs.common.outputs.cache-key }} - - name: Run black + - name: Run Ruff run: | . venv/bin/activate - black --verbose esphome tests + ruff format esphome tests - name: Suggested changes run: script/ci-suggest-changes if: always() @@ -255,7 +255,7 @@ jobs: runs-on: ubuntu-24.04 needs: - common - - black + - ruff - ci-custom - clang-format - flake8 @@ -303,14 +303,14 @@ jobs: - name: Cache platformio if: github.ref == 'refs/heads/dev' - uses: actions/cache@v4.2.0 + uses: actions/cache@v4.2.2 with: path: ~/.platformio key: platformio-${{ matrix.pio_cache_key }} - name: Cache platformio if: github.ref != 'refs/heads/dev' - uses: actions/cache/restore@v4.2.0 + uses: actions/cache/restore@v4.2.2 with: path: ~/.platformio key: platformio-${{ matrix.pio_cache_key }} @@ -482,7 +482,7 @@ jobs: runs-on: ubuntu-24.04 needs: - common - - black + - ruff - ci-custom - clang-format - flake8 diff --git a/.github/workflows/matchers/lint-python.json b/.github/workflows/matchers/lint-python.json index 6a09f04770..6f750f209a 100644 --- a/.github/workflows/matchers/lint-python.json +++ b/.github/workflows/matchers/lint-python.json @@ -1,11 +1,11 @@ { "problemMatcher": [ { - "owner": "black", + "owner": "ruff", "severity": "error", "pattern": [ { - "regexp": "^(.*): (Please format this file with the black formatter)", + "regexp": "^(.*): (Please format this file with the ruff formatter)", "file": 1, "message": 2 } diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index aa41cf2790..3cc668c113 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -89,10 +89,10 @@ jobs: python-version: "3.9" - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3.9.0 + uses: docker/setup-buildx-action@v3.10.0 - name: Set up QEMU if: matrix.platform != 'linux/amd64' - uses: docker/setup-qemu-action@v3.4.0 + uses: docker/setup-qemu-action@v3.6.0 - name: Log in to docker hub uses: docker/login-action@v3.3.0 @@ -140,7 +140,7 @@ jobs: echo name=$(cat /tmp/platform) >> $GITHUB_OUTPUT - name: Upload digests - uses: actions/upload-artifact@v4.6.0 + uses: actions/upload-artifact@v4.6.1 with: name: digests-${{ steps.sanitize.outputs.name }} path: /tmp/digests @@ -176,14 +176,14 @@ jobs: - uses: actions/checkout@v4.1.7 - name: Download digests - uses: actions/download-artifact@v4.1.8 + uses: actions/download-artifact@v4.1.9 with: pattern: digests-* path: /tmp/digests merge-multiple: true - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3.9.0 + uses: docker/setup-buildx-action@v3.10.0 - name: Log in to docker hub if: matrix.registry == 'dockerhub' diff --git a/.github/workflows/sync-device-classes.yml b/.github/workflows/sync-device-classes.yml index 9abbb20e86..52adcefc32 100644 --- a/.github/workflows/sync-device-classes.yml +++ b/.github/workflows/sync-device-classes.yml @@ -36,7 +36,7 @@ jobs: python ./script/sync-device_class.py - name: Commit changes - uses: peter-evans/create-pull-request@v7.0.6 + uses: peter-evans/create-pull-request@v7.0.7 with: commit-message: "Synchronise Device Classes from Home Assistant" committer: esphomebot diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 212d822ff8..667a8f2e8b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,7 +4,7 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.5.4 + rev: v0.9.2 hooks: # Run the linter. - id: ruff diff --git a/CODEOWNERS b/CODEOWNERS index 26e36befe5..204d2b58bd 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -93,6 +93,7 @@ esphome/components/captive_portal/* @OttoWinter esphome/components/ccs811/* @habbie esphome/components/cd74hc4067/* @asoehlke esphome/components/ch422g/* @clydebarrow @jesterret +esphome/components/chsc6x/* @kkosik20 esphome/components/climate/* @esphome/core esphome/components/climate_ir/* @glmnet esphome/components/color_temperature/* @jesserockz @@ -234,6 +235,7 @@ esphome/components/kuntze/* @ssieb esphome/components/lcd_menu/* @numo68 esphome/components/ld2410/* @regevbr @sebcaps esphome/components/ld2420/* @descipher +esphome/components/ld2450/* @hareeshmu esphome/components/ledc/* @OttoWinter esphome/components/libretiny/* @kuba2k2 esphome/components/libretiny_pwm/* @kuba2k2 @@ -297,6 +299,7 @@ esphome/components/mopeka_std_check/* @Fabian-Schmidt esphome/components/mpl3115a2/* @kbickar esphome/components/mpu6886/* @fabaff esphome/components/ms8607/* @e28eta +esphome/components/msa3xx/* @latonita esphome/components/nau7802/* @cujomalainey esphome/components/network/* @esphome/core esphome/components/nextion/* @edwardtfn @senexcrenshaw @@ -445,6 +448,7 @@ esphome/components/tmp102/* @timsavage esphome/components/tmp1075/* @sybrenstuvel esphome/components/tmp117/* @Azimath esphome/components/tof10120/* @wstrzalka +esphome/components/tormatic/* @ti-mo esphome/components/toshiba/* @kbx81 esphome/components/touchscreen/* @jesserockz @nielsnl68 esphome/components/tsl2591/* @wjcarpenter diff --git a/docker/Dockerfile b/docker/Dockerfile index 6da5c52d64..ee375ba690 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -33,9 +33,9 @@ RUN \ python3-venv=3.11.2-1+b1 \ python3-wheel=0.38.4-2 \ iputils-ping=3:20221126-1+deb12u1 \ - git=1:2.39.5-0+deb12u1 \ - curl=7.88.1-10+deb12u8 \ - openssh-client=1:9.2p1-2+deb12u4 \ + git=1:2.39.5-0+deb12u2 \ + curl=7.88.1-10+deb12u12 \ + openssh-client=1:9.2p1-2+deb12u5 \ python3-cffi=1.15.1-5 \ libcairo2=1.16.0-7 \ libmagic1=1:5.44-3 \ @@ -76,7 +76,7 @@ BUILD_DEPS=" python3-dev=3.11.2-1+b1 zlib1g-dev=1:1.2.13.dfsg-1 libjpeg-dev=1:2.1.5-2 - libfreetype-dev=2.12.1+dfsg-5+deb12u3 + libfreetype-dev=2.12.1+dfsg-5+deb12u4 libssl-dev=3.0.15-1~deb12u1 libffi-dev=3.4.4-1 cargo=0.66.0+ds1-1 @@ -84,7 +84,7 @@ BUILD_DEPS=" " LIB_DEPS=" libtiff6=4.5.0-6+deb12u1 - libopenjp2-7=2.5.0-2 + libopenjp2-7=2.5.0-2+deb12u1 " if [ "$TARGETARCH$TARGETVARIANT" = "arm64" ] then @@ -160,7 +160,7 @@ RUN \ apt-get update \ # Use pinned versions so that we get updates with build caching && apt-get install -y --no-install-recommends \ - nginx-light=1.22.1-9 \ + nginx-light=1.22.1-9+deb12u1 \ && rm -rf \ /tmp/* \ /var/{cache,log}/* \ diff --git a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/run b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/run index f973dfcaf8..cdbaff6c04 100755 --- a/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/run +++ b/docker/ha-addon-rootfs/etc/s6-overlay/s6-rc.d/esphome/run @@ -23,10 +23,6 @@ if bashio::config.true 'streamer_mode'; then export ESPHOME_STREAMER_MODE=true fi -if bashio::config.true 'status_use_ping'; then - export ESPHOME_DASHBOARD_USE_PING=true -fi - if bashio::config.has_value 'relative_url'; then export ESPHOME_DASHBOARD_RELATIVE_URL=$(bashio::config 'relative_url') fi diff --git a/esphome/__main__.py b/esphome/__main__.py index 2a0bd8f2b3..43b5504704 100644 --- a/esphome/__main__.py +++ b/esphome/__main__.py @@ -2,6 +2,7 @@ import argparse from datetime import datetime import functools +import importlib import logging import os import re @@ -66,7 +67,7 @@ def choose_prompt(options, purpose: str = None): return options[0][1] safe_print( - f'Found multiple options{f" for {purpose}" if purpose else ""}, please choose one:' + f"Found multiple options{f' for {purpose}' if purpose else ''}, please choose one:" ) for i, (desc, _) in enumerate(options): safe_print(f" [{i + 1}] {desc}") @@ -336,6 +337,13 @@ def check_permissions(port): def upload_program(config, args, host): + try: + module = importlib.import_module("esphome.components." + CORE.target_platform) + if getattr(module, "upload_program")(config, args, host): + return 0 + except AttributeError: + pass + if get_port_type(host) == "SERIAL": check_permissions(host) if CORE.target_platform in (PLATFORM_ESP32, PLATFORM_ESP8266): diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index 534098e5fd..d59b5e0d3e 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -227,6 +227,9 @@ message DeviceInfoResponse { uint32 voice_assistant_feature_flags = 17; string suggested_area = 16; + + // The Bluetooth mac address of the device. For example "AC:BC:32:89:0E:AA" + string bluetooth_mac_address = 18; } message ListEntitiesRequest { @@ -1564,6 +1567,8 @@ message VoiceAssistantAnnounceRequest { string media_id = 1; string text = 2; + string preannounce_media_id = 3; + bool start_conversation = 4; } message VoiceAssistantAnnounceFinished { diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index bb55a2ccf6..9d7b8c1780 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -28,8 +28,38 @@ namespace api { static const char *const TAG = "api.connection"; static const int ESP32_CAMERA_STOP_STREAM = 5000; +// helper for allowing only unique entries in the queue +void DeferredMessageQueue::dmq_push_back_with_dedup_(void *source, send_message_t *send_message) { + DeferredMessage item(source, send_message); + + auto iter = std::find_if(this->deferred_queue_.begin(), this->deferred_queue_.end(), + [&item](const DeferredMessage &test) -> bool { return test == item; }); + + if (iter != this->deferred_queue_.end()) { + (*iter) = item; + } else { + this->deferred_queue_.push_back(item); + } +} + +void DeferredMessageQueue::process_queue() { + while (!deferred_queue_.empty()) { + DeferredMessage &de = deferred_queue_.front(); + if (de.send_message_(this->api_connection_, de.source_)) { + // O(n) but memory efficiency is more important than speed here which is why std::vector was chosen + deferred_queue_.erase(deferred_queue_.begin()); + } else { + break; + } + } +} + +void DeferredMessageQueue::defer(void *source, send_message_t *send_message) { + this->dmq_push_back_with_dedup_(source, send_message); +} + APIConnection::APIConnection(std::unique_ptr sock, APIServer *parent) - : parent_(parent), initial_state_iterator_(this), list_entities_iterator_(this) { + : parent_(parent), deferred_message_queue_(this), initial_state_iterator_(this), list_entities_iterator_(this) { this->proto_write_buffer_.reserve(64); #if defined(USE_API_PLAINTEXT) @@ -116,8 +146,12 @@ void APIConnection::loop() { return; } - this->list_entities_iterator_.advance(); - this->initial_state_iterator_.advance(); + this->deferred_message_queue_.process_queue(); + + if (!this->list_entities_iterator_.completed()) + this->list_entities_iterator_.advance(); + if (!this->initial_state_iterator_.completed() && this->list_entities_iterator_.completed()) + this->initial_state_iterator_.advance(); static uint32_t keepalive = 60000; static uint8_t max_ping_retries = 60; @@ -210,13 +244,31 @@ bool APIConnection::send_binary_sensor_state(binary_sensor::BinarySensor *binary if (!this->state_subscription_) return false; + if (!APIConnection::try_send_binary_sensor_state(this, binary_sensor, state)) { + this->deferred_message_queue_.defer(binary_sensor, try_send_binary_sensor_state); + } + + return true; +} +void APIConnection::send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor) { + if (!APIConnection::try_send_binary_sensor_info(this, binary_sensor)) { + this->deferred_message_queue_.defer(binary_sensor, try_send_binary_sensor_info); + } +} +bool APIConnection::try_send_binary_sensor_state(APIConnection *api, void *v_binary_sensor) { + binary_sensor::BinarySensor *binary_sensor = reinterpret_cast(v_binary_sensor); + return APIConnection::try_send_binary_sensor_state(api, binary_sensor, binary_sensor->state); +} +bool APIConnection::try_send_binary_sensor_state(APIConnection *api, binary_sensor::BinarySensor *binary_sensor, + bool state) { BinarySensorStateResponse resp; resp.key = binary_sensor->get_object_id_hash(); resp.state = state; resp.missing_state = !binary_sensor->has_state(); - return this->send_binary_sensor_state_response(resp); + return api->send_binary_sensor_state_response(resp); } -bool APIConnection::send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor) { +bool APIConnection::try_send_binary_sensor_info(APIConnection *api, void *v_binary_sensor) { + binary_sensor::BinarySensor *binary_sensor = reinterpret_cast(v_binary_sensor); ListEntitiesBinarySensorResponse msg; msg.object_id = binary_sensor->get_object_id(); msg.key = binary_sensor->get_object_id_hash(); @@ -228,7 +280,7 @@ bool APIConnection::send_binary_sensor_info(binary_sensor::BinarySensor *binary_ msg.disabled_by_default = binary_sensor->is_disabled_by_default(); msg.icon = binary_sensor->get_icon(); msg.entity_category = static_cast(binary_sensor->get_entity_category()); - return this->send_list_entities_binary_sensor_response(msg); + return api->send_list_entities_binary_sensor_response(msg); } #endif @@ -237,6 +289,19 @@ bool APIConnection::send_cover_state(cover::Cover *cover) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_cover_state(this, cover)) { + this->deferred_message_queue_.defer(cover, try_send_cover_state); + } + + return true; +} +void APIConnection::send_cover_info(cover::Cover *cover) { + if (!APIConnection::try_send_cover_info(this, cover)) { + this->deferred_message_queue_.defer(cover, try_send_cover_info); + } +} +bool APIConnection::try_send_cover_state(APIConnection *api, void *v_cover) { + cover::Cover *cover = reinterpret_cast(v_cover); auto traits = cover->get_traits(); CoverStateResponse resp{}; resp.key = cover->get_object_id_hash(); @@ -246,9 +311,10 @@ bool APIConnection::send_cover_state(cover::Cover *cover) { if (traits.get_supports_tilt()) resp.tilt = cover->tilt; resp.current_operation = static_cast(cover->current_operation); - return this->send_cover_state_response(resp); + return api->send_cover_state_response(resp); } -bool APIConnection::send_cover_info(cover::Cover *cover) { +bool APIConnection::try_send_cover_info(APIConnection *api, void *v_cover) { + cover::Cover *cover = reinterpret_cast(v_cover); auto traits = cover->get_traits(); ListEntitiesCoverResponse msg; msg.key = cover->get_object_id_hash(); @@ -264,7 +330,7 @@ bool APIConnection::send_cover_info(cover::Cover *cover) { msg.disabled_by_default = cover->is_disabled_by_default(); msg.icon = cover->get_icon(); msg.entity_category = static_cast(cover->get_entity_category()); - return this->send_list_entities_cover_response(msg); + return api->send_list_entities_cover_response(msg); } void APIConnection::cover_command(const CoverCommandRequest &msg) { cover::Cover *cover = App.get_cover_by_key(msg.key); @@ -300,6 +366,19 @@ bool APIConnection::send_fan_state(fan::Fan *fan) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_fan_state(this, fan)) { + this->deferred_message_queue_.defer(fan, try_send_fan_state); + } + + return true; +} +void APIConnection::send_fan_info(fan::Fan *fan) { + if (!APIConnection::try_send_fan_info(this, fan)) { + this->deferred_message_queue_.defer(fan, try_send_fan_info); + } +} +bool APIConnection::try_send_fan_state(APIConnection *api, void *v_fan) { + fan::Fan *fan = reinterpret_cast(v_fan); auto traits = fan->get_traits(); FanStateResponse resp{}; resp.key = fan->get_object_id_hash(); @@ -313,9 +392,10 @@ bool APIConnection::send_fan_state(fan::Fan *fan) { resp.direction = static_cast(fan->direction); if (traits.supports_preset_modes()) resp.preset_mode = fan->preset_mode; - return this->send_fan_state_response(resp); + return api->send_fan_state_response(resp); } -bool APIConnection::send_fan_info(fan::Fan *fan) { +bool APIConnection::try_send_fan_info(APIConnection *api, void *v_fan) { + fan::Fan *fan = reinterpret_cast(v_fan); auto traits = fan->get_traits(); ListEntitiesFanResponse msg; msg.key = fan->get_object_id_hash(); @@ -332,7 +412,7 @@ bool APIConnection::send_fan_info(fan::Fan *fan) { msg.disabled_by_default = fan->is_disabled_by_default(); msg.icon = fan->get_icon(); msg.entity_category = static_cast(fan->get_entity_category()); - return this->send_list_entities_fan_response(msg); + return api->send_list_entities_fan_response(msg); } void APIConnection::fan_command(const FanCommandRequest &msg) { fan::Fan *fan = App.get_fan_by_key(msg.key); @@ -361,6 +441,19 @@ bool APIConnection::send_light_state(light::LightState *light) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_light_state(this, light)) { + this->deferred_message_queue_.defer(light, try_send_light_state); + } + + return true; +} +void APIConnection::send_light_info(light::LightState *light) { + if (!APIConnection::try_send_light_info(this, light)) { + this->deferred_message_queue_.defer(light, try_send_light_info); + } +} +bool APIConnection::try_send_light_state(APIConnection *api, void *v_light) { + light::LightState *light = reinterpret_cast(v_light); auto traits = light->get_traits(); auto values = light->remote_values; auto color_mode = values.get_color_mode(); @@ -380,9 +473,10 @@ bool APIConnection::send_light_state(light::LightState *light) { resp.warm_white = values.get_warm_white(); if (light->supports_effects()) resp.effect = light->get_effect_name(); - return this->send_light_state_response(resp); + return api->send_light_state_response(resp); } -bool APIConnection::send_light_info(light::LightState *light) { +bool APIConnection::try_send_light_info(APIConnection *api, void *v_light) { + light::LightState *light = reinterpret_cast(v_light); auto traits = light->get_traits(); ListEntitiesLightResponse msg; msg.key = light->get_object_id_hash(); @@ -415,7 +509,7 @@ bool APIConnection::send_light_info(light::LightState *light) { for (auto *effect : light->get_effects()) msg.effects.push_back(effect->get_name()); } - return this->send_list_entities_light_response(msg); + return api->send_list_entities_light_response(msg); } void APIConnection::light_command(const LightCommandRequest &msg) { light::LightState *light = App.get_light_by_key(msg.key); @@ -459,13 +553,30 @@ bool APIConnection::send_sensor_state(sensor::Sensor *sensor, float state) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_sensor_state(this, sensor, state)) { + this->deferred_message_queue_.defer(sensor, try_send_sensor_state); + } + + return true; +} +void APIConnection::send_sensor_info(sensor::Sensor *sensor) { + if (!APIConnection::try_send_sensor_info(this, sensor)) { + this->deferred_message_queue_.defer(sensor, try_send_sensor_info); + } +} +bool APIConnection::try_send_sensor_state(APIConnection *api, void *v_sensor) { + sensor::Sensor *sensor = reinterpret_cast(v_sensor); + return APIConnection::try_send_sensor_state(api, sensor, sensor->state); +} +bool APIConnection::try_send_sensor_state(APIConnection *api, sensor::Sensor *sensor, float state) { SensorStateResponse resp{}; resp.key = sensor->get_object_id_hash(); resp.state = state; resp.missing_state = !sensor->has_state(); - return this->send_sensor_state_response(resp); + return api->send_sensor_state_response(resp); } -bool APIConnection::send_sensor_info(sensor::Sensor *sensor) { +bool APIConnection::try_send_sensor_info(APIConnection *api, void *v_sensor) { + sensor::Sensor *sensor = reinterpret_cast(v_sensor); ListEntitiesSensorResponse msg; msg.key = sensor->get_object_id_hash(); msg.object_id = sensor->get_object_id(); @@ -482,7 +593,7 @@ bool APIConnection::send_sensor_info(sensor::Sensor *sensor) { msg.state_class = static_cast(sensor->get_state_class()); msg.disabled_by_default = sensor->is_disabled_by_default(); msg.entity_category = static_cast(sensor->get_entity_category()); - return this->send_list_entities_sensor_response(msg); + return api->send_list_entities_sensor_response(msg); } #endif @@ -491,12 +602,29 @@ bool APIConnection::send_switch_state(switch_::Switch *a_switch, bool state) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_switch_state(this, a_switch, state)) { + this->deferred_message_queue_.defer(a_switch, try_send_switch_state); + } + + return true; +} +void APIConnection::send_switch_info(switch_::Switch *a_switch) { + if (!APIConnection::try_send_switch_info(this, a_switch)) { + this->deferred_message_queue_.defer(a_switch, try_send_switch_info); + } +} +bool APIConnection::try_send_switch_state(APIConnection *api, void *v_a_switch) { + switch_::Switch *a_switch = reinterpret_cast(v_a_switch); + return APIConnection::try_send_switch_state(api, a_switch, a_switch->state); +} +bool APIConnection::try_send_switch_state(APIConnection *api, switch_::Switch *a_switch, bool state) { SwitchStateResponse resp{}; resp.key = a_switch->get_object_id_hash(); resp.state = state; - return this->send_switch_state_response(resp); + return api->send_switch_state_response(resp); } -bool APIConnection::send_switch_info(switch_::Switch *a_switch) { +bool APIConnection::try_send_switch_info(APIConnection *api, void *v_a_switch) { + switch_::Switch *a_switch = reinterpret_cast(v_a_switch); ListEntitiesSwitchResponse msg; msg.key = a_switch->get_object_id_hash(); msg.object_id = a_switch->get_object_id(); @@ -508,7 +636,7 @@ bool APIConnection::send_switch_info(switch_::Switch *a_switch) { msg.disabled_by_default = a_switch->is_disabled_by_default(); msg.entity_category = static_cast(a_switch->get_entity_category()); msg.device_class = a_switch->get_device_class(); - return this->send_list_entities_switch_response(msg); + return api->send_list_entities_switch_response(msg); } void APIConnection::switch_command(const SwitchCommandRequest &msg) { switch_::Switch *a_switch = App.get_switch_by_key(msg.key); @@ -528,13 +656,31 @@ bool APIConnection::send_text_sensor_state(text_sensor::TextSensor *text_sensor, if (!this->state_subscription_) return false; + if (!APIConnection::try_send_text_sensor_state(this, text_sensor, std::move(state))) { + this->deferred_message_queue_.defer(text_sensor, try_send_text_sensor_state); + } + + return true; +} +void APIConnection::send_text_sensor_info(text_sensor::TextSensor *text_sensor) { + if (!APIConnection::try_send_text_sensor_info(this, text_sensor)) { + this->deferred_message_queue_.defer(text_sensor, try_send_text_sensor_info); + } +} +bool APIConnection::try_send_text_sensor_state(APIConnection *api, void *v_text_sensor) { + text_sensor::TextSensor *text_sensor = reinterpret_cast(v_text_sensor); + return APIConnection::try_send_text_sensor_state(api, text_sensor, text_sensor->state); +} +bool APIConnection::try_send_text_sensor_state(APIConnection *api, text_sensor::TextSensor *text_sensor, + std::string state) { TextSensorStateResponse resp{}; resp.key = text_sensor->get_object_id_hash(); resp.state = std::move(state); resp.missing_state = !text_sensor->has_state(); - return this->send_text_sensor_state_response(resp); + return api->send_text_sensor_state_response(resp); } -bool APIConnection::send_text_sensor_info(text_sensor::TextSensor *text_sensor) { +bool APIConnection::try_send_text_sensor_info(APIConnection *api, void *v_text_sensor) { + text_sensor::TextSensor *text_sensor = reinterpret_cast(v_text_sensor); ListEntitiesTextSensorResponse msg; msg.key = text_sensor->get_object_id_hash(); msg.object_id = text_sensor->get_object_id(); @@ -546,7 +692,7 @@ bool APIConnection::send_text_sensor_info(text_sensor::TextSensor *text_sensor) msg.disabled_by_default = text_sensor->is_disabled_by_default(); msg.entity_category = static_cast(text_sensor->get_entity_category()); msg.device_class = text_sensor->get_device_class(); - return this->send_list_entities_text_sensor_response(msg); + return api->send_list_entities_text_sensor_response(msg); } #endif @@ -555,6 +701,19 @@ bool APIConnection::send_climate_state(climate::Climate *climate) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_climate_state(this, climate)) { + this->deferred_message_queue_.defer(climate, try_send_climate_state); + } + + return true; +} +void APIConnection::send_climate_info(climate::Climate *climate) { + if (!APIConnection::try_send_climate_info(this, climate)) { + this->deferred_message_queue_.defer(climate, try_send_climate_info); + } +} +bool APIConnection::try_send_climate_state(APIConnection *api, void *v_climate) { + climate::Climate *climate = reinterpret_cast(v_climate); auto traits = climate->get_traits(); ClimateStateResponse resp{}; resp.key = climate->get_object_id_hash(); @@ -583,9 +742,10 @@ bool APIConnection::send_climate_state(climate::Climate *climate) { resp.current_humidity = climate->current_humidity; if (traits.get_supports_target_humidity()) resp.target_humidity = climate->target_humidity; - return this->send_climate_state_response(resp); + return api->send_climate_state_response(resp); } -bool APIConnection::send_climate_info(climate::Climate *climate) { +bool APIConnection::try_send_climate_info(APIConnection *api, void *v_climate) { + climate::Climate *climate = reinterpret_cast(v_climate); auto traits = climate->get_traits(); ListEntitiesClimateResponse msg; msg.key = climate->get_object_id_hash(); @@ -626,7 +786,7 @@ bool APIConnection::send_climate_info(climate::Climate *climate) { msg.supported_custom_presets.push_back(custom_preset); for (auto swing_mode : traits.get_supported_swing_modes()) msg.supported_swing_modes.push_back(static_cast(swing_mode)); - return this->send_list_entities_climate_response(msg); + return api->send_list_entities_climate_response(msg); } void APIConnection::climate_command(const ClimateCommandRequest &msg) { climate::Climate *climate = App.get_climate_by_key(msg.key); @@ -663,13 +823,30 @@ bool APIConnection::send_number_state(number::Number *number, float state) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_number_state(this, number, state)) { + this->deferred_message_queue_.defer(number, try_send_number_state); + } + + return true; +} +void APIConnection::send_number_info(number::Number *number) { + if (!APIConnection::try_send_number_info(this, number)) { + this->deferred_message_queue_.defer(number, try_send_number_info); + } +} +bool APIConnection::try_send_number_state(APIConnection *api, void *v_number) { + number::Number *number = reinterpret_cast(v_number); + return APIConnection::try_send_number_state(api, number, number->state); +} +bool APIConnection::try_send_number_state(APIConnection *api, number::Number *number, float state) { NumberStateResponse resp{}; resp.key = number->get_object_id_hash(); resp.state = state; resp.missing_state = !number->has_state(); - return this->send_number_state_response(resp); + return api->send_number_state_response(resp); } -bool APIConnection::send_number_info(number::Number *number) { +bool APIConnection::try_send_number_info(APIConnection *api, void *v_number) { + number::Number *number = reinterpret_cast(v_number); ListEntitiesNumberResponse msg; msg.key = number->get_object_id_hash(); msg.object_id = number->get_object_id(); @@ -687,7 +864,7 @@ bool APIConnection::send_number_info(number::Number *number) { msg.max_value = number->traits.get_max_value(); msg.step = number->traits.get_step(); - return this->send_list_entities_number_response(msg); + return api->send_list_entities_number_response(msg); } void APIConnection::number_command(const NumberCommandRequest &msg) { number::Number *number = App.get_number_by_key(msg.key); @@ -705,15 +882,29 @@ bool APIConnection::send_date_state(datetime::DateEntity *date) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_date_state(this, date)) { + this->deferred_message_queue_.defer(date, try_send_date_state); + } + + return true; +} +void APIConnection::send_date_info(datetime::DateEntity *date) { + if (!APIConnection::try_send_date_info(this, date)) { + this->deferred_message_queue_.defer(date, try_send_date_info); + } +} +bool APIConnection::try_send_date_state(APIConnection *api, void *v_date) { + datetime::DateEntity *date = reinterpret_cast(v_date); DateStateResponse resp{}; resp.key = date->get_object_id_hash(); resp.missing_state = !date->has_state(); resp.year = date->year; resp.month = date->month; resp.day = date->day; - return this->send_date_state_response(resp); + return api->send_date_state_response(resp); } -bool APIConnection::send_date_info(datetime::DateEntity *date) { +bool APIConnection::try_send_date_info(APIConnection *api, void *v_date) { + datetime::DateEntity *date = reinterpret_cast(v_date); ListEntitiesDateResponse msg; msg.key = date->get_object_id_hash(); msg.object_id = date->get_object_id(); @@ -724,7 +915,7 @@ bool APIConnection::send_date_info(datetime::DateEntity *date) { msg.disabled_by_default = date->is_disabled_by_default(); msg.entity_category = static_cast(date->get_entity_category()); - return this->send_list_entities_date_response(msg); + return api->send_list_entities_date_response(msg); } void APIConnection::date_command(const DateCommandRequest &msg) { datetime::DateEntity *date = App.get_date_by_key(msg.key); @@ -742,15 +933,29 @@ bool APIConnection::send_time_state(datetime::TimeEntity *time) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_time_state(this, time)) { + this->deferred_message_queue_.defer(time, try_send_time_state); + } + + return true; +} +void APIConnection::send_time_info(datetime::TimeEntity *time) { + if (!APIConnection::try_send_time_info(this, time)) { + this->deferred_message_queue_.defer(time, try_send_time_info); + } +} +bool APIConnection::try_send_time_state(APIConnection *api, void *v_time) { + datetime::TimeEntity *time = reinterpret_cast(v_time); TimeStateResponse resp{}; resp.key = time->get_object_id_hash(); resp.missing_state = !time->has_state(); resp.hour = time->hour; resp.minute = time->minute; resp.second = time->second; - return this->send_time_state_response(resp); + return api->send_time_state_response(resp); } -bool APIConnection::send_time_info(datetime::TimeEntity *time) { +bool APIConnection::try_send_time_info(APIConnection *api, void *v_time) { + datetime::TimeEntity *time = reinterpret_cast(v_time); ListEntitiesTimeResponse msg; msg.key = time->get_object_id_hash(); msg.object_id = time->get_object_id(); @@ -761,7 +966,7 @@ bool APIConnection::send_time_info(datetime::TimeEntity *time) { msg.disabled_by_default = time->is_disabled_by_default(); msg.entity_category = static_cast(time->get_entity_category()); - return this->send_list_entities_time_response(msg); + return api->send_list_entities_time_response(msg); } void APIConnection::time_command(const TimeCommandRequest &msg) { datetime::TimeEntity *time = App.get_time_by_key(msg.key); @@ -779,6 +984,19 @@ bool APIConnection::send_datetime_state(datetime::DateTimeEntity *datetime) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_datetime_state(this, datetime)) { + this->deferred_message_queue_.defer(datetime, try_send_datetime_state); + } + + return true; +} +void APIConnection::send_datetime_info(datetime::DateTimeEntity *datetime) { + if (!APIConnection::try_send_datetime_info(this, datetime)) { + this->deferred_message_queue_.defer(datetime, try_send_datetime_info); + } +} +bool APIConnection::try_send_datetime_state(APIConnection *api, void *v_datetime) { + datetime::DateTimeEntity *datetime = reinterpret_cast(v_datetime); DateTimeStateResponse resp{}; resp.key = datetime->get_object_id_hash(); resp.missing_state = !datetime->has_state(); @@ -786,9 +1004,10 @@ bool APIConnection::send_datetime_state(datetime::DateTimeEntity *datetime) { ESPTime state = datetime->state_as_esptime(); resp.epoch_seconds = state.timestamp; } - return this->send_date_time_state_response(resp); + return api->send_date_time_state_response(resp); } -bool APIConnection::send_datetime_info(datetime::DateTimeEntity *datetime) { +bool APIConnection::try_send_datetime_info(APIConnection *api, void *v_datetime) { + datetime::DateTimeEntity *datetime = reinterpret_cast(v_datetime); ListEntitiesDateTimeResponse msg; msg.key = datetime->get_object_id_hash(); msg.object_id = datetime->get_object_id(); @@ -799,7 +1018,7 @@ bool APIConnection::send_datetime_info(datetime::DateTimeEntity *datetime) { msg.disabled_by_default = datetime->is_disabled_by_default(); msg.entity_category = static_cast(datetime->get_entity_category()); - return this->send_list_entities_date_time_response(msg); + return api->send_list_entities_date_time_response(msg); } void APIConnection::datetime_command(const DateTimeCommandRequest &msg) { datetime::DateTimeEntity *datetime = App.get_datetime_by_key(msg.key); @@ -817,13 +1036,30 @@ bool APIConnection::send_text_state(text::Text *text, std::string state) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_text_state(this, text, std::move(state))) { + this->deferred_message_queue_.defer(text, try_send_text_state); + } + + return true; +} +void APIConnection::send_text_info(text::Text *text) { + if (!APIConnection::try_send_text_info(this, text)) { + this->deferred_message_queue_.defer(text, try_send_text_info); + } +} +bool APIConnection::try_send_text_state(APIConnection *api, void *v_text) { + text::Text *text = reinterpret_cast(v_text); + return APIConnection::try_send_text_state(api, text, text->state); +} +bool APIConnection::try_send_text_state(APIConnection *api, text::Text *text, std::string state) { TextStateResponse resp{}; resp.key = text->get_object_id_hash(); resp.state = std::move(state); resp.missing_state = !text->has_state(); - return this->send_text_state_response(resp); + return api->send_text_state_response(resp); } -bool APIConnection::send_text_info(text::Text *text) { +bool APIConnection::try_send_text_info(APIConnection *api, void *v_text) { + text::Text *text = reinterpret_cast(v_text); ListEntitiesTextResponse msg; msg.key = text->get_object_id_hash(); msg.object_id = text->get_object_id(); @@ -837,7 +1073,7 @@ bool APIConnection::send_text_info(text::Text *text) { msg.max_length = text->traits.get_max_length(); msg.pattern = text->traits.get_pattern(); - return this->send_list_entities_text_response(msg); + return api->send_list_entities_text_response(msg); } void APIConnection::text_command(const TextCommandRequest &msg) { text::Text *text = App.get_text_by_key(msg.key); @@ -855,13 +1091,30 @@ bool APIConnection::send_select_state(select::Select *select, std::string state) if (!this->state_subscription_) return false; + if (!APIConnection::try_send_select_state(this, select, std::move(state))) { + this->deferred_message_queue_.defer(select, try_send_select_state); + } + + return true; +} +void APIConnection::send_select_info(select::Select *select) { + if (!APIConnection::try_send_select_info(this, select)) { + this->deferred_message_queue_.defer(select, try_send_select_info); + } +} +bool APIConnection::try_send_select_state(APIConnection *api, void *v_select) { + select::Select *select = reinterpret_cast(v_select); + return APIConnection::try_send_select_state(api, select, select->state); +} +bool APIConnection::try_send_select_state(APIConnection *api, select::Select *select, std::string state) { SelectStateResponse resp{}; resp.key = select->get_object_id_hash(); resp.state = std::move(state); resp.missing_state = !select->has_state(); - return this->send_select_state_response(resp); + return api->send_select_state_response(resp); } -bool APIConnection::send_select_info(select::Select *select) { +bool APIConnection::try_send_select_info(APIConnection *api, void *v_select) { + select::Select *select = reinterpret_cast(v_select); ListEntitiesSelectResponse msg; msg.key = select->get_object_id_hash(); msg.object_id = select->get_object_id(); @@ -875,7 +1128,7 @@ bool APIConnection::send_select_info(select::Select *select) { for (const auto &option : select->traits.get_options()) msg.options.push_back(option); - return this->send_list_entities_select_response(msg); + return api->send_list_entities_select_response(msg); } void APIConnection::select_command(const SelectCommandRequest &msg) { select::Select *select = App.get_select_by_key(msg.key); @@ -889,7 +1142,13 @@ void APIConnection::select_command(const SelectCommandRequest &msg) { #endif #ifdef USE_BUTTON -bool APIConnection::send_button_info(button::Button *button) { +void APIConnection::send_button_info(button::Button *button) { + if (!APIConnection::try_send_button_info(this, button)) { + this->deferred_message_queue_.defer(button, try_send_button_info); + } +} +bool APIConnection::try_send_button_info(APIConnection *api, void *v_button) { + button::Button *button = reinterpret_cast(v_button); ListEntitiesButtonResponse msg; msg.key = button->get_object_id_hash(); msg.object_id = button->get_object_id(); @@ -900,7 +1159,7 @@ bool APIConnection::send_button_info(button::Button *button) { msg.disabled_by_default = button->is_disabled_by_default(); msg.entity_category = static_cast(button->get_entity_category()); msg.device_class = button->get_device_class(); - return this->send_list_entities_button_response(msg); + return api->send_list_entities_button_response(msg); } void APIConnection::button_command(const ButtonCommandRequest &msg) { button::Button *button = App.get_button_by_key(msg.key); @@ -916,12 +1175,29 @@ bool APIConnection::send_lock_state(lock::Lock *a_lock, lock::LockState state) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_lock_state(this, a_lock, state)) { + this->deferred_message_queue_.defer(a_lock, try_send_lock_state); + } + + return true; +} +void APIConnection::send_lock_info(lock::Lock *a_lock) { + if (!APIConnection::try_send_lock_info(this, a_lock)) { + this->deferred_message_queue_.defer(a_lock, try_send_lock_info); + } +} +bool APIConnection::try_send_lock_state(APIConnection *api, void *v_a_lock) { + lock::Lock *a_lock = reinterpret_cast(v_a_lock); + return APIConnection::try_send_lock_state(api, a_lock, a_lock->state); +} +bool APIConnection::try_send_lock_state(APIConnection *api, lock::Lock *a_lock, lock::LockState state) { LockStateResponse resp{}; resp.key = a_lock->get_object_id_hash(); resp.state = static_cast(state); - return this->send_lock_state_response(resp); + return api->send_lock_state_response(resp); } -bool APIConnection::send_lock_info(lock::Lock *a_lock) { +bool APIConnection::try_send_lock_info(APIConnection *api, void *v_a_lock) { + lock::Lock *a_lock = reinterpret_cast(v_a_lock); ListEntitiesLockResponse msg; msg.key = a_lock->get_object_id_hash(); msg.object_id = a_lock->get_object_id(); @@ -934,7 +1210,7 @@ bool APIConnection::send_lock_info(lock::Lock *a_lock) { msg.entity_category = static_cast(a_lock->get_entity_category()); msg.supports_open = a_lock->traits.get_supports_open(); msg.requires_code = a_lock->traits.get_requires_code(); - return this->send_list_entities_lock_response(msg); + return api->send_list_entities_lock_response(msg); } void APIConnection::lock_command(const LockCommandRequest &msg) { lock::Lock *a_lock = App.get_lock_by_key(msg.key); @@ -960,13 +1236,27 @@ bool APIConnection::send_valve_state(valve::Valve *valve) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_valve_state(this, valve)) { + this->deferred_message_queue_.defer(valve, try_send_valve_state); + } + + return true; +} +void APIConnection::send_valve_info(valve::Valve *valve) { + if (!APIConnection::try_send_valve_info(this, valve)) { + this->deferred_message_queue_.defer(valve, try_send_valve_info); + } +} +bool APIConnection::try_send_valve_state(APIConnection *api, void *v_valve) { + valve::Valve *valve = reinterpret_cast(v_valve); ValveStateResponse resp{}; resp.key = valve->get_object_id_hash(); resp.position = valve->position; resp.current_operation = static_cast(valve->current_operation); - return this->send_valve_state_response(resp); + return api->send_valve_state_response(resp); } -bool APIConnection::send_valve_info(valve::Valve *valve) { +bool APIConnection::try_send_valve_info(APIConnection *api, void *v_valve) { + valve::Valve *valve = reinterpret_cast(v_valve); auto traits = valve->get_traits(); ListEntitiesValveResponse msg; msg.key = valve->get_object_id_hash(); @@ -981,7 +1271,7 @@ bool APIConnection::send_valve_info(valve::Valve *valve) { msg.assumed_state = traits.get_is_assumed_state(); msg.supports_position = traits.get_supports_position(); msg.supports_stop = traits.get_supports_stop(); - return this->send_list_entities_valve_response(msg); + return api->send_list_entities_valve_response(msg); } void APIConnection::valve_command(const ValveCommandRequest &msg) { valve::Valve *valve = App.get_valve_by_key(msg.key); @@ -1002,6 +1292,19 @@ bool APIConnection::send_media_player_state(media_player::MediaPlayer *media_pla if (!this->state_subscription_) return false; + if (!APIConnection::try_send_media_player_state(this, media_player)) { + this->deferred_message_queue_.defer(media_player, try_send_media_player_state); + } + + return true; +} +void APIConnection::send_media_player_info(media_player::MediaPlayer *media_player) { + if (!APIConnection::try_send_media_player_info(this, media_player)) { + this->deferred_message_queue_.defer(media_player, try_send_media_player_info); + } +} +bool APIConnection::try_send_media_player_state(APIConnection *api, void *v_media_player) { + media_player::MediaPlayer *media_player = reinterpret_cast(v_media_player); MediaPlayerStateResponse resp{}; resp.key = media_player->get_object_id_hash(); @@ -1011,9 +1314,10 @@ bool APIConnection::send_media_player_state(media_player::MediaPlayer *media_pla resp.state = static_cast(report_state); resp.volume = media_player->volume; resp.muted = media_player->is_muted(); - return this->send_media_player_state_response(resp); + return api->send_media_player_state_response(resp); } -bool APIConnection::send_media_player_info(media_player::MediaPlayer *media_player) { +bool APIConnection::try_send_media_player_info(APIConnection *api, void *v_media_player) { + media_player::MediaPlayer *media_player = reinterpret_cast(v_media_player); ListEntitiesMediaPlayerResponse msg; msg.key = media_player->get_object_id_hash(); msg.object_id = media_player->get_object_id(); @@ -1037,7 +1341,7 @@ bool APIConnection::send_media_player_info(media_player::MediaPlayer *media_play msg.supported_formats.push_back(media_format); } - return this->send_list_entities_media_player_response(msg); + return api->send_list_entities_media_player_response(msg); } void APIConnection::media_player_command(const MediaPlayerCommandRequest &msg) { media_player::MediaPlayer *media_player = App.get_media_player_by_key(msg.key); @@ -1062,7 +1366,7 @@ void APIConnection::media_player_command(const MediaPlayerCommandRequest &msg) { #endif #ifdef USE_ESP32_CAMERA -void APIConnection::send_camera_state(std::shared_ptr image) { +void APIConnection::set_camera_state(std::shared_ptr image) { if (!this->state_subscription_) return; if (this->image_reader_.available()) @@ -1071,7 +1375,13 @@ void APIConnection::send_camera_state(std::shared_ptr image->was_requested_by(esphome::esp32_camera::IDLE)) this->image_reader_.set_image(std::move(image)); } -bool APIConnection::send_camera_info(esp32_camera::ESP32Camera *camera) { +void APIConnection::send_camera_info(esp32_camera::ESP32Camera *camera) { + if (!APIConnection::try_send_camera_info(this, camera)) { + this->deferred_message_queue_.defer(camera, try_send_camera_info); + } +} +bool APIConnection::try_send_camera_info(APIConnection *api, void *v_camera) { + esp32_camera::ESP32Camera *camera = reinterpret_cast(v_camera); ListEntitiesCameraResponse msg; msg.key = camera->get_object_id_hash(); msg.object_id = camera->get_object_id(); @@ -1081,7 +1391,7 @@ bool APIConnection::send_camera_info(esp32_camera::ESP32Camera *camera) { msg.disabled_by_default = camera->is_disabled_by_default(); msg.icon = camera->get_icon(); msg.entity_category = static_cast(camera->get_entity_category()); - return this->send_list_entities_camera_response(msg); + return api->send_list_entities_camera_response(msg); } void APIConnection::camera_image(const CameraImageRequest &msg) { if (esp32_camera::global_esp32_camera == nullptr) @@ -1268,12 +1578,28 @@ bool APIConnection::send_alarm_control_panel_state(alarm_control_panel::AlarmCon if (!this->state_subscription_) return false; + if (!APIConnection::try_send_alarm_control_panel_state(this, a_alarm_control_panel)) { + this->deferred_message_queue_.defer(a_alarm_control_panel, try_send_alarm_control_panel_state); + } + + return true; +} +void APIConnection::send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) { + if (!APIConnection::try_send_alarm_control_panel_info(this, a_alarm_control_panel)) { + this->deferred_message_queue_.defer(a_alarm_control_panel, try_send_alarm_control_panel_info); + } +} +bool APIConnection::try_send_alarm_control_panel_state(APIConnection *api, void *v_a_alarm_control_panel) { + alarm_control_panel::AlarmControlPanel *a_alarm_control_panel = + reinterpret_cast(v_a_alarm_control_panel); AlarmControlPanelStateResponse resp{}; resp.key = a_alarm_control_panel->get_object_id_hash(); resp.state = static_cast(a_alarm_control_panel->get_state()); - return this->send_alarm_control_panel_state_response(resp); + return api->send_alarm_control_panel_state_response(resp); } -bool APIConnection::send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) { +bool APIConnection::try_send_alarm_control_panel_info(APIConnection *api, void *v_a_alarm_control_panel) { + alarm_control_panel::AlarmControlPanel *a_alarm_control_panel = + reinterpret_cast(v_a_alarm_control_panel); ListEntitiesAlarmControlPanelResponse msg; msg.key = a_alarm_control_panel->get_object_id_hash(); msg.object_id = a_alarm_control_panel->get_object_id(); @@ -1285,7 +1611,7 @@ bool APIConnection::send_alarm_control_panel_info(alarm_control_panel::AlarmCont msg.supported_features = a_alarm_control_panel->get_supported_features(); msg.requires_code = a_alarm_control_panel->get_requires_code(); msg.requires_code_to_arm = a_alarm_control_panel->get_requires_code_to_arm(); - return this->send_list_entities_alarm_control_panel_response(msg); + return api->send_list_entities_alarm_control_panel_response(msg); } void APIConnection::alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) { alarm_control_panel::AlarmControlPanel *a_alarm_control_panel = App.get_alarm_control_panel_by_key(msg.key); @@ -1322,13 +1648,28 @@ void APIConnection::alarm_control_panel_command(const AlarmControlPanelCommandRe #endif #ifdef USE_EVENT -bool APIConnection::send_event(event::Event *event, std::string event_type) { +void APIConnection::send_event(event::Event *event, std::string event_type) { + if (!APIConnection::try_send_event(this, event, std::move(event_type))) { + this->deferred_message_queue_.defer(event, try_send_event); + } +} +void APIConnection::send_event_info(event::Event *event) { + if (!APIConnection::try_send_event_info(this, event)) { + this->deferred_message_queue_.defer(event, try_send_event_info); + } +} +bool APIConnection::try_send_event(APIConnection *api, void *v_event) { + event::Event *event = reinterpret_cast(v_event); + return APIConnection::try_send_event(api, event, *(event->last_event_type)); +} +bool APIConnection::try_send_event(APIConnection *api, event::Event *event, std::string event_type) { EventResponse resp{}; resp.key = event->get_object_id_hash(); resp.event_type = std::move(event_type); - return this->send_event_response(resp); + return api->send_event_response(resp); } -bool APIConnection::send_event_info(event::Event *event) { +bool APIConnection::try_send_event_info(APIConnection *api, void *v_event) { + event::Event *event = reinterpret_cast(v_event); ListEntitiesEventResponse msg; msg.key = event->get_object_id_hash(); msg.object_id = event->get_object_id(); @@ -1341,7 +1682,7 @@ bool APIConnection::send_event_info(event::Event *event) { msg.device_class = event->get_device_class(); for (const auto &event_type : event->get_event_types()) msg.event_types.push_back(event_type); - return this->send_list_entities_event_response(msg); + return api->send_list_entities_event_response(msg); } #endif @@ -1350,6 +1691,19 @@ bool APIConnection::send_update_state(update::UpdateEntity *update) { if (!this->state_subscription_) return false; + if (!APIConnection::try_send_update_state(this, update)) { + this->deferred_message_queue_.defer(update, try_send_update_state); + } + + return true; +} +void APIConnection::send_update_info(update::UpdateEntity *update) { + if (!APIConnection::try_send_update_info(this, update)) { + this->deferred_message_queue_.defer(update, try_send_update_info); + } +} +bool APIConnection::try_send_update_state(APIConnection *api, void *v_update) { + update::UpdateEntity *update = reinterpret_cast(v_update); UpdateStateResponse resp{}; resp.key = update->get_object_id_hash(); resp.missing_state = !update->has_state(); @@ -1366,9 +1720,10 @@ bool APIConnection::send_update_state(update::UpdateEntity *update) { resp.release_url = update->update_info.release_url; } - return this->send_update_state_response(resp); + return api->send_update_state_response(resp); } -bool APIConnection::send_update_info(update::UpdateEntity *update) { +bool APIConnection::try_send_update_info(APIConnection *api, void *v_update) { + update::UpdateEntity *update = reinterpret_cast(v_update); ListEntitiesUpdateResponse msg; msg.key = update->get_object_id_hash(); msg.object_id = update->get_object_id(); @@ -1379,7 +1734,7 @@ bool APIConnection::send_update_info(update::UpdateEntity *update) { msg.disabled_by_default = update->is_disabled_by_default(); msg.entity_category = static_cast(update->get_entity_category()); msg.device_class = update->get_device_class(); - return this->send_list_entities_update_response(msg); + return api->send_list_entities_update_response(msg); } void APIConnection::update_command(const UpdateCommandRequest &msg) { update::UpdateEntity *update = App.get_update_by_key(msg.key); @@ -1403,7 +1758,7 @@ void APIConnection::update_command(const UpdateCommandRequest &msg) { } #endif -bool APIConnection::send_log_message(int level, const char *tag, const char *line) { +bool APIConnection::try_send_log_message(int level, const char *tag, const char *line) { if (this->log_subscription_ < level) return false; @@ -1488,6 +1843,7 @@ DeviceInfoResponse APIConnection::device_info(const DeviceInfoRequest &msg) { #ifdef USE_BLUETOOTH_PROXY resp.legacy_bluetooth_proxy_version = bluetooth_proxy::global_bluetooth_proxy->get_legacy_version(); resp.bluetooth_proxy_feature_flags = bluetooth_proxy::global_bluetooth_proxy->get_feature_flags(); + resp.bluetooth_mac_address = bluetooth_proxy::global_bluetooth_proxy->get_bluetooth_mac_address_pretty(); #endif #ifdef USE_VOICE_ASSISTANT resp.legacy_voice_assistant_version = voice_assistant::global_voice_assistant->get_legacy_version(); diff --git a/esphome/components/api/api_connection.h b/esphome/components/api/api_connection.h index 043aaee421..f17080a6c8 100644 --- a/esphome/components/api/api_connection.h +++ b/esphome/components/api/api_connection.h @@ -14,6 +14,46 @@ namespace esphome { namespace api { +using send_message_t = bool(APIConnection *, void *); + +/* + This class holds a pointer to the source component that wants to publish a message, and a pointer to a function that + will lazily publish that message. The two pointers allow dedup in the deferred queue if multiple publishes for the + same component are backed up, and take up only 8 bytes of memory. The entry in the deferred queue (a std::vector) is + the DeferredMessage instance itself (not a pointer to one elsewhere in heap) so still only 8 bytes per entry. Even + 100 backed up messages (you'd have to have at least 100 sensors publishing because of dedup) would take up only 0.8 + kB. +*/ +class DeferredMessageQueue { + struct DeferredMessage { + friend class DeferredMessageQueue; + + protected: + void *source_; + send_message_t *send_message_; + + public: + DeferredMessage(void *source, send_message_t *send_message) : source_(source), send_message_(send_message) {} + bool operator==(const DeferredMessage &test) const { + return (source_ == test.source_ && send_message_ == test.send_message_); + } + } __attribute__((packed)); + + protected: + // vector is used very specifically for its zero memory overhead even though items are popped from the front (memory + // footprint is more important than speed here) + std::vector deferred_queue_; + APIConnection *api_connection_; + + // helper for allowing only unique entries in the queue + void dmq_push_back_with_dedup_(void *source, send_message_t *send_message); + + public: + DeferredMessageQueue(APIConnection *api_connection) : api_connection_(api_connection) {} + void process_queue(); + void defer(void *source, send_message_t *send_message); +}; + class APIConnection : public APIServerConnection { public: APIConnection(std::unique_ptr socket, APIServer *parent); @@ -28,96 +68,140 @@ class APIConnection : public APIServerConnection { } #ifdef USE_BINARY_SENSOR bool send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor, bool state); - bool send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor); + void send_binary_sensor_info(binary_sensor::BinarySensor *binary_sensor); + static bool try_send_binary_sensor_state(APIConnection *api, void *v_binary_sensor); + static bool try_send_binary_sensor_state(APIConnection *api, binary_sensor::BinarySensor *binary_sensor, bool state); + static bool try_send_binary_sensor_info(APIConnection *api, void *v_binary_sensor); #endif #ifdef USE_COVER bool send_cover_state(cover::Cover *cover); - bool send_cover_info(cover::Cover *cover); + void send_cover_info(cover::Cover *cover); + static bool try_send_cover_state(APIConnection *api, void *v_cover); + static bool try_send_cover_info(APIConnection *api, void *v_cover); void cover_command(const CoverCommandRequest &msg) override; #endif #ifdef USE_FAN bool send_fan_state(fan::Fan *fan); - bool send_fan_info(fan::Fan *fan); + void send_fan_info(fan::Fan *fan); + static bool try_send_fan_state(APIConnection *api, void *v_fan); + static bool try_send_fan_info(APIConnection *api, void *v_fan); void fan_command(const FanCommandRequest &msg) override; #endif #ifdef USE_LIGHT bool send_light_state(light::LightState *light); - bool send_light_info(light::LightState *light); + void send_light_info(light::LightState *light); + static bool try_send_light_state(APIConnection *api, void *v_light); + static bool try_send_light_info(APIConnection *api, void *v_light); void light_command(const LightCommandRequest &msg) override; #endif #ifdef USE_SENSOR bool send_sensor_state(sensor::Sensor *sensor, float state); - bool send_sensor_info(sensor::Sensor *sensor); + void send_sensor_info(sensor::Sensor *sensor); + static bool try_send_sensor_state(APIConnection *api, void *v_sensor); + static bool try_send_sensor_state(APIConnection *api, sensor::Sensor *sensor, float state); + static bool try_send_sensor_info(APIConnection *api, void *v_sensor); #endif #ifdef USE_SWITCH bool send_switch_state(switch_::Switch *a_switch, bool state); - bool send_switch_info(switch_::Switch *a_switch); + void send_switch_info(switch_::Switch *a_switch); + static bool try_send_switch_state(APIConnection *api, void *v_a_switch); + static bool try_send_switch_state(APIConnection *api, switch_::Switch *a_switch, bool state); + static bool try_send_switch_info(APIConnection *api, void *v_a_switch); void switch_command(const SwitchCommandRequest &msg) override; #endif #ifdef USE_TEXT_SENSOR bool send_text_sensor_state(text_sensor::TextSensor *text_sensor, std::string state); - bool send_text_sensor_info(text_sensor::TextSensor *text_sensor); + void send_text_sensor_info(text_sensor::TextSensor *text_sensor); + static bool try_send_text_sensor_state(APIConnection *api, void *v_text_sensor); + static bool try_send_text_sensor_state(APIConnection *api, text_sensor::TextSensor *text_sensor, std::string state); + static bool try_send_text_sensor_info(APIConnection *api, void *v_text_sensor); #endif #ifdef USE_ESP32_CAMERA - void send_camera_state(std::shared_ptr image); - bool send_camera_info(esp32_camera::ESP32Camera *camera); + void set_camera_state(std::shared_ptr image); + void send_camera_info(esp32_camera::ESP32Camera *camera); + static bool try_send_camera_info(APIConnection *api, void *v_camera); void camera_image(const CameraImageRequest &msg) override; #endif #ifdef USE_CLIMATE bool send_climate_state(climate::Climate *climate); - bool send_climate_info(climate::Climate *climate); + void send_climate_info(climate::Climate *climate); + static bool try_send_climate_state(APIConnection *api, void *v_climate); + static bool try_send_climate_info(APIConnection *api, void *v_climate); void climate_command(const ClimateCommandRequest &msg) override; #endif #ifdef USE_NUMBER bool send_number_state(number::Number *number, float state); - bool send_number_info(number::Number *number); + void send_number_info(number::Number *number); + static bool try_send_number_state(APIConnection *api, void *v_number); + static bool try_send_number_state(APIConnection *api, number::Number *number, float state); + static bool try_send_number_info(APIConnection *api, void *v_number); void number_command(const NumberCommandRequest &msg) override; #endif #ifdef USE_DATETIME_DATE bool send_date_state(datetime::DateEntity *date); - bool send_date_info(datetime::DateEntity *date); + void send_date_info(datetime::DateEntity *date); + static bool try_send_date_state(APIConnection *api, void *v_date); + static bool try_send_date_info(APIConnection *api, void *v_date); void date_command(const DateCommandRequest &msg) override; #endif #ifdef USE_DATETIME_TIME bool send_time_state(datetime::TimeEntity *time); - bool send_time_info(datetime::TimeEntity *time); + void send_time_info(datetime::TimeEntity *time); + static bool try_send_time_state(APIConnection *api, void *v_time); + static bool try_send_time_info(APIConnection *api, void *v_time); void time_command(const TimeCommandRequest &msg) override; #endif #ifdef USE_DATETIME_DATETIME bool send_datetime_state(datetime::DateTimeEntity *datetime); - bool send_datetime_info(datetime::DateTimeEntity *datetime); + void send_datetime_info(datetime::DateTimeEntity *datetime); + static bool try_send_datetime_state(APIConnection *api, void *v_datetime); + static bool try_send_datetime_info(APIConnection *api, void *v_datetime); void datetime_command(const DateTimeCommandRequest &msg) override; #endif #ifdef USE_TEXT bool send_text_state(text::Text *text, std::string state); - bool send_text_info(text::Text *text); + void send_text_info(text::Text *text); + static bool try_send_text_state(APIConnection *api, void *v_text); + static bool try_send_text_state(APIConnection *api, text::Text *text, std::string state); + static bool try_send_text_info(APIConnection *api, void *v_text); void text_command(const TextCommandRequest &msg) override; #endif #ifdef USE_SELECT bool send_select_state(select::Select *select, std::string state); - bool send_select_info(select::Select *select); + void send_select_info(select::Select *select); + static bool try_send_select_state(APIConnection *api, void *v_select); + static bool try_send_select_state(APIConnection *api, select::Select *select, std::string state); + static bool try_send_select_info(APIConnection *api, void *v_select); void select_command(const SelectCommandRequest &msg) override; #endif #ifdef USE_BUTTON - bool send_button_info(button::Button *button); + void send_button_info(button::Button *button); + static bool try_send_button_info(APIConnection *api, void *v_button); void button_command(const ButtonCommandRequest &msg) override; #endif #ifdef USE_LOCK bool send_lock_state(lock::Lock *a_lock, lock::LockState state); - bool send_lock_info(lock::Lock *a_lock); + void send_lock_info(lock::Lock *a_lock); + static bool try_send_lock_state(APIConnection *api, void *v_a_lock); + static bool try_send_lock_state(APIConnection *api, lock::Lock *a_lock, lock::LockState state); + static bool try_send_lock_info(APIConnection *api, void *v_a_lock); void lock_command(const LockCommandRequest &msg) override; #endif #ifdef USE_VALVE bool send_valve_state(valve::Valve *valve); - bool send_valve_info(valve::Valve *valve); + void send_valve_info(valve::Valve *valve); + static bool try_send_valve_state(APIConnection *api, void *v_valve); + static bool try_send_valve_info(APIConnection *api, void *v_valve); void valve_command(const ValveCommandRequest &msg) override; #endif #ifdef USE_MEDIA_PLAYER bool send_media_player_state(media_player::MediaPlayer *media_player); - bool send_media_player_info(media_player::MediaPlayer *media_player); + void send_media_player_info(media_player::MediaPlayer *media_player); + static bool try_send_media_player_state(APIConnection *api, void *v_media_player); + static bool try_send_media_player_info(APIConnection *api, void *v_media_player); void media_player_command(const MediaPlayerCommandRequest &msg) override; #endif - bool send_log_message(int level, const char *tag, const char *line); + bool try_send_log_message(int level, const char *tag, const char *line); void send_homeassistant_service_call(const HomeassistantServiceResponse &call) { if (!this->service_call_subscription_) return; @@ -160,18 +244,25 @@ class APIConnection : public APIServerConnection { #ifdef USE_ALARM_CONTROL_PANEL bool send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel); - bool send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel); + void send_alarm_control_panel_info(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel); + static bool try_send_alarm_control_panel_state(APIConnection *api, void *v_a_alarm_control_panel); + static bool try_send_alarm_control_panel_info(APIConnection *api, void *v_a_alarm_control_panel); void alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) override; #endif #ifdef USE_EVENT - bool send_event(event::Event *event, std::string event_type); - bool send_event_info(event::Event *event); + void send_event(event::Event *event, std::string event_type); + void send_event_info(event::Event *event); + static bool try_send_event(APIConnection *api, void *v_event); + static bool try_send_event(APIConnection *api, event::Event *event, std::string event_type); + static bool try_send_event_info(APIConnection *api, void *v_event); #endif #ifdef USE_UPDATE bool send_update_state(update::UpdateEntity *update); - bool send_update_info(update::UpdateEntity *update); + void send_update_info(update::UpdateEntity *update); + static bool try_send_update_state(APIConnection *api, void *v_update); + static bool try_send_update_info(APIConnection *api, void *v_update); void update_command(const UpdateCommandRequest &msg) override; #endif @@ -262,6 +353,7 @@ class APIConnection : public APIServerConnection { bool service_call_subscription_{false}; bool next_close_ = false; APIServer *parent_; + DeferredMessageQueue deferred_message_queue_; InitialStateIterator initial_state_iterator_; ListEntitiesIterator list_entities_iterator_; int state_subs_at_ = -1; diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index 41016e510f..8001a74b6d 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -838,6 +838,10 @@ bool DeviceInfoResponse::decode_length(uint32_t field_id, ProtoLengthDelimited v this->suggested_area = value.as_string(); return true; } + case 18: { + this->bluetooth_mac_address = value.as_string(); + return true; + } default: return false; } @@ -860,6 +864,7 @@ void DeviceInfoResponse::encode(ProtoWriteBuffer buffer) const { buffer.encode_uint32(14, this->legacy_voice_assistant_version); buffer.encode_uint32(17, this->voice_assistant_feature_flags); buffer.encode_string(16, this->suggested_area); + buffer.encode_string(18, this->bluetooth_mac_address); } #ifdef HAS_PROTO_MESSAGE_DUMP void DeviceInfoResponse::dump_to(std::string &out) const { @@ -937,6 +942,10 @@ void DeviceInfoResponse::dump_to(std::string &out) const { out.append(" suggested_area: "); out.append("'").append(this->suggested_area).append("'"); out.append("\n"); + + out.append(" bluetooth_mac_address: "); + out.append("'").append(this->bluetooth_mac_address).append("'"); + out.append("\n"); out.append("}"); } #endif @@ -7085,6 +7094,16 @@ void VoiceAssistantTimerEventResponse::dump_to(std::string &out) const { out.append("}"); } #endif +bool VoiceAssistantAnnounceRequest::decode_varint(uint32_t field_id, ProtoVarInt value) { + switch (field_id) { + case 4: { + this->start_conversation = value.as_bool(); + return true; + } + default: + return false; + } +} bool VoiceAssistantAnnounceRequest::decode_length(uint32_t field_id, ProtoLengthDelimited value) { switch (field_id) { case 1: { @@ -7095,6 +7114,10 @@ bool VoiceAssistantAnnounceRequest::decode_length(uint32_t field_id, ProtoLength this->text = value.as_string(); return true; } + case 3: { + this->preannounce_media_id = value.as_string(); + return true; + } default: return false; } @@ -7102,6 +7125,8 @@ bool VoiceAssistantAnnounceRequest::decode_length(uint32_t field_id, ProtoLength void VoiceAssistantAnnounceRequest::encode(ProtoWriteBuffer buffer) const { buffer.encode_string(1, this->media_id); buffer.encode_string(2, this->text); + buffer.encode_string(3, this->preannounce_media_id); + buffer.encode_bool(4, this->start_conversation); } #ifdef HAS_PROTO_MESSAGE_DUMP void VoiceAssistantAnnounceRequest::dump_to(std::string &out) const { @@ -7114,6 +7139,14 @@ void VoiceAssistantAnnounceRequest::dump_to(std::string &out) const { out.append(" text: "); out.append("'").append(this->text).append("'"); out.append("\n"); + + out.append(" preannounce_media_id: "); + out.append("'").append(this->preannounce_media_id).append("'"); + out.append("\n"); + + out.append(" start_conversation: "); + out.append(YESNO(this->start_conversation)); + out.append("\n"); out.append("}"); } #endif diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index a3fccbc641..455e3ff6cf 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -354,6 +354,7 @@ class DeviceInfoResponse : public ProtoMessage { uint32_t legacy_voice_assistant_version{0}; uint32_t voice_assistant_feature_flags{0}; std::string suggested_area{}; + std::string bluetooth_mac_address{}; void encode(ProtoWriteBuffer buffer) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; @@ -1831,6 +1832,8 @@ class VoiceAssistantAnnounceRequest : public ProtoMessage { public: std::string media_id{}; std::string text{}; + std::string preannounce_media_id{}; + bool start_conversation{false}; void encode(ProtoWriteBuffer buffer) const override; #ifdef HAS_PROTO_MESSAGE_DUMP void dump_to(std::string &out) const override; @@ -1838,6 +1841,7 @@ class VoiceAssistantAnnounceRequest : public ProtoMessage { protected: bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override; + bool decode_varint(uint32_t field_id, ProtoVarInt value) override; }; class VoiceAssistantAnnounceFinished : public ProtoMessage { public: diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index f16b5a13cf..7b21a174a0 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -72,7 +72,7 @@ void APIServer::setup() { logger::global_logger->add_on_log_callback([this](int level, const char *tag, const char *message) { for (auto &c : this->clients_) { if (!c->remove_) - c->send_log_message(level, tag, message); + c->try_send_log_message(level, tag, message); } }); } @@ -86,7 +86,7 @@ void APIServer::setup() { [this](const std::shared_ptr &image) { for (auto &c : this->clients_) { if (!c->remove_) - c->send_camera_state(image); + c->set_camera_state(image); } }); } diff --git a/esphome/components/api/list_entities.cpp b/esphome/components/api/list_entities.cpp index 8540c5cee8..574aa2525b 100644 --- a/esphome/components/api/list_entities.cpp +++ b/esphome/components/api/list_entities.cpp @@ -10,37 +10,63 @@ namespace api { #ifdef USE_BINARY_SENSOR bool ListEntitiesIterator::on_binary_sensor(binary_sensor::BinarySensor *binary_sensor) { - return this->client_->send_binary_sensor_info(binary_sensor); + this->client_->send_binary_sensor_info(binary_sensor); + return true; } #endif #ifdef USE_COVER -bool ListEntitiesIterator::on_cover(cover::Cover *cover) { return this->client_->send_cover_info(cover); } +bool ListEntitiesIterator::on_cover(cover::Cover *cover) { + this->client_->send_cover_info(cover); + return true; +} #endif #ifdef USE_FAN -bool ListEntitiesIterator::on_fan(fan::Fan *fan) { return this->client_->send_fan_info(fan); } +bool ListEntitiesIterator::on_fan(fan::Fan *fan) { + this->client_->send_fan_info(fan); + return true; +} #endif #ifdef USE_LIGHT -bool ListEntitiesIterator::on_light(light::LightState *light) { return this->client_->send_light_info(light); } +bool ListEntitiesIterator::on_light(light::LightState *light) { + this->client_->send_light_info(light); + return true; +} #endif #ifdef USE_SENSOR -bool ListEntitiesIterator::on_sensor(sensor::Sensor *sensor) { return this->client_->send_sensor_info(sensor); } +bool ListEntitiesIterator::on_sensor(sensor::Sensor *sensor) { + this->client_->send_sensor_info(sensor); + return true; +} #endif #ifdef USE_SWITCH -bool ListEntitiesIterator::on_switch(switch_::Switch *a_switch) { return this->client_->send_switch_info(a_switch); } +bool ListEntitiesIterator::on_switch(switch_::Switch *a_switch) { + this->client_->send_switch_info(a_switch); + return true; +} #endif #ifdef USE_BUTTON -bool ListEntitiesIterator::on_button(button::Button *button) { return this->client_->send_button_info(button); } +bool ListEntitiesIterator::on_button(button::Button *button) { + this->client_->send_button_info(button); + return true; +} #endif #ifdef USE_TEXT_SENSOR bool ListEntitiesIterator::on_text_sensor(text_sensor::TextSensor *text_sensor) { - return this->client_->send_text_sensor_info(text_sensor); + this->client_->send_text_sensor_info(text_sensor); + return true; } #endif #ifdef USE_LOCK -bool ListEntitiesIterator::on_lock(lock::Lock *a_lock) { return this->client_->send_lock_info(a_lock); } +bool ListEntitiesIterator::on_lock(lock::Lock *a_lock) { + this->client_->send_lock_info(a_lock); + return true; +} #endif #ifdef USE_VALVE -bool ListEntitiesIterator::on_valve(valve::Valve *valve) { return this->client_->send_valve_info(valve); } +bool ListEntitiesIterator::on_valve(valve::Valve *valve) { + this->client_->send_valve_info(valve); + return true; +} #endif bool ListEntitiesIterator::on_end() { return this->client_->send_list_info_done(); } @@ -52,55 +78,83 @@ bool ListEntitiesIterator::on_service(UserServiceDescriptor *service) { #ifdef USE_ESP32_CAMERA bool ListEntitiesIterator::on_camera(esp32_camera::ESP32Camera *camera) { - return this->client_->send_camera_info(camera); + this->client_->send_camera_info(camera); + return true; } #endif #ifdef USE_CLIMATE -bool ListEntitiesIterator::on_climate(climate::Climate *climate) { return this->client_->send_climate_info(climate); } +bool ListEntitiesIterator::on_climate(climate::Climate *climate) { + this->client_->send_climate_info(climate); + return true; +} #endif #ifdef USE_NUMBER -bool ListEntitiesIterator::on_number(number::Number *number) { return this->client_->send_number_info(number); } +bool ListEntitiesIterator::on_number(number::Number *number) { + this->client_->send_number_info(number); + return true; +} #endif #ifdef USE_DATETIME_DATE -bool ListEntitiesIterator::on_date(datetime::DateEntity *date) { return this->client_->send_date_info(date); } +bool ListEntitiesIterator::on_date(datetime::DateEntity *date) { + this->client_->send_date_info(date); + return true; +} #endif #ifdef USE_DATETIME_TIME -bool ListEntitiesIterator::on_time(datetime::TimeEntity *time) { return this->client_->send_time_info(time); } +bool ListEntitiesIterator::on_time(datetime::TimeEntity *time) { + this->client_->send_time_info(time); + return true; +} #endif #ifdef USE_DATETIME_DATETIME bool ListEntitiesIterator::on_datetime(datetime::DateTimeEntity *datetime) { - return this->client_->send_datetime_info(datetime); + this->client_->send_datetime_info(datetime); + return true; } #endif #ifdef USE_TEXT -bool ListEntitiesIterator::on_text(text::Text *text) { return this->client_->send_text_info(text); } +bool ListEntitiesIterator::on_text(text::Text *text) { + this->client_->send_text_info(text); + return true; +} #endif #ifdef USE_SELECT -bool ListEntitiesIterator::on_select(select::Select *select) { return this->client_->send_select_info(select); } +bool ListEntitiesIterator::on_select(select::Select *select) { + this->client_->send_select_info(select); + return true; +} #endif #ifdef USE_MEDIA_PLAYER bool ListEntitiesIterator::on_media_player(media_player::MediaPlayer *media_player) { - return this->client_->send_media_player_info(media_player); + this->client_->send_media_player_info(media_player); + return true; } #endif #ifdef USE_ALARM_CONTROL_PANEL bool ListEntitiesIterator::on_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) { - return this->client_->send_alarm_control_panel_info(a_alarm_control_panel); + this->client_->send_alarm_control_panel_info(a_alarm_control_panel); + return true; } #endif #ifdef USE_EVENT -bool ListEntitiesIterator::on_event(event::Event *event) { return this->client_->send_event_info(event); } +bool ListEntitiesIterator::on_event(event::Event *event) { + this->client_->send_event_info(event); + return true; +} #endif #ifdef USE_UPDATE -bool ListEntitiesIterator::on_update(update::UpdateEntity *update) { return this->client_->send_update_info(update); } +bool ListEntitiesIterator::on_update(update::UpdateEntity *update) { + this->client_->send_update_info(update); + return true; +} #endif } // namespace api diff --git a/esphome/components/api/list_entities.h b/esphome/components/api/list_entities.h index 1821bbee4c..e77f21c7a1 100644 --- a/esphome/components/api/list_entities.h +++ b/esphome/components/api/list_entities.h @@ -80,6 +80,7 @@ class ListEntitiesIterator : public ComponentIterator { bool on_update(update::UpdateEntity *update) override; #endif bool on_end() override; + bool completed() { return this->state_ == IteratorState::NONE; } protected: APIConnection *client_; diff --git a/esphome/components/api/subscribe_state.h b/esphome/components/api/subscribe_state.h index efda32affb..3966c97af5 100644 --- a/esphome/components/api/subscribe_state.h +++ b/esphome/components/api/subscribe_state.h @@ -76,6 +76,8 @@ class InitialStateIterator : public ComponentIterator { #ifdef USE_UPDATE bool on_update(update::UpdateEntity *update) override; #endif + bool completed() { return this->state_ == IteratorState::NONE; } + protected: APIConnection *client_; }; diff --git a/esphome/components/audio/__init__.py b/esphome/components/audio/__init__.py index 31d3c39ffa..f8ec8cbd85 100644 --- a/esphome/components/audio/__init__.py +++ b/esphome/components/audio/__init__.py @@ -118,4 +118,4 @@ def final_validate_audio_schema( async def to_code(config): - cg.add_library("esphome/esp-audio-libs", "1.1.1") + cg.add_library("esphome/esp-audio-libs", "1.1.3") diff --git a/esphome/components/audio/audio_decoder.cpp b/esphome/components/audio/audio_decoder.cpp index ab358ad805..60489d7d78 100644 --- a/esphome/components/audio/audio_decoder.cpp +++ b/esphome/components/audio/audio_decoder.cpp @@ -66,19 +66,30 @@ esp_err_t AudioDecoder::start(AudioFileType audio_file_type) { case AudioFileType::FLAC: this->flac_decoder_ = make_unique(); this->free_buffer_required_ = - this->output_transfer_buffer_->capacity(); // We'll revise this after reading the header + this->output_transfer_buffer_->capacity(); // Adjusted and reallocated after reading the header break; #endif #ifdef USE_AUDIO_MP3_SUPPORT case AudioFileType::MP3: this->mp3_decoder_ = esp_audio_libs::helix_decoder::MP3InitDecoder(); + + // MP3 always has 1152 samples per chunk this->free_buffer_required_ = 1152 * sizeof(int16_t) * 2; // samples * size per sample * channels + + // Always reallocate the output transfer buffer to the smallest necessary size + this->output_transfer_buffer_->reallocate(this->free_buffer_required_); break; #endif case AudioFileType::WAV: this->wav_decoder_ = make_unique(); this->wav_decoder_->reset(); + + // Processing WAVs doesn't actually require a specific amount of buffer size, as it is already in PCM format. + // Thus, we don't reallocate to a minimum size. this->free_buffer_required_ = 1024; + if (this->output_transfer_buffer_->capacity() < this->free_buffer_required_) { + this->output_transfer_buffer_->reallocate(this->free_buffer_required_); + } break; case AudioFileType::NONE: default: @@ -116,10 +127,18 @@ AudioDecoderState AudioDecoder::decode(bool stop_gracefully) { uint32_t decoding_start = millis(); + bool first_loop_iteration = true; + + size_t bytes_processed = 0; + size_t bytes_available_before_processing = 0; + while (state == FileDecoderState::MORE_TO_PROCESS) { // Transfer decoded out if (!this->pause_output_) { - size_t bytes_written = this->output_transfer_buffer_->transfer_data_to_sink(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS)); + // Never shift the data in the output transfer buffer to avoid unnecessary, slow data moves + size_t bytes_written = + this->output_transfer_buffer_->transfer_data_to_sink(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS), false); + if (this->audio_stream_info_.has_value()) { this->accumulated_frames_written_ += this->audio_stream_info_.value().bytes_to_frames(bytes_written); this->playback_ms_ += @@ -138,12 +157,24 @@ AudioDecoderState AudioDecoder::decode(bool stop_gracefully) { // Decode more audio - size_t bytes_read = this->input_transfer_buffer_->transfer_data_from_source(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS)); + // Only shift data on the first loop iteration to avoid unnecessary, slow moves + size_t bytes_read = this->input_transfer_buffer_->transfer_data_from_source(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS), + first_loop_iteration); - if ((this->potentially_failed_count_ > 0) && (bytes_read == 0)) { + if (!first_loop_iteration && (this->input_transfer_buffer_->available() < bytes_processed)) { + // Less data is available than what was processed in last iteration, so don't attempt to decode. + // This attempts to avoid the decoder from consistently trying to decode an incomplete frame. The transfer buffer + // will shift the remaining data to the start and copy more from the source the next time the decode function is + // called + break; + } + + bytes_available_before_processing = this->input_transfer_buffer_->available(); + + if ((this->potentially_failed_count_ > 10) && (bytes_read == 0)) { // Failed to decode in last attempt and there is no new data - if (this->input_transfer_buffer_->free() == 0) { + if ((this->input_transfer_buffer_->free() == 0) && first_loop_iteration) { // The input buffer is full. Since it previously failed on the exact same data, we can never recover state = FileDecoderState::FAILED; } else { @@ -175,6 +206,9 @@ AudioDecoderState AudioDecoder::decode(bool stop_gracefully) { } } + first_loop_iteration = false; + bytes_processed = bytes_available_before_processing - this->input_transfer_buffer_->available(); + if (state == FileDecoderState::POTENTIALLY_FAILED) { ++this->potentially_failed_count_; } else if (state == FileDecoderState::END_OF_FILE) { @@ -207,13 +241,11 @@ FileDecoderState AudioDecoder::decode_flac_() { size_t bytes_consumed = this->flac_decoder_->get_bytes_index(); this->input_transfer_buffer_->decrease_buffer_length(bytes_consumed); + // Reallocate the output transfer buffer to the smallest necessary size this->free_buffer_required_ = flac_decoder_->get_output_buffer_size_bytes(); - if (this->output_transfer_buffer_->capacity() < this->free_buffer_required_) { - // Output buffer is not big enough - if (!this->output_transfer_buffer_->reallocate(this->free_buffer_required_)) { - // Couldn't reallocate output buffer - return FileDecoderState::FAILED; - } + if (!this->output_transfer_buffer_->reallocate(this->free_buffer_required_)) { + // Couldn't reallocate output buffer + return FileDecoderState::FAILED; } this->audio_stream_info_ = diff --git a/esphome/components/audio/audio_reader.cpp b/esphome/components/audio/audio_reader.cpp index 90b73a1f46..b82c6db9ee 100644 --- a/esphome/components/audio/audio_reader.cpp +++ b/esphome/components/audio/audio_reader.cpp @@ -259,14 +259,14 @@ AudioReaderState AudioReader::file_read_() { } AudioReaderState AudioReader::http_read_() { - this->output_transfer_buffer_->transfer_data_to_sink(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS)); + this->output_transfer_buffer_->transfer_data_to_sink(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS), false); if (esp_http_client_is_complete_data_received(this->client_)) { if (this->output_transfer_buffer_->available() == 0) { this->cleanup_connection_(); return AudioReaderState::FINISHED; } - } else { + } else if (this->output_transfer_buffer_->free() > 0) { size_t bytes_to_read = this->output_transfer_buffer_->free(); int received_len = esp_http_client_read(this->client_, (char *) this->output_transfer_buffer_->get_buffer_end(), bytes_to_read); diff --git a/esphome/components/audio/audio_resampler.cpp b/esphome/components/audio/audio_resampler.cpp index 05e9ff6ca1..a7621225a1 100644 --- a/esphome/components/audio/audio_resampler.cpp +++ b/esphome/components/audio/audio_resampler.cpp @@ -93,8 +93,9 @@ AudioResamplerState AudioResampler::resample(bool stop_gracefully, int32_t *ms_d } if (!this->pause_output_) { - // Move audio data to the sink - this->output_transfer_buffer_->transfer_data_to_sink(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS)); + // Move audio data to the sink without shifting the data in the output transfer buffer to avoid unnecessary, slow + // data moves + this->output_transfer_buffer_->transfer_data_to_sink(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS), false); } else { // If paused, block to avoid wasting CPU resources delay(READ_WRITE_TIMEOUT_MS); @@ -115,6 +116,7 @@ AudioResamplerState AudioResampler::resample(bool stop_gracefully, int32_t *ms_d if ((this->input_stream_info_.get_sample_rate() != this->output_stream_info_.get_sample_rate()) || (this->input_stream_info_.get_bits_per_sample() != this->output_stream_info_.get_bits_per_sample())) { + // Adjust gain by -3 dB to avoid clipping due to the resampling process esp_audio_libs::resampler::ResamplerResults results = this->resampler_->resample(this->input_transfer_buffer_->get_buffer_start(), this->output_transfer_buffer_->get_buffer_end(), frames_available, frames_free, -3); diff --git a/esphome/components/audio/audio_transfer_buffer.cpp b/esphome/components/audio/audio_transfer_buffer.cpp index 9b6067aac4..1566884c3d 100644 --- a/esphome/components/audio/audio_transfer_buffer.cpp +++ b/esphome/components/audio/audio_transfer_buffer.cpp @@ -33,12 +33,17 @@ size_t AudioTransferBuffer::free() const { if (this->buffer_size_ == 0) { return 0; } - return this->buffer_size_ - (this->buffer_length_ - (this->data_start_ - this->buffer_)); + return this->buffer_size_ - (this->buffer_length_ + (this->data_start_ - this->buffer_)); } void AudioTransferBuffer::decrease_buffer_length(size_t bytes) { this->buffer_length_ -= bytes; - this->data_start_ += bytes; + if (this->buffer_length_ > 0) { + this->data_start_ += bytes; + } else { + // All the data in the buffer has been consumed, reset the start pointer + this->data_start_ = this->buffer_; + } } void AudioTransferBuffer::increase_buffer_length(size_t bytes) { this->buffer_length_ += bytes; } @@ -71,7 +76,7 @@ bool AudioTransferBuffer::has_buffered_data() const { bool AudioTransferBuffer::reallocate(size_t new_buffer_size) { if (this->buffer_length_ > 0) { - // Already has data in the buffer, fail + // Buffer currently has data, so reallocation is impossible return false; } this->deallocate_buffer_(); @@ -106,12 +111,14 @@ void AudioTransferBuffer::deallocate_buffer_() { this->buffer_length_ = 0; } -size_t AudioSourceTransferBuffer::transfer_data_from_source(TickType_t ticks_to_wait) { - // Shift data in buffer to start - if (this->buffer_length_ > 0) { - memmove(this->buffer_, this->data_start_, this->buffer_length_); +size_t AudioSourceTransferBuffer::transfer_data_from_source(TickType_t ticks_to_wait, bool pre_shift) { + if (pre_shift) { + // Shift data in buffer to start + if (this->buffer_length_ > 0) { + memmove(this->buffer_, this->data_start_, this->buffer_length_); + } + this->data_start_ = this->buffer_; } - this->data_start_ = this->buffer_; size_t bytes_to_read = this->free(); size_t bytes_read = 0; @@ -125,7 +132,7 @@ size_t AudioSourceTransferBuffer::transfer_data_from_source(TickType_t ticks_to_ return bytes_read; } -size_t AudioSinkTransferBuffer::transfer_data_to_sink(TickType_t ticks_to_wait) { +size_t AudioSinkTransferBuffer::transfer_data_to_sink(TickType_t ticks_to_wait, bool post_shift) { size_t bytes_written = 0; if (this->available()) { #ifdef USE_SPEAKER @@ -139,11 +146,14 @@ size_t AudioSinkTransferBuffer::transfer_data_to_sink(TickType_t ticks_to_wait) } this->decrease_buffer_length(bytes_written); + } + if (post_shift) { // Shift unwritten data to the start of the buffer memmove(this->buffer_, this->data_start_, this->buffer_length_); this->data_start_ = this->buffer_; } + return bytes_written; } diff --git a/esphome/components/audio/audio_transfer_buffer.h b/esphome/components/audio/audio_transfer_buffer.h index 4e461db56d..edb484e7d2 100644 --- a/esphome/components/audio/audio_transfer_buffer.h +++ b/esphome/components/audio/audio_transfer_buffer.h @@ -60,6 +60,7 @@ class AudioTransferBuffer { protected: /// @brief Allocates the transfer buffer in external memory, if available. + /// @param buffer_size The number of bytes to allocate /// @return True is successful, false otherwise. bool allocate_buffer_(size_t buffer_size); @@ -89,8 +90,10 @@ class AudioSinkTransferBuffer : public AudioTransferBuffer { /// @brief Writes any available data in the transfer buffer to the sink. /// @param ticks_to_wait FreeRTOS ticks to block while waiting for the sink to have enough space + /// @param post_shift If true, all remaining data is moved to the start of the buffer after transferring to the sink. + /// Defaults to true. /// @return Number of bytes written - size_t transfer_data_to_sink(TickType_t ticks_to_wait); + size_t transfer_data_to_sink(TickType_t ticks_to_wait, bool post_shift = true); /// @brief Adds a ring buffer as the transfer buffer's sink. /// @param ring_buffer weak_ptr to the allocated ring buffer @@ -125,8 +128,10 @@ class AudioSourceTransferBuffer : public AudioTransferBuffer { /// @brief Reads any available data from the sink into the transfer buffer. /// @param ticks_to_wait FreeRTOS ticks to block while waiting for the source to have enough data + /// @param pre_shift If true, any unwritten data is moved to the start of the buffer before transferring from the + /// source. Defaults to true. /// @return Number of bytes read - size_t transfer_data_from_source(TickType_t ticks_to_wait); + size_t transfer_data_from_source(TickType_t ticks_to_wait, bool pre_shift = true); /// @brief Adds a ring buffer as the transfer buffer's source. /// @param ring_buffer weak_ptr to the allocated ring buffer diff --git a/esphome/components/bluetooth_proxy/bluetooth_proxy.h b/esphome/components/bluetooth_proxy/bluetooth_proxy.h index 35a37f934a..e0345ff248 100644 --- a/esphome/components/bluetooth_proxy/bluetooth_proxy.h +++ b/esphome/components/bluetooth_proxy/bluetooth_proxy.h @@ -15,6 +15,9 @@ #include "bluetooth_connection.h" +#include +#include + namespace esphome { namespace bluetooth_proxy { @@ -114,6 +117,11 @@ class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Com return flags; } + std::string get_bluetooth_mac_address_pretty() { + const uint8_t *mac = esp_bt_dev_get_address(); + return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + } + protected: void send_api_packet_(const esp32_ble_tracker::ESPBTDevice &device); diff --git a/esphome/components/bmp085/bmp085.cpp b/esphome/components/bmp085/bmp085.cpp index 44e686fe1a..caf2264390 100644 --- a/esphome/components/bmp085/bmp085.cpp +++ b/esphome/components/bmp085/bmp085.cpp @@ -95,7 +95,7 @@ void BMP085Component::read_pressure_() { return; } - uint32_t value = (uint32_t(buffer[0]) << 16) | (uint32_t(buffer[1]) << 8) | uint32_t(buffer[0]); + uint32_t value = (uint32_t(buffer[0]) << 16) | (uint32_t(buffer[1]) << 8) | uint32_t(buffer[2]); if ((value >> 5) == 0) { ESP_LOGW(TAG, "Invalid pressure!"); this->status_set_warning(); diff --git a/esphome/components/chsc6x/__init__.py b/esphome/components/chsc6x/__init__.py new file mode 100644 index 0000000000..80f3a0b33e --- /dev/null +++ b/esphome/components/chsc6x/__init__.py @@ -0,0 +1,2 @@ +CODEOWNERS = ["@kkosik20"] +DEPENDENCIES = ["i2c"] diff --git a/esphome/components/chsc6x/chsc6x_touchscreen.cpp b/esphome/components/chsc6x/chsc6x_touchscreen.cpp new file mode 100644 index 0000000000..5ad4329718 --- /dev/null +++ b/esphome/components/chsc6x/chsc6x_touchscreen.cpp @@ -0,0 +1,47 @@ +#include "chsc6x_touchscreen.h" + +namespace esphome { +namespace chsc6x { + +void CHSC6XTouchscreen::setup() { + ESP_LOGCONFIG(TAG, "Setting up CHSC6X Touchscreen..."); + if (this->interrupt_pin_ != nullptr) { + this->interrupt_pin_->setup(); + this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE); + } + if (this->x_raw_max_ == this->x_raw_min_) { + this->x_raw_max_ = this->display_->get_native_width(); + } + if (this->y_raw_max_ == this->y_raw_min_) { + this->y_raw_max_ = this->display_->get_native_height(); + } + + ESP_LOGCONFIG(TAG, "CHSC6X Touchscreen setup complete"); +} + +void CHSC6XTouchscreen::update_touches() { + uint8_t data[CHSC6X_REG_STATUS_LEN]; + if (!this->read_bytes(CHSC6X_REG_STATUS, data, sizeof(data))) { + return; + } + + uint8_t num_of_touches = data[CHSC6X_REG_STATUS_TOUCH]; + + if (num_of_touches == 1) { + uint16_t x = data[CHSC6X_REG_STATUS_X_COR]; + uint16_t y = data[CHSC6X_REG_STATUS_Y_COR]; + this->add_raw_touch_position_(0, x, y); + } +} + +void CHSC6XTouchscreen::dump_config() { + ESP_LOGCONFIG(TAG, "CHSC6X Touchscreen:"); + LOG_I2C_DEVICE(this); + LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_); + ESP_LOGCONFIG(TAG, " Touch timeout: %d", this->touch_timeout_); + ESP_LOGCONFIG(TAG, " x_raw_max_: %d", this->x_raw_max_); + ESP_LOGCONFIG(TAG, " y_raw_max_: %d", this->y_raw_max_); +} + +} // namespace chsc6x +} // namespace esphome diff --git a/esphome/components/chsc6x/chsc6x_touchscreen.h b/esphome/components/chsc6x/chsc6x_touchscreen.h new file mode 100644 index 0000000000..25b79ad34a --- /dev/null +++ b/esphome/components/chsc6x/chsc6x_touchscreen.h @@ -0,0 +1,34 @@ +#pragma once + +#include "esphome/components/i2c/i2c.h" +#include "esphome/components/touchscreen/touchscreen.h" +#include "esphome/core/component.h" +#include "esphome/core/hal.h" +#include "esphome/core/log.h" + +namespace esphome { +namespace chsc6x { + +static const char *const TAG = "chsc6x.touchscreen"; + +static const uint8_t CHSC6X_REG_STATUS = 0x00; +static const uint8_t CHSC6X_REG_STATUS_TOUCH = 0x00; +static const uint8_t CHSC6X_REG_STATUS_X_COR = 0x02; +static const uint8_t CHSC6X_REG_STATUS_Y_COR = 0x04; +static const uint8_t CHSC6X_REG_STATUS_LEN = 0x05; +static const uint8_t CHSC6X_CHIP_ID = 0x2e; + +class CHSC6XTouchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice { + public: + void setup() override; + void update_touches() override; + void dump_config() override; + + void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; } + + protected: + InternalGPIOPin *interrupt_pin_{}; +}; + +} // namespace chsc6x +} // namespace esphome diff --git a/esphome/components/chsc6x/touchscreen.py b/esphome/components/chsc6x/touchscreen.py new file mode 100644 index 0000000000..759e38609e --- /dev/null +++ b/esphome/components/chsc6x/touchscreen.py @@ -0,0 +1,33 @@ +from esphome import pins +import esphome.codegen as cg +from esphome.components import i2c, touchscreen +import esphome.config_validation as cv +from esphome.const import CONF_ID, CONF_INTERRUPT_PIN + +chsc6x_ns = cg.esphome_ns.namespace("chsc6x") + +CHSC6XTouchscreen = chsc6x_ns.class_( + "CHSC6XTouchscreen", + touchscreen.Touchscreen, + i2c.I2CDevice, +) + +CONFIG_SCHEMA = ( + touchscreen.touchscreen_schema("100ms") + .extend( + { + cv.GenerateID(): cv.declare_id(CHSC6XTouchscreen), + cv.Optional(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema, + } + ) + .extend(i2c.i2c_device_schema(0x2E)) +) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await touchscreen.register_touchscreen(var, config) + await i2c.register_i2c_device(var, config) + + if interrupt_pin := config.get(CONF_INTERRUPT_PIN): + cg.add(var.set_interrupt_pin(await cg.gpio_pin_expression(interrupt_pin))) diff --git a/esphome/components/climate/__init__.py b/esphome/components/climate/__init__.py index aa705e7332..445507c620 100644 --- a/esphome/components/climate/__init__.py +++ b/esphome/components/climate/__init__.py @@ -128,7 +128,6 @@ VISUAL_TEMPERATURE_STEP_SCHEMA = cv.Schema( def visual_temperature_step(value): - # Allow defining target/current temperature steps separately if isinstance(value, dict): return VISUAL_TEMPERATURE_STEP_SCHEMA(value) diff --git a/esphome/components/cst816/binary_sensor/__init__.py b/esphome/components/cst816/binary_sensor/__init__.py index b3fd5bb852..7907cc93ef 100644 --- a/esphome/components/cst816/binary_sensor/__init__.py +++ b/esphome/components/cst816/binary_sensor/__init__.py @@ -1,28 +1,5 @@ -import esphome.codegen as cg import esphome.config_validation as cv -from esphome.components import binary_sensor -from .. import cst816_ns -from ..touchscreen import CST816Touchscreen, CST816ButtonListener - -CONF_CST816_ID = "cst816_id" - -CST816Button = cst816_ns.class_( - "CST816Button", - binary_sensor.BinarySensor, - cg.Component, - CST816ButtonListener, - cg.Parented.template(CST816Touchscreen), +CONFIG_SCHEMA = cv.invalid( + "The CST816 binary sensor has been removed. Instead use the touchscreen binary sensor with the 'use_raw' flag set." ) - -CONFIG_SCHEMA = binary_sensor.binary_sensor_schema(CST816Button).extend( - { - cv.GenerateID(CONF_CST816_ID): cv.use_id(CST816Touchscreen), - } -) - - -async def to_code(config): - var = await binary_sensor.new_binary_sensor(config) - await cg.register_component(var, config) - await cg.register_parented(var, config[CONF_CST816_ID]) diff --git a/esphome/components/cst816/binary_sensor/cst816_button.h b/esphome/components/cst816/binary_sensor/cst816_button.h deleted file mode 100644 index 4ae856d506..0000000000 --- a/esphome/components/cst816/binary_sensor/cst816_button.h +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "esphome/components/binary_sensor/binary_sensor.h" -#include "esphome/components/cst816/touchscreen/cst816_touchscreen.h" -#include "esphome/core/component.h" -#include "esphome/core/helpers.h" - -namespace esphome { -namespace cst816 { - -class CST816Button : public binary_sensor::BinarySensor, - public Component, - public CST816ButtonListener, - public Parented { - public: - void setup() override { - this->parent_->register_button_listener(this); - this->publish_initial_state(false); - } - - void dump_config() override { LOG_BINARY_SENSOR("", "CST816 Button", this); } - - void update_button(bool state) override { this->publish_state(state); } -}; - -} // namespace cst816 -} // namespace esphome diff --git a/esphome/components/cst816/touchscreen/cst816_touchscreen.cpp b/esphome/components/cst816/touchscreen/cst816_touchscreen.cpp index 7dcb130e20..607f209c4a 100644 --- a/esphome/components/cst816/touchscreen/cst816_touchscreen.cpp +++ b/esphome/components/cst816/touchscreen/cst816_touchscreen.cpp @@ -37,14 +37,6 @@ void CST816Touchscreen::continue_setup_() { ESP_LOGCONFIG(TAG, "CST816 Touchscreen setup complete"); } -void CST816Touchscreen::update_button_state_(bool state) { - if (this->button_touched_ == state) - return; - this->button_touched_ = state; - for (auto *listener : this->button_listeners_) - listener->update_button(state); -} - void CST816Touchscreen::setup() { ESP_LOGCONFIG(TAG, "Setting up CST816 Touchscreen..."); if (this->reset_pin_ != nullptr) { @@ -68,18 +60,13 @@ void CST816Touchscreen::update_touches() { } uint8_t num_of_touches = data[REG_TOUCH_NUM] & 3; if (num_of_touches == 0) { - this->update_button_state_(false); return; } uint16_t x = encode_uint16(data[REG_XPOS_HIGH] & 0xF, data[REG_XPOS_LOW]); uint16_t y = encode_uint16(data[REG_YPOS_HIGH] & 0xF, data[REG_YPOS_LOW]); ESP_LOGV(TAG, "Read touch %d/%d", x, y); - if (x >= this->x_raw_max_) { - this->update_button_state_(true); - } else { - this->add_raw_touch_position_(0, x, y); - } + this->add_raw_touch_position_(0, x, y); } void CST816Touchscreen::dump_config() { @@ -87,6 +74,8 @@ void CST816Touchscreen::dump_config() { LOG_I2C_DEVICE(this); LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_); LOG_PIN(" Reset Pin: ", this->reset_pin_); + ESP_LOGCONFIG(TAG, " X Raw Min: %d, X Raw Max: %d", this->x_raw_min_, this->x_raw_max_); + ESP_LOGCONFIG(TAG, " Y Raw Min: %d, Y Raw Max: %d", this->y_raw_min_, this->y_raw_max_); const char *name; switch (this->chip_id_) { case CST820_CHIP_ID: diff --git a/esphome/components/cst816/touchscreen/cst816_touchscreen.h b/esphome/components/cst816/touchscreen/cst816_touchscreen.h index dc00e675ba..99ea085e37 100644 --- a/esphome/components/cst816/touchscreen/cst816_touchscreen.h +++ b/esphome/components/cst816/touchscreen/cst816_touchscreen.h @@ -40,7 +40,6 @@ class CST816Touchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice public: void setup() override; void update_touches() override; - void register_button_listener(CST816ButtonListener *listener) { this->button_listeners_.push_back(listener); } void dump_config() override; void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; } @@ -49,14 +48,11 @@ class CST816Touchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice protected: void continue_setup_(); - void update_button_state_(bool state); InternalGPIOPin *interrupt_pin_{}; GPIOPin *reset_pin_{}; uint8_t chip_id_{}; bool skip_probe_{}; // if set, do not expect to be able to probe the controller on the i2c bus. - std::vector button_listeners_; - bool button_touched_{}; }; } // namespace cst816 diff --git a/esphome/components/esp32_ble_beacon/__init__.py b/esphome/components/esp32_ble_beacon/__init__.py index f97f289a0a..6e0d103aa0 100644 --- a/esphome/components/esp32_ble_beacon/__init__.py +++ b/esphome/components/esp32_ble_beacon/__init__.py @@ -66,7 +66,9 @@ FINAL_VALIDATE_SCHEMA = esp32_ble.validate_variant async def to_code(config): uuid = config[CONF_UUID].hex - uuid_arr = [cg.RawExpression(f"0x{uuid[i:i + 2]}") for i in range(0, len(uuid), 2)] + uuid_arr = [ + cg.RawExpression(f"0x{uuid[i : i + 2]}") for i in range(0, len(uuid), 2) + ] var = cg.new_Pvariable(config[CONF_ID], uuid_arr) parent = await cg.get_variable(config[esp32_ble.CONF_BLE_ID]) diff --git a/esphome/components/esp8266/gpio.py b/esphome/components/esp8266/gpio.py index 53016d2130..050efaacae 100644 --- a/esphome/components/esp8266/gpio.py +++ b/esphome/components/esp8266/gpio.py @@ -112,8 +112,7 @@ def validate_supports(value): ) if is_pullup and num == 16: raise cv.Invalid( - "GPIO Pin 16 does not support pullup pin mode. " - "Please choose another pin.", + "GPIO Pin 16 does not support pullup pin mode. Please choose another pin.", [CONF_MODE, CONF_PULLUP], ) if is_pulldown and num != 16: diff --git a/esphome/components/font/__init__.py b/esphome/components/font/__init__.py index 4f569379be..084574d09f 100644 --- a/esphome/components/font/__init__.py +++ b/esphome/components/font/__init__.py @@ -1,3 +1,4 @@ +from collections.abc import MutableMapping import functools import hashlib import logging @@ -6,10 +7,10 @@ from pathlib import Path import re import esphome_glyphsets as glyphsets -import freetype +from freetype import Face, ft_pixel_mode_grays, ft_pixel_mode_mono import requests -from esphome import core, external_files +from esphome import external_files import esphome.codegen as cg import esphome.config_validation as cv from esphome.const import ( @@ -26,7 +27,7 @@ from esphome.const import ( CONF_WEIGHT, ) from esphome.core import CORE, HexInt -from esphome.helpers import copy_file_if_changed, cpp_string_escape +from esphome.helpers import cpp_string_escape _LOGGER = logging.getLogger(__name__) @@ -49,13 +50,42 @@ CONF_IGNORE_MISSING_GLYPHS = "ignore_missing_glyphs" # Cache loaded freetype fonts -class FontCache(dict): - def __missing__(self, key): - try: - res = self[key] = freetype.Face(key) - return res - except freetype.FT_Exception as e: - raise cv.Invalid(f"Could not load Font file {key}: {e}") from e +class FontCache(MutableMapping): + @staticmethod + def get_name(value): + if CONF_FAMILY in value: + return ( + f"{value[CONF_FAMILY]}:{int(value[CONF_ITALIC])}:{value[CONF_WEIGHT]}" + ) + if CONF_URL in value: + return value[CONF_URL] + return value[CONF_PATH] + + @staticmethod + def _keytransform(value): + if CONF_FAMILY in value: + return f"gfont:{value[CONF_FAMILY]}:{int(value[CONF_ITALIC])}:{value[CONF_WEIGHT]}" + if CONF_URL in value: + return f"url:{value[CONF_URL]}" + return f"file:{value[CONF_PATH]}" + + def __init__(self): + self.store = {} + + def __delitem__(self, key): + del self.store[self._keytransform(key)] + + def __iter__(self): + return iter(self.store) + + def __len__(self): + return len(self.store) + + def __getitem__(self, item): + return self.store[self._keytransform(item)] + + def __setitem__(self, key, value): + self.store[self._keytransform(key)] = Face(str(value)) FONT_CACHE = FontCache() @@ -109,14 +139,21 @@ def check_missing_glyphs(file, codepoints, warning: bool = False): ) if count > 10: missing_str += f"\n and {count - 10} more." - message = f"Font {Path(file).name} is missing {count} glyph{'s' if count != 1 else ''}:\n {missing_str}" + message = f"Font {FontCache.get_name(file)} is missing {count} glyph{'s' if count != 1 else ''}:\n {missing_str}" if warning: _LOGGER.warning(message) else: raise cv.Invalid(message) -def validate_glyphs(config): +def pt_to_px(pt): + """ + Convert a point size to pixels, rounding up to the nearest pixel + """ + return (pt + 63) // 64 + + +def validate_font_config(config): """ Check for duplicate codepoints, then check that all requested codepoints actually have glyphs defined in the appropriate font file. @@ -142,43 +179,51 @@ def validate_glyphs(config): ) # Make setpoints and glyphspoints disjoint setpoints.difference_update(glyphspoints) - if fileconf[CONF_TYPE] == TYPE_LOCAL_BITMAP: - # Pillow only allows 256 glyphs per bitmap font. Not sure if that is a Pillow limitation - # or a file format limitation - if any(x >= 256 for x in setpoints.copy().union(glyphspoints)): - raise cv.Invalid("Codepoints in bitmap fonts must be in the range 0-255") - else: - # for TT fonts, check that glyphs are actually present - # Check extras against their own font, exclude from parent font codepoints - for extra in config[CONF_EXTRAS]: - points = {ord(x) for x in flatten(extra[CONF_GLYPHS])} - glyphspoints.difference_update(points) - setpoints.difference_update(points) - check_missing_glyphs(extra[CONF_FILE][CONF_PATH], points) + # check that glyphs are actually present + # Check extras against their own font, exclude from parent font codepoints + for extra in config[CONF_EXTRAS]: + points = {ord(x) for x in flatten(extra[CONF_GLYPHS])} + glyphspoints.difference_update(points) + setpoints.difference_update(points) + check_missing_glyphs(extra[CONF_FILE], points) - # A named glyph that can't be provided is an error - check_missing_glyphs(fileconf[CONF_PATH], glyphspoints) - # A missing glyph from a set is a warning. - if not config[CONF_IGNORE_MISSING_GLYPHS]: - check_missing_glyphs(fileconf[CONF_PATH], setpoints, warning=True) + # A named glyph that can't be provided is an error + + check_missing_glyphs(fileconf, glyphspoints) + # A missing glyph from a set is a warning. + if not config[CONF_IGNORE_MISSING_GLYPHS]: + check_missing_glyphs(fileconf, setpoints, warning=True) # Populate the default after the above checks so that use of the default doesn't trigger errors + font = FONT_CACHE[fileconf] if not config[CONF_GLYPHS] and not config[CONF_GLYPHSETS]: - if fileconf[CONF_TYPE] == TYPE_LOCAL_BITMAP: - config[CONF_GLYPHS] = [DEFAULT_GLYPHS] - else: - # set a default glyphset, intersected with what the font actually offers - font = FONT_CACHE[fileconf[CONF_PATH]] - config[CONF_GLYPHS] = [ - chr(x) - for x in glyphsets.unicodes_per_glyphset(DEFAULT_GLYPHSET) - if font.get_char_index(x) != 0 - ] + # set a default glyphset, intersected with what the font actually offers + config[CONF_GLYPHS] = [ + chr(x) + for x in glyphsets.unicodes_per_glyphset(DEFAULT_GLYPHSET) + if font.get_char_index(x) != 0 + ] + + if font.has_fixed_sizes: + sizes = [pt_to_px(x.size) for x in font.available_sizes] + if not sizes: + raise cv.Invalid( + f"Font {FontCache.get_name(fileconf)} has no available sizes" + ) + if CONF_SIZE not in config: + config[CONF_SIZE] = sizes[0] + elif config[CONF_SIZE] not in sizes: + sizes = ", ".join(str(x) for x in sizes) + raise cv.Invalid( + f"Font {FontCache.get_name(fileconf)} only has size{'s' if len(sizes) != 1 else ''} {sizes} available" + ) + elif CONF_SIZE not in config: + config[CONF_SIZE] = 20 return config -FONT_EXTENSIONS = (".ttf", ".woff", ".otf") +FONT_EXTENSIONS = (".ttf", ".woff", ".otf", "bdf", ".pcf") def validate_truetype_file(value): @@ -187,24 +232,30 @@ def validate_truetype_file(value): f"Please unzip the font archive '{value}' first and then use the .ttf files inside." ) if not any(map(value.lower().endswith, FONT_EXTENSIONS)): - raise cv.Invalid(f"Only {FONT_EXTENSIONS} files are supported.") + raise cv.Invalid(f"Only {', '.join(FONT_EXTENSIONS)} files are supported.") return CORE.relative_config_path(cv.file_(value)) +def add_local_file(value): + if value in FONT_CACHE: + return value + path = value[CONF_PATH] + if not os.path.isfile(path): + raise cv.Invalid(f"File '{path}' not found.") + FONT_CACHE[value] = path + return value + + TYPE_LOCAL = "local" -TYPE_LOCAL_BITMAP = "local_bitmap" TYPE_GFONTS = "gfonts" TYPE_WEB = "web" -LOCAL_SCHEMA = cv.Schema( - { - cv.Required(CONF_PATH): validate_truetype_file, - } -) - -LOCAL_BITMAP_SCHEMA = cv.Schema( - { - cv.Required(CONF_PATH): cv.file_, - } +LOCAL_SCHEMA = cv.All( + cv.Schema( + { + cv.Required(CONF_PATH): validate_truetype_file, + } + ), + add_local_file, ) FULLPATH_SCHEMA = cv.maybe_simple_value( @@ -235,56 +286,59 @@ def _compute_local_font_path(value: dict) -> Path: h.update(url.encode()) key = h.hexdigest()[:8] base_dir = external_files.compute_local_file_dir(DOMAIN) - _LOGGER.debug("_compute_local_font_path: base_dir=%s", base_dir / key) + _LOGGER.debug("_compute_local_font_path: %s", base_dir / key) return base_dir / key -def get_font_path(value, font_type) -> Path: - if font_type == TYPE_GFONTS: - name = f"{value[CONF_FAMILY]}@{value[CONF_WEIGHT]}@{value[CONF_ITALIC]}@v1" - return external_files.compute_local_file_dir(DOMAIN) / f"{name}.ttf" - if font_type == TYPE_WEB: - return _compute_local_font_path(value) / "font.ttf" - assert False - - def download_gfont(value): + if value in FONT_CACHE: + return value name = ( f"{value[CONF_FAMILY]}:ital,wght@{int(value[CONF_ITALIC])},{value[CONF_WEIGHT]}" ) url = f"https://fonts.googleapis.com/css2?family={name}" - path = get_font_path(value, TYPE_GFONTS) - _LOGGER.debug("download_gfont: path=%s", path) + path = ( + external_files.compute_local_file_dir(DOMAIN) + / f"{value[CONF_FAMILY]}@{value[CONF_WEIGHT]}@{value[CONF_ITALIC]}@v1.ttf" + ) + if not external_files.is_file_recent(str(path), value[CONF_REFRESH]): + _LOGGER.debug("download_gfont: path=%s", path) + try: + req = requests.get(url, timeout=external_files.NETWORK_TIMEOUT) + req.raise_for_status() + except requests.exceptions.RequestException as e: + raise cv.Invalid( + f"Could not download font at {url}, please check the fonts exists " + f"at google fonts ({e})" + ) + match = re.search(r"src:\s+url\((.+)\)\s+format\('truetype'\);", req.text) + if match is None: + raise cv.Invalid( + f"Could not extract ttf file from gfonts response for {name}, " + f"please report this." + ) - try: - req = requests.get(url, timeout=external_files.NETWORK_TIMEOUT) - req.raise_for_status() - except requests.exceptions.RequestException as e: - raise cv.Invalid( - f"Could not download font at {url}, please check the fonts exists " - f"at google fonts ({e})" - ) - match = re.search(r"src:\s+url\((.+)\)\s+format\('truetype'\);", req.text) - if match is None: - raise cv.Invalid( - f"Could not extract ttf file from gfonts response for {name}, " - f"please report this." - ) + ttf_url = match.group(1) + _LOGGER.debug("download_gfont: ttf_url=%s", ttf_url) - ttf_url = match.group(1) - _LOGGER.debug("download_gfont: ttf_url=%s", ttf_url) - - external_files.download_content(ttf_url, path) - return FULLPATH_SCHEMA(path) + external_files.download_content(ttf_url, path) + # In case the remote file is not modified, the download_content function will return the existing file, + # so update the modification time to now. + path.touch() + FONT_CACHE[value] = path + return value def download_web_font(value): + if value in FONT_CACHE: + return value url = value[CONF_URL] - path = get_font_path(value, TYPE_WEB) + path = _compute_local_font_path(value) / "font.ttf" external_files.download_content(url, path) _LOGGER.debug("download_web_font: path=%s", path) - return FULLPATH_SCHEMA(path) + FONT_CACHE[value] = path + return value EXTERNAL_FONT_SCHEMA = cv.Schema( @@ -340,15 +394,6 @@ def validate_file_shorthand(value): } ) - if value.endswith(".pcf") or value.endswith(".bdf"): - value = convert_bitmap_to_pillow_font( - CORE.relative_config_path(cv.file_(value)) - ) - return { - CONF_TYPE: TYPE_LOCAL_BITMAP, - CONF_PATH: value, - } - return font_file_schema( { CONF_TYPE: TYPE_LOCAL, @@ -361,7 +406,6 @@ TYPED_FILE_SCHEMA = cv.typed_schema( { TYPE_LOCAL: LOCAL_SCHEMA, TYPE_GFONTS: GFONTS_SCHEMA, - TYPE_LOCAL_BITMAP: LOCAL_BITMAP_SCHEMA, TYPE_WEB: WEB_FONT_SCHEMA, } ) @@ -391,7 +435,7 @@ FONT_SCHEMA = cv.Schema( cv.one_of(*glyphsets.defined_glyphsets()) ), cv.Optional(CONF_IGNORE_MISSING_GLYPHS, default=False): cv.boolean, - cv.Optional(CONF_SIZE, default=20): cv.int_range(min=1), + cv.Optional(CONF_SIZE): cv.int_range(min=1), cv.Optional(CONF_BPP, default=1): cv.one_of(1, 2, 4, 8), cv.Optional(CONF_EXTRAS, default=[]): cv.ensure_list( cv.Schema( @@ -406,114 +450,19 @@ FONT_SCHEMA = cv.Schema( }, ) -CONFIG_SCHEMA = cv.All(FONT_SCHEMA, validate_glyphs) - - -# PIL doesn't provide a consistent interface for both TrueType and bitmap -# fonts. So, we use our own wrappers to give us the consistency that we need. - - -class TrueTypeFontWrapper: - def __init__(self, font): - self.font = font - - def getoffset(self, glyph): - _, (offset_x, offset_y) = self.font.font.getsize(glyph) - return offset_x, offset_y - - def getmask(self, glyph, **kwargs): - return self.font.getmask(str(glyph), **kwargs) - - def getmetrics(self, glyphs): - return self.font.getmetrics() - - -class BitmapFontWrapper: - def __init__(self, font): - self.font = font - self.max_height = 0 - - def getoffset(self, glyph): - return 0, 0 - - def getmask(self, glyph, **kwargs): - return self.font.getmask(str(glyph), **kwargs) - - def getmetrics(self, glyphs): - max_height = 0 - for glyph in glyphs: - mask = self.getmask(glyph, mode="1") - _, height = mask.size - max_height = max(max_height, height) - return max_height, 0 +CONFIG_SCHEMA = cv.All(FONT_SCHEMA, validate_font_config) class EFont: - def __init__(self, file, size, codepoints): + def __init__(self, file, codepoints): self.codepoints = codepoints - path = file[CONF_PATH] - self.name = Path(path).name - ftype = file[CONF_TYPE] - if ftype == TYPE_LOCAL_BITMAP: - self.font = load_bitmap_font(path) - else: - self.font = load_ttf_font(path, size) - self.ascent, self.descent = self.font.getmetrics(codepoints) - - -def convert_bitmap_to_pillow_font(filepath): - from PIL import BdfFontFile, PcfFontFile - - local_bitmap_font_file = external_files.compute_local_file_dir( - DOMAIN, - ) / os.path.basename(filepath) - - copy_file_if_changed(filepath, local_bitmap_font_file) - - local_pil_font_file = local_bitmap_font_file.with_suffix(".pil") - with open(local_bitmap_font_file, "rb") as fp: - try: - try: - p = PcfFontFile.PcfFontFile(fp) - except SyntaxError: - fp.seek(0) - p = BdfFontFile.BdfFontFile(fp) - - # Convert to pillow-formatted fonts, which have a .pil and .pbm extension. - p.save(local_pil_font_file) - except (SyntaxError, OSError) as err: - raise core.EsphomeError( - f"Failed to parse as bitmap font: '{filepath}': {err}" - ) - - return str(local_pil_font_file) - - -def load_bitmap_font(filepath): - from PIL import ImageFont - - try: - font = ImageFont.load(str(filepath)) - except Exception as e: - raise core.EsphomeError(f"Failed to load bitmap font file: {filepath}: {e}") - - return BitmapFontWrapper(font) - - -def load_ttf_font(path, size): - from PIL import ImageFont - - try: - font = ImageFont.truetype(str(path), size) - except Exception as e: - raise core.EsphomeError(f"Could not load TrueType file {path}: {e}") - - return TrueTypeFontWrapper(font) + self.font: Face = FONT_CACHE[file] class GlyphInfo: - def __init__(self, data_len, offset_x, offset_y, width, height): + def __init__(self, data_len, advance, offset_x, offset_y, width, height): self.data_len = data_len + self.advance = advance self.offset_x = offset_x self.offset_y = offset_y self.width = width @@ -537,15 +486,14 @@ async def to_code(config): } # get the codepoints from the glyphs key, flatten to a list of chrs and combine with the points from glyphsets point_set.update(flatten(config[CONF_GLYPHS])) - size = config[CONF_SIZE] # Create the codepoint to font file map - base_font = EFont(config[CONF_FILE], size, point_set) - point_font_map: dict[str, EFont] = {c: base_font for c in point_set} + base_font = FONT_CACHE[config[CONF_FILE]] + point_font_map: dict[str, Face] = {c: base_font for c in point_set} # process extras, updating the map and extending the codepoint list for extra in config[CONF_EXTRAS]: extra_points = flatten(extra[CONF_GLYPHS]) point_set.update(extra_points) - extra_font = EFont(extra[CONF_FILE], size, extra_points) + extra_font = FONT_CACHE[extra[CONF_FILE]] point_font_map.update({c: extra_font for c in extra_points}) codepoints = list(point_set) @@ -553,28 +501,54 @@ async def to_code(config): glyph_args = {} data = [] bpp = config[CONF_BPP] - if bpp == 1: - mode = "1" - scale = 1 - else: - mode = "L" - scale = 256 // (1 << bpp) + mode = ft_pixel_mode_grays + scale = 256 // (1 << bpp) + size = config[CONF_SIZE] # create the data array for all glyphs for codepoint in codepoints: font = point_font_map[codepoint] - mask = font.font.getmask(codepoint, mode=mode) - offset_x, offset_y = font.font.getoffset(codepoint) - width, height = mask.size + format = font.get_format().decode("utf-8") + if format != "PCF": + font.set_pixel_sizes(size, 0) + font.load_char(codepoint) + font.glyph.render(mode) + width = font.glyph.bitmap.width + height = font.glyph.bitmap.rows + buffer = font.glyph.bitmap.buffer + pitch = font.glyph.bitmap.pitch glyph_data = [0] * ((height * width * bpp + 7) // 8) + src_mode = font.glyph.bitmap.pixel_mode pos = 0 for y in range(height): for x in range(width): - pixel = mask.getpixel((x, y)) // scale + if src_mode == ft_pixel_mode_mono: + pixel = ( + (1 << bpp) - 1 + if buffer[y * pitch + x // 8] & (1 << (7 - x % 8)) + else 0 + ) + else: + pixel = buffer[y * pitch + x] // scale for bit_num in range(bpp): if pixel & (1 << (bpp - bit_num - 1)): glyph_data[pos // 8] |= 0x80 >> (pos % 8) pos += 1 - glyph_args[codepoint] = GlyphInfo(len(data), offset_x, offset_y, width, height) + ascender = pt_to_px(font.size.ascender) + if ascender == 0: + if font.has_fixed_sizes: + ascender = size + else: + _LOGGER.error( + "Unable to determine ascender of font %s", config[CONF_FILE] + ) + glyph_args[codepoint] = GlyphInfo( + len(data), + pt_to_px(font.glyph.metrics.horiAdvance), + font.glyph.bitmap_left, + ascender - font.glyph.bitmap_top, + width, + height, + ) data += glyph_data rhs = [HexInt(x) for x in data] @@ -598,6 +572,7 @@ async def to_code(config): f"{str(prog_arr)} + {str(glyph_args[codepoint].data_len)}" ), ), + ("advance", glyph_args[codepoint].advance), ("offset_x", glyph_args[codepoint].offset_x), ("offset_y", glyph_args[codepoint].offset_y), ("width", glyph_args[codepoint].width), @@ -607,11 +582,19 @@ async def to_code(config): glyphs = cg.static_const_array(config[CONF_RAW_GLYPH_ID], glyph_initializer) + font_height = pt_to_px(base_font.size.height) + ascender = pt_to_px(base_font.size.ascender) + if font_height == 0: + if base_font.has_fixed_sizes: + font_height = size + ascender = font_height + else: + _LOGGER.error("Unable to determine height of font %s", config[CONF_FILE]) cg.new_Pvariable( config[CONF_ID], glyphs, len(glyph_initializer), - base_font.ascent, - base_font.ascent + base_font.descent, + ascender, + font_height, bpp, ) diff --git a/esphome/components/font/font.cpp b/esphome/components/font/font.cpp index 8c4cba34b3..32464d87ee 100644 --- a/esphome/components/font/font.cpp +++ b/esphome/components/font/font.cpp @@ -81,7 +81,7 @@ void Font::measure(const char *str, int *width, int *x_offset, int *baseline, in if (glyph_n < 0) { // Unknown char, skip if (!this->get_glyphs().empty()) - x += this->get_glyphs()[0].glyph_data_->width; + x += this->get_glyphs()[0].glyph_data_->advance; i++; continue; } @@ -92,7 +92,7 @@ void Font::measure(const char *str, int *width, int *x_offset, int *baseline, in } else { min_x = std::min(min_x, x + glyph.glyph_data_->offset_x); } - x += glyph.glyph_data_->width + glyph.glyph_data_->offset_x; + x += glyph.glyph_data_->advance; i += match_length; has_char = true; @@ -111,7 +111,7 @@ void Font::print(int x_start, int y_start, display::Display *display, Color colo // Unknown char, skip ESP_LOGW(TAG, "Encountered character without representation in font: '%c'", text[i]); if (!this->get_glyphs().empty()) { - uint8_t glyph_width = this->get_glyphs()[0].glyph_data_->width; + uint8_t glyph_width = this->get_glyphs()[0].glyph_data_->advance; display->filled_rectangle(x_at, y_start, glyph_width, this->height_, color); x_at += glyph_width; } @@ -161,7 +161,7 @@ void Font::print(int x_start, int y_start, display::Display *display, Color colo } } } - x_at += glyph.glyph_data_->width + glyph.glyph_data_->offset_x; + x_at += glyph.glyph_data_->advance; i += match_length; } diff --git a/esphome/components/font/font.h b/esphome/components/font/font.h index 5cde694d91..9ee23b3ec5 100644 --- a/esphome/components/font/font.h +++ b/esphome/components/font/font.h @@ -15,6 +15,7 @@ class Font; struct GlyphData { const uint8_t *a_char; const uint8_t *data; + int advance; int offset_x; int offset_y; int width; diff --git a/esphome/components/graph/graph.cpp b/esphome/components/graph/graph.cpp index cbe059b255..5abf2ade0d 100644 --- a/esphome/components/graph/graph.cpp +++ b/esphome/components/graph/graph.cpp @@ -132,6 +132,10 @@ void Graph::draw(Display *buff, uint16_t x_offset, uint16_t y_offset, Color colo yrange = ymax - ymin; } + // Store graph limts + this->graph_limit_max_ = ymax; + this->graph_limit_min_ = ymin; + /// Draw grid if (!std::isnan(this->gridspacing_y_)) { for (int y = yn; y <= ym; y++) { diff --git a/esphome/components/graph/graph.h b/esphome/components/graph/graph.h index 34accb7d3a..468583ca21 100644 --- a/esphome/components/graph/graph.h +++ b/esphome/components/graph/graph.h @@ -161,11 +161,15 @@ class Graph : public Component { uint32_t get_duration() { return duration_; } uint32_t get_width() { return width_; } uint32_t get_height() { return height_; } + float get_graph_limit_min() { return graph_limit_min_; } + float get_graph_limit_max() { return graph_limit_max_; } protected: uint32_t duration_; /// in seconds uint32_t width_; /// in pixels uint32_t height_; /// in pixels + float graph_limit_min_{NAN}; + float graph_limit_max_{NAN}; float min_value_{NAN}; float max_value_{NAN}; float min_range_{1.0}; diff --git a/esphome/components/haier/climate.py b/esphome/components/haier/climate.py index f2dc7174cb..f77d624649 100644 --- a/esphome/components/haier/climate.py +++ b/esphome/components/haier/climate.py @@ -1,9 +1,15 @@ -import logging -import esphome.codegen as cg -import esphome.config_validation as cv -import esphome.final_validate as fv -from esphome.components import uart, climate, logger +import logging + from esphome import automation +import esphome.codegen as cg +from esphome.components import climate, logger, uart +from esphome.components.climate import ( + CONF_CURRENT_TEMPERATURE, + ClimateMode, + ClimatePreset, + ClimateSwingMode, +) +import esphome.config_validation as cv from esphome.const import ( CONF_BEEPER, CONF_DISPLAY, @@ -24,12 +30,7 @@ from esphome.const import ( CONF_VISUAL, CONF_WIFI, ) -from esphome.components.climate import ( - ClimateMode, - ClimatePreset, - ClimateSwingMode, - CONF_CURRENT_TEMPERATURE, -) +import esphome.final_validate as fv _LOGGER = logging.getLogger(__name__) diff --git a/esphome/components/hbridge/switch/hbridge_switch.cpp b/esphome/components/hbridge/switch/hbridge_switch.cpp index 12d1c01bca..c0949be947 100644 --- a/esphome/components/hbridge/switch/hbridge_switch.cpp +++ b/esphome/components/hbridge/switch/hbridge_switch.cpp @@ -12,7 +12,7 @@ float HBridgeSwitch::get_setup_priority() const { return setup_priority::HARDWAR void HBridgeSwitch::setup() { ESP_LOGCONFIG(TAG, "Setting up H-Bridge Switch '%s'...", this->name_.c_str()); - optional initial_state = this->get_initial_state_with_restore_mode().value_or(false); + optional initial_state = this->get_initial_state_with_restore_mode(); // Like GPIOSwitch does, set the pin state both before and after pin setup() this->on_pin_->digital_write(false); @@ -24,7 +24,7 @@ void HBridgeSwitch::setup() { this->off_pin_->digital_write(false); if (initial_state.has_value()) - this->write_state(initial_state); + this->write_state(initial_state.value()); } void HBridgeSwitch::dump_config() { diff --git a/esphome/components/heatpumpir/climate.py b/esphome/components/heatpumpir/climate.py index 9d31668deb..598071590b 100644 --- a/esphome/components/heatpumpir/climate.py +++ b/esphome/components/heatpumpir/climate.py @@ -53,6 +53,7 @@ PROTOCOLS = { "mitsubishi_sez": Protocol.PROTOCOL_MITSUBISHI_SEZ, "panasonic_ckp": Protocol.PROTOCOL_PANASONIC_CKP, "panasonic_dke": Protocol.PROTOCOL_PANASONIC_DKE, + "panasonic_eke": Protocol.PROTOCOL_PANASONIC_EKE, "panasonic_jke": Protocol.PROTOCOL_PANASONIC_JKE, "panasonic_lke": Protocol.PROTOCOL_PANASONIC_LKE, "panasonic_nke": Protocol.PROTOCOL_PANASONIC_NKE, @@ -127,6 +128,6 @@ def to_code(config): cg.add(var.set_max_temperature(config[CONF_MAX_TEMPERATURE])) cg.add(var.set_min_temperature(config[CONF_MIN_TEMPERATURE])) - cg.add_library("tonia/HeatpumpIR", "1.0.27") + cg.add_library("tonia/HeatpumpIR", "1.0.32") if CORE.is_libretiny: CORE.add_platformio_option("lib_ignore", "IRremoteESP8266") diff --git a/esphome/components/heatpumpir/heatpumpir.cpp b/esphome/components/heatpumpir/heatpumpir.cpp index 55f0599cba..d3476c6a71 100644 --- a/esphome/components/heatpumpir/heatpumpir.cpp +++ b/esphome/components/heatpumpir/heatpumpir.cpp @@ -47,6 +47,7 @@ const std::map> PROTOCOL_CONSTRUCTOR_MAP {PROTOCOL_MITSUBISHI_SEZ, []() { return new MitsubishiSEZKDXXHeatpumpIR(); }}, // NOLINT {PROTOCOL_PANASONIC_CKP, []() { return new PanasonicCKPHeatpumpIR(); }}, // NOLINT {PROTOCOL_PANASONIC_DKE, []() { return new PanasonicDKEHeatpumpIR(); }}, // NOLINT + {PROTOCOL_PANASONIC_EKE, []() { return new PanasonicEKEHeatpumpIR(); }}, // NOLINT {PROTOCOL_PANASONIC_JKE, []() { return new PanasonicJKEHeatpumpIR(); }}, // NOLINT {PROTOCOL_PANASONIC_LKE, []() { return new PanasonicLKEHeatpumpIR(); }}, // NOLINT {PROTOCOL_PANASONIC_NKE, []() { return new PanasonicNKEHeatpumpIR(); }}, // NOLINT diff --git a/esphome/components/heatpumpir/heatpumpir.h b/esphome/components/heatpumpir/heatpumpir.h index f6e7ff3cd6..b740d27af7 100644 --- a/esphome/components/heatpumpir/heatpumpir.h +++ b/esphome/components/heatpumpir/heatpumpir.h @@ -47,6 +47,7 @@ enum Protocol { PROTOCOL_MITSUBISHI_SEZ, PROTOCOL_PANASONIC_CKP, PROTOCOL_PANASONIC_DKE, + PROTOCOL_PANASONIC_EKE, PROTOCOL_PANASONIC_JKE, PROTOCOL_PANASONIC_LKE, PROTOCOL_PANASONIC_NKE, diff --git a/esphome/components/i2c/__init__.py b/esphome/components/i2c/__init__.py index f52a0edb9f..e47dec650d 100644 --- a/esphome/components/i2c/__init__.py +++ b/esphome/components/i2c/__init__.py @@ -1,23 +1,23 @@ +from esphome import pins import esphome.codegen as cg import esphome.config_validation as cv -import esphome.final_validate as fv -from esphome import pins from esphome.const import ( + CONF_ADDRESS, CONF_FREQUENCY, - CONF_TIMEOUT, + CONF_I2C_ID, CONF_ID, CONF_INPUT, CONF_OUTPUT, CONF_SCAN, CONF_SCL, CONF_SDA, - CONF_ADDRESS, - CONF_I2C_ID, + CONF_TIMEOUT, PLATFORM_ESP32, PLATFORM_ESP8266, PLATFORM_RP2040, ) -from esphome.core import coroutine_with_priority, CORE +from esphome.core import CORE, coroutine_with_priority +import esphome.final_validate as fv CODEOWNERS = ["@esphome/core"] i2c_ns = cg.esphome_ns.namespace("i2c") diff --git a/esphome/components/i2c/i2c_bus_esp_idf.cpp b/esphome/components/i2c/i2c_bus_esp_idf.cpp index c7bba70582..c5d6dd8b2a 100644 --- a/esphome/components/i2c/i2c_bus_esp_idf.cpp +++ b/esphome/components/i2c/i2c_bus_esp_idf.cpp @@ -8,6 +8,10 @@ #include "esphome/core/helpers.h" #include "esphome/core/log.h" +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 0) +#define SOC_HP_I2C_NUM SOC_I2C_NUM +#endif + namespace esphome { namespace i2c { @@ -17,14 +21,14 @@ void IDFI2CBus::setup() { ESP_LOGCONFIG(TAG, "Setting up I2C bus..."); static i2c_port_t next_port = I2C_NUM_0; port_ = next_port; -#if SOC_I2C_NUM > 1 +#if SOC_HP_I2C_NUM > 1 next_port = (next_port == I2C_NUM_0) ? I2C_NUM_1 : I2C_NUM_MAX; #else next_port = I2C_NUM_MAX; #endif if (port_ == I2C_NUM_MAX) { - ESP_LOGE(TAG, "Too many I2C buses configured. Max %u supported.", SOC_I2C_NUM); + ESP_LOGE(TAG, "Too many I2C buses configured. Max %u supported.", SOC_HP_I2C_NUM); this->mark_failed(); return; } diff --git a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp index 2a3fa9f5f3..da25914c87 100644 --- a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp +++ b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp @@ -203,7 +203,7 @@ size_t I2SAudioSpeaker::play(const uint8_t *data, size_t length, TickType_t tick this->start(); } - if ((this->state_ != speaker::STATE_RUNNING) || (this->audio_ring_buffer_.use_count() == 1)) { + if ((this->state_ != speaker::STATE_RUNNING) || (this->audio_ring_buffer_.use_count() != 1)) { // Unable to write data to a running speaker, so delay the max amount of time so it can get ready vTaskDelay(ticks_to_wait); ticks_to_wait = 0; diff --git a/esphome/components/ili9xxx/display.py b/esphome/components/ili9xxx/display.py index e3abb7e98c..14b7f15218 100644 --- a/esphome/components/ili9xxx/display.py +++ b/esphome/components/ili9xxx/display.py @@ -57,6 +57,7 @@ ColorOrder = display.display_ns.enum("ColorMode") MODELS = { "GC9A01A": ili9xxx_ns.class_("ILI9XXXGC9A01A", ILI9XXXDisplay), + "GC9D01N": ili9xxx_ns.class_("ILI9XXXGC9D01N", ILI9XXXDisplay), "M5STACK": ili9xxx_ns.class_("ILI9XXXM5Stack", ILI9XXXDisplay), "M5CORE": ili9xxx_ns.class_("ILI9XXXM5CORE", ILI9XXXDisplay), "TFT_2.4": ili9xxx_ns.class_("ILI9XXXILI9341", ILI9XXXDisplay), diff --git a/esphome/components/ili9xxx/ili9xxx_display.h b/esphome/components/ili9xxx/ili9xxx_display.h index 87d7c86e5c..0babcedc48 100644 --- a/esphome/components/ili9xxx/ili9xxx_display.h +++ b/esphome/components/ili9xxx/ili9xxx_display.h @@ -272,6 +272,11 @@ class ILI9XXXGC9A01A : public ILI9XXXDisplay { ILI9XXXGC9A01A() : ILI9XXXDisplay(INITCMD_GC9A01A, 240, 240) {} }; +class ILI9XXXGC9D01N : public ILI9XXXDisplay { + public: + ILI9XXXGC9D01N() : ILI9XXXDisplay(INITCMD_GC9D01N, 160, 160) {} +}; + //----------- ILI9XXX_24_TFT display -------------- class ILI9XXXST7735 : public ILI9XXXDisplay { public: diff --git a/esphome/components/ili9xxx/ili9xxx_init.h b/esphome/components/ili9xxx/ili9xxx_init.h index b176680f43..f05b884be6 100644 --- a/esphome/components/ili9xxx/ili9xxx_init.h +++ b/esphome/components/ili9xxx/ili9xxx_init.h @@ -367,6 +367,65 @@ static const uint8_t PROGMEM INITCMD_GC9A01A[] = { 0x00 // End of list }; +static const uint8_t PROGMEM INITCMD_GC9D01N[] = { + // Enable Inter_command + 0xFE, 0, // Inter Register Enable 1 (FEh) + 0xEF, 0, // Inter Register Enable 2 (EFh) + // Inter_command is now enabled + 0x80, 1, 0xFF, + 0x81, 1, 0xFF, + 0x82, 1, 0xFF, + 0x83, 1, 0xFF, + 0x84, 1, 0xFF, + 0x85, 1, 0xFF, + 0x86, 1, 0xFF, + 0x87, 1, 0xFF, + 0x88, 1, 0xFF, + 0x89, 1, 0xFF, + 0x8A, 1, 0xFF, + 0x8B, 1, 0xFF, + 0x8C, 1, 0xFF, + 0x8D, 1, 0xFF, + 0x8E, 1, 0xFF, + 0x8F, 1, 0xFF, + 0X3A, 1, 0x05, // COLMOD: Pixel Format Set (3Ah) MCU interface, 16 bits / pixel + 0xEC, 1, 0x01, // Inversion (ECh) DINV=1+2H1V column for Dual Gate (BFh=0) + // According to datasheet Inversion (ECh) value 0x01 isn't valid, but Lilygo uses it everywhere + 0x74, 7, 0x02, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x98, 1, 0x3e, + 0x99, 1, 0x3e, + 0xB5, 2, 0x0D, 0x0D, // Blanking Porch Control (B5h) VFP=14 VBP=14 HBP=Off + 0x60, 4, 0x38, 0x0F, 0x79, 0x67, + 0x61, 4, 0x38, 0x11, 0x79, 0x67, + 0x64, 6, 0x38, 0x17, 0x71, 0x5F, 0x79, 0x67, + 0x65, 6, 0x38, 0x13, 0x71, 0x5B, 0x79, 0x67, + 0x6A, 2, 0x00, 0x00, + 0x6C, 7, 0x22, 0x02, 0x22, 0x02, 0x22, 0x22, 0x50, + 0x6E, 32, 0x03, 0x03, 0x01, 0x01, 0x00, 0x00, 0x0F, 0x0F, + 0x0D, 0x0D, 0x0B, 0x0B, 0x09, 0x09, 0x00, 0x00, + 0x00, 0x00, 0x0A, 0x0A, 0x0C, 0x0C, 0x0E, 0x0E, + 0x10, 0x10, 0x00, 0x00, 0x02, 0x02, 0x04, 0x04, + 0xBF, 1, 0x01, // Dual-Single gate select (BFh) 01h = dual gate mode + 0xF9, 1, 0x40, + 0x9B, 5, 0x3B, 0x93, 0x33, 0x7F, 0x00, + 0x7E, 1, 0x30, + 0x70, 6, 0x0D, 0x02, 0x08, 0x0D, 0x02, 0x08, + 0x71, 3, 0x0D, 0x02, 0x08, + 0x91, 2, 0x0E, 0x09, + // Set VREG1A, VREG1B, VREG2A, VREG2B voltage + // According to datasheet set either 0xC3/0xC4 or 0xC9 only, but Lilygo sets both of them + 0xC3, 5, 0x19, 0xC4, 0x19, 0xC9, 0x3C, + 0xF0, 6, 0x53, 0x15, 0x0A, 0x04, 0x00, 0x3E, // SET_GAMMA1 (F0h) + 0xF1, 6, 0x56, 0xA8, 0x7F, 0x33, 0x34, 0x5F, // SET_GAMMA2 (F1h) + 0xF2, 6, 0x53, 0x15, 0x0A, 0x04, 0x00, 0x3A, // SET_GAMMA3 (F2h) + 0xF3, 6, 0x52, 0xA4, 0x7F, 0x33, 0x34, 0xDF, // SET_GAMMA4 (F3h) + ILI9XXX_SLPOUT, 0, // Sleep Out Mode (11h) + ILI9XXX_DELAY(10), + ILI9XXX_DISPON, 0, // Display ON (29h) + ILI9XXX_DELAY(20), + 0x00 // End of list +}; + static const uint8_t PROGMEM INITCMD_ST7735[] = { ILI9XXX_SWRESET, 0, // Soft reset, then delay 10ms ILI9XXX_DELAY(10), diff --git a/esphome/components/ld2450/__init__.py b/esphome/components/ld2450/__init__.py new file mode 100644 index 0000000000..37f68a8f3e --- /dev/null +++ b/esphome/components/ld2450/__init__.py @@ -0,0 +1,51 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import uart +from esphome.const import ( + CONF_ID, + CONF_THROTTLE, +) + +DEPENDENCIES = ["uart"] +CODEOWNERS = ["@hareeshmu"] +MULTI_CONF = True + +ld2450_ns = cg.esphome_ns.namespace("ld2450") +LD2450Component = ld2450_ns.class_("LD2450Component", cg.Component, uart.UARTDevice) + +CONF_LD2450_ID = "ld2450_id" + +CONFIG_SCHEMA = cv.All( + cv.Schema( + { + cv.GenerateID(): cv.declare_id(LD2450Component), + cv.Optional(CONF_THROTTLE, default="1000ms"): cv.All( + cv.positive_time_period_milliseconds, + cv.Range(min=cv.TimePeriod(milliseconds=1)), + ), + } + ) + .extend(uart.UART_DEVICE_SCHEMA) + .extend(cv.COMPONENT_SCHEMA) +) + +LD2450BaseSchema = cv.Schema( + { + cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), + }, +) + +FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema( + "ld2450", + require_tx=True, + require_rx=True, + parity="NONE", + stop_bits=1, +) + + +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) + cg.add(var.set_throttle(config[CONF_THROTTLE])) diff --git a/esphome/components/ld2450/binary_sensor.py b/esphome/components/ld2450/binary_sensor.py new file mode 100644 index 0000000000..d0082ac21a --- /dev/null +++ b/esphome/components/ld2450/binary_sensor.py @@ -0,0 +1,47 @@ +import esphome.codegen as cg +from esphome.components import binary_sensor +import esphome.config_validation as cv +from esphome.const import ( + CONF_HAS_MOVING_TARGET, + CONF_HAS_STILL_TARGET, + CONF_HAS_TARGET, + DEVICE_CLASS_MOTION, + DEVICE_CLASS_OCCUPANCY, +) + +from . import CONF_LD2450_ID, LD2450Component + +DEPENDENCIES = ["ld2450"] + +ICON_MEDITATION = "mdi:meditation" +ICON_SHIELD_ACCOUNT = "mdi:shield-account" +ICON_TARGET_ACCOUNT = "mdi:target-account" + +CONFIG_SCHEMA = { + cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), + cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema( + device_class=DEVICE_CLASS_OCCUPANCY, + icon=ICON_SHIELD_ACCOUNT, + ), + cv.Optional(CONF_HAS_MOVING_TARGET): binary_sensor.binary_sensor_schema( + device_class=DEVICE_CLASS_MOTION, + icon=ICON_TARGET_ACCOUNT, + ), + cv.Optional(CONF_HAS_STILL_TARGET): binary_sensor.binary_sensor_schema( + device_class=DEVICE_CLASS_OCCUPANCY, + icon=ICON_MEDITATION, + ), +} + + +async def to_code(config): + ld2450_component = await cg.get_variable(config[CONF_LD2450_ID]) + if has_target_config := config.get(CONF_HAS_TARGET): + sens = await binary_sensor.new_binary_sensor(has_target_config) + cg.add(ld2450_component.set_target_binary_sensor(sens)) + if has_moving_target_config := config.get(CONF_HAS_MOVING_TARGET): + sens = await binary_sensor.new_binary_sensor(has_moving_target_config) + cg.add(ld2450_component.set_moving_target_binary_sensor(sens)) + if has_still_target_config := config.get(CONF_HAS_STILL_TARGET): + sens = await binary_sensor.new_binary_sensor(has_still_target_config) + cg.add(ld2450_component.set_still_target_binary_sensor(sens)) diff --git a/esphome/components/ld2450/button/__init__.py b/esphome/components/ld2450/button/__init__.py new file mode 100644 index 0000000000..39671d3a3b --- /dev/null +++ b/esphome/components/ld2450/button/__init__.py @@ -0,0 +1,45 @@ +import esphome.codegen as cg +from esphome.components import button +import esphome.config_validation as cv +from esphome.const import ( + CONF_FACTORY_RESET, + CONF_RESTART, + DEVICE_CLASS_RESTART, + ENTITY_CATEGORY_CONFIG, + ENTITY_CATEGORY_DIAGNOSTIC, + ICON_RESTART, + ICON_RESTART_ALERT, +) + +from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns + +ResetButton = ld2450_ns.class_("ResetButton", button.Button) +RestartButton = ld2450_ns.class_("RestartButton", button.Button) + +CONFIG_SCHEMA = { + cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), + cv.Optional(CONF_FACTORY_RESET): button.button_schema( + ResetButton, + device_class=DEVICE_CLASS_RESTART, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_RESTART_ALERT, + ), + cv.Optional(CONF_RESTART): button.button_schema( + RestartButton, + device_class=DEVICE_CLASS_RESTART, + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + icon=ICON_RESTART, + ), +} + + +async def to_code(config): + ld2450_component = await cg.get_variable(config[CONF_LD2450_ID]) + if factory_reset_config := config.get(CONF_FACTORY_RESET): + b = await button.new_button(factory_reset_config) + await cg.register_parented(b, config[CONF_LD2450_ID]) + cg.add(ld2450_component.set_reset_button(b)) + if restart_config := config.get(CONF_RESTART): + b = await button.new_button(restart_config) + await cg.register_parented(b, config[CONF_LD2450_ID]) + cg.add(ld2450_component.set_restart_button(b)) diff --git a/esphome/components/ld2450/button/reset_button.cpp b/esphome/components/ld2450/button/reset_button.cpp new file mode 100644 index 0000000000..e96ec99cc5 --- /dev/null +++ b/esphome/components/ld2450/button/reset_button.cpp @@ -0,0 +1,9 @@ +#include "reset_button.h" + +namespace esphome { +namespace ld2450 { + +void ResetButton::press_action() { this->parent_->factory_reset(); } + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/button/reset_button.h b/esphome/components/ld2450/button/reset_button.h new file mode 100644 index 0000000000..73804fa6d6 --- /dev/null +++ b/esphome/components/ld2450/button/reset_button.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/button/button.h" +#include "../ld2450.h" + +namespace esphome { +namespace ld2450 { + +class ResetButton : public button::Button, public Parented { + public: + ResetButton() = default; + + protected: + void press_action() override; +}; + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/button/restart_button.cpp b/esphome/components/ld2450/button/restart_button.cpp new file mode 100644 index 0000000000..ee2f5ac12f --- /dev/null +++ b/esphome/components/ld2450/button/restart_button.cpp @@ -0,0 +1,9 @@ +#include "restart_button.h" + +namespace esphome { +namespace ld2450 { + +void RestartButton::press_action() { this->parent_->restart_and_read_all_info(); } + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/button/restart_button.h b/esphome/components/ld2450/button/restart_button.h new file mode 100644 index 0000000000..a44ae5a4d2 --- /dev/null +++ b/esphome/components/ld2450/button/restart_button.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/button/button.h" +#include "../ld2450.h" + +namespace esphome { +namespace ld2450 { + +class RestartButton : public button::Button, public Parented { + public: + RestartButton() = default; + + protected: + void press_action() override; +}; + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/ld2450.cpp b/esphome/components/ld2450/ld2450.cpp new file mode 100644 index 0000000000..5bd7635dec --- /dev/null +++ b/esphome/components/ld2450/ld2450.cpp @@ -0,0 +1,876 @@ +#include "ld2450.h" +#include +#ifdef USE_NUMBER +#include "esphome/components/number/number.h" +#endif +#ifdef USE_SENSOR +#include "esphome/components/sensor/sensor.h" +#endif +#include "esphome/core/component.h" + +#define highbyte(val) (uint8_t)((val) >> 8) +#define lowbyte(val) (uint8_t)((val) &0xff) + +namespace esphome { +namespace ld2450 { + +static const char *const TAG = "ld2450"; +static const char *const UNKNOWN_MAC("unknown"); + +// LD2450 UART Serial Commands +static const uint8_t CMD_ENABLE_CONF = 0x00FF; +static const uint8_t CMD_DISABLE_CONF = 0x00FE; +static const uint8_t CMD_VERSION = 0x00A0; +static const uint8_t CMD_MAC = 0x00A5; +static const uint8_t CMD_RESET = 0x00A2; +static const uint8_t CMD_RESTART = 0x00A3; +static const uint8_t CMD_BLUETOOTH = 0x00A4; +static const uint8_t CMD_SINGLE_TARGET_MODE = 0x0080; +static const uint8_t CMD_MULTI_TARGET_MODE = 0x0090; +static const uint8_t CMD_QUERY_TARGET_MODE = 0x0091; +static const uint8_t CMD_SET_BAUD_RATE = 0x00A1; +static const uint8_t CMD_QUERY_ZONE = 0x00C1; +static const uint8_t CMD_SET_ZONE = 0x00C2; + +static inline uint16_t convert_seconds_to_ms(uint16_t value) { return value * 1000; }; + +static inline std::string convert_signed_int_to_hex(int value) { + auto value_as_str = str_snprintf("%04x", 4, value & 0xFFFF); + return value_as_str; +} + +static inline void convert_int_values_to_hex(const int *values, uint8_t *bytes) { + for (int i = 0; i < 4; i++) { + std::string temp_hex = convert_signed_int_to_hex(values[i]); + bytes[i * 2] = std::stoi(temp_hex.substr(2, 2), nullptr, 16); // Store high byte + bytes[i * 2 + 1] = std::stoi(temp_hex.substr(0, 2), nullptr, 16); // Store low byte + } +} + +static inline int16_t decode_coordinate(uint8_t low_byte, uint8_t high_byte) { + int16_t coordinate = (high_byte & 0x7F) << 8 | low_byte; + if ((high_byte & 0x80) == 0) { + coordinate = -coordinate; + } + return coordinate; // mm +} + +static inline int16_t decode_speed(uint8_t low_byte, uint8_t high_byte) { + int16_t speed = (high_byte & 0x7F) << 8 | low_byte; + if ((high_byte & 0x80) == 0) { + speed = -speed; + } + return speed * 10; // mm/s +} + +static inline int16_t hex_to_signed_int(const uint8_t *buffer, uint8_t offset) { + uint16_t hex_val = (buffer[offset + 1] << 8) | buffer[offset]; + int16_t dec_val = static_cast(hex_val); + if (dec_val & 0x8000) { + dec_val -= 65536; + } + return dec_val; +} + +static inline float calculate_angle(float base, float hypotenuse) { + if (base < 0.0 || hypotenuse <= 0.0) { + return 0.0; + } + float angle_radians = std::acos(base / hypotenuse); + float angle_degrees = angle_radians * (180.0 / M_PI); + return angle_degrees; +} + +static inline std::string get_direction(int16_t speed) { + static const char *const APPROACHING = "Approaching"; + static const char *const MOVING_AWAY = "Moving away"; + static const char *const STATIONARY = "Stationary"; + + if (speed > 0) { + return MOVING_AWAY; + } + if (speed < 0) { + return APPROACHING; + } + return STATIONARY; +} + +static inline std::string format_mac(uint8_t *buffer) { + return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, buffer[10], buffer[11], buffer[12], buffer[13], buffer[14], + buffer[15]); +} + +static inline std::string format_version(uint8_t *buffer) { + return str_sprintf("%u.%02X.%02X%02X%02X%02X", buffer[13], buffer[12], buffer[17], buffer[16], buffer[15], + buffer[14]); +} + +LD2450Component::LD2450Component() {} + +void LD2450Component::setup() { + ESP_LOGCONFIG(TAG, "Setting up HLK-LD2450..."); +#ifdef USE_NUMBER + if (this->presence_timeout_number_ != nullptr) { + this->pref_ = global_preferences->make_preference(this->presence_timeout_number_->get_object_id_hash()); + this->set_presence_timeout(); + } +#endif + this->restart_and_read_all_info(); +} + +void LD2450Component::dump_config() { + ESP_LOGCONFIG(TAG, "HLK-LD2450 Human motion tracking radar module:"); +#ifdef USE_BINARY_SENSOR + LOG_BINARY_SENSOR(" ", "TargetBinarySensor", this->target_binary_sensor_); + LOG_BINARY_SENSOR(" ", "MovingTargetBinarySensor", this->moving_target_binary_sensor_); + LOG_BINARY_SENSOR(" ", "StillTargetBinarySensor", this->still_target_binary_sensor_); +#endif +#ifdef USE_SWITCH + LOG_SWITCH(" ", "BluetoothSwitch", this->bluetooth_switch_); + LOG_SWITCH(" ", "MultiTargetSwitch", this->multi_target_switch_); +#endif +#ifdef USE_BUTTON + LOG_BUTTON(" ", "ResetButton", this->reset_button_); + LOG_BUTTON(" ", "RestartButton", this->restart_button_); +#endif +#ifdef USE_SENSOR + LOG_SENSOR(" ", "TargetCountSensor", this->target_count_sensor_); + LOG_SENSOR(" ", "StillTargetCountSensor", this->still_target_count_sensor_); + LOG_SENSOR(" ", "MovingTargetCountSensor", this->moving_target_count_sensor_); + for (sensor::Sensor *s : this->move_x_sensors_) { + LOG_SENSOR(" ", "NthTargetXSensor", s); + } + for (sensor::Sensor *s : this->move_y_sensors_) { + LOG_SENSOR(" ", "NthTargetYSensor", s); + } + for (sensor::Sensor *s : this->move_speed_sensors_) { + LOG_SENSOR(" ", "NthTargetSpeedSensor", s); + } + for (sensor::Sensor *s : this->move_angle_sensors_) { + LOG_SENSOR(" ", "NthTargetAngleSensor", s); + } + for (sensor::Sensor *s : this->move_distance_sensors_) { + LOG_SENSOR(" ", "NthTargetDistanceSensor", s); + } + for (sensor::Sensor *s : this->move_resolution_sensors_) { + LOG_SENSOR(" ", "NthTargetResolutionSensor", s); + } + for (sensor::Sensor *s : this->zone_target_count_sensors_) { + LOG_SENSOR(" ", "NthZoneTargetCountSensor", s); + } + for (sensor::Sensor *s : this->zone_still_target_count_sensors_) { + LOG_SENSOR(" ", "NthZoneStillTargetCountSensor", s); + } + for (sensor::Sensor *s : this->zone_moving_target_count_sensors_) { + LOG_SENSOR(" ", "NthZoneMovingTargetCountSensor", s); + } +#endif +#ifdef USE_TEXT_SENSOR + LOG_TEXT_SENSOR(" ", "VersionTextSensor", this->version_text_sensor_); + LOG_TEXT_SENSOR(" ", "MacTextSensor", this->mac_text_sensor_); + for (text_sensor::TextSensor *s : this->direction_text_sensors_) { + LOG_TEXT_SENSOR(" ", "NthDirectionTextSensor", s); + } +#endif +#ifdef USE_NUMBER + for (auto n : this->zone_numbers_) { + LOG_NUMBER(" ", "ZoneX1Number", n.x1); + LOG_NUMBER(" ", "ZoneY1Number", n.y1); + LOG_NUMBER(" ", "ZoneX2Number", n.x2); + LOG_NUMBER(" ", "ZoneY2Number", n.y2); + } +#endif +#ifdef USE_SELECT + LOG_SELECT(" ", "BaudRateSelect", this->baud_rate_select_); + LOG_SELECT(" ", "ZoneTypeSelect", this->zone_type_select_); +#endif +#ifdef USE_NUMBER + LOG_NUMBER(" ", "PresenceTimeoutNumber", this->presence_timeout_number_); +#endif + ESP_LOGCONFIG(TAG, " Throttle : %ums", this->throttle_); + ESP_LOGCONFIG(TAG, " MAC Address : %s", const_cast(this->mac_.c_str())); + ESP_LOGCONFIG(TAG, " Firmware version : %s", const_cast(this->version_.c_str())); +} + +void LD2450Component::loop() { + while (this->available()) { + this->readline_(read(), this->buffer_data_, MAX_LINE_LENGTH); + } +} + +// Count targets in zone +uint8_t LD2450Component::count_targets_in_zone_(const Zone &zone, bool is_moving) { + uint8_t count = 0; + for (auto &index : this->target_info_) { + if (index.x > zone.x1 && index.x < zone.x2 && index.y > zone.y1 && index.y < zone.y2 && + index.is_moving == is_moving) { + count++; + } + } + return count; +} + +// Service reset_radar_zone +void LD2450Component::reset_radar_zone() { + this->zone_type_ = 0; + for (auto &i : this->zone_config_) { + i.x1 = 0; + i.y1 = 0; + i.x2 = 0; + i.y2 = 0; + } + this->send_set_zone_command_(); +} + +void LD2450Component::set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_t zone1_y1, int32_t zone1_x2, + int32_t zone1_y2, int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2, + int32_t zone2_y2, int32_t zone3_x1, int32_t zone3_y1, int32_t zone3_x2, + int32_t zone3_y2) { + this->zone_type_ = zone_type; + int zone_parameters[12] = {zone1_x1, zone1_y1, zone1_x2, zone1_y2, zone2_x1, zone2_y1, + zone2_x2, zone2_y2, zone3_x1, zone3_y1, zone3_x2, zone3_y2}; + for (int i = 0; i < MAX_ZONES; i++) { + this->zone_config_[i].x1 = zone_parameters[i * 4]; + this->zone_config_[i].y1 = zone_parameters[i * 4 + 1]; + this->zone_config_[i].x2 = zone_parameters[i * 4 + 2]; + this->zone_config_[i].y2 = zone_parameters[i * 4 + 3]; + } + this->send_set_zone_command_(); +} + +// Set Zone on LD2450 Sensor +void LD2450Component::send_set_zone_command_() { + uint8_t cmd_value[26] = {}; + uint8_t zone_type_bytes[2] = {static_cast(this->zone_type_), 0x00}; + uint8_t area_config[24] = {}; + for (int i = 0; i < MAX_ZONES; i++) { + int values[4] = {this->zone_config_[i].x1, this->zone_config_[i].y1, this->zone_config_[i].x2, + this->zone_config_[i].y2}; + ld2450::convert_int_values_to_hex(values, area_config + (i * 8)); + } + std::memcpy(cmd_value, zone_type_bytes, 2); + std::memcpy(cmd_value + 2, area_config, 24); + this->set_config_mode_(true); + this->send_command_(CMD_SET_ZONE, cmd_value, 26); + this->set_config_mode_(false); +} + +// Check presense timeout to reset presence status +bool LD2450Component::get_timeout_status_(uint32_t check_millis) { + if (check_millis == 0) { + return true; + } + if (this->timeout_ == 0) { + this->timeout_ = ld2450::convert_seconds_to_ms(DEFAULT_PRESENCE_TIMEOUT); + } + auto current_millis = millis(); + return current_millis - check_millis >= this->timeout_; +} + +// Extract, store and publish zone details LD2450 buffer +void LD2450Component::process_zone_(uint8_t *buffer) { + uint8_t index, start; + for (index = 0; index < MAX_ZONES; index++) { + start = 12 + index * 8; + this->zone_config_[index].x1 = ld2450::hex_to_signed_int(buffer, start); + this->zone_config_[index].y1 = ld2450::hex_to_signed_int(buffer, start + 2); + this->zone_config_[index].x2 = ld2450::hex_to_signed_int(buffer, start + 4); + this->zone_config_[index].y2 = ld2450::hex_to_signed_int(buffer, start + 6); +#ifdef USE_NUMBER + // only one null check as all coordinates are required for a single zone + if (this->zone_numbers_[index].x1 != nullptr) { + this->zone_numbers_[index].x1->publish_state(this->zone_config_[index].x1); + this->zone_numbers_[index].y1->publish_state(this->zone_config_[index].y1); + this->zone_numbers_[index].x2->publish_state(this->zone_config_[index].x2); + this->zone_numbers_[index].y2->publish_state(this->zone_config_[index].y2); + } +#endif + } +} + +// Read all info from LD2450 buffer +void LD2450Component::read_all_info() { + this->set_config_mode_(true); + this->get_version_(); + this->get_mac_(); + this->query_target_tracking_mode_(); + this->query_zone_(); + this->set_config_mode_(false); +#ifdef USE_SELECT + const auto baud_rate = std::to_string(this->parent_->get_baud_rate()); + if (this->baud_rate_select_ != nullptr && this->baud_rate_select_->state != baud_rate) { + this->baud_rate_select_->publish_state(baud_rate); + } + this->publish_zone_type(); +#endif +} + +// Read zone info from LD2450 buffer +void LD2450Component::query_zone_info() { + this->set_config_mode_(true); + this->query_zone_(); + this->set_config_mode_(false); +} + +// Restart LD2450 and read all info from buffer +void LD2450Component::restart_and_read_all_info() { + this->set_config_mode_(true); + this->restart_(); + this->set_timeout(1500, [this]() { this->read_all_info(); }); +} + +// Send command with values to LD2450 +void LD2450Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) { + ESP_LOGV(TAG, "Sending command %02X", command); + // frame header + this->write_array(CMD_FRAME_HEADER, 4); + // length bytes + int len = 2; + if (command_value != nullptr) { + len += command_value_len; + } + this->write_byte(lowbyte(len)); + this->write_byte(highbyte(len)); + // command + this->write_byte(lowbyte(command)); + this->write_byte(highbyte(command)); + // command value bytes + if (command_value != nullptr) { + for (int i = 0; i < command_value_len; i++) { + this->write_byte(command_value[i]); + } + } + // footer + this->write_array(CMD_FRAME_END, 4); + // FIXME to remove + delay(50); // NOLINT +} + +// LD2450 Radar data message: +// [AA FF 03 00] [0E 03 B1 86 10 00 40 01] [00 00 00 00 00 00 00 00] [00 00 00 00 00 00 00 00] [55 CC] +// Header Target 1 Target 2 Target 3 End +void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) { + if (len < 29) { // header (4 bytes) + 8 x 3 target data + footer (2 bytes) + ESP_LOGE(TAG, "Periodic data: invalid message length"); + return; + } + if (buffer[0] != 0xAA || buffer[1] != 0xFF || buffer[2] != 0x03 || buffer[3] != 0x00) { // header + ESP_LOGE(TAG, "Periodic data: invalid message header"); + return; + } + if (buffer[len - 2] != 0x55 || buffer[len - 1] != 0xCC) { // footer + ESP_LOGE(TAG, "Periodic data: invalid message footer"); + return; + } + + auto current_millis = millis(); + if (current_millis - this->last_periodic_millis_ < this->throttle_) { + ESP_LOGV(TAG, "Throttling: %d", this->throttle_); + return; + } + + this->last_periodic_millis_ = current_millis; + + int16_t target_count = 0; + int16_t still_target_count = 0; + int16_t moving_target_count = 0; + int16_t start = 0; + int16_t val = 0; + uint8_t index = 0; + int16_t tx = 0; + int16_t ty = 0; + int16_t td = 0; + int16_t ts = 0; + int16_t angle = 0; + std::string direction{}; + bool is_moving = false; + +#if defined(USE_BINARY_SENSOR) || defined(USE_SENSOR) || defined(USE_TEXT_SENSOR) + // Loop thru targets + for (index = 0; index < MAX_TARGETS; index++) { +#ifdef USE_SENSOR + // X + start = TARGET_X + index * 8; + is_moving = false; + sensor::Sensor *sx = this->move_x_sensors_[index]; + if (sx != nullptr) { + val = ld2450::decode_coordinate(buffer[start], buffer[start + 1]); + tx = val; + sx->publish_state(val); + } + // Y + start = TARGET_Y + index * 8; + sensor::Sensor *sy = this->move_y_sensors_[index]; + if (sy != nullptr) { + val = ld2450::decode_coordinate(buffer[start], buffer[start + 1]); + ty = val; + sy->publish_state(val); + } + // RESOLUTION + start = TARGET_RESOLUTION + index * 8; + sensor::Sensor *sr = this->move_resolution_sensors_[index]; + if (sr != nullptr) { + val = (buffer[start + 1] << 8) | buffer[start]; + sr->publish_state(val); + } +#endif + // SPEED + start = TARGET_SPEED + index * 8; + val = ld2450::decode_speed(buffer[start], buffer[start + 1]); + ts = val; + if (val) { + is_moving = true; + moving_target_count++; + } +#ifdef USE_SENSOR + sensor::Sensor *ss = this->move_speed_sensors_[index]; + if (ss != nullptr) { + ss->publish_state(val); + } +#endif + // DISTANCE + val = (uint16_t) sqrt( + pow(ld2450::decode_coordinate(buffer[TARGET_X + index * 8], buffer[(TARGET_X + index * 8) + 1]), 2) + + pow(ld2450::decode_coordinate(buffer[TARGET_Y + index * 8], buffer[(TARGET_Y + index * 8) + 1]), 2)); + td = val; + if (val > 0) { + target_count++; + } +#ifdef USE_SENSOR + sensor::Sensor *sd = this->move_distance_sensors_[index]; + if (sd != nullptr) { + sd->publish_state(val); + } + // ANGLE + angle = calculate_angle(static_cast(ty), static_cast(td)); + if (tx > 0) { + angle = angle * -1; + } + sensor::Sensor *sa = this->move_angle_sensors_[index]; + if (sa != nullptr) { + sa->publish_state(angle); + } +#endif +#ifdef USE_TEXT_SENSOR + // DIRECTION + direction = get_direction(ts); + if (td == 0) { + direction = "NA"; + } + text_sensor::TextSensor *tsd = this->direction_text_sensors_[index]; + if (tsd != nullptr) { + tsd->publish_state(direction); + } +#endif + + // Store target info for zone target count + this->target_info_[index].x = tx; + this->target_info_[index].y = ty; + this->target_info_[index].is_moving = is_moving; + + } // End loop thru targets + + still_target_count = target_count - moving_target_count; +#endif + +#ifdef USE_SENSOR + // Loop thru zones + uint8_t zone_still_targets = 0; + uint8_t zone_moving_targets = 0; + uint8_t zone_all_targets = 0; + for (index = 0; index < MAX_ZONES; index++) { + zone_still_targets = this->count_targets_in_zone_(this->zone_config_[index], false); + zone_moving_targets = this->count_targets_in_zone_(this->zone_config_[index], true); + zone_all_targets = zone_still_targets + zone_moving_targets; + + // Publish Still Target Count in Zones + sensor::Sensor *szstc = this->zone_still_target_count_sensors_[index]; + if (szstc != nullptr) { + szstc->publish_state(zone_still_targets); + } + // Publish Moving Target Count in Zones + sensor::Sensor *szmtc = this->zone_moving_target_count_sensors_[index]; + if (szmtc != nullptr) { + szmtc->publish_state(zone_moving_targets); + } + // Publish All Target Count in Zones + sensor::Sensor *sztc = this->zone_target_count_sensors_[index]; + if (sztc != nullptr) { + sztc->publish_state(zone_all_targets); + } + + } // End loop thru zones + + // Target Count + if (this->target_count_sensor_ != nullptr) { + this->target_count_sensor_->publish_state(target_count); + } + // Still Target Count + if (this->still_target_count_sensor_ != nullptr) { + this->still_target_count_sensor_->publish_state(still_target_count); + } + // Moving Target Count + if (this->moving_target_count_sensor_ != nullptr) { + this->moving_target_count_sensor_->publish_state(moving_target_count); + } +#endif + +#ifdef USE_BINARY_SENSOR + // Target Presence + if (this->target_binary_sensor_ != nullptr) { + if (target_count > 0) { + this->target_binary_sensor_->publish_state(true); + } else { + if (this->get_timeout_status_(this->presence_millis_)) { + this->target_binary_sensor_->publish_state(false); + } else { + ESP_LOGV(TAG, "Clear presence waiting timeout: %d", this->timeout_); + } + } + } + // Moving Target Presence + if (this->moving_target_binary_sensor_ != nullptr) { + if (moving_target_count > 0) { + this->moving_target_binary_sensor_->publish_state(true); + } else { + if (this->get_timeout_status_(this->moving_presence_millis_)) { + this->moving_target_binary_sensor_->publish_state(false); + } + } + } + // Still Target Presence + if (this->still_target_binary_sensor_ != nullptr) { + if (still_target_count > 0) { + this->still_target_binary_sensor_->publish_state(true); + } else { + if (this->get_timeout_status_(this->still_presence_millis_)) { + this->still_target_binary_sensor_->publish_state(false); + } + } + } +#endif +#ifdef USE_SENSOR + // For presence timeout check + if (target_count > 0) { + this->presence_millis_ = millis(); + } + if (moving_target_count > 0) { + this->moving_presence_millis_ = millis(); + } + if (still_target_count > 0) { + this->still_presence_millis_ = millis(); + } +#endif +} + +bool LD2450Component::handle_ack_data_(uint8_t *buffer, uint8_t len) { + ESP_LOGV(TAG, "Handling ack data for command %02X", buffer[COMMAND]); + if (len < 10) { + ESP_LOGE(TAG, "Ack data: invalid length"); + return true; + } + if (buffer[0] != 0xFD || buffer[1] != 0xFC || buffer[2] != 0xFB || buffer[3] != 0xFA) { // frame header + ESP_LOGE(TAG, "Ack data: invalid header (command %02X)", buffer[COMMAND]); + return true; + } + if (buffer[COMMAND_STATUS] != 0x01) { + ESP_LOGE(TAG, "Ack data: invalid status"); + return true; + } + if (buffer[8] || buffer[9]) { + ESP_LOGE(TAG, "Ack data: last buffer was %u, %u", buffer[8], buffer[9]); + return true; + } + + switch (buffer[COMMAND]) { + case lowbyte(CMD_ENABLE_CONF): + ESP_LOGV(TAG, "Got enable conf command"); + break; + case lowbyte(CMD_DISABLE_CONF): + ESP_LOGV(TAG, "Got disable conf command"); + break; + case lowbyte(CMD_SET_BAUD_RATE): + ESP_LOGV(TAG, "Got baud rate change command"); +#ifdef USE_SELECT + if (this->baud_rate_select_ != nullptr) { + ESP_LOGV(TAG, "Change baud rate to %s", this->baud_rate_select_->state.c_str()); + } +#endif + break; + case lowbyte(CMD_VERSION): + this->version_ = ld2450::format_version(buffer); + ESP_LOGV(TAG, "Firmware version: %s", this->version_.c_str()); +#ifdef USE_TEXT_SENSOR + if (this->version_text_sensor_ != nullptr) { + this->version_text_sensor_->publish_state(this->version_); + } +#endif + break; + case lowbyte(CMD_MAC): + if (len < 20) { + return false; + } + this->mac_ = ld2450::format_mac(buffer); + ESP_LOGV(TAG, "MAC address: %s", this->mac_.c_str()); +#ifdef USE_TEXT_SENSOR + if (this->mac_text_sensor_ != nullptr) { + this->mac_text_sensor_->publish_state(this->mac_); + } +#endif +#ifdef USE_SWITCH + if (this->bluetooth_switch_ != nullptr) { + this->bluetooth_switch_->publish_state(this->mac_ != UNKNOWN_MAC); + } +#endif + break; + case lowbyte(CMD_BLUETOOTH): + ESP_LOGV(TAG, "Got Bluetooth command"); + break; + case lowbyte(CMD_SINGLE_TARGET_MODE): + ESP_LOGV(TAG, "Got single target conf command"); +#ifdef USE_SWITCH + if (this->multi_target_switch_ != nullptr) { + this->multi_target_switch_->publish_state(false); + } +#endif + break; + case lowbyte(CMD_MULTI_TARGET_MODE): + ESP_LOGV(TAG, "Got multi target conf command"); +#ifdef USE_SWITCH + if (this->multi_target_switch_ != nullptr) { + this->multi_target_switch_->publish_state(true); + } +#endif + break; + case lowbyte(CMD_QUERY_TARGET_MODE): + ESP_LOGV(TAG, "Got query target tracking mode command"); +#ifdef USE_SWITCH + if (this->multi_target_switch_ != nullptr) { + this->multi_target_switch_->publish_state(buffer[10] == 0x02); + } +#endif + break; + case lowbyte(CMD_QUERY_ZONE): + ESP_LOGV(TAG, "Got query zone conf command"); + this->zone_type_ = std::stoi(std::to_string(buffer[10]), nullptr, 16); + this->publish_zone_type(); +#ifdef USE_SELECT + if (this->zone_type_select_ != nullptr) { + ESP_LOGV(TAG, "Change zone type to: %s", this->zone_type_select_->state.c_str()); + } +#endif + if (buffer[10] == 0x00) { + ESP_LOGV(TAG, "Zone: Disabled"); + } + if (buffer[10] == 0x01) { + ESP_LOGV(TAG, "Zone: Area detection"); + } + if (buffer[10] == 0x02) { + ESP_LOGV(TAG, "Zone: Area filter"); + } + this->process_zone_(buffer); + break; + case lowbyte(CMD_SET_ZONE): + ESP_LOGV(TAG, "Got set zone conf command"); + this->query_zone_info(); + break; + default: + break; + } + return true; +} + +// Read LD2450 buffer data +void LD2450Component::readline_(int readch, uint8_t *buffer, uint8_t len) { + if (readch < 0) { + return; + } + if (this->buffer_pos_ < len - 1) { + buffer[this->buffer_pos_++] = readch; + buffer[this->buffer_pos_] = 0; + } else { + this->buffer_pos_ = 0; + } + if (this->buffer_pos_ < 4) { + return; + } + if (buffer[this->buffer_pos_ - 2] == 0x55 && buffer[this->buffer_pos_ - 1] == 0xCC) { + ESP_LOGV(TAG, "Handle periodic radar data"); + this->handle_periodic_data_(buffer, this->buffer_pos_); + this->buffer_pos_ = 0; // Reset position index for next frame + } else if (buffer[this->buffer_pos_ - 4] == 0x04 && buffer[this->buffer_pos_ - 3] == 0x03 && + buffer[this->buffer_pos_ - 2] == 0x02 && buffer[this->buffer_pos_ - 1] == 0x01) { + ESP_LOGV(TAG, "Handle command ack data"); + if (this->handle_ack_data_(buffer, this->buffer_pos_)) { + this->buffer_pos_ = 0; // Reset position index for next frame + } else { + ESP_LOGV(TAG, "Command ack data invalid"); + } + } +} + +// Set Config Mode - Pre-requisite sending commands +void LD2450Component::set_config_mode_(bool enable) { + uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF; + uint8_t cmd_value[2] = {0x01, 0x00}; + this->send_command_(cmd, enable ? cmd_value : nullptr, 2); +} + +// Set Bluetooth Enable/Disable +void LD2450Component::set_bluetooth(bool enable) { + this->set_config_mode_(true); + uint8_t enable_cmd_value[2] = {0x01, 0x00}; + uint8_t disable_cmd_value[2] = {0x00, 0x00}; + this->send_command_(CMD_BLUETOOTH, enable ? enable_cmd_value : disable_cmd_value, 2); + this->set_timeout(200, [this]() { this->restart_and_read_all_info(); }); +} + +// Set Baud rate +void LD2450Component::set_baud_rate(const std::string &state) { + this->set_config_mode_(true); + uint8_t cmd_value[2] = {BAUD_RATE_ENUM_TO_INT.at(state), 0x00}; + this->send_command_(CMD_SET_BAUD_RATE, cmd_value, 2); + this->set_timeout(200, [this]() { this->restart_(); }); +} + +// Set Zone Type - one of: Disabled, Detection, Filter +void LD2450Component::set_zone_type(const std::string &state) { + ESP_LOGV(TAG, "Set zone type: %s", state.c_str()); + uint8_t zone_type = ZONE_TYPE_ENUM_TO_INT.at(state); + this->zone_type_ = zone_type; + this->send_set_zone_command_(); +} + +// Publish Zone Type to Select component +void LD2450Component::publish_zone_type() { +#ifdef USE_SELECT + std::string zone_type = ZONE_TYPE_INT_TO_ENUM.at(static_cast(this->zone_type_)); + if (this->zone_type_select_ != nullptr) { + this->zone_type_select_->publish_state(zone_type); + } +#endif +} + +// Set Single/Multiplayer target detection +void LD2450Component::set_multi_target(bool enable) { + this->set_config_mode_(true); + uint8_t cmd = enable ? CMD_MULTI_TARGET_MODE : CMD_SINGLE_TARGET_MODE; + this->send_command_(cmd, nullptr, 0); + this->set_config_mode_(false); +} + +// LD2450 factory reset +void LD2450Component::factory_reset() { + this->set_config_mode_(true); + this->send_command_(CMD_RESET, nullptr, 0); + this->set_timeout(200, [this]() { this->restart_and_read_all_info(); }); +} + +// Restart LD2450 module +void LD2450Component::restart_() { this->send_command_(CMD_RESTART, nullptr, 0); } + +// Get LD2450 firmware version +void LD2450Component::get_version_() { this->send_command_(CMD_VERSION, nullptr, 0); } + +// Get LD2450 mac address +void LD2450Component::get_mac_() { + uint8_t cmd_value[2] = {0x01, 0x00}; + this->send_command_(CMD_MAC, cmd_value, 2); +} + +// Query for target tracking mode +void LD2450Component::query_target_tracking_mode_() { this->send_command_(CMD_QUERY_TARGET_MODE, nullptr, 0); } + +// Query for zone info +void LD2450Component::query_zone_() { this->send_command_(CMD_QUERY_ZONE, nullptr, 0); } + +#ifdef USE_SENSOR +void LD2450Component::set_move_x_sensor(uint8_t target, sensor::Sensor *s) { this->move_x_sensors_[target] = s; } +void LD2450Component::set_move_y_sensor(uint8_t target, sensor::Sensor *s) { this->move_y_sensors_[target] = s; } +void LD2450Component::set_move_speed_sensor(uint8_t target, sensor::Sensor *s) { + this->move_speed_sensors_[target] = s; +} +void LD2450Component::set_move_angle_sensor(uint8_t target, sensor::Sensor *s) { + this->move_angle_sensors_[target] = s; +} +void LD2450Component::set_move_distance_sensor(uint8_t target, sensor::Sensor *s) { + this->move_distance_sensors_[target] = s; +} +void LD2450Component::set_move_resolution_sensor(uint8_t target, sensor::Sensor *s) { + this->move_resolution_sensors_[target] = s; +} +void LD2450Component::set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s) { + this->zone_target_count_sensors_[zone] = s; +} +void LD2450Component::set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s) { + this->zone_still_target_count_sensors_[zone] = s; +} +void LD2450Component::set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s) { + this->zone_moving_target_count_sensors_[zone] = s; +} +#endif +#ifdef USE_TEXT_SENSOR +void LD2450Component::set_direction_text_sensor(uint8_t target, text_sensor::TextSensor *s) { + this->direction_text_sensors_[target] = s; +} +#endif + +// Send Zone coordinates data to LD2450 +#ifdef USE_NUMBER +void LD2450Component::set_zone_coordinate(uint8_t zone) { + number::Number *x1sens = this->zone_numbers_[zone].x1; + number::Number *y1sens = this->zone_numbers_[zone].y1; + number::Number *x2sens = this->zone_numbers_[zone].x2; + number::Number *y2sens = this->zone_numbers_[zone].y2; + if (!x1sens->has_state() || !y1sens->has_state() || !x2sens->has_state() || !y2sens->has_state()) { + return; + } + this->zone_config_[zone].x1 = static_cast(x1sens->state); + this->zone_config_[zone].y1 = static_cast(y1sens->state); + this->zone_config_[zone].x2 = static_cast(x2sens->state); + this->zone_config_[zone].y2 = static_cast(y2sens->state); + this->send_set_zone_command_(); +} + +void LD2450Component::set_zone_numbers(uint8_t zone, number::Number *x1, number::Number *y1, number::Number *x2, + number::Number *y2) { + if (zone < MAX_ZONES) { + this->zone_numbers_[zone].x1 = x1; + this->zone_numbers_[zone].y1 = y1; + this->zone_numbers_[zone].x2 = x2; + this->zone_numbers_[zone].y2 = y2; + } +} +#endif + +// Set Presence Timeout load and save from flash +#ifdef USE_NUMBER +void LD2450Component::set_presence_timeout() { + if (this->presence_timeout_number_ != nullptr) { + if (this->presence_timeout_number_->state == 0) { + float timeout = this->restore_from_flash_(); + this->presence_timeout_number_->publish_state(timeout); + this->timeout_ = ld2450::convert_seconds_to_ms(timeout); + } + if (this->presence_timeout_number_->has_state()) { + this->save_to_flash_(this->presence_timeout_number_->state); + this->timeout_ = ld2450::convert_seconds_to_ms(this->presence_timeout_number_->state); + } + } +} + +// Save Presence Timeout to flash +void LD2450Component::save_to_flash_(float value) { this->pref_.save(&value); } + +// Load Presence Timeout from flash +float LD2450Component::restore_from_flash_() { + float value; + if (!this->pref_.load(&value)) { + value = DEFAULT_PRESENCE_TIMEOUT; + } + return value; +} +#endif + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/ld2450.h b/esphome/components/ld2450/ld2450.h new file mode 100644 index 0000000000..32e4bc02e4 --- /dev/null +++ b/esphome/components/ld2450/ld2450.h @@ -0,0 +1,234 @@ +#pragma once + +#include +#include +#include "esphome/components/uart/uart.h" +#include "esphome/core/component.h" +#include "esphome/core/defines.h" +#include "esphome/core/helpers.h" +#include "esphome/core/preferences.h" +#ifdef USE_SENSOR +#include "esphome/components/sensor/sensor.h" +#endif +#ifdef USE_NUMBER +#include "esphome/components/number/number.h" +#endif +#ifdef USE_SWITCH +#include "esphome/components/switch/switch.h" +#endif +#ifdef USE_BUTTON +#include "esphome/components/button/button.h" +#endif +#ifdef USE_SELECT +#include "esphome/components/select/select.h" +#endif +#ifdef USE_TEXT_SENSOR +#include "esphome/components/text_sensor/text_sensor.h" +#endif +#ifdef USE_BINARY_SENSOR +#include "esphome/components/binary_sensor/binary_sensor.h" +#endif + +#ifndef M_PI +#define M_PI 3.14 +#endif + +namespace esphome { +namespace ld2450 { + +// Constants +static const uint8_t DEFAULT_PRESENCE_TIMEOUT = 5; // Timeout to reset presense status 5 sec. +static const uint8_t MAX_LINE_LENGTH = 60; // Max characters for serial buffer +static const uint8_t MAX_TARGETS = 3; // Max 3 Targets in LD2450 +static const uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450 + +// Target coordinate struct +struct Target { + int16_t x; + int16_t y; + bool is_moving; +}; + +// Zone coordinate struct +struct Zone { + int16_t x1 = 0; + int16_t y1 = 0; + int16_t x2 = 0; + int16_t y2 = 0; +}; + +#ifdef USE_NUMBER +struct ZoneOfNumbers { + number::Number *x1 = nullptr; + number::Number *y1 = nullptr; + number::Number *x2 = nullptr; + number::Number *y2 = nullptr; +}; +#endif + +enum BaudRateStructure : uint8_t { + BAUD_RATE_9600 = 1, + BAUD_RATE_19200 = 2, + BAUD_RATE_38400 = 3, + BAUD_RATE_57600 = 4, + BAUD_RATE_115200 = 5, + BAUD_RATE_230400 = 6, + BAUD_RATE_256000 = 7, + BAUD_RATE_460800 = 8 +}; + +// Convert baud rate enum to int +static const std::map BAUD_RATE_ENUM_TO_INT{ + {"9600", BAUD_RATE_9600}, {"19200", BAUD_RATE_19200}, {"38400", BAUD_RATE_38400}, + {"57600", BAUD_RATE_57600}, {"115200", BAUD_RATE_115200}, {"230400", BAUD_RATE_230400}, + {"256000", BAUD_RATE_256000}, {"460800", BAUD_RATE_460800}}; + +// Zone type struct +enum ZoneTypeStructure : uint8_t { ZONE_DISABLED = 0, ZONE_DETECTION = 1, ZONE_FILTER = 2 }; + +// Convert zone type int to enum +static const std::map ZONE_TYPE_INT_TO_ENUM{ + {ZONE_DISABLED, "Disabled"}, {ZONE_DETECTION, "Detection"}, {ZONE_FILTER, "Filter"}}; + +// Convert zone type enum to int +static const std::map ZONE_TYPE_ENUM_TO_INT{ + {"Disabled", ZONE_DISABLED}, {"Detection", ZONE_DETECTION}, {"Filter", ZONE_FILTER}}; + +// LD2450 serial command header & footer +static const uint8_t CMD_FRAME_HEADER[4] = {0xFD, 0xFC, 0xFB, 0xFA}; +static const uint8_t CMD_FRAME_END[4] = {0x04, 0x03, 0x02, 0x01}; + +enum PeriodicDataStructure : uint8_t { + TARGET_X = 4, + TARGET_Y = 6, + TARGET_SPEED = 8, + TARGET_RESOLUTION = 10, +}; + +enum PeriodicDataValue : uint8_t { HEAD = 0XAA, END = 0x55, CHECK = 0x00 }; + +enum AckDataStructure : uint8_t { COMMAND = 6, COMMAND_STATUS = 7 }; + +class LD2450Component : public Component, public uart::UARTDevice { +#ifdef USE_SENSOR + SUB_SENSOR(target_count) + SUB_SENSOR(still_target_count) + SUB_SENSOR(moving_target_count) +#endif +#ifdef USE_BINARY_SENSOR + SUB_BINARY_SENSOR(target) + SUB_BINARY_SENSOR(moving_target) + SUB_BINARY_SENSOR(still_target) +#endif +#ifdef USE_TEXT_SENSOR + SUB_TEXT_SENSOR(version) + SUB_TEXT_SENSOR(mac) +#endif +#ifdef USE_SELECT + SUB_SELECT(baud_rate) + SUB_SELECT(zone_type) +#endif +#ifdef USE_SWITCH + SUB_SWITCH(bluetooth) + SUB_SWITCH(multi_target) +#endif +#ifdef USE_BUTTON + SUB_BUTTON(reset) + SUB_BUTTON(restart) +#endif +#ifdef USE_NUMBER + SUB_NUMBER(presence_timeout) +#endif + + public: + LD2450Component(); + void setup() override; + void dump_config() override; + void loop() override; + void set_presence_timeout(); + void set_throttle(uint16_t value) { this->throttle_ = value; }; + void read_all_info(); + void query_zone_info(); + void restart_and_read_all_info(); + void set_bluetooth(bool enable); + void set_multi_target(bool enable); + void set_baud_rate(const std::string &state); + void set_zone_type(const std::string &state); + void publish_zone_type(); + void factory_reset(); +#ifdef USE_TEXT_SENSOR + void set_direction_text_sensor(uint8_t target, text_sensor::TextSensor *s); +#endif +#ifdef USE_NUMBER + void set_zone_coordinate(uint8_t zone); + void set_zone_numbers(uint8_t zone, number::Number *x1, number::Number *y1, number::Number *x2, number::Number *y2); +#endif +#ifdef USE_SENSOR + void set_move_x_sensor(uint8_t target, sensor::Sensor *s); + void set_move_y_sensor(uint8_t target, sensor::Sensor *s); + void set_move_speed_sensor(uint8_t target, sensor::Sensor *s); + void set_move_angle_sensor(uint8_t target, sensor::Sensor *s); + void set_move_distance_sensor(uint8_t target, sensor::Sensor *s); + void set_move_resolution_sensor(uint8_t target, sensor::Sensor *s); + void set_zone_target_count_sensor(uint8_t zone, sensor::Sensor *s); + void set_zone_still_target_count_sensor(uint8_t zone, sensor::Sensor *s); + void set_zone_moving_target_count_sensor(uint8_t zone, sensor::Sensor *s); +#endif + void reset_radar_zone(); + void set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_t zone1_y1, int32_t zone1_x2, int32_t zone1_y2, + int32_t zone2_x1, int32_t zone2_y1, int32_t zone2_x2, int32_t zone2_y2, int32_t zone3_x1, + int32_t zone3_y1, int32_t zone3_x2, int32_t zone3_y2); + + protected: + void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len); + void set_config_mode_(bool enable); + void handle_periodic_data_(uint8_t *buffer, uint8_t len); + bool handle_ack_data_(uint8_t *buffer, uint8_t len); + void process_zone_(uint8_t *buffer); + void readline_(int readch, uint8_t *buffer, uint8_t len); + void get_version_(); + void get_mac_(); + void query_target_tracking_mode_(); + void query_zone_(); + void restart_(); + void send_set_zone_command_(); + void save_to_flash_(float value); + float restore_from_flash_(); + bool get_timeout_status_(uint32_t check_millis); + uint8_t count_targets_in_zone_(const Zone &zone, bool is_moving); + + Target target_info_[MAX_TARGETS]; + Zone zone_config_[MAX_ZONES]; + uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer + uint8_t buffer_data_[MAX_LINE_LENGTH]; + uint32_t last_periodic_millis_ = 0; + uint32_t presence_millis_ = 0; + uint32_t still_presence_millis_ = 0; + uint32_t moving_presence_millis_ = 0; + uint16_t throttle_ = 0; + uint16_t timeout_ = 5; + uint8_t zone_type_ = 0; + std::string version_{}; + std::string mac_{}; +#ifdef USE_NUMBER + ESPPreferenceObject pref_; // only used when numbers are in use + ZoneOfNumbers zone_numbers_[MAX_ZONES]; +#endif +#ifdef USE_SENSOR + std::vector move_x_sensors_ = std::vector(MAX_TARGETS); + std::vector move_y_sensors_ = std::vector(MAX_TARGETS); + std::vector move_speed_sensors_ = std::vector(MAX_TARGETS); + std::vector move_angle_sensors_ = std::vector(MAX_TARGETS); + std::vector move_distance_sensors_ = std::vector(MAX_TARGETS); + std::vector move_resolution_sensors_ = std::vector(MAX_TARGETS); + std::vector zone_target_count_sensors_ = std::vector(MAX_ZONES); + std::vector zone_still_target_count_sensors_ = std::vector(MAX_ZONES); + std::vector zone_moving_target_count_sensors_ = std::vector(MAX_ZONES); +#endif +#ifdef USE_TEXT_SENSOR + std::vector direction_text_sensors_ = std::vector(3); +#endif +}; + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/number/__init__.py b/esphome/components/ld2450/number/__init__.py new file mode 100644 index 0000000000..d2098f6131 --- /dev/null +++ b/esphome/components/ld2450/number/__init__.py @@ -0,0 +1,121 @@ +import esphome.codegen as cg +from esphome.components import number +import esphome.config_validation as cv +from esphome.const import ( + CONF_ID, + DEVICE_CLASS_DISTANCE, + ENTITY_CATEGORY_CONFIG, + ICON_TIMELAPSE, + UNIT_MILLIMETER, + UNIT_SECOND, +) + +from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns + +CONF_PRESENCE_TIMEOUT = "presence_timeout" +CONF_X1 = "x1" +CONF_X2 = "x2" +CONF_Y1 = "y1" +CONF_Y2 = "y2" +ICON_ARROW_BOTTOM_RIGHT = "mdi:arrow-bottom-right" +ICON_ARROW_BOTTOM_RIGHT_BOLD_BOX_OUTLINE = "mdi:arrow-bottom-right-bold-box-outline" +ICON_ARROW_TOP_LEFT = "mdi:arrow-top-left" +ICON_ARROW_TOP_LEFT_BOLD_BOX_OUTLINE = "mdi:arrow-top-left-bold-box-outline" +MAX_ZONES = 3 + +PresenceTimeoutNumber = ld2450_ns.class_("PresenceTimeoutNumber", number.Number) +ZoneCoordinateNumber = ld2450_ns.class_("ZoneCoordinateNumber", number.Number) + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), + cv.Required(CONF_PRESENCE_TIMEOUT): number.number_schema( + PresenceTimeoutNumber, + unit_of_measurement=UNIT_SECOND, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_TIMELAPSE, + ), + } +) + +CONFIG_SCHEMA = CONFIG_SCHEMA.extend( + { + cv.Optional(f"zone_{n + 1}"): cv.Schema( + { + cv.Required(CONF_X1): number.number_schema( + ZoneCoordinateNumber, + device_class=DEVICE_CLASS_DISTANCE, + unit_of_measurement=UNIT_MILLIMETER, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_ARROW_TOP_LEFT_BOLD_BOX_OUTLINE, + ), + cv.Required(CONF_Y1): number.number_schema( + ZoneCoordinateNumber, + device_class=DEVICE_CLASS_DISTANCE, + unit_of_measurement=UNIT_MILLIMETER, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_ARROW_TOP_LEFT, + ), + cv.Required(CONF_X2): number.number_schema( + ZoneCoordinateNumber, + device_class=DEVICE_CLASS_DISTANCE, + unit_of_measurement=UNIT_MILLIMETER, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_ARROW_BOTTOM_RIGHT_BOLD_BOX_OUTLINE, + ), + cv.Required(CONF_Y2): number.number_schema( + ZoneCoordinateNumber, + device_class=DEVICE_CLASS_DISTANCE, + unit_of_measurement=UNIT_MILLIMETER, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_ARROW_BOTTOM_RIGHT, + ), + } + ) + for n in range(MAX_ZONES) + } +) + + +async def to_code(config): + ld2450_component = await cg.get_variable(config[CONF_LD2450_ID]) + if presence_timeout_config := config.get(CONF_PRESENCE_TIMEOUT): + n = await number.new_number( + presence_timeout_config, + min_value=0, + max_value=3600, + step=1, + ) + await cg.register_parented(n, config[CONF_LD2450_ID]) + cg.add(ld2450_component.set_presence_timeout_number(n)) + for zone_num in range(MAX_ZONES): + if zone_conf := config.get(f"zone_{zone_num + 1}"): + zone_x1_config = zone_conf.get(CONF_X1) + x1 = cg.new_Pvariable(zone_x1_config[CONF_ID], zone_num) + await number.register_number( + x1, zone_x1_config, min_value=-4860, max_value=4860, step=1 + ) + await cg.register_parented(x1, config[CONF_LD2450_ID]) + + zone_y1_config = zone_conf.get(CONF_Y1) + y1 = cg.new_Pvariable(zone_y1_config[CONF_ID], zone_num) + await number.register_number( + y1, zone_y1_config, min_value=0, max_value=7560, step=1 + ) + await cg.register_parented(y1, config[CONF_LD2450_ID]) + + zone_x2_config = zone_conf.get(CONF_X2) + x2 = cg.new_Pvariable(zone_x2_config[CONF_ID], zone_num) + await number.register_number( + x2, zone_x2_config, min_value=-4860, max_value=4860, step=1 + ) + await cg.register_parented(x2, config[CONF_LD2450_ID]) + + zone_y2_config = zone_conf.get(CONF_Y2) + y2 = cg.new_Pvariable(zone_y2_config[CONF_ID], zone_num) + await number.register_number( + y2, zone_y2_config, min_value=0, max_value=7560, step=1 + ) + await cg.register_parented(y2, config[CONF_LD2450_ID]) + + cg.add(ld2450_component.set_zone_numbers(zone_num, x1, y1, x2, y2)) diff --git a/esphome/components/ld2450/number/presence_timeout_number.cpp b/esphome/components/ld2450/number/presence_timeout_number.cpp new file mode 100644 index 0000000000..ecfe71f484 --- /dev/null +++ b/esphome/components/ld2450/number/presence_timeout_number.cpp @@ -0,0 +1,12 @@ +#include "presence_timeout_number.h" + +namespace esphome { +namespace ld2450 { + +void PresenceTimeoutNumber::control(float value) { + this->publish_state(value); + this->parent_->set_presence_timeout(); +} + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/number/presence_timeout_number.h b/esphome/components/ld2450/number/presence_timeout_number.h new file mode 100644 index 0000000000..b18699792f --- /dev/null +++ b/esphome/components/ld2450/number/presence_timeout_number.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/number/number.h" +#include "../ld2450.h" + +namespace esphome { +namespace ld2450 { + +class PresenceTimeoutNumber : public number::Number, public Parented { + public: + PresenceTimeoutNumber() = default; + + protected: + void control(float value) override; +}; + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/number/zone_coordinate_number.cpp b/esphome/components/ld2450/number/zone_coordinate_number.cpp new file mode 100644 index 0000000000..5338d7e5ee --- /dev/null +++ b/esphome/components/ld2450/number/zone_coordinate_number.cpp @@ -0,0 +1,14 @@ +#include "zone_coordinate_number.h" + +namespace esphome { +namespace ld2450 { + +ZoneCoordinateNumber::ZoneCoordinateNumber(uint8_t zone) : zone_(zone) {} + +void ZoneCoordinateNumber::control(float value) { + this->publish_state(value); + this->parent_->set_zone_coordinate(this->zone_); +} + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/number/zone_coordinate_number.h b/esphome/components/ld2450/number/zone_coordinate_number.h new file mode 100644 index 0000000000..72b83889c4 --- /dev/null +++ b/esphome/components/ld2450/number/zone_coordinate_number.h @@ -0,0 +1,19 @@ +#pragma once + +#include "esphome/components/number/number.h" +#include "../ld2450.h" + +namespace esphome { +namespace ld2450 { + +class ZoneCoordinateNumber : public number::Number, public Parented { + public: + ZoneCoordinateNumber(uint8_t zone); + + protected: + uint8_t zone_; + void control(float value) override; +}; + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/select/__init__.py b/esphome/components/ld2450/select/__init__.py new file mode 100644 index 0000000000..25dd819637 --- /dev/null +++ b/esphome/components/ld2450/select/__init__.py @@ -0,0 +1,56 @@ +import esphome.codegen as cg +from esphome.components import select +import esphome.config_validation as cv +from esphome.const import CONF_BAUD_RATE, ENTITY_CATEGORY_CONFIG, ICON_THERMOMETER + +from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns + +CONF_ZONE_TYPE = "zone_type" + +BaudRateSelect = ld2450_ns.class_("BaudRateSelect", select.Select) +ZoneTypeSelect = ld2450_ns.class_("ZoneTypeSelect", select.Select) + +CONFIG_SCHEMA = { + cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), + cv.Optional(CONF_BAUD_RATE): select.select_schema( + BaudRateSelect, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_THERMOMETER, + ), + cv.Optional(CONF_ZONE_TYPE): select.select_schema( + ZoneTypeSelect, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_THERMOMETER, + ), +} + + +async def to_code(config): + ld2450_component = await cg.get_variable(config[CONF_LD2450_ID]) + if baud_rate_config := config.get(CONF_BAUD_RATE): + s = await select.new_select( + baud_rate_config, + options=[ + "9600", + "19200", + "38400", + "57600", + "115200", + "230400", + "256000", + "460800", + ], + ) + await cg.register_parented(s, config[CONF_LD2450_ID]) + cg.add(ld2450_component.set_baud_rate_select(s)) + if zone_type_config := config.get(CONF_ZONE_TYPE): + s = await select.new_select( + zone_type_config, + options=[ + "Disabled", + "Detection", + "Filter", + ], + ) + await cg.register_parented(s, config[CONF_LD2450_ID]) + cg.add(ld2450_component.set_zone_type_select(s)) diff --git a/esphome/components/ld2450/select/baud_rate_select.cpp b/esphome/components/ld2450/select/baud_rate_select.cpp new file mode 100644 index 0000000000..06439aaa75 --- /dev/null +++ b/esphome/components/ld2450/select/baud_rate_select.cpp @@ -0,0 +1,12 @@ +#include "baud_rate_select.h" + +namespace esphome { +namespace ld2450 { + +void BaudRateSelect::control(const std::string &value) { + this->publish_state(value); + this->parent_->set_baud_rate(state); +} + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/select/baud_rate_select.h b/esphome/components/ld2450/select/baud_rate_select.h new file mode 100644 index 0000000000..04fe65b4fd --- /dev/null +++ b/esphome/components/ld2450/select/baud_rate_select.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/select/select.h" +#include "../ld2450.h" + +namespace esphome { +namespace ld2450 { + +class BaudRateSelect : public select::Select, public Parented { + public: + BaudRateSelect() = default; + + protected: + void control(const std::string &value) override; +}; + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/select/zone_type_select.cpp b/esphome/components/ld2450/select/zone_type_select.cpp new file mode 100644 index 0000000000..a9f6155142 --- /dev/null +++ b/esphome/components/ld2450/select/zone_type_select.cpp @@ -0,0 +1,12 @@ +#include "zone_type_select.h" + +namespace esphome { +namespace ld2450 { + +void ZoneTypeSelect::control(const std::string &value) { + this->publish_state(value); + this->parent_->set_zone_type(state); +} + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/select/zone_type_select.h b/esphome/components/ld2450/select/zone_type_select.h new file mode 100644 index 0000000000..8aafeb6beb --- /dev/null +++ b/esphome/components/ld2450/select/zone_type_select.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/select/select.h" +#include "../ld2450.h" + +namespace esphome { +namespace ld2450 { + +class ZoneTypeSelect : public select::Select, public Parented { + public: + ZoneTypeSelect() = default; + + protected: + void control(const std::string &value) override; +}; + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/sensor.py b/esphome/components/ld2450/sensor.py new file mode 100644 index 0000000000..21580c5801 --- /dev/null +++ b/esphome/components/ld2450/sensor.py @@ -0,0 +1,156 @@ +import esphome.codegen as cg +from esphome.components import sensor +import esphome.config_validation as cv +from esphome.const import ( + CONF_ANGLE, + CONF_DISTANCE, + CONF_RESOLUTION, + CONF_SPEED, + DEVICE_CLASS_DISTANCE, + DEVICE_CLASS_SPEED, + UNIT_DEGREES, + UNIT_MILLIMETER, +) + +from . import CONF_LD2450_ID, LD2450Component + +DEPENDENCIES = ["ld2450"] + +CONF_MOVING_TARGET_COUNT = "moving_target_count" +CONF_STILL_TARGET_COUNT = "still_target_count" +CONF_TARGET_COUNT = "target_count" +CONF_X = "x" +CONF_Y = "y" + +ICON_ACCOUNT_GROUP = "mdi:account-group" +ICON_ACCOUNT_SWITCH = "mdi:account-switch" +ICON_ALPHA_X_BOX_OUTLINE = "mdi:alpha-x-box-outline" +ICON_ALPHA_Y_BOX_OUTLINE = "mdi:alpha-y-box-outline" +ICON_FORMAT_TEXT_ROTATION_ANGLE_UP = "mdi:format-text-rotation-angle-up" +ICON_HUMAN_GREETING_PROXIMITY = "mdi:human-greeting-proximity" +ICON_MAP_MARKER_ACCOUNT = "mdi:map-marker-account" +ICON_MAP_MARKER_DISTANCE = "mdi:map-marker-distance" +ICON_RELATION_ZERO_OR_ONE_TO_ZERO_OR_ONE = "mdi:relation-zero-or-one-to-zero-or-one" +ICON_SPEEDOMETER_SLOW = "mdi:speedometer-slow" + +MAX_TARGETS = 3 +MAX_ZONES = 3 + +UNIT_MILLIMETER_PER_SECOND = "mm/s" + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), + cv.Optional(CONF_TARGET_COUNT): sensor.sensor_schema( + icon=ICON_ACCOUNT_GROUP, + ), + cv.Optional(CONF_STILL_TARGET_COUNT): sensor.sensor_schema( + icon=ICON_HUMAN_GREETING_PROXIMITY, + ), + cv.Optional(CONF_MOVING_TARGET_COUNT): sensor.sensor_schema( + icon=ICON_ACCOUNT_SWITCH, + ), + } +) + +CONFIG_SCHEMA = CONFIG_SCHEMA.extend( + { + cv.Optional(f"target_{n + 1}"): cv.Schema( + { + cv.Optional(CONF_X): sensor.sensor_schema( + device_class=DEVICE_CLASS_DISTANCE, + unit_of_measurement=UNIT_MILLIMETER, + icon=ICON_ALPHA_X_BOX_OUTLINE, + ), + cv.Optional(CONF_Y): sensor.sensor_schema( + device_class=DEVICE_CLASS_DISTANCE, + unit_of_measurement=UNIT_MILLIMETER, + icon=ICON_ALPHA_Y_BOX_OUTLINE, + ), + cv.Optional(CONF_SPEED): sensor.sensor_schema( + device_class=DEVICE_CLASS_SPEED, + unit_of_measurement=UNIT_MILLIMETER_PER_SECOND, + icon=ICON_SPEEDOMETER_SLOW, + ), + cv.Optional(CONF_ANGLE): sensor.sensor_schema( + unit_of_measurement=UNIT_DEGREES, + icon=ICON_FORMAT_TEXT_ROTATION_ANGLE_UP, + ), + cv.Optional(CONF_DISTANCE): sensor.sensor_schema( + device_class=DEVICE_CLASS_DISTANCE, + unit_of_measurement=UNIT_MILLIMETER, + icon=ICON_MAP_MARKER_DISTANCE, + ), + cv.Optional(CONF_RESOLUTION): sensor.sensor_schema( + device_class=DEVICE_CLASS_DISTANCE, + unit_of_measurement=UNIT_MILLIMETER, + icon=ICON_RELATION_ZERO_OR_ONE_TO_ZERO_OR_ONE, + ), + } + ) + for n in range(MAX_TARGETS) + }, + { + cv.Optional(f"zone_{n + 1}"): cv.Schema( + { + cv.Optional(CONF_TARGET_COUNT): sensor.sensor_schema( + icon=ICON_MAP_MARKER_ACCOUNT, + ), + cv.Optional(CONF_STILL_TARGET_COUNT): sensor.sensor_schema( + icon=ICON_MAP_MARKER_ACCOUNT, + ), + cv.Optional(CONF_MOVING_TARGET_COUNT): sensor.sensor_schema( + icon=ICON_MAP_MARKER_ACCOUNT, + ), + } + ) + for n in range(MAX_ZONES) + }, +) + + +async def to_code(config): + ld2450_component = await cg.get_variable(config[CONF_LD2450_ID]) + + if target_count_config := config.get(CONF_TARGET_COUNT): + sens = await sensor.new_sensor(target_count_config) + cg.add(ld2450_component.set_target_count_sensor(sens)) + + if still_target_count_config := config.get(CONF_STILL_TARGET_COUNT): + sens = await sensor.new_sensor(still_target_count_config) + cg.add(ld2450_component.set_still_target_count_sensor(sens)) + + if moving_target_count_config := config.get(CONF_MOVING_TARGET_COUNT): + sens = await sensor.new_sensor(moving_target_count_config) + cg.add(ld2450_component.set_moving_target_count_sensor(sens)) + for n in range(MAX_TARGETS): + if target_conf := config.get(f"target_{n + 1}"): + if x_config := target_conf.get(CONF_X): + sens = await sensor.new_sensor(x_config) + cg.add(ld2450_component.set_move_x_sensor(n, sens)) + if y_config := target_conf.get(CONF_Y): + sens = await sensor.new_sensor(y_config) + cg.add(ld2450_component.set_move_y_sensor(n, sens)) + if speed_config := target_conf.get(CONF_SPEED): + sens = await sensor.new_sensor(speed_config) + cg.add(ld2450_component.set_move_speed_sensor(n, sens)) + if angle_config := target_conf.get(CONF_ANGLE): + sens = await sensor.new_sensor(angle_config) + cg.add(ld2450_component.set_move_angle_sensor(n, sens)) + if distance_config := target_conf.get(CONF_DISTANCE): + sens = await sensor.new_sensor(distance_config) + cg.add(ld2450_component.set_move_distance_sensor(n, sens)) + if resolution_config := target_conf.get(CONF_RESOLUTION): + sens = await sensor.new_sensor(resolution_config) + cg.add(ld2450_component.set_move_resolution_sensor(n, sens)) + for n in range(MAX_ZONES): + if zone_config := config.get(f"zone_{n + 1}"): + if target_count_config := zone_config.get(CONF_TARGET_COUNT): + sens = await sensor.new_sensor(target_count_config) + cg.add(ld2450_component.set_zone_target_count_sensor(n, sens)) + if still_target_count_config := zone_config.get(CONF_STILL_TARGET_COUNT): + sens = await sensor.new_sensor(still_target_count_config) + cg.add(ld2450_component.set_zone_still_target_count_sensor(n, sens)) + if moving_target_count_config := zone_config.get(CONF_MOVING_TARGET_COUNT): + sens = await sensor.new_sensor(moving_target_count_config) + cg.add(ld2450_component.set_zone_moving_target_count_sensor(n, sens)) diff --git a/esphome/components/ld2450/switch/__init__.py b/esphome/components/ld2450/switch/__init__.py new file mode 100644 index 0000000000..fb3969cf50 --- /dev/null +++ b/esphome/components/ld2450/switch/__init__.py @@ -0,0 +1,45 @@ +import esphome.codegen as cg +from esphome.components import switch +import esphome.config_validation as cv +from esphome.const import ( + DEVICE_CLASS_SWITCH, + ENTITY_CATEGORY_CONFIG, + ICON_BLUETOOTH, + ICON_PULSE, +) + +from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns + +BluetoothSwitch = ld2450_ns.class_("BluetoothSwitch", switch.Switch) +MultiTargetSwitch = ld2450_ns.class_("MultiTargetSwitch", switch.Switch) + +CONF_BLUETOOTH = "bluetooth" +CONF_MULTI_TARGET = "multi_target" + +CONFIG_SCHEMA = { + cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), + cv.Optional(CONF_BLUETOOTH): switch.switch_schema( + BluetoothSwitch, + device_class=DEVICE_CLASS_SWITCH, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_BLUETOOTH, + ), + cv.Optional(CONF_MULTI_TARGET): switch.switch_schema( + MultiTargetSwitch, + device_class=DEVICE_CLASS_SWITCH, + entity_category=ENTITY_CATEGORY_CONFIG, + icon=ICON_PULSE, + ), +} + + +async def to_code(config): + ld2450_component = await cg.get_variable(config[CONF_LD2450_ID]) + if bluetooth_config := config.get(CONF_BLUETOOTH): + s = await switch.new_switch(bluetooth_config) + await cg.register_parented(s, config[CONF_LD2450_ID]) + cg.add(ld2450_component.set_bluetooth_switch(s)) + if multi_target_config := config.get(CONF_MULTI_TARGET): + s = await switch.new_switch(multi_target_config) + await cg.register_parented(s, config[CONF_LD2450_ID]) + cg.add(ld2450_component.set_multi_target_switch(s)) diff --git a/esphome/components/ld2450/switch/bluetooth_switch.cpp b/esphome/components/ld2450/switch/bluetooth_switch.cpp new file mode 100644 index 0000000000..fa0d4fb06a --- /dev/null +++ b/esphome/components/ld2450/switch/bluetooth_switch.cpp @@ -0,0 +1,12 @@ +#include "bluetooth_switch.h" + +namespace esphome { +namespace ld2450 { + +void BluetoothSwitch::write_state(bool state) { + this->publish_state(state); + this->parent_->set_bluetooth(state); +} + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/switch/bluetooth_switch.h b/esphome/components/ld2450/switch/bluetooth_switch.h new file mode 100644 index 0000000000..3c1c4f755c --- /dev/null +++ b/esphome/components/ld2450/switch/bluetooth_switch.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/switch/switch.h" +#include "../ld2450.h" + +namespace esphome { +namespace ld2450 { + +class BluetoothSwitch : public switch_::Switch, public Parented { + public: + BluetoothSwitch() = default; + + protected: + void write_state(bool state) override; +}; + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/switch/multi_target_switch.cpp b/esphome/components/ld2450/switch/multi_target_switch.cpp new file mode 100644 index 0000000000..a163e29fc5 --- /dev/null +++ b/esphome/components/ld2450/switch/multi_target_switch.cpp @@ -0,0 +1,12 @@ +#include "multi_target_switch.h" + +namespace esphome { +namespace ld2450 { + +void MultiTargetSwitch::write_state(bool state) { + this->publish_state(state); + this->parent_->set_multi_target(state); +} + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/switch/multi_target_switch.h b/esphome/components/ld2450/switch/multi_target_switch.h new file mode 100644 index 0000000000..ca6253588d --- /dev/null +++ b/esphome/components/ld2450/switch/multi_target_switch.h @@ -0,0 +1,18 @@ +#pragma once + +#include "esphome/components/switch/switch.h" +#include "../ld2450.h" + +namespace esphome { +namespace ld2450 { + +class MultiTargetSwitch : public switch_::Switch, public Parented { + public: + MultiTargetSwitch() = default; + + protected: + void write_state(bool state) override; +}; + +} // namespace ld2450 +} // namespace esphome diff --git a/esphome/components/ld2450/text_sensor.py b/esphome/components/ld2450/text_sensor.py new file mode 100644 index 0000000000..6c11024b89 --- /dev/null +++ b/esphome/components/ld2450/text_sensor.py @@ -0,0 +1,62 @@ +import esphome.codegen as cg +from esphome.components import text_sensor +import esphome.config_validation as cv +from esphome.const import ( + CONF_DIRECTION, + CONF_MAC_ADDRESS, + CONF_VERSION, + ENTITY_CATEGORY_DIAGNOSTIC, + ENTITY_CATEGORY_NONE, + ICON_BLUETOOTH, + ICON_CHIP, + ICON_SIGN_DIRECTION, +) + +from . import CONF_LD2450_ID, LD2450Component + +DEPENDENCIES = ["ld2450"] + +MAX_TARGETS = 3 + +CONFIG_SCHEMA = cv.Schema( + { + cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component), + cv.Optional(CONF_VERSION): text_sensor.text_sensor_schema( + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + icon=ICON_CHIP, + ), + cv.Optional(CONF_MAC_ADDRESS): text_sensor.text_sensor_schema( + entity_category=ENTITY_CATEGORY_DIAGNOSTIC, + icon=ICON_BLUETOOTH, + ), + } +) + +CONFIG_SCHEMA = CONFIG_SCHEMA.extend( + { + cv.Optional(f"target_{n + 1}"): cv.Schema( + { + cv.Optional(CONF_DIRECTION): text_sensor.text_sensor_schema( + entity_category=ENTITY_CATEGORY_NONE, + icon=ICON_SIGN_DIRECTION, + ), + } + ) + for n in range(MAX_TARGETS) + } +) + + +async def to_code(config): + ld2450_component = await cg.get_variable(config[CONF_LD2450_ID]) + if version_config := config.get(CONF_VERSION): + sens = await text_sensor.new_text_sensor(version_config) + cg.add(ld2450_component.set_version_text_sensor(sens)) + if mac_address_config := config.get(CONF_MAC_ADDRESS): + sens = await text_sensor.new_text_sensor(mac_address_config) + cg.add(ld2450_component.set_mac_text_sensor(sens)) + for n in range(MAX_TARGETS): + if direction_conf := config.get(f"target_{n + 1}"): + if direction_config := direction_conf.get(CONF_DIRECTION): + sens = await text_sensor.new_text_sensor(direction_config) + cg.add(ld2450_component.set_direction_text_sensor(n, sens)) diff --git a/esphome/components/lvgl/defines.py b/esphome/components/lvgl/defines.py index 119a358e1d..56a5a4b9e4 100644 --- a/esphome/components/lvgl/defines.py +++ b/esphome/components/lvgl/defines.py @@ -462,8 +462,6 @@ CONF_LVGL_ID = "lvgl_id" CONF_LONG_MODE = "long_mode" CONF_MSGBOXES = "msgboxes" CONF_OBJ = "obj" -CONF_OFFSET_X = "offset_x" -CONF_OFFSET_Y = "offset_y" CONF_ONE_CHECKED = "one_checked" CONF_ONE_LINE = "one_line" CONF_ON_PAUSE = "on_pause" diff --git a/esphome/components/lvgl/font.cpp b/esphome/components/lvgl/font.cpp index 9c172f07f5..a0d5127570 100644 --- a/esphome/components/lvgl/font.cpp +++ b/esphome/components/lvgl/font.cpp @@ -19,7 +19,7 @@ static bool get_glyph_dsc_cb(const lv_font_t *font, lv_font_glyph_dsc_t *dsc, ui const auto *gd = fe->get_glyph_data(unicode_letter); if (gd == nullptr) return false; - dsc->adv_w = gd->offset_x + gd->width; + dsc->adv_w = gd->advance; dsc->ofs_x = gd->offset_x; dsc->ofs_y = fe->height - gd->height - gd->offset_y - fe->baseline; dsc->box_w = gd->width; diff --git a/esphome/components/lvgl/lvgl_esphome.cpp b/esphome/components/lvgl/lvgl_esphome.cpp index a9fe56fb32..6030cb90f1 100644 --- a/esphome/components/lvgl/lvgl_esphome.cpp +++ b/esphome/components/lvgl/lvgl_esphome.cpp @@ -416,37 +416,45 @@ LvglComponent::LvglComponent(std::vector displays, float buf buffer_frac_(buffer_frac), full_refresh_(full_refresh), resume_on_input_(resume_on_input) { - auto *display = this->displays_[0]; - size_t buffer_pixels = display->get_width() * display->get_height() / this->buffer_frac_; - auto buf_bytes = buffer_pixels * LV_COLOR_DEPTH / 8; - this->rotation = display->get_rotation(); - if (this->rotation != display::DISPLAY_ROTATION_0_DEGREES) { - this->rotate_buf_ = static_cast(lv_custom_mem_alloc(buf_bytes)); // NOLINT - if (this->rotate_buf_ == nullptr) - return; - } - auto *buf = lv_custom_mem_alloc(buf_bytes); // NOLINT - if (buf == nullptr) - return; - lv_disp_draw_buf_init(&this->draw_buf_, buf, nullptr, buffer_pixels); + lv_disp_draw_buf_init(&this->draw_buf_, nullptr, nullptr, 0); lv_disp_drv_init(&this->disp_drv_); this->disp_drv_.draw_buf = &this->draw_buf_; this->disp_drv_.user_data = this; this->disp_drv_.full_refresh = this->full_refresh_; this->disp_drv_.flush_cb = static_flush_cb; this->disp_drv_.rounder_cb = rounder_cb; - this->disp_drv_.hor_res = (lv_coord_t) display->get_width(); - this->disp_drv_.ver_res = (lv_coord_t) display->get_height(); + this->disp_drv_.hor_res = 0; + this->disp_drv_.ver_res = 0; this->disp_ = lv_disp_drv_register(&this->disp_drv_); } void LvglComponent::setup() { - if (this->draw_buf_.buf1 == nullptr) { + ESP_LOGCONFIG(TAG, "LVGL Setup starts"); + auto *display = this->displays_[0]; + auto width = display->get_width(); + auto height = display->get_height(); + size_t buffer_pixels = width * height / this->buffer_frac_; + auto buf_bytes = buffer_pixels * LV_COLOR_DEPTH / 8; + auto *buffer = lv_custom_mem_alloc(buf_bytes); // NOLINT + if (buffer == nullptr) { this->mark_failed(); this->status_set_error("Memory allocation failure"); return; } - ESP_LOGCONFIG(TAG, "LVGL Setup starts"); + lv_disp_draw_buf_init(&this->draw_buf_, buffer, nullptr, buf_bytes); + this->disp_drv_.hor_res = width; + this->disp_drv_.ver_res = height; + // this->setup_driver_(display->get_width(), display->get_height()); + lv_disp_drv_update(this->disp_, &this->disp_drv_); + this->rotation = display->get_rotation(); + if (this->rotation != display::DISPLAY_ROTATION_0_DEGREES) { + this->rotate_buf_ = static_cast(lv_custom_mem_alloc(this->draw_buf_.size)); // NOLINT + if (this->rotate_buf_ == nullptr) { + this->mark_failed(); + this->status_set_error("Memory allocation failure"); + return; + } + } #if LV_USE_LOG lv_log_register_print_cb([](const char *buf) { auto next = strchr(buf, ')'); @@ -458,8 +466,8 @@ void LvglComponent::setup() { }); #endif // Rotation will be handled by our drawing function, so reset the display rotation. - for (auto *display : this->displays_) - display->set_rotation(display::DISPLAY_ROTATION_0_DEGREES); + for (auto *disp : this->displays_) + disp->set_rotation(display::DISPLAY_ROTATION_0_DEGREES); this->show_page(0, LV_SCR_LOAD_ANIM_NONE, 0); lv_disp_trig_activity(this->disp_); ESP_LOGCONFIG(TAG, "LVGL Setup complete"); diff --git a/esphome/components/lvgl/widgets/img.py b/esphome/components/lvgl/widgets/img.py index 46077190d0..d9de8d821a 100644 --- a/esphome/components/lvgl/widgets/img.py +++ b/esphome/components/lvgl/widgets/img.py @@ -1,11 +1,9 @@ import esphome.config_validation as cv -from esphome.const import CONF_ANGLE, CONF_MODE +from esphome.const import CONF_ANGLE, CONF_MODE, CONF_OFFSET_X, CONF_OFFSET_Y from ..defines import ( CONF_ANTIALIAS, CONF_MAIN, - CONF_OFFSET_X, - CONF_OFFSET_Y, CONF_PIVOT_X, CONF_PIVOT_Y, CONF_SRC, diff --git a/esphome/components/mcp2515/mcp2515.cpp b/esphome/components/mcp2515/mcp2515.cpp index fe4a68b583..23104f5aeb 100644 --- a/esphome/components/mcp2515/mcp2515.cpp +++ b/esphome/components/mcp2515/mcp2515.cpp @@ -550,6 +550,7 @@ canbus::Error MCP2515::set_bitrate_(canbus::CanSpeed can_speed, CanClock can_clo cfg3 = MCP_16MHZ_40KBPS_CFG3; break; case (canbus::CAN_50KBPS): // 50Kbps + cfg1 = MCP_16MHZ_50KBPS_CFG1; cfg2 = MCP_16MHZ_50KBPS_CFG2; cfg3 = MCP_16MHZ_50KBPS_CFG3; break; diff --git a/esphome/components/mdns/__init__.py b/esphome/components/mdns/__init__.py index 1bc290b582..9a198280dd 100644 --- a/esphome/components/mdns/__init__.py +++ b/esphome/components/mdns/__init__.py @@ -91,7 +91,7 @@ async def to_code(config): add_idf_component( name="mdns", repo="https://github.com/espressif/esp-protocols.git", - ref="mdns-v1.5.1", + ref="mdns-v1.8.0", path="components/mdns", ) diff --git a/esphome/components/mixer/speaker/mixer_speaker.cpp b/esphome/components/mixer/speaker/mixer_speaker.cpp index 60cff95eb2..121a62392c 100644 --- a/esphome/components/mixer/speaker/mixer_speaker.cpp +++ b/esphome/components/mixer/speaker/mixer_speaker.cpp @@ -177,11 +177,15 @@ void SourceSpeaker::set_mute_state(bool mute_state) { this->parent_->get_output_speaker()->set_mute_state(mute_state); } +bool SourceSpeaker::get_mute_state() { return this->parent_->get_output_speaker()->get_mute_state(); } + void SourceSpeaker::set_volume(float volume) { this->volume_ = volume; this->parent_->get_output_speaker()->set_volume(volume); } +float SourceSpeaker::get_volume() { return this->parent_->get_output_speaker()->get_volume(); } + size_t SourceSpeaker::process_data_from_source(TickType_t ticks_to_wait) { if (!this->transfer_buffer_.use_count()) { return 0; @@ -490,7 +494,8 @@ void MixerSpeaker::audio_mixer_task(void *params) { break; } - output_transfer_buffer->transfer_data_to_sink(pdMS_TO_TICKS(TASK_DELAY_MS)); + // Never shift the data in the output transfer buffer to avoid unnecessary, slow data moves + output_transfer_buffer->transfer_data_to_sink(pdMS_TO_TICKS(TASK_DELAY_MS), false); const uint32_t output_frames_free = this_mixer->audio_stream_info_.value().bytes_to_frames(output_transfer_buffer->free()); diff --git a/esphome/components/mixer/speaker/mixer_speaker.h b/esphome/components/mixer/speaker/mixer_speaker.h index b2cb3e1e39..0bd6b5f4c8 100644 --- a/esphome/components/mixer/speaker/mixer_speaker.h +++ b/esphome/components/mixer/speaker/mixer_speaker.h @@ -53,9 +53,11 @@ class SourceSpeaker : public speaker::Speaker, public Component { /// @brief Mute state changes are passed to the parent's output speaker void set_mute_state(bool mute_state) override; + bool get_mute_state() override; /// @brief Volume state changes are passed to the parent's output speaker void set_volume(float volume) override; + float get_volume() override; void set_pause_state(bool pause_state) override { this->pause_state_ = pause_state; } bool get_pause_state() const override { return this->pause_state_; } diff --git a/esphome/components/mlx90393/sensor.py b/esphome/components/mlx90393/sensor.py index fe01d8ebfc..cb9cb84aae 100644 --- a/esphome/components/mlx90393/sensor.py +++ b/esphome/components/mlx90393/sensor.py @@ -1,20 +1,21 @@ +from esphome import pins import esphome.codegen as cg -import esphome.config_validation as cv from esphome.components import i2c, sensor +import esphome.config_validation as cv from esphome.const import ( + CONF_FILTER, + CONF_GAIN, CONF_ID, - UNIT_MICROTESLA, - UNIT_CELSIUS, - STATE_CLASS_MEASUREMENT, + CONF_OVERSAMPLING, + CONF_RESOLUTION, + CONF_TEMPERATURE, + CONF_TEMPERATURE_COMPENSATION, ICON_MAGNET, ICON_THERMOMETER, - CONF_GAIN, - CONF_RESOLUTION, - CONF_OVERSAMPLING, - CONF_FILTER, - CONF_TEMPERATURE, + STATE_CLASS_MEASUREMENT, + UNIT_CELSIUS, + UNIT_MICROTESLA, ) -from esphome import pins CODEOWNERS = ["@functionpointer"] DEPENDENCIES = ["i2c"] @@ -26,30 +27,46 @@ MLX90393Component = mlx90393_ns.class_( ) GAIN = { - "1X": 7, - "1_33X": 6, - "1_67X": 5, - "2X": 4, - "2_5X": 3, - "3X": 2, - "4X": 1, - "5X": 0, + "1X": 0, + "1_25X": 1, + "1_67X": 2, + "2X": 3, + "2_5X": 4, + "3X": 5, + "3_75X": 6, + "5X": 7, } RESOLUTION = { - "16BIT": 0, - "17BIT": 1, - "18BIT": 2, - "19BIT": 3, + "DIV_8": 3, + "DIV_4": 2, + "DIV_2": 1, + "DIV_1": 0, } CONF_X_AXIS = "x_axis" CONF_Y_AXIS = "y_axis" CONF_Z_AXIS = "z_axis" CONF_DRDY_PIN = "drdy_pin" +CONF_HALLCONF = "hallconf" -def mlx90393_axis_schema(default_resolution: str): +def _validate(config): + if config[CONF_TEMPERATURE_COMPENSATION]: + for axis in [CONF_X_AXIS, CONF_Y_AXIS, CONF_Z_AXIS]: + if axis not in config: + continue + if (res := config[axis][CONF_RESOLUTION]) in [ + "DIV_8", + "DIV_4", + ]: + raise cv.Invalid( + f"{axis}: {CONF_RESOLUTION} cannot be {res} with {CONF_TEMPERATURE_COMPENSATION} enabled" + ) + return config + + +def mlx90393_axis_schema(): return sensor.sensor_schema( unit_of_measurement=UNIT_MICROTESLA, accuracy_decimals=0, @@ -58,7 +75,7 @@ def mlx90393_axis_schema(default_resolution: str): ).extend( cv.Schema( { - cv.Optional(CONF_RESOLUTION, default=default_resolution): cv.enum( + cv.Optional(CONF_RESOLUTION, default="DIV_4"): cv.enum( RESOLUTION, upper=True, space="_" ) } @@ -66,19 +83,19 @@ def mlx90393_axis_schema(default_resolution: str): ) -CONFIG_SCHEMA = ( +CONFIG_SCHEMA = cv.All( cv.Schema( { cv.GenerateID(): cv.declare_id(MLX90393Component), - cv.Optional(CONF_GAIN, default="2_5X"): cv.enum( - GAIN, upper=True, space="_" - ), + cv.Optional(CONF_GAIN, default="1X"): cv.enum(GAIN, upper=True, space="_"), cv.Optional(CONF_DRDY_PIN): pins.gpio_input_pin_schema, - cv.Optional(CONF_OVERSAMPLING, default=2): cv.int_range(min=0, max=3), + cv.Optional(CONF_OVERSAMPLING, default=0): cv.int_range(min=0, max=3), cv.Optional(CONF_FILTER, default=6): cv.int_range(min=0, max=7), - cv.Optional(CONF_X_AXIS): mlx90393_axis_schema("19BIT"), - cv.Optional(CONF_Y_AXIS): mlx90393_axis_schema("19BIT"), - cv.Optional(CONF_Z_AXIS): mlx90393_axis_schema("16BIT"), + cv.Optional(CONF_X_AXIS): mlx90393_axis_schema(), + cv.Optional(CONF_Y_AXIS): mlx90393_axis_schema(), + cv.Optional(CONF_Z_AXIS): mlx90393_axis_schema(), + cv.Optional(CONF_TEMPERATURE_COMPENSATION, default=False): bool, + cv.Optional(CONF_HALLCONF, default=0xC): cv.one_of(0xC, 0x0), cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema( unit_of_measurement=UNIT_CELSIUS, accuracy_decimals=1, @@ -96,7 +113,8 @@ CONFIG_SCHEMA = ( }, ) .extend(cv.polling_component_schema("60s")) - .extend(i2c.i2c_device_schema(0x0C)) + .extend(i2c.i2c_device_schema(0x0C)), + _validate, ) @@ -111,6 +129,8 @@ async def to_code(config): cg.add(var.set_gain(GAIN[config[CONF_GAIN]])) cg.add(var.set_oversampling(config[CONF_OVERSAMPLING])) cg.add(var.set_filter(config[CONF_FILTER])) + cg.add(var.set_temperature_compensation(config[CONF_TEMPERATURE_COMPENSATION])) + cg.add(var.set_hallconf(config[CONF_HALLCONF])) if CONF_X_AXIS in config: sens = await sensor.new_sensor(config[CONF_X_AXIS]) diff --git a/esphome/components/mlx90393/sensor_mlx90393.cpp b/esphome/components/mlx90393/sensor_mlx90393.cpp index d4431a7334..e86080fe9c 100644 --- a/esphome/components/mlx90393/sensor_mlx90393.cpp +++ b/esphome/components/mlx90393/sensor_mlx90393.cpp @@ -43,6 +43,10 @@ void MLX90393Cls::setup() { this->mlx_.setDigitalFiltering(this->filter_); this->mlx_.setTemperatureOverSampling(this->temperature_oversampling_); + + this->mlx_.setTemperatureCompensation(this->temperature_compensation_); + + this->mlx_.setHallConf(this->hallconf_); } void MLX90393Cls::dump_config() { diff --git a/esphome/components/mlx90393/sensor_mlx90393.h b/esphome/components/mlx90393/sensor_mlx90393.h index 8dfb7e6a13..479891a76c 100644 --- a/esphome/components/mlx90393/sensor_mlx90393.h +++ b/esphome/components/mlx90393/sensor_mlx90393.h @@ -29,7 +29,10 @@ class MLX90393Cls : public PollingComponent, public i2c::I2CDevice, public MLX90 void set_resolution(uint8_t xyz, uint8_t res) { resolutions_[xyz] = res; } void set_filter(uint8_t filter) { filter_ = filter; } void set_gain(uint8_t gain_sel) { gain_ = gain_sel; } - + void set_temperature_compensation(bool temperature_compensation) { + temperature_compensation_ = temperature_compensation; + } + void set_hallconf(uint8_t hallconf) { hallconf_ = hallconf; } // overrides for MLX library // disable lint because it keeps suggesting const uint8_t *response. @@ -49,9 +52,11 @@ class MLX90393Cls : public PollingComponent, public i2c::I2CDevice, public MLX90 sensor::Sensor *t_sensor_{nullptr}; uint8_t gain_; uint8_t oversampling_; - uint8_t temperature_oversampling_ = 0; + uint8_t temperature_oversampling_{0}; uint8_t filter_; - uint8_t resolutions_[3] = {0}; + uint8_t resolutions_[3]{0}; + bool temperature_compensation_{false}; + uint8_t hallconf_{0xC}; GPIOPin *drdy_pin_{nullptr}; }; diff --git a/esphome/components/mqtt/__init__.py b/esphome/components/mqtt/__init__.py index e1002478a1..99f8ad76d8 100644 --- a/esphome/components/mqtt/__init__.py +++ b/esphome/components/mqtt/__init__.py @@ -36,6 +36,7 @@ from esphome.const import ( CONF_PAYLOAD_AVAILABLE, CONF_PAYLOAD_NOT_AVAILABLE, CONF_PORT, + CONF_PUBLISH_NAN_AS_NONE, CONF_QOS, CONF_REBOOT_TIMEOUT, CONF_RETAIN, @@ -49,7 +50,6 @@ from esphome.const import ( CONF_USE_ABBREVIATIONS, CONF_USERNAME, CONF_WILL_MESSAGE, - CONF_PUBLISH_NAN_AS_NONE, PLATFORM_BK72XX, PLATFORM_ESP32, PLATFORM_ESP8266, @@ -406,7 +406,7 @@ async def to_code(config): if CONF_SSL_FINGERPRINTS in config: for fingerprint in config[CONF_SSL_FINGERPRINTS]: arr = [ - cg.RawExpression(f"0x{fingerprint[i:i + 2]}") for i in range(0, 40, 2) + cg.RawExpression(f"0x{fingerprint[i : i + 2]}") for i in range(0, 40, 2) ] cg.add(var.add_ssl_fingerprint(arr)) cg.add_build_flag("-DASYNC_TCP_SSL_ENABLED=1") diff --git a/esphome/components/msa3xx/__init__.py b/esphome/components/msa3xx/__init__.py new file mode 100644 index 0000000000..04514b584f --- /dev/null +++ b/esphome/components/msa3xx/__init__.py @@ -0,0 +1,189 @@ +from esphome import automation +import esphome.codegen as cg +from esphome.components import i2c +import esphome.config_validation as cv +from esphome.const import ( + CONF_CALIBRATION, + CONF_ID, + CONF_MIRROR_X, + CONF_MIRROR_Y, + CONF_OFFSET_X, + CONF_OFFSET_Y, + CONF_OFFSET_Z, + CONF_RANGE, + CONF_RESOLUTION, + CONF_SWAP_XY, + CONF_TRANSFORM, + CONF_TYPE, +) + +CODEOWNERS = ["@latonita"] +DEPENDENCIES = ["i2c"] + +MULTI_CONF = True + +CONF_MSA3XX_ID = "msa3xx_id" + +CONF_MIRROR_Z = "mirror_z" +CONF_ON_ACTIVE = "on_active" +CONF_ON_DOUBLE_TAP = "on_double_tap" +CONF_ON_FREEFALL = "on_freefall" +CONF_ON_ORIENTATION = "on_orientation" +CONF_ON_TAP = "on_tap" + +MODEL_MSA301 = "MSA301" +MODEL_MSA311 = "MSA311" + +msa3xx_ns = cg.esphome_ns.namespace("msa3xx") +MSA3xxComponent = msa3xx_ns.class_( + "MSA3xxComponent", cg.PollingComponent, i2c.I2CDevice +) + +MSAModels = msa3xx_ns.enum("Model", True) +MSA_MODELS = { + MODEL_MSA301: MSAModels.MSA301, + MODEL_MSA311: MSAModels.MSA311, +} + +MSARange = msa3xx_ns.enum("Range", True) +MSA_RANGES = { + "2G": MSARange.RANGE_2G, + "4G": MSARange.RANGE_4G, + "8G": MSARange.RANGE_8G, + "16G": MSARange.RANGE_16G, +} + +MSAResolution = msa3xx_ns.enum("Resolution", True) +RESOLUTIONS_MSA301 = { + 14: MSAResolution.RES_14BIT, + 12: MSAResolution.RES_12BIT, + 10: MSAResolution.RES_10BIT, + 8: MSAResolution.RES_8BIT, +} + +RESOLUTIONS_MSA311 = { + 12: MSAResolution.RES_12BIT, +} + +_COMMON_SCHEMA = cv.Schema( + { + cv.GenerateID(): cv.declare_id(MSA3xxComponent), + cv.Optional(CONF_RANGE, default="2G"): cv.enum(MSA_RANGES, upper=True), + cv.Optional(CONF_CALIBRATION): cv.Schema( + { + cv.Optional(CONF_OFFSET_X, default=0): cv.float_range( + min=-4.5, max=4.5 + ), + cv.Optional(CONF_OFFSET_Y, default=0): cv.float_range( + min=-4.5, max=4.5 + ), + cv.Optional(CONF_OFFSET_Z, default=0): cv.float_range( + min=-4.5, max=4.5 + ), + } + ), + cv.Optional(CONF_TRANSFORM): cv.Schema( + { + cv.Optional(CONF_MIRROR_X, default=False): cv.boolean, + cv.Optional(CONF_MIRROR_Y, default=False): cv.boolean, + cv.Optional(CONF_MIRROR_Z, default=False): cv.boolean, + cv.Optional(CONF_SWAP_XY, default=False): cv.boolean, + } + ), + cv.Optional(CONF_ON_ACTIVE): automation.validate_automation(single=True), + cv.Optional(CONF_ON_TAP): automation.validate_automation(single=True), + cv.Optional(CONF_ON_DOUBLE_TAP): automation.validate_automation(single=True), + cv.Optional(CONF_ON_FREEFALL): automation.validate_automation(single=True), + cv.Optional(CONF_ON_ORIENTATION): automation.validate_automation(single=True), + } +).extend(cv.polling_component_schema("10s")) + + +CONFIG_SCHEMA = cv.typed_schema( + { + MODEL_MSA301: _COMMON_SCHEMA.extend( + { + cv.Optional(CONF_RESOLUTION, default=14): cv.enum(RESOLUTIONS_MSA301), + } + ).extend(i2c.i2c_device_schema(0x26)), + MODEL_MSA311: _COMMON_SCHEMA.extend( + { + cv.Optional(CONF_RESOLUTION, default=12): cv.enum(RESOLUTIONS_MSA311), + } + ).extend(i2c.i2c_device_schema(0x62)), + }, + upper=True, + enum=MSA_MODELS, +) + +MSA_SENSOR_SCHEMA = cv.Schema( + { + cv.GenerateID(CONF_MSA3XX_ID): cv.use_id(MSA3xxComponent), + } +) + + +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_model(config[CONF_TYPE])) + cg.add(var.set_range(MSA_RANGES[config[CONF_RANGE]])) + cg.add(var.set_resolution(RESOLUTIONS_MSA301[config[CONF_RESOLUTION]])) + + if transform := config.get(CONF_TRANSFORM): + cg.add( + var.set_transform( + transform[CONF_MIRROR_X], + transform[CONF_MIRROR_Y], + transform[CONF_MIRROR_Z], + transform[CONF_SWAP_XY], + ) + ) + + if calibration_config := config.get(CONF_CALIBRATION): + cg.add( + var.set_offset( + calibration_config[CONF_OFFSET_X], + calibration_config[CONF_OFFSET_Y], + calibration_config[CONF_OFFSET_Z], + ) + ) + + # Triggers secton + + if CONF_ON_ORIENTATION in config: + await automation.build_automation( + var.get_orientation_trigger(), + [], + config[CONF_ON_ORIENTATION], + ) + + if CONF_ON_TAP in config: + await automation.build_automation( + var.get_tap_trigger(), + [], + config[CONF_ON_TAP], + ) + + if CONF_ON_DOUBLE_TAP in config: + await automation.build_automation( + var.get_double_tap_trigger(), + [], + config[CONF_ON_DOUBLE_TAP], + ) + + if CONF_ON_ACTIVE in config: + await automation.build_automation( + var.get_active_trigger(), + [], + config[CONF_ON_ACTIVE], + ) + + if CONF_ON_FREEFALL in config: + await automation.build_automation( + var.get_freefall_trigger(), + [], + config[CONF_ON_FREEFALL], + ) diff --git a/esphome/components/msa3xx/binary_sensor.py b/esphome/components/msa3xx/binary_sensor.py new file mode 100644 index 0000000000..793d5190af --- /dev/null +++ b/esphome/components/msa3xx/binary_sensor.py @@ -0,0 +1,40 @@ +import esphome.codegen as cg +from esphome.components import binary_sensor +import esphome.config_validation as cv +from esphome.const import CONF_ACTIVE, CONF_NAME, DEVICE_CLASS_VIBRATION, ICON_VIBRATE + +from . import CONF_MSA3XX_ID, MSA_SENSOR_SCHEMA + +CODEOWNERS = ["@latonita"] +DEPENDENCIES = ["msa3xx"] + +CONF_TAP = "tap" +CONF_DOUBLE_TAP = "double_tap" + +ICON_TAP = "mdi:gesture-tap" +ICON_DOUBLE_TAP = "mdi:gesture-double-tap" + +EVENT_SENSORS = (CONF_TAP, CONF_DOUBLE_TAP, CONF_ACTIVE) +ICONS = (ICON_TAP, ICON_DOUBLE_TAP, ICON_VIBRATE) + +CONFIG_SCHEMA = MSA_SENSOR_SCHEMA.extend( + { + cv.Optional(event): cv.maybe_simple_value( + binary_sensor.binary_sensor_schema( + device_class=DEVICE_CLASS_VIBRATION, + icon=icon, + ), + key=CONF_NAME, + ) + for event, icon in zip(EVENT_SENSORS, ICONS) + } +) + + +async def to_code(config): + hub = await cg.get_variable(config[CONF_MSA3XX_ID]) + + for sensor in EVENT_SENSORS: + if sensor in config: + sens = await binary_sensor.new_binary_sensor(config[sensor]) + cg.add(getattr(hub, f"set_{sensor}_binary_sensor")(sens)) diff --git a/esphome/components/msa3xx/msa3xx.cpp b/esphome/components/msa3xx/msa3xx.cpp new file mode 100644 index 0000000000..8ecb319955 --- /dev/null +++ b/esphome/components/msa3xx/msa3xx.cpp @@ -0,0 +1,417 @@ +#include "msa3xx.h" +#include "esphome/core/log.h" +#include "esphome/core/hal.h" +#include "esphome/core/helpers.h" + +namespace esphome { +namespace msa3xx { + +static const char *const TAG = "msa3xx"; + +const uint8_t MSA_3XX_PART_ID = 0x13; + +const float GRAVITY_EARTH = 9.80665f; +const float LSB_COEFF = 1000.0f / (GRAVITY_EARTH * 3.9); // LSB to 1 LSB = 3.9mg = 0.0039g +const float G_OFFSET_MIN = -4.5f; // -127...127 LSB = +- 0.4953g = +- 4.857 m/s^2 => +- 4.5 for the safe +const float G_OFFSET_MAX = 4.5f; // -127...127 LSB = +- 0.4953g = +- 4.857 m/s^2 => +- 4.5 for the safe + +const uint8_t RESOLUTION[] = {14, 12, 10, 8}; + +const uint32_t TAP_COOLDOWN_MS = 500; +const uint32_t DOUBLE_TAP_COOLDOWN_MS = 500; +const uint32_t ACTIVITY_COOLDOWN_MS = 500; + +const char *model_to_string(Model model) { + switch (model) { + case Model::MSA301: + return "MSA301"; + case Model::MSA311: + return "MSA311"; + default: + return "Unknown"; + } +} + +const char *power_mode_to_string(PowerMode power_mode) { + switch (power_mode) { + case PowerMode::NORMAL: + return "Normal"; + case PowerMode::LOW_POWER: + return "Low Power"; + case PowerMode::SUSPEND: + return "Suspend"; + default: + return "Unknown"; + } +} + +const char *res_to_string(Resolution resolution) { + switch (resolution) { + case Resolution::RES_14BIT: + return "14-bit"; + case Resolution::RES_12BIT: + return "12-bit"; + case Resolution::RES_10BIT: + return "10-bit"; + case Resolution::RES_8BIT: + return "8-bit"; + default: + return "Unknown"; + } +} + +const char *range_to_string(Range range) { + switch (range) { + case Range::RANGE_2G: + return "±2g"; + case Range::RANGE_4G: + return "±4g"; + case Range::RANGE_8G: + return "±8g"; + case Range::RANGE_16G: + return "±16g"; + default: + return "Unknown"; + } +} + +const char *bandwidth_to_string(Bandwidth bandwidth) { + switch (bandwidth) { + case Bandwidth::BW_1_95HZ: + return "1.95 Hz"; + case Bandwidth::BW_3_9HZ: + return "3.9 Hz"; + case Bandwidth::BW_7_81HZ: + return "7.81 Hz"; + case Bandwidth::BW_15_63HZ: + return "15.63 Hz"; + case Bandwidth::BW_31_25HZ: + return "31.25 Hz"; + case Bandwidth::BW_62_5HZ: + return "62.5 Hz"; + case Bandwidth::BW_125HZ: + return "125 Hz"; + case Bandwidth::BW_250HZ: + return "250 Hz"; + case Bandwidth::BW_500HZ: + return "500 Hz"; + default: + return "Unknown"; + } +} + +const char *orientation_xy_to_string(OrientationXY orientation) { + switch (orientation) { + case OrientationXY::PORTRAIT_UPRIGHT: + return "Portrait Upright"; + case OrientationXY::PORTRAIT_UPSIDE_DOWN: + return "Portrait Upside Down"; + case OrientationXY::LANDSCAPE_LEFT: + return "Landscape Left"; + case OrientationXY::LANDSCAPE_RIGHT: + return "Landscape Right"; + default: + return "Unknown"; + } +} + +const char *orientation_z_to_string(bool orientation) { return orientation ? "Downwards looking" : "Upwards looking"; } + +void MSA3xxComponent::setup() { + ESP_LOGCONFIG(TAG, "Setting up MSA3xx..."); + + uint8_t part_id{0xff}; + if (!this->read_byte(static_cast(RegisterMap::PART_ID), &part_id) || (part_id != MSA_3XX_PART_ID)) { + ESP_LOGE(TAG, "Part ID is wrong or missing. Got 0x%02X", part_id); + this->mark_failed(); + return; + } + + // Resolution LSB/g + // Range : MSA301 : MSA311 + // S2g : 1024 (2^10) : 4096 (2^12) + // S4g : 512 (2^9) : 2048 (2^11) + // S8g : 256 (2^8) : 1024 (2^10) + // S16g : 128 (2^7) : 512 (2^9) + if (this->model_ == Model::MSA301) { + this->device_params_.accel_data_width = 14; + this->device_params_.scale_factor_exp = static_cast(this->range_) - 12; + } else if (this->model_ == Model::MSA311) { + this->device_params_.accel_data_width = 12; + this->device_params_.scale_factor_exp = static_cast(this->range_) - 10; + } else { + ESP_LOGE(TAG, "Unknown model"); + this->mark_failed(); + return; + } + + this->setup_odr_(this->data_rate_); + this->setup_power_mode_bandwidth_(this->power_mode_, this->bandwidth_); + this->setup_range_resolution_(this->range_, this->resolution_); // 2g...16g, 14...8 bit + this->setup_offset_(this->offset_x_, this->offset_y_, this->offset_z_); // calibration offsets + this->write_byte(static_cast(RegisterMap::TAP_DURATION), 0b11000100); // set tap duration 250ms + this->write_byte(static_cast(RegisterMap::SWAP_POLARITY), this->swap_.raw); // set axes polarity + this->write_byte(static_cast(RegisterMap::INT_SET_0), 0b01110111); // enable all interrupts + this->write_byte(static_cast(RegisterMap::INT_SET_1), 0b00011000); // including orientation +} + +void MSA3xxComponent::dump_config() { + ESP_LOGCONFIG(TAG, "MSA3xx:"); + LOG_I2C_DEVICE(this); + if (this->is_failed()) { + ESP_LOGE(TAG, "Communication with MSA3xx failed!"); + } + ESP_LOGCONFIG(TAG, " Model: %s", model_to_string(this->model_)); + ESP_LOGCONFIG(TAG, " Power Mode: %s", power_mode_to_string(this->power_mode_)); + ESP_LOGCONFIG(TAG, " Bandwidth: %s", bandwidth_to_string(this->bandwidth_)); + ESP_LOGCONFIG(TAG, " Range: %s", range_to_string(this->range_)); + ESP_LOGCONFIG(TAG, " Resolution: %s", res_to_string(this->resolution_)); + ESP_LOGCONFIG(TAG, " Offsets: {%.3f m/s², %.3f m/s², %.3f m/s²}", this->offset_x_, this->offset_y_, this->offset_z_); + ESP_LOGCONFIG(TAG, " Transform: {mirror_x=%s, mirror_y=%s, mirror_z=%s, swap_xy=%s}", YESNO(this->swap_.x_polarity), + YESNO(this->swap_.y_polarity), YESNO(this->swap_.z_polarity), YESNO(this->swap_.x_y_swap)); + LOG_UPDATE_INTERVAL(this); + +#ifdef USE_BINARY_SENSOR + LOG_BINARY_SENSOR(" ", "Tap", this->tap_binary_sensor_); + LOG_BINARY_SENSOR(" ", "Double Tap", this->double_tap_binary_sensor_); + LOG_BINARY_SENSOR(" ", "Active", this->active_binary_sensor_); +#endif + +#ifdef USE_SENSOR + LOG_SENSOR(" ", "Acceleration X", this->acceleration_x_sensor_); + LOG_SENSOR(" ", "Acceleration Y", this->acceleration_y_sensor_); + LOG_SENSOR(" ", "Acceleration Z", this->acceleration_z_sensor_); +#endif + +#ifdef USE_TEXT_SENSOR + LOG_TEXT_SENSOR(" ", "Orientation XY", this->orientation_xy_text_sensor_); + LOG_TEXT_SENSOR(" ", "Orientation Z", this->orientation_z_text_sensor_); +#endif +} + +bool MSA3xxComponent::read_data_() { + uint8_t accel_data[6]; + if (!this->read_bytes(static_cast(RegisterMap::ACC_X_LSB), accel_data, 6)) { + return false; + } + + auto raw_to_x_bit = [](uint16_t lsb, uint16_t msb, uint8_t data_bits) -> uint16_t { + return ((msb << 8) | lsb) >> (16 - data_bits); + }; + + auto lpf = [](float new_value, float old_value, float alpha = 0.5f) { + return alpha * new_value + (1.0f - alpha) * old_value; + }; + + this->data_.lsb_x = + this->twos_complement_(raw_to_x_bit(accel_data[0], accel_data[1], this->device_params_.accel_data_width), + this->device_params_.accel_data_width); + this->data_.lsb_y = + this->twos_complement_(raw_to_x_bit(accel_data[2], accel_data[3], this->device_params_.accel_data_width), + this->device_params_.accel_data_width); + this->data_.lsb_z = + this->twos_complement_(raw_to_x_bit(accel_data[4], accel_data[5], this->device_params_.accel_data_width), + this->device_params_.accel_data_width); + + this->data_.x = lpf(ldexp(this->data_.lsb_x, this->device_params_.scale_factor_exp) * GRAVITY_EARTH, this->data_.x); + this->data_.y = lpf(ldexp(this->data_.lsb_y, this->device_params_.scale_factor_exp) * GRAVITY_EARTH, this->data_.y); + this->data_.z = lpf(ldexp(this->data_.lsb_z, this->device_params_.scale_factor_exp) * GRAVITY_EARTH, this->data_.z); + + return true; +} + +bool MSA3xxComponent::read_motion_status_() { + if (!this->read_byte(static_cast(RegisterMap::MOTION_INTERRUPT), &this->status_.motion_int.raw)) { + return false; + } + + if (!this->read_byte(static_cast(RegisterMap::ORIENTATION_STATUS), &this->status_.orientation.raw)) { + return false; + } + + return true; +} + +void MSA3xxComponent::loop() { + if (!this->is_ready()) { + return; + } + + RegMotionInterrupt old_motion_int = this->status_.motion_int; + + if (!this->read_data_() || !this->read_motion_status_()) { + this->status_set_warning(); + return; + } + + this->process_motions_(old_motion_int); +} + +void MSA3xxComponent::update() { + ESP_LOGV(TAG, "Updating MSA3xx..."); + + if (!this->is_ready()) { + ESP_LOGV(TAG, "Component MSA3xx not ready for update"); + return; + } + ESP_LOGV(TAG, "Acceleration: {x = %+1.3f m/s², y = %+1.3f m/s², z = %+1.3f m/s²}; ", this->data_.x, this->data_.y, + this->data_.z); + + ESP_LOGV(TAG, "Orientation: {XY = %s, Z = %s}", orientation_xy_to_string(this->status_.orientation.orient_xy), + orientation_z_to_string(this->status_.orientation.orient_z)); + +#ifdef USE_SENSOR + if (this->acceleration_x_sensor_ != nullptr) + this->acceleration_x_sensor_->publish_state(this->data_.x); + if (this->acceleration_y_sensor_ != nullptr) + this->acceleration_y_sensor_->publish_state(this->data_.y); + if (this->acceleration_z_sensor_ != nullptr) + this->acceleration_z_sensor_->publish_state(this->data_.z); +#endif + +#ifdef USE_TEXT_SENSOR + if (this->orientation_xy_text_sensor_ != nullptr && + (this->status_.orientation.orient_xy != this->status_.orientation_old.orient_xy || + this->status_.never_published)) { + this->orientation_xy_text_sensor_->publish_state(orientation_xy_to_string(this->status_.orientation.orient_xy)); + } + if (this->orientation_z_text_sensor_ != nullptr && + (this->status_.orientation.orient_z != this->status_.orientation_old.orient_z || this->status_.never_published)) { + this->orientation_z_text_sensor_->publish_state(orientation_z_to_string(this->status_.orientation.orient_z)); + } + this->status_.orientation_old = this->status_.orientation; +#endif + + this->status_.never_published = false; + this->status_clear_warning(); +} +float MSA3xxComponent::get_setup_priority() const { return setup_priority::DATA; } + +void MSA3xxComponent::set_offset(float offset_x, float offset_y, float offset_z) { + this->offset_x_ = offset_x; + this->offset_y_ = offset_y; + this->offset_z_ = offset_z; +} + +void MSA3xxComponent::set_transform(bool mirror_x, bool mirror_y, bool mirror_z, bool swap_xy) { + this->swap_.x_polarity = mirror_x; + this->swap_.y_polarity = mirror_y; + this->swap_.z_polarity = mirror_z; + this->swap_.x_y_swap = swap_xy; +} + +void MSA3xxComponent::setup_odr_(DataRate rate) { + RegOutputDataRate reg_odr; + auto reg = this->read_byte(static_cast(RegisterMap::ODR)); + if (reg.has_value()) { + reg_odr.raw = reg.value(); + } else { + reg_odr.raw = 0x0F; // defaut from datasheet + } + + reg_odr.x_axis_disable = false; + reg_odr.y_axis_disable = false; + reg_odr.z_axis_disable = false; + reg_odr.odr = rate; + + this->write_byte(static_cast(RegisterMap::ODR), reg_odr.raw); +} + +void MSA3xxComponent::setup_power_mode_bandwidth_(PowerMode power_mode, Bandwidth bandwidth) { + // 0x11 POWER_MODE_BANDWIDTH + auto reg = this->read_byte(static_cast(RegisterMap::POWER_MODE_BANDWIDTH)); + + RegPowerModeBandwidth power_mode_bandwidth; + if (reg.has_value()) { + power_mode_bandwidth.raw = reg.value(); + } else { + power_mode_bandwidth.raw = 0xde; // defaut from datasheet + } + + power_mode_bandwidth.power_mode = power_mode; + power_mode_bandwidth.low_power_bandwidth = bandwidth; + + this->write_byte(static_cast(RegisterMap::POWER_MODE_BANDWIDTH), power_mode_bandwidth.raw); +} + +void MSA3xxComponent::setup_range_resolution_(Range range, Resolution resolution) { + RegRangeResolution reg; + reg.raw = this->read_byte(static_cast(RegisterMap::RANGE_RESOLUTION)).value_or(0x00); + reg.range = range; + if (this->model_ == Model::MSA301) { + reg.resolution = resolution; + } + this->write_byte(static_cast(RegisterMap::RANGE_RESOLUTION), reg.raw); +} + +void MSA3xxComponent::setup_offset_(float offset_x, float offset_y, float offset_z) { + uint8_t offset[3]; + + auto offset_g_to_lsb = [](float accel) -> int8_t { + float acccel_clamped = clamp(accel, G_OFFSET_MIN, G_OFFSET_MAX); + return static_cast(acccel_clamped * LSB_COEFF); + }; + + offset[0] = offset_g_to_lsb(offset_x); + offset[1] = offset_g_to_lsb(offset_y); + offset[2] = offset_g_to_lsb(offset_z); + + ESP_LOGV(TAG, "Offset (%.3f, %.3f, %.3f)=>LSB(%d, %d, %d)", offset_x, offset_y, offset_z, offset[0], offset[1], + offset[2]); + + this->write_bytes(static_cast(RegisterMap::OFFSET_COMP_X), (uint8_t *) &offset, 3); +} + +int64_t MSA3xxComponent::twos_complement_(uint64_t value, uint8_t bits) { + if (value > (1ULL << (bits - 1))) { + return (int64_t) (value - (1ULL << bits)); + } else { + return (int64_t) value; + } +} + +void binary_event_debounce(bool state, bool old_state, uint32_t now, uint32_t &last_ms, Trigger<> &trigger, + uint32_t cooldown_ms, void *bs, const char *desc) { + if (state && now - last_ms > cooldown_ms) { + ESP_LOGV(TAG, "%s detected", desc); + trigger.trigger(); + last_ms = now; +#ifdef USE_BINARY_SENSOR + if (bs != nullptr) { + static_cast(bs)->publish_state(true); + } +#endif + } else if (!state && now - last_ms > cooldown_ms && bs != nullptr) { +#ifdef USE_BINARY_SENSOR + static_cast(bs)->publish_state(false); +#endif + } +} + +#ifdef USE_BINARY_SENSOR +#define BS_OPTIONAL_PTR(x) ((void *) (x)) +#else +#define BS_OPTIONAL_PTR(x) (nullptr) +#endif + +void MSA3xxComponent::process_motions_(RegMotionInterrupt old) { + uint32_t now = millis(); + + binary_event_debounce(this->status_.motion_int.single_tap_interrupt, old.single_tap_interrupt, now, + this->status_.last_tap_ms, this->tap_trigger_, TAP_COOLDOWN_MS, + BS_OPTIONAL_PTR(this->tap_binary_sensor_), "Tap"); + binary_event_debounce(this->status_.motion_int.double_tap_interrupt, old.double_tap_interrupt, now, + this->status_.last_double_tap_ms, this->double_tap_trigger_, DOUBLE_TAP_COOLDOWN_MS, + BS_OPTIONAL_PTR(this->double_tap_binary_sensor_), "Double Tap"); + binary_event_debounce(this->status_.motion_int.active_interrupt, old.active_interrupt, now, + this->status_.last_action_ms, this->active_trigger_, ACTIVITY_COOLDOWN_MS, + BS_OPTIONAL_PTR(this->active_binary_sensor_), "Activity"); + + if (this->status_.motion_int.orientation_interrupt) { + ESP_LOGVV(TAG, "Orientation changed"); + this->orientation_trigger_.trigger(); + } +} + +} // namespace msa3xx +} // namespace esphome diff --git a/esphome/components/msa3xx/msa3xx.h b/esphome/components/msa3xx/msa3xx.h new file mode 100644 index 0000000000..644109dab0 --- /dev/null +++ b/esphome/components/msa3xx/msa3xx.h @@ -0,0 +1,311 @@ +#pragma once + +#include "esphome/core/component.h" +#include "esphome/components/i2c/i2c.h" +#include "esphome/core/automation.h" + +#ifdef USE_BINARY_SENSOR +#include "esphome/components/binary_sensor/binary_sensor.h" +#endif +#ifdef USE_SENSOR +#include "esphome/components/sensor/sensor.h" +#endif +#ifdef USE_TEXT_SENSOR +#include "esphome/components/text_sensor/text_sensor.h" +#endif + +namespace esphome { +namespace msa3xx { + +// Combined register map of MSA301 and MSA311 +// Differences +// What | MSA301 | MSA11 | +// - Resolution | 14-bit | 12-bit | +// + +// I2c address +enum class Model : uint8_t { + MSA301 = 0x26, + MSA311 = 0x62, +}; + +// Combined MSA301 and MSA311 register map +enum class RegisterMap : uint8_t { + SOFT_RESET = 0x00, + PART_ID = 0x01, + ACC_X_LSB = 0x02, + ACC_X_MSB = 0x03, + ACC_Y_LSB = 0x04, + ACC_Y_MSB = 0x05, + ACC_Z_LSB = 0x06, + ACC_Z_MSB = 0x07, + MOTION_INTERRUPT = 0x09, + DATA_INTERRUPT = 0x0A, + TAP_ACTIVE_STATUS = 0x0B, + ORIENTATION_STATUS = 0x0C, + RESOLUTION_RANGE_CONFIG = 0x0D, + RANGE_RESOLUTION = 0x0F, + ODR = 0x10, + POWER_MODE_BANDWIDTH = 0x11, + SWAP_POLARITY = 0x12, + INT_SET_0 = 0x16, + INT_SET_1 = 0x17, + INT_MAP_0 = 0x19, + INT_MAP_1 = 0x1A, + INT_CONFIG = 0x20, + INT_LATCH = 0x21, + FREEFALL_DURATION = 0x22, + FREEFALL_THRESHOLD = 0x23, + FREEFALL_HYSTERESIS = 0x24, + ACTIVE_DURATION = 0x27, + ACTIVE_THRESHOLD = 0x28, + TAP_DURATION = 0x2A, + TAP_THRESHOLD = 0x2B, + ORIENTATION_CONFIG = 0x2C, + Z_BLOCK = 0x2D, + OFFSET_COMP_X = 0x38, + OFFSET_COMP_Y = 0x39, + OFFSET_COMP_Z = 0x3A, +}; + +enum class Range : uint8_t { + RANGE_2G = 0b00, + RANGE_4G = 0b01, + RANGE_8G = 0b10, + RANGE_16G = 0b11, +}; + +enum class Resolution : uint8_t { + RES_14BIT = 0b00, + RES_12BIT = 0b01, + RES_10BIT = 0b10, + RES_8BIT = 0b11, +}; + +enum class PowerMode : uint8_t { + NORMAL = 0b00, + LOW_POWER = 0b01, + SUSPEND = 0b11, +}; + +enum class Bandwidth : uint8_t { + BW_1_95HZ = 0b0000, + BW_3_9HZ = 0b0011, + BW_7_81HZ = 0b0100, + BW_15_63HZ = 0b0101, + BW_31_25HZ = 0b0110, + BW_62_5HZ = 0b0111, + BW_125HZ = 0b1000, + BW_250HZ = 0b1001, + BW_500HZ = 0b1010, +}; + +enum class DataRate : uint8_t { + ODR_1HZ = 0b0000, // not available in normal mode + ODR_1_95HZ = 0b0001, // not available in normal mode + ODR_3_9HZ = 0b0010, + ODR_7_81HZ = 0b0011, + ODR_15_63HZ = 0b0100, + ODR_31_25HZ = 0b0101, + ODR_62_5HZ = 0b0110, + ODR_125HZ = 0b0111, + ODR_250HZ = 0b1000, + ODR_500HZ = 0b1001, // not available in low power mode + ODR_1000HZ = 0b1010, // not available in low power mode +}; + +enum class OrientationXY : uint8_t { + PORTRAIT_UPRIGHT = 0b00, + PORTRAIT_UPSIDE_DOWN = 0b01, + LANDSCAPE_LEFT = 0b10, + LANDSCAPE_RIGHT = 0b11, +}; + +union Orientation { + struct { + OrientationXY xy : 2; + bool z : 1; + uint8_t reserved : 5; + } __attribute__((packed)); + uint8_t raw; +}; + +// 0x09 +union RegMotionInterrupt { + struct { + bool freefall_interrupt : 1; + bool reserved_1 : 1; + bool active_interrupt : 1; + bool reserved_3 : 1; + bool double_tap_interrupt : 1; + bool single_tap_interrupt : 1; + bool orientation_interrupt : 1; + bool reserved_7 : 1; + } __attribute__((packed)); + uint8_t raw; +}; + +// 0x0C +union RegOrientationStatus { + struct { + uint8_t reserved_0_3 : 4; + OrientationXY orient_xy : 2; + bool orient_z : 1; + uint8_t reserved_7 : 1; + } __attribute__((packed)); + uint8_t raw{0x00}; +}; + +// 0x0f +union RegRangeResolution { + struct { + Range range : 2; + Resolution resolution : 2; + uint8_t reserved_2 : 4; + } __attribute__((packed)); + uint8_t raw{0x00}; +}; + +// 0x10 +union RegOutputDataRate { + struct { + DataRate odr : 4; + uint8_t reserved_4 : 1; + bool z_axis_disable : 1; + bool y_axis_disable : 1; + bool x_axis_disable : 1; + } __attribute__((packed)); + uint8_t raw{0xde}; +}; + +// 0x11 +union RegPowerModeBandwidth { + struct { + uint8_t reserved_0 : 1; + Bandwidth low_power_bandwidth : 4; + uint8_t reserved_5 : 1; + PowerMode power_mode : 2; + } __attribute__((packed)); + uint8_t raw{0xde}; +}; + +// 0x12 +union RegSwapPolarity { + struct { + bool x_y_swap : 1; + bool z_polarity : 1; + bool y_polarity : 1; + bool x_polarity : 1; + uint8_t reserved : 4; + } __attribute__((packed)); + uint8_t raw{0}; +}; + +// 0x2a +union RegTapDuration { + struct { + uint8_t duration : 3; + uint8_t reserved : 3; + bool tap_shock : 1; + bool tap_quiet : 1; + } __attribute__((packed)); + uint8_t raw{0x04}; +}; + +class MSA3xxComponent : public PollingComponent, public i2c::I2CDevice { + public: + void setup() override; + void dump_config() override; + + void loop() override; + void update() override; + + float get_setup_priority() const override; + + void set_model(Model model) { this->model_ = model; } + void set_offset(float offset_x, float offset_y, float offset_z); + void set_range(Range range) { this->range_ = range; } + void set_bandwidth(Bandwidth bandwidth) { this->bandwidth_ = bandwidth; } + void set_resolution(Resolution resolution) { this->resolution_ = resolution; } + void set_transform(bool mirror_x, bool mirror_y, bool mirror_z, bool swap_xy); + +#ifdef USE_BINARY_SENSOR + SUB_BINARY_SENSOR(tap) + SUB_BINARY_SENSOR(double_tap) + SUB_BINARY_SENSOR(active) +#endif + +#ifdef USE_SENSOR + SUB_SENSOR(acceleration_x) + SUB_SENSOR(acceleration_y) + SUB_SENSOR(acceleration_z) +#endif + +#ifdef USE_TEXT_SENSOR + SUB_TEXT_SENSOR(orientation_xy) + SUB_TEXT_SENSOR(orientation_z) +#endif + + Trigger<> *get_tap_trigger() { return &this->tap_trigger_; } + Trigger<> *get_double_tap_trigger() { return &this->double_tap_trigger_; } + Trigger<> *get_orientation_trigger() { return &this->orientation_trigger_; } + Trigger<> *get_freefall_trigger() { return &this->freefall_trigger_; } + Trigger<> *get_active_trigger() { return &this->active_trigger_; } + + protected: + Model model_{Model::MSA311}; + + PowerMode power_mode_{PowerMode::NORMAL}; + DataRate data_rate_{DataRate::ODR_250HZ}; + Bandwidth bandwidth_{Bandwidth::BW_250HZ}; + Range range_{Range::RANGE_2G}; + Resolution resolution_{Resolution::RES_14BIT}; + float offset_x_, offset_y_, offset_z_; // in m/s² + RegSwapPolarity swap_; + + struct { + int scale_factor_exp; + uint8_t accel_data_width; + } device_params_{}; + + struct { + int16_t lsb_x, lsb_y, lsb_z; + float x, y, z; + } data_{}; + + struct { + RegMotionInterrupt motion_int; + RegOrientationStatus orientation; + RegOrientationStatus orientation_old; + + uint32_t last_tap_ms{0}; + uint32_t last_double_tap_ms{0}; + uint32_t last_action_ms{0}; + + bool never_published{true}; + } status_{}; + + void setup_odr_(DataRate rate); + void setup_power_mode_bandwidth_(PowerMode power_mode, Bandwidth bandwidth); + void setup_range_resolution_(Range range, Resolution resolution); + void setup_offset_(float offset_x, float offset_y, float offset_z); + + bool read_data_(); + bool read_motion_status_(); + + int64_t twos_complement_(uint64_t value, uint8_t bits); + + // + // Actons / Triggers + // + Trigger<> tap_trigger_; + Trigger<> double_tap_trigger_; + Trigger<> orientation_trigger_; + Trigger<> freefall_trigger_; + Trigger<> active_trigger_; + + void process_motions_(RegMotionInterrupt old); +}; + +} // namespace msa3xx +} // namespace esphome diff --git a/esphome/components/msa3xx/sensor.py b/esphome/components/msa3xx/sensor.py new file mode 100644 index 0000000000..63f050fa05 --- /dev/null +++ b/esphome/components/msa3xx/sensor.py @@ -0,0 +1,42 @@ +import esphome.codegen as cg +from esphome.components import sensor +import esphome.config_validation as cv +from esphome.const import ( + CONF_ACCELERATION_X, + CONF_ACCELERATION_Y, + CONF_ACCELERATION_Z, + CONF_NAME, + ICON_BRIEFCASE_DOWNLOAD, + STATE_CLASS_MEASUREMENT, + UNIT_METER_PER_SECOND_SQUARED, +) + +from . import CONF_MSA3XX_ID, MSA_SENSOR_SCHEMA + +CODEOWNERS = ["@latonita"] +DEPENDENCIES = ["msa3xx"] + +ACCELERATION_SENSORS = (CONF_ACCELERATION_X, CONF_ACCELERATION_Y, CONF_ACCELERATION_Z) + +accel_schema = cv.maybe_simple_value( + sensor.sensor_schema( + unit_of_measurement=UNIT_METER_PER_SECOND_SQUARED, + icon=ICON_BRIEFCASE_DOWNLOAD, + accuracy_decimals=2, + state_class=STATE_CLASS_MEASUREMENT, + ), + key=CONF_NAME, +) + + +CONFIG_SCHEMA = MSA_SENSOR_SCHEMA.extend( + {cv.Optional(sensor): accel_schema for sensor in ACCELERATION_SENSORS} +) + + +async def to_code(config): + hub = await cg.get_variable(config[CONF_MSA3XX_ID]) + for accel_key in ACCELERATION_SENSORS: + if accel_key in config: + sens = await sensor.new_sensor(config[accel_key]) + cg.add(getattr(hub, f"set_{accel_key}_sensor")(sens)) diff --git a/esphome/components/msa3xx/text_sensor.py b/esphome/components/msa3xx/text_sensor.py new file mode 100644 index 0000000000..c53a4aa139 --- /dev/null +++ b/esphome/components/msa3xx/text_sensor.py @@ -0,0 +1,38 @@ +import esphome.codegen as cg +from esphome.components import text_sensor +import esphome.config_validation as cv +from esphome.const import CONF_NAME + +from . import CONF_MSA3XX_ID, MSA_SENSOR_SCHEMA + +CODEOWNERS = ["@latonita"] +DEPENDENCIES = ["msa3xx"] + +CONF_ORIENTATION_XY = "orientation_xy" +CONF_ORIENTATION_Z = "orientation_z" +ICON_SCREEN_ROTATION = "mdi:screen-rotation" + +ORIENTATION_SENSORS = (CONF_ORIENTATION_XY, CONF_ORIENTATION_Z) + +CONFIG_SCHEMA = MSA_SENSOR_SCHEMA.extend( + { + cv.Optional(sensor): cv.maybe_simple_value( + text_sensor.text_sensor_schema(icon=ICON_SCREEN_ROTATION), + key=CONF_NAME, + ) + for sensor in ORIENTATION_SENSORS + } +) + + +async def setup_conf(config, key, hub): + if sensor_config := config.get(key): + var = await text_sensor.new_text_sensor(sensor_config) + cg.add(getattr(hub, f"set_{key}_text_sensor")(var)) + + +async def to_code(config): + hub = await cg.get_variable(config[CONF_MSA3XX_ID]) + + for key in ORIENTATION_SENSORS: + await setup_conf(config, key, hub) diff --git a/esphome/components/nfc/binary_sensor/__init__.py b/esphome/components/nfc/binary_sensor/__init__.py index 21c8298ea8..47cf014550 100644 --- a/esphome/components/nfc/binary_sensor/__init__.py +++ b/esphome/components/nfc/binary_sensor/__init__.py @@ -1,9 +1,10 @@ import esphome.codegen as cg -import esphome.config_validation as cv from esphome.components import binary_sensor +import esphome.config_validation as cv from esphome.const import CONF_UID from esphome.core import HexInt -from .. import nfc_ns, Nfcc, NfcTagListener + +from .. import Nfcc, NfcTagListener, nfc_ns DEPENDENCIES = ["nfc"] @@ -25,8 +26,7 @@ def validate_uid(value): for x in value.split("-"): if len(x) != 2: raise cv.Invalid( - "Each part (separated by '-') of the UID must be two characters " - "long." + "Each part (separated by '-') of the UID must be two characters long." ) try: x = int(x, 16) diff --git a/esphome/components/opentherm/binary_sensor/__init__.py b/esphome/components/opentherm/binary_sensor/__init__.py index 643734f90c..d4c7861a1d 100644 --- a/esphome/components/opentherm/binary_sensor/__init__.py +++ b/esphome/components/opentherm/binary_sensor/__init__.py @@ -1,8 +1,9 @@ from typing import Any -import esphome.config_validation as cv from esphome.components import binary_sensor -from .. import const, schema, validate, generate +import esphome.config_validation as cv + +from .. import const, generate, schema, validate DEPENDENCIES = [const.OPENTHERM] COMPONENT_TYPE = const.BINARY_SENSOR @@ -11,8 +12,7 @@ COMPONENT_TYPE = const.BINARY_SENSOR def get_entity_validation_schema(entity: schema.BinarySensorSchema) -> cv.Schema: return binary_sensor.binary_sensor_schema( device_class=( - entity.device_class - or binary_sensor._UNDEF # pylint: disable=protected-access + entity.device_class or binary_sensor._UNDEF # pylint: disable=protected-access ), icon=(entity.icon or binary_sensor._UNDEF), # pylint: disable=protected-access ) diff --git a/esphome/components/opentherm/generate.py b/esphome/components/opentherm/generate.py index 6b6a0255a8..a97754d52c 100644 --- a/esphome/components/opentherm/generate.py +++ b/esphome/components/opentherm/generate.py @@ -3,8 +3,9 @@ from typing import Any, Callable, Optional import esphome.codegen as cg from esphome.const import CONF_ID + from . import const -from .schema import TSchema, SettingSchema +from .schema import SettingSchema, TSchema opentherm_ns = cg.esphome_ns.namespace("opentherm") OpenthermHub = opentherm_ns.class_("OpenthermHub", cg.Component) @@ -112,11 +113,10 @@ def add_messages(hub: cg.MockObj, keys: list[str], schemas: dict[str, TSchema]): msg_expr = cg.RawExpression(f"esphome::opentherm::MessageId::{msg}") if keep_updated: cg.add(hub.add_repeating_message(msg_expr)) + elif order is not None: + cg.add(hub.add_initial_message(msg_expr, order)) else: - if order is not None: - cg.add(hub.add_initial_message(msg_expr, order)) - else: - cg.add(hub.add_initial_message(msg_expr)) + cg.add(hub.add_initial_message(msg_expr)) def add_property_set(var: cg.MockObj, config_key: str, config: dict[str, Any]) -> None: @@ -128,7 +128,7 @@ Create = Callable[[dict[str, Any], str, cg.MockObj], Awaitable[cg.Pvariable]] def create_only_conf( - create: Callable[[dict[str, Any]], Awaitable[cg.Pvariable]] + create: Callable[[dict[str, Any]], Awaitable[cg.Pvariable]], ) -> Create: return lambda conf, _key, _hub: create(conf) diff --git a/esphome/components/opentherm/sensor/__init__.py b/esphome/components/opentherm/sensor/__init__.py index 546a79054b..86c842b299 100644 --- a/esphome/components/opentherm/sensor/__init__.py +++ b/esphome/components/opentherm/sensor/__init__.py @@ -1,8 +1,9 @@ from typing import Any -import esphome.config_validation as cv from esphome.components import sensor -from .. import const, schema, validate, generate +import esphome.config_validation as cv + +from .. import const, generate, schema, validate DEPENDENCIES = [const.OPENTHERM] COMPONENT_TYPE = const.SENSOR @@ -22,11 +23,9 @@ MSG_DATA_TYPES = { def get_entity_validation_schema(entity: schema.SensorSchema) -> cv.Schema: return sensor.sensor_schema( - unit_of_measurement=entity.unit_of_measurement - or sensor._UNDEF, # pylint: disable=protected-access + unit_of_measurement=entity.unit_of_measurement or sensor._UNDEF, # pylint: disable=protected-access accuracy_decimals=entity.accuracy_decimals, - device_class=entity.device_class - or sensor._UNDEF, # pylint: disable=protected-access + device_class=entity.device_class or sensor._UNDEF, # pylint: disable=protected-access icon=entity.icon or sensor._UNDEF, # pylint: disable=protected-access state_class=entity.state_class, ).extend( diff --git a/esphome/components/packages/__init__.py b/esphome/components/packages/__init__.py index 2b064a90cf..f4d11e7bd0 100644 --- a/esphome/components/packages/__init__.py +++ b/esphome/components/packages/__init__.py @@ -1,8 +1,8 @@ from pathlib import Path -import esphome.config_validation as cv from esphome import git, yaml_util from esphome.config_helpers import merge_config +import esphome.config_validation as cv from esphome.const import ( CONF_ESPHOME, CONF_FILE, @@ -10,12 +10,14 @@ from esphome.const import ( CONF_MIN_VERSION, CONF_PACKAGES, CONF_PASSWORD, + CONF_PATH, CONF_REF, CONF_REFRESH, CONF_URL, CONF_USERNAME, + CONF_VARS, + __version__ as ESPHOME_VERSION, ) -from esphome.const import __version__ as ESPHOME_VERSION from esphome.core import EsphomeError DOMAIN = CONF_PACKAGES @@ -74,7 +76,19 @@ BASE_SCHEMA = cv.All( cv.Optional(CONF_PASSWORD): cv.string, cv.Exclusive(CONF_FILE, "files"): validate_yaml_filename, cv.Exclusive(CONF_FILES, "files"): cv.All( - cv.ensure_list(validate_yaml_filename), + cv.ensure_list( + cv.Any( + validate_yaml_filename, + cv.Schema( + { + cv.Required(CONF_PATH): validate_yaml_filename, + cv.Optional(CONF_VARS, default={}): cv.Schema( + {cv.string: cv.string} + ), + } + ), + ) + ), cv.Length(min=1), ), cv.Optional(CONF_REF): cv.git_ref, @@ -106,16 +120,25 @@ def _process_base_package(config: dict) -> dict: username=config.get(CONF_USERNAME), password=config.get(CONF_PASSWORD), ) - files: list[str] = config[CONF_FILES] + files = [] + + for file in config[CONF_FILES]: + if isinstance(file, str): + files.append({CONF_PATH: file, CONF_VARS: {}}) + else: + files.append(file) def get_packages(files) -> dict: packages = {} - for file in files: - yaml_file: Path = repo_dir / file + for idx, file in enumerate(files): + filename = file[CONF_PATH] + yaml_file: Path = repo_dir / filename + vars = file.get(CONF_VARS, {}) if not yaml_file.is_file(): raise cv.Invalid( - f"{file} does not exist in repository", path=[CONF_FILES] + f"{filename} does not exist in repository", + path=[CONF_FILES, idx, CONF_PATH], ) try: @@ -131,11 +154,12 @@ def _process_base_package(config: dict) -> dict: raise cv.Invalid( f"Current ESPHome Version is too old to use this package: {ESPHOME_VERSION} < {min_version}" ) - - packages[file] = new_yaml + vars = {k: str(v) for k, v in vars.items()} + new_yaml = yaml_util.substitute_vars(new_yaml, vars) + packages[f"{filename}{idx}"] = new_yaml except EsphomeError as e: raise cv.Invalid( - f"{file} is not a valid YAML file. Please check the file contents.\n{e}" + f"{filename} is not a valid YAML file. Please check the file contents.\n{e}" ) from e return packages @@ -154,7 +178,7 @@ def _process_base_package(config: dict) -> dict: error = er if packages is None: - raise cv.Invalid(f"Failed to load packages. {error}") + raise cv.Invalid(f"Failed to load packages. {error}", path=error.path) return {"packages": packages} diff --git a/esphome/components/pn532/binary_sensor.py b/esphome/components/pn532/binary_sensor.py index 9bcae30750..b9c3103c65 100644 --- a/esphome/components/pn532/binary_sensor.py +++ b/esphome/components/pn532/binary_sensor.py @@ -1,9 +1,10 @@ import esphome.codegen as cg -import esphome.config_validation as cv from esphome.components import binary_sensor +import esphome.config_validation as cv from esphome.const import CONF_UID from esphome.core import HexInt -from . import pn532_ns, PN532, CONF_PN532_ID + +from . import CONF_PN532_ID, PN532, pn532_ns DEPENDENCIES = ["pn532"] @@ -13,8 +14,7 @@ def validate_uid(value): for x in value.split("-"): if len(x) != 2: raise cv.Invalid( - "Each part (separated by '-') of the UID must be two characters " - "long." + "Each part (separated by '-') of the UID must be two characters long." ) try: x = int(x, 16) diff --git a/esphome/components/rc522/binary_sensor.py b/esphome/components/rc522/binary_sensor.py index 716c0eca76..87f81c2223 100644 --- a/esphome/components/rc522/binary_sensor.py +++ b/esphome/components/rc522/binary_sensor.py @@ -1,9 +1,10 @@ import esphome.codegen as cg -import esphome.config_validation as cv from esphome.components import binary_sensor +import esphome.config_validation as cv from esphome.const import CONF_UID from esphome.core import HexInt -from . import rc522_ns, RC522, CONF_RC522_ID + +from . import CONF_RC522_ID, RC522, rc522_ns DEPENDENCIES = ["rc522"] @@ -13,8 +14,7 @@ def validate_uid(value): for x in value.split("-"): if len(x) != 2: raise cv.Invalid( - "Each part (separated by '-') of the UID must be two characters " - "long." + "Each part (separated by '-') of the UID must be two characters long." ) try: x = int(x, 16) diff --git a/esphome/components/resampler/speaker/resampler_speaker.h b/esphome/components/resampler/speaker/resampler_speaker.h index c44f740fa2..d5e3f2b6d6 100644 --- a/esphome/components/resampler/speaker/resampler_speaker.h +++ b/esphome/components/resampler/speaker/resampler_speaker.h @@ -34,9 +34,11 @@ class ResamplerSpeaker : public Component, public speaker::Speaker { /// @brief Mute state changes are passed to the parent's output speaker void set_mute_state(bool mute_state) override; + bool get_mute_state() override { return this->output_speaker_->get_mute_state(); } /// @brief Volume state changes are passed to the parent's output speaker void set_volume(float volume) override; + float get_volume() override { return this->output_speaker_->get_volume(); } void set_output_speaker(speaker::Speaker *speaker) { this->output_speaker_ = speaker; } void set_task_stack_in_psram(bool task_stack_in_psram) { this->task_stack_in_psram_ = task_stack_in_psram; } diff --git a/esphome/components/socket/bsd_sockets_impl.cpp b/esphome/components/socket/bsd_sockets_impl.cpp index f07f5c8f81..1b3916fcab 100644 --- a/esphome/components/socket/bsd_sockets_impl.cpp +++ b/esphome/components/socket/bsd_sockets_impl.cpp @@ -46,6 +46,7 @@ class BSDSocketImpl : public Socket { close(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall) } } + int connect(const struct sockaddr *addr, socklen_t addrlen) override { return ::connect(fd_, addr, addrlen); } std::unique_ptr accept(struct sockaddr *addr, socklen_t *addrlen) override { int fd = ::accept(fd_, addr, addrlen); if (fd == -1) diff --git a/esphome/components/socket/lwip_sockets_impl.cpp b/esphome/components/socket/lwip_sockets_impl.cpp index eaf6ac2c6f..c41e42fc83 100644 --- a/esphome/components/socket/lwip_sockets_impl.cpp +++ b/esphome/components/socket/lwip_sockets_impl.cpp @@ -39,6 +39,7 @@ class LwIPSocketImpl : public Socket { close(); // NOLINT(clang-analyzer-optin.cplusplus.VirtualCall) } } + int connect(const struct sockaddr *addr, socklen_t addrlen) override { return lwip_connect(fd_, addr, addrlen); } std::unique_ptr accept(struct sockaddr *addr, socklen_t *addrlen) override { int fd = lwip_accept(fd_, addr, addrlen); if (fd == -1) diff --git a/esphome/components/socket/socket.h b/esphome/components/socket/socket.h index cefdb51e0d..917f3c4c7f 100644 --- a/esphome/components/socket/socket.h +++ b/esphome/components/socket/socket.h @@ -21,7 +21,9 @@ class Socket { virtual int close() = 0; // not supported yet: // virtual int connect(const std::string &address) = 0; - // virtual int connect(const struct sockaddr *addr, socklen_t addrlen) = 0; +#if defined(USE_SOCKET_IMPL_LWIP_SOCKETS) || defined(USE_SOCKET_IMPL_BSD_SOCKETS) + virtual int connect(const struct sockaddr *addr, socklen_t addrlen) = 0; +#endif virtual int shutdown(int how) = 0; virtual int getpeername(struct sockaddr *addr, socklen_t *addrlen) = 0; diff --git a/esphome/components/speaker/speaker.h b/esphome/components/speaker/speaker.h index 74c4822eca..c4cf912fa6 100644 --- a/esphome/components/speaker/speaker.h +++ b/esphome/components/speaker/speaker.h @@ -76,7 +76,7 @@ class Speaker { } #endif }; - float get_volume() { return this->volume_; } + virtual float get_volume() { return this->volume_; } virtual void set_mute_state(bool mute_state) { this->mute_state_ = mute_state; @@ -90,7 +90,7 @@ class Speaker { } #endif } - bool get_mute_state() { return this->mute_state_; } + virtual bool get_mute_state() { return this->mute_state_; } #ifdef USE_AUDIO_DAC void set_audio_dac(audio_dac::AudioDac *audio_dac) { this->audio_dac_ = audio_dac; } diff --git a/esphome/components/ssd1306_base/__init__.py b/esphome/components/ssd1306_base/__init__.py index 1fe74dfcb5..ab2c7a5496 100644 --- a/esphome/components/ssd1306_base/__init__.py +++ b/esphome/components/ssd1306_base/__init__.py @@ -1,15 +1,17 @@ -import esphome.codegen as cg -import esphome.config_validation as cv from esphome import pins +import esphome.codegen as cg from esphome.components import display +import esphome.config_validation as cv from esphome.const import ( - CONF_EXTERNAL_VCC, - CONF_LAMBDA, - CONF_MODEL, - CONF_RESET_PIN, CONF_BRIGHTNESS, CONF_CONTRAST, + CONF_EXTERNAL_VCC, CONF_INVERT, + CONF_LAMBDA, + CONF_MODEL, + CONF_OFFSET_X, + CONF_OFFSET_Y, + CONF_RESET_PIN, ) ssd1306_base_ns = cg.esphome_ns.namespace("ssd1306_base") @@ -18,8 +20,6 @@ SSD1306Model = ssd1306_base_ns.enum("SSD1306Model") CONF_FLIP_X = "flip_x" CONF_FLIP_Y = "flip_y" -CONF_OFFSET_X = "offset_x" -CONF_OFFSET_Y = "offset_y" MODELS = { "SSD1306_128X32": SSD1306Model.SSD1306_MODEL_128_32, diff --git a/esphome/components/thermostat/climate.py b/esphome/components/thermostat/climate.py index a529bbd474..638aad7c06 100644 --- a/esphome/components/thermostat/climate.py +++ b/esphome/components/thermostat/climate.py @@ -137,7 +137,7 @@ def validate_temperature_preset(preset, root_config, name, requirements): def generate_comparable_preset(config, name): - comparable_preset = f"{CONF_PRESET}:\n" f" - {CONF_NAME}: {name}\n" + comparable_preset = f"{CONF_PRESET}:\n - {CONF_NAME}: {name}\n" if CONF_DEFAULT_TARGET_TEMPERATURE_LOW in config: comparable_preset += f" {CONF_DEFAULT_TARGET_TEMPERATURE_LOW}: {config[CONF_DEFAULT_TARGET_TEMPERATURE_LOW]}\n" diff --git a/esphome/components/tmp1075/tmp1075.cpp b/esphome/components/tmp1075/tmp1075.cpp index 38ed2bea31..68253a11f1 100644 --- a/esphome/components/tmp1075/tmp1075.cpp +++ b/esphome/components/tmp1075/tmp1075.cpp @@ -18,14 +18,9 @@ static uint16_t temp2regvalue(float temp); static float regvalue2temp(uint16_t regvalue); void TMP1075Sensor::setup() { - uint8_t die_id; - if (!this->read_byte(REG_DIEID, &die_id)) { - ESP_LOGW(TAG, "'%s' - unable to read ID", this->name_.c_str()); - this->mark_failed(); - return; - } - if (die_id != EXPECT_DIEID) { - ESP_LOGW(TAG, "'%s' - unexpected ID 0x%x found, expected 0x%x", this->name_.c_str(), die_id, EXPECT_DIEID); + uint8_t cfg; + if (!this->read_byte(REG_CFGR, &cfg)) { + ESP_LOGE(TAG, "'%s' - unable to read", this->name_.c_str()); this->mark_failed(); return; } @@ -37,9 +32,10 @@ void TMP1075Sensor::update() { uint16_t regvalue; if (!read_byte_16(REG_TEMP, ®value)) { ESP_LOGW(TAG, "'%s' - unable to read temperature register", this->name_.c_str()); - this->status_set_warning(); + this->status_set_warning("can't read"); return; } + this->status_clear_warning(); const float temp = regvalue2temp(regvalue); this->publish_state(temp); @@ -89,9 +85,9 @@ void TMP1075Sensor::write_config() { } void TMP1075Sensor::send_config_() { - ESP_LOGV(TAG, "'%s' - sending configuration %04x", this->name_.c_str(), config_.regvalue); + ESP_LOGV(TAG, "'%s' - sending configuration %02x", this->name_.c_str(), config_.regvalue); log_config_(); - if (!this->write_byte_16(REG_CFGR, config_.regvalue)) { + if (!this->write_byte(REG_CFGR, config_.regvalue)) { ESP_LOGW(TAG, "'%s' - unable to write configuration register", this->name_.c_str()); return; } diff --git a/esphome/components/tmp1075/tmp1075.h b/esphome/components/tmp1075/tmp1075.h index db2bac517a..84e2e8abe4 100644 --- a/esphome/components/tmp1075/tmp1075.h +++ b/esphome/components/tmp1075/tmp1075.h @@ -36,9 +36,8 @@ struct TMP1075Config { uint8_t shutdown : 1; // Sets the device in shutdown mode to conserve power. // 0: Device is in continuous conversion // 1: Device is in shutdown mode - uint8_t unused : 8; } fields; - uint16_t regvalue; + uint8_t regvalue; }; }; diff --git a/esphome/components/tormatic/__init__.py b/esphome/components/tormatic/__init__.py new file mode 100644 index 0000000000..7f3f05a3cd --- /dev/null +++ b/esphome/components/tormatic/__init__.py @@ -0,0 +1 @@ +CODEOWNERS = ["@ti-mo"] diff --git a/esphome/components/tormatic/cover.py b/esphome/components/tormatic/cover.py new file mode 100644 index 0000000000..f1cfe09a05 --- /dev/null +++ b/esphome/components/tormatic/cover.py @@ -0,0 +1,47 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome.components import cover, uart +from esphome.const import ( + CONF_CLOSE_DURATION, + CONF_ID, + CONF_OPEN_DURATION, +) + +tormatic_ns = cg.esphome_ns.namespace("tormatic") +Tormatic = tormatic_ns.class_("Tormatic", cover.Cover, cg.PollingComponent) + +CONFIG_SCHEMA = ( + cover.COVER_SCHEMA.extend(uart.UART_DEVICE_SCHEMA) + .extend(cv.polling_component_schema("300ms")) + .extend( + { + cv.GenerateID(): cv.declare_id(Tormatic), + cv.Optional( + CONF_OPEN_DURATION, default="15s" + ): cv.positive_time_period_milliseconds, + cv.Optional( + CONF_CLOSE_DURATION, default="22s" + ): cv.positive_time_period_milliseconds, + } + ) +) + +FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema( + "tormatic", + baud_rate=9600, + require_tx=True, + require_rx=True, + data_bits=8, + parity="NONE", + stop_bits=1, +) + + +async def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + await cg.register_component(var, config) + await cover.register_cover(var, config) + await uart.register_uart_device(var, config) + + cg.add(var.set_close_duration(config[CONF_CLOSE_DURATION])) + cg.add(var.set_open_duration(config[CONF_OPEN_DURATION])) diff --git a/esphome/components/tormatic/tormatic_cover.cpp b/esphome/components/tormatic/tormatic_cover.cpp new file mode 100644 index 0000000000..35224c8ec7 --- /dev/null +++ b/esphome/components/tormatic/tormatic_cover.cpp @@ -0,0 +1,355 @@ +#include + +#include "tormatic_cover.h" + +using namespace std; + +namespace esphome { +namespace tormatic { + +static const char *const TAG = "tormatic.cover"; + +using namespace esphome::cover; + +void Tormatic::setup() { + auto restore = this->restore_state_(); + if (restore.has_value()) { + restore->apply(this); + return; + } + + // Assume gate is closed without preexisting state. + this->position = 0.0f; +} + +cover::CoverTraits Tormatic::get_traits() { + auto traits = CoverTraits(); + traits.set_supports_stop(true); + traits.set_supports_position(true); + traits.set_is_assumed_state(false); + return traits; +} + +void Tormatic::dump_config() { + LOG_COVER("", "Tormatic Cover", this); + this->check_uart_settings(9600, 1, uart::UART_CONFIG_PARITY_NONE, 8); + + ESP_LOGCONFIG(TAG, " Open Duration: %.1fs", this->open_duration_ / 1e3f); + ESP_LOGCONFIG(TAG, " Close Duration: %.1fs", this->close_duration_ / 1e3f); + + auto restore = this->restore_state_(); + if (restore.has_value()) { + ESP_LOGCONFIG(TAG, " Saved position %d%%", (int) (restore->position * 100.f)); + } +} + +void Tormatic::update() { this->request_gate_status_(); } + +void Tormatic::loop() { + auto o_status = this->read_gate_status_(); + if (o_status) { + auto status = o_status.value(); + + this->recalibrate_duration_(status); + this->handle_gate_status_(status); + } + + this->recompute_position_(); + this->stop_at_target_(); +} + +void Tormatic::control(const cover::CoverCall &call) { + if (call.get_stop()) { + this->send_gate_command_(PAUSED); + return; + } + + if (call.get_position().has_value()) { + auto pos = call.get_position().value(); + this->control_position_(pos); + return; + } +} + +// Wrap the Cover's publish_state with a rate limiter. Publishes if the last +// publish was longer than ratelimit milliseconds ago. 0 to disable. +void Tormatic::publish_state(bool save, uint32_t ratelimit) { + auto now = millis(); + if ((now - this->last_publish_time_) < ratelimit) { + return; + } + this->last_publish_time_ = now; + + Cover::publish_state(save); +}; + +// Recalibrate the gate's estimated open or close duration based on the +// actual time the operation took. +void Tormatic::recalibrate_duration_(GateStatus s) { + if (this->current_status_ == s) { + return; + } + + auto now = millis(); + auto old = this->current_status_; + + // Gate paused halfway through opening or closing, invalidate the start time + // of the current operation. Close/open durations can only be accurately + // calibrated on full open or close cycle due to motor acceleration. + if (s == PAUSED) { + ESP_LOGD(TAG, "Gate paused, clearing direction start time"); + this->direction_start_time_ = 0; + return; + } + + // Record the start time of a state transition if the gate was in the fully + // open or closed position before the command. + if ((old == CLOSED && s == OPENING) || (old == OPENED && s == CLOSING)) { + ESP_LOGD(TAG, "Gate started moving from fully open or closed state"); + this->direction_start_time_ = now; + return; + } + + // The gate was resumed from a paused state, don't attempt recalibration. + if (this->direction_start_time_ == 0) { + return; + } + + if (s == OPENED) { + this->open_duration_ = now - this->direction_start_time_; + ESP_LOGI(TAG, "Recalibrated the gate's open duration to %dms", this->open_duration_); + } + if (s == CLOSED) { + this->close_duration_ = now - this->direction_start_time_; + ESP_LOGI(TAG, "Recalibrated the gate's close duration to %dms", this->close_duration_); + } + + this->direction_start_time_ = 0; +} + +// Set the Cover's internal state based on a status message +// received from the unit. +void Tormatic::handle_gate_status_(GateStatus s) { + if (this->current_status_ == s) { + return; + } + + ESP_LOGI(TAG, "Status changed from %s to %s", gate_status_to_str(this->current_status_), gate_status_to_str(s)); + + switch (s) { + case OPENED: + // The Novoferm 423 doesn't respond to the first 'Close' command after + // being opened completely. Sending a pause command after opening fixes + // that. + this->send_gate_command_(PAUSED); + + this->position = COVER_OPEN; + break; + case CLOSED: + this->position = COVER_CLOSED; + break; + default: + break; + } + + this->current_status_ = s; + this->current_operation = gate_status_to_cover_operation(s); + + this->publish_state(true); + + // This timestamp is used to generate position deltas on every loop() while + // the gate is moving. Bump it on each state transition so the first tick + // doesn't generate a huge delta. + this->last_recompute_time_ = millis(); +} + +// Recompute the gate's position and publish the results while +// the gate is moving. No-op when the gate is idle. +void Tormatic::recompute_position_() { + if (this->current_operation == COVER_OPERATION_IDLE) { + return; + } + + const uint32_t now = millis(); + uint32_t diff = now - this->last_recompute_time_; + + auto direction = +1.0f; + uint32_t duration = this->open_duration_; + if (this->current_operation == COVER_OPERATION_CLOSING) { + direction = -1.0f; + duration = this->close_duration_; + } + + auto delta = direction * diff / duration; + + this->position = clamp(this->position + delta, COVER_CLOSED, COVER_OPEN); + + this->last_recompute_time_ = now; + + this->publish_state(true, 250); +} + +// Start moving the gate in the direction of the target position. +void Tormatic::control_position_(float target) { + if (target == this->position) { + return; + } + + if (target == COVER_OPEN) { + ESP_LOGI(TAG, "Fully opening gate"); + this->send_gate_command_(OPENED); + return; + } + if (target == COVER_CLOSED) { + ESP_LOGI(TAG, "Fully closing gate"); + this->send_gate_command_(CLOSED); + return; + } + + // Don't set target position when fully opening or closing the gate, the gate + // stops automatically when it reaches the configured open/closed positions. + this->target_position_ = target; + + if (target > this->position) { + ESP_LOGI(TAG, "Opening gate towards %.1f", target); + this->send_gate_command_(OPENED); + return; + } + + if (target < this->position) { + ESP_LOGI(TAG, "Closing gate towards %.1f", target); + this->send_gate_command_(CLOSED); + return; + } +} + +// Stop the gate if it is moving at or beyond its target position. Target +// position is only set when the gate is requested to move to a halfway +// position. +void Tormatic::stop_at_target_() { + if (this->current_operation == COVER_OPERATION_IDLE) { + return; + } + if (!this->target_position_) { + return; + } + auto target = this->target_position_.value(); + + if (this->current_operation == COVER_OPERATION_OPENING && this->position < target) { + return; + } + if (this->current_operation == COVER_OPERATION_CLOSING && this->position > target) { + return; + } + + this->send_gate_command_(PAUSED); + this->target_position_.reset(); +} + +// Read a GateStatus from the unit. The unit only sends messages in response to +// status requests or commands, so a message needs to be sent first. +optional Tormatic::read_gate_status_() { + if (this->available() < sizeof(MessageHeader)) { + return {}; + } + + auto o_hdr = this->read_data_(); + if (!o_hdr) { + ESP_LOGE(TAG, "Timeout reading message header"); + return {}; + } + auto hdr = o_hdr.value(); + + switch (hdr.type) { + case STATUS: { + if (hdr.payload_size() != sizeof(StatusReply)) { + ESP_LOGE(TAG, "Header specifies payload size %d but size of StatusReply is %d", hdr.payload_size(), + sizeof(StatusReply)); + } + + // Read a StatusReply requested by update(). + auto o_status = this->read_data_(); + if (!o_status) { + return {}; + } + auto status = o_status.value(); + + return status.state; + } + + case COMMAND: + // Commands initiated by control() are simply echoed back by the unit, but + // don't guarantee that the unit's internal state has been transitioned, + // nor that the motor started moving. A subsequent status request may + // still return the previous state. Discard these messages, don't use them + // to drive the Cover state machine. + break; + + default: + // Unknown message type, drain the remaining amount of bytes specified in + // the header. + ESP_LOGE(TAG, "Reading remaining %d payload bytes of unknown type 0x%x", hdr.payload_size(), hdr.type); + break; + } + + // Drain any unhandled payload bytes described by the message header, if any. + this->drain_rx_(hdr.payload_size()); + + return {}; +} + +// Send a message to the unit requesting the gate's status. +void Tormatic::request_gate_status_() { + ESP_LOGV(TAG, "Requesting gate status"); + StatusRequest req(GATE); + this->send_message_(STATUS, req); +} + +// Send a message to the unit issuing a command. +void Tormatic::send_gate_command_(GateStatus s) { + ESP_LOGI(TAG, "Sending gate command %s", gate_status_to_str(s)); + CommandRequestReply req(s); + this->send_message_(COMMAND, req); +} + +template void Tormatic::send_message_(MessageType t, T req) { + MessageHeader hdr(t, ++this->seq_tx_, sizeof(req)); + + auto out = serialize(hdr); + auto reqv = serialize(req); + out.insert(out.end(), reqv.begin(), reqv.end()); + + this->write_array(out); +} + +template optional Tormatic::read_data_() { + T obj; + uint32_t start = millis(); + + auto ok = this->read_array((uint8_t *) &obj, sizeof(obj)); + if (!ok) { + // Couldn't read object successfully, timeout? + return {}; + } + obj.byteswap(); + + ESP_LOGV(TAG, "Read %s in %d ms", obj.print().c_str(), millis() - start); + return obj; +} + +// Drain up to n amount of bytes from the uart rx buffer. +void Tormatic::drain_rx_(uint16_t n) { + uint8_t data; + uint16_t count = 0; + while (this->available()) { + this->read_byte(&data); + count++; + + if (n > 0 && count >= n) { + return; + } + } +} + +} // namespace tormatic +} // namespace esphome diff --git a/esphome/components/tormatic/tormatic_cover.h b/esphome/components/tormatic/tormatic_cover.h new file mode 100644 index 0000000000..33a2e1db8f --- /dev/null +++ b/esphome/components/tormatic/tormatic_cover.h @@ -0,0 +1,60 @@ +#pragma once + +#include "esphome/components/uart/uart.h" +#include "esphome/components/cover/cover.h" + +#include "tormatic_protocol.h" + +namespace esphome { +namespace tormatic { + +using namespace esphome::cover; + +class Tormatic : public cover::Cover, public uart::UARTDevice, public PollingComponent { + public: + void setup() override; + void loop() override; + void update() override; + void dump_config() override; + float get_setup_priority() const override { return setup_priority::DATA; }; + + void set_open_duration(uint32_t duration) { this->open_duration_ = duration; } + void set_close_duration(uint32_t duration) { this->close_duration_ = duration; } + + void publish_state(bool save = true, uint32_t ratelimit = 0); + + cover::CoverTraits get_traits() override; + + protected: + void control(const cover::CoverCall &call) override; + + void recalibrate_duration_(GateStatus s); + void recompute_position_(); + void control_position_(float target); + void stop_at_target_(); + + template void send_message_(MessageType t, T r); + template optional read_data_(); + void drain_rx_(uint16_t n = 0); + + void request_gate_status_(); + optional read_gate_status_(); + + void send_gate_command_(GateStatus s); + void handle_gate_status_(GateStatus s); + + uint32_t seq_tx_{0}; + + GateStatus current_status_{PAUSED}; + + uint32_t open_duration_{0}; + uint32_t close_duration_{0}; + uint32_t last_publish_time_{0}; + uint32_t last_recompute_time_{0}; + uint32_t direction_start_time_{0}; + GateStatus next_command_{OPENED}; + optional target_position_{}; +}; + +} // namespace tormatic +} // namespace esphome diff --git a/esphome/components/tormatic/tormatic_protocol.h b/esphome/components/tormatic/tormatic_protocol.h new file mode 100644 index 0000000000..e26535e985 --- /dev/null +++ b/esphome/components/tormatic/tormatic_protocol.h @@ -0,0 +1,211 @@ +#pragma once + +#include "esphome/components/cover/cover.h" + +/** + * This file implements the UART protocol spoken over the on-board Micro-USB + * (Type B) connector of Tormatic and Novoferm gates manufactured as of 2016. + * All communication is initiated by the component. The unit doesn't send data + * without being asked first. + * + * There are two main message types: status requests and commands. + * + * Querying the gate's status: + * + * | sequence | length | type | payload | + * | 0xF3 0xCB | 0x00 0x00 0x00 0x06 | 0x01 0x04 | 0x00 0x0A 0x00 0x01 | + * | 0xF3 0xCB | 0x00 0x00 0x00 0x05 | 0x01 0x04 | 0x02 0x03 0x00 | + * + * This request asks for the gate status (0x0A); the only other value observed + * in the request was 0x0B, but replies were always zero. Presumably this + * queries another sensor on the unit like a safety breaker, but this is not + * relevant for an esphome cover component. + * + * The second byte of the reply is set to 0x03 when the gate is in fully open + * position. Other valid values for the second byte are: (0x0) Paused, (0x1) + * Closed, (0x2) Ventilating, (0x3) Opened, (0x4) Opening, (0x5) Closing. The + * meaning of the other bytes is currently unknown and ignored by the component. + * + * Controlling the gate: + * + * | sequence | length | type | payload | + * | 0x40 0xFF | 0x00 0x00 0x00 0x06 | 0x01 0x06 | 0x00 0x0A 0x00 0x03 | + * | 0x40 0xFF | 0x00 0x00 0x00 0x06 | 0x01 0x06 | 0x00 0x0A 0x00 0x03 | + * + * The unit acks any commands by echoing back the message in full. However, + * this does _not_ mean the gate has started closing. The component only + * considers status replies as authoritative and simply fires off commands, + * ignoring the echoed messages. + * + * The payload structure is as follows: [0x00, 0x0A] (gate), followed by + * one of the states normally carried in status replies: (0x0) Pause, (0x1) + * Close, (0x2) Ventilate (open ~20%), (0x3) Open/high-torque reverse. The + * protocol implementation in this file simply reuses the GateStatus enum + * for this purpose. + */ + +namespace esphome { +namespace tormatic { + +using namespace esphome::cover; + +// MessageType is the type of message that follows the MessageHeader. +enum MessageType : uint16_t { + STATUS = 0x0104, + COMMAND = 0x0106, +}; + +inline const char *message_type_to_str(MessageType t) { + switch (t) { + case STATUS: + return "Status"; + case COMMAND: + return "Command"; + default: + return "Unknown"; + } +} + +// MessageHeader appears at the start of every message, both requests and replies. +struct MessageHeader { + uint16_t seq; + uint32_t len; + MessageType type; + + MessageHeader() = default; + MessageHeader(MessageType type, uint16_t seq, uint32_t payload_size) { + this->type = type; + this->seq = seq; + // len includes the length of the type field. It was + // included in MessageHeader to avoid having to parse + // it as part of the payload. + this->len = payload_size + sizeof(this->type); + } + + std::string print() { + return str_sprintf("MessageHeader: seq %d, len %d, type %s", this->seq, this->len, message_type_to_str(this->type)); + } + + void byteswap() { + this->len = convert_big_endian(this->len); + this->seq = convert_big_endian(this->seq); + this->type = convert_big_endian(this->type); + } + + // payload_size returns the amount of payload bytes to be read from the uart + // buffer after reading the header. + uint32_t payload_size() { return this->len - sizeof(this->type); } +} __attribute__((packed)); + +// StatusType denotes which 'page' of information needs to be retrieved. +// On my Novoferm 423, only the GATE status type returns values, Unknown +// only contains zeroes. +enum StatusType : uint16_t { + GATE = 0x0A, + UNKNOWN = 0x0B, +}; + +// GateStatus defines the current state of the gate, received in a StatusReply +// and sent in a Command. +enum GateStatus : uint8_t { + PAUSED, + CLOSED, + VENTILATING, + OPENED, + OPENING, + CLOSING, +}; + +inline CoverOperation gate_status_to_cover_operation(GateStatus s) { + switch (s) { + case OPENING: + return COVER_OPERATION_OPENING; + case CLOSING: + return COVER_OPERATION_CLOSING; + case OPENED: + case CLOSED: + case PAUSED: + case VENTILATING: + return COVER_OPERATION_IDLE; + } + return COVER_OPERATION_IDLE; +} + +inline const char *gate_status_to_str(GateStatus s) { + switch (s) { + case PAUSED: + return "Paused"; + case CLOSED: + return "Closed"; + case VENTILATING: + return "Ventilating"; + case OPENED: + return "Opened"; + case OPENING: + return "Opening"; + case CLOSING: + return "Closing"; + default: + return "Unknown"; + } +} + +// A StatusRequest is sent to request the gate's current status. +struct StatusRequest { + StatusType type; + uint16_t trailer = 0x1; + + StatusRequest() = default; + StatusRequest(StatusType type) { this->type = type; } + + void byteswap() { + this->type = convert_big_endian(this->type); + this->trailer = convert_big_endian(this->trailer); + } +} __attribute__((packed)); + +// StatusReply is received from the unit in response to a StatusRequest. +struct StatusReply { + uint8_t ack = 0x2; + GateStatus state; + uint8_t trailer = 0x0; + + std::string print() { return str_sprintf("StatusReply: state %s", gate_status_to_str(this->state)); } + + void byteswap(){}; +} __attribute__((packed)); + +// Serialize the given object to a new byte vector. +// Invokes the object's byteswap() method. +template std::vector serialize(T obj) { + obj.byteswap(); + + std::vector out(sizeof(T)); + memcpy(out.data(), &obj, sizeof(T)); + + return out; +} + +// Command tells the gate to start or stop moving. +// It is echoed back by the unit on success. +struct CommandRequestReply { + // The part of the unit to control. For now only the gate is supported. + StatusType type = GATE; + uint8_t pad = 0x0; + // The desired state: + // PAUSED = stop + // VENTILATING = move to ~20% open + // CLOSED = close + // OPENED = open/high-torque reverse when closing + GateStatus state; + + CommandRequestReply() = default; + CommandRequestReply(GateStatus state) { this->state = state; } + + std::string print() { return str_sprintf("CommandRequestReply: state %s", gate_status_to_str(this->state)); } + + void byteswap() { this->type = convert_big_endian(this->type); } +} __attribute__((packed)); + +} // namespace tormatic +} // namespace esphome diff --git a/esphome/components/touchscreen/binary_sensor/__init__.py b/esphome/components/touchscreen/binary_sensor/__init__.py index 45fefbf814..5ce0defb31 100644 --- a/esphome/components/touchscreen/binary_sensor/__init__.py +++ b/esphome/components/touchscreen/binary_sensor/__init__.py @@ -19,6 +19,7 @@ CONF_X_MIN = "x_min" CONF_X_MAX = "x_max" CONF_Y_MIN = "y_min" CONF_Y_MAX = "y_max" +CONF_USE_RAW = "use_raw" def _validate_coords(config): @@ -46,6 +47,7 @@ CONFIG_SCHEMA = cv.All( .extend( { cv.GenerateID(CONF_TOUCHSCREEN_ID): cv.use_id(Touchscreen), + cv.Optional(CONF_USE_RAW, default=False): cv.boolean, cv.Required(CONF_X_MIN): cv.int_range(min=0, max=2000), cv.Required(CONF_X_MAX): cv.int_range(min=0, max=2000), cv.Required(CONF_Y_MIN): cv.int_range(min=0, max=2000), @@ -69,6 +71,7 @@ async def to_code(config): await cg.register_component(var, config) await cg.register_parented(var, config[CONF_TOUCHSCREEN_ID]) + cg.add(var.set_use_raw(config[CONF_USE_RAW])) cg.add( var.set_area( config[CONF_X_MIN], diff --git a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp index 6cd12d4d0d..0662cebf87 100644 --- a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp +++ b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.cpp @@ -9,7 +9,13 @@ void TouchscreenBinarySensor::setup() { } void TouchscreenBinarySensor::touch(TouchPoint tp) { - bool touched = (tp.x >= this->x_min_ && tp.x <= this->x_max_ && tp.y >= this->y_min_ && tp.y <= this->y_max_); + bool touched; + if (this->use_raw_) { + touched = + (tp.x_raw >= this->x_min_ && tp.x_raw <= this->x_max_ && tp.y_raw >= this->y_min_ && tp.y_raw <= this->y_max_); + } else { + touched = (tp.x >= this->x_min_ && tp.x <= this->x_max_ && tp.y >= this->y_min_ && tp.y <= this->y_max_); + } if (!this->pages_.empty()) { auto *current_page = this->parent_->get_display()->get_active_page(); diff --git a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h index 862f41064c..79055e6c95 100644 --- a/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h +++ b/esphome/components/touchscreen/binary_sensor/touchscreen_binary_sensor.h @@ -25,6 +25,7 @@ class TouchscreenBinarySensor : public binary_sensor::BinarySensor, this->y_min_ = y_min; this->y_max_ = y_max; } + void set_use_raw(bool use_raw) { this->use_raw_ = use_raw; } int16_t get_x_min() { return this->x_min_; } int16_t get_x_max() { return this->x_max_; } int16_t get_y_min() { return this->y_min_; } @@ -38,7 +39,8 @@ class TouchscreenBinarySensor : public binary_sensor::BinarySensor, void release() override; protected: - int16_t x_min_, x_max_, y_min_, y_max_; + int16_t x_min_{}, x_max_{}, y_min_{}, y_max_{}; + bool use_raw_{}; std::vector pages_{}; }; diff --git a/esphome/components/touchscreen/touchscreen.cpp b/esphome/components/touchscreen/touchscreen.cpp index dfe723aedf..11207908fa 100644 --- a/esphome/components/touchscreen/touchscreen.cpp +++ b/esphome/components/touchscreen/touchscreen.cpp @@ -74,6 +74,9 @@ void Touchscreen::loop() { void Touchscreen::add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_raw, int16_t z_raw) { TouchPoint tp; uint16_t x, y; + if (this->swap_x_y_) { + std::swap(x_raw, y_raw); + } if (this->touches_.count(id) == 0) { tp.state = STATE_PRESSED; tp.id = id; @@ -90,10 +93,6 @@ void Touchscreen::add_raw_touch_position_(uint8_t id, int16_t x_raw, int16_t y_r x = this->normalize_(x_raw, this->x_raw_min_, this->x_raw_max_, this->invert_x_); y = this->normalize_(y_raw, this->y_raw_min_, this->y_raw_max_, this->invert_y_); - if (this->swap_x_y_) { - std::swap(x, y); - } - tp.x = (uint16_t) ((int) x * this->display_width_ / 0x1000); tp.y = (uint16_t) ((int) y * this->display_height_ / 0x1000); } else { diff --git a/esphome/components/udp/udp_component.h b/esphome/components/udp/udp_component.h index fb9b93e255..02f998ded7 100644 --- a/esphome/components/udp/udp_component.h +++ b/esphome/components/udp/udp_component.h @@ -70,7 +70,9 @@ class UDPComponent : public PollingComponent { } #endif void add_address(const char *addr) { this->addresses_.emplace_back(addr); } +#ifdef USE_NETWORK void set_listen_address(const char *listen_addr) { this->listen_address_ = network::IPAddress(listen_addr); } +#endif void set_port(uint16_t port) { this->port_ = port; } float get_setup_priority() const override { return setup_priority::AFTER_WIFI; } @@ -144,8 +146,9 @@ class UDPComponent : public PollingComponent { std::vector binary_sensors_{}; std::map> remote_binary_sensors_{}; #endif - +#ifdef USE_NETWORK optional listen_address_{}; +#endif std::map providers_{}; std::vector ping_header_{}; std::vector header_{}; diff --git a/esphome/components/web_server/list_entities.cpp b/esphome/components/web_server/list_entities.cpp index a02f84c34b..fb02821760 100644 --- a/esphome/components/web_server/list_entities.cpp +++ b/esphome/components/web_server/list_entities.cpp @@ -9,180 +9,184 @@ namespace esphome { namespace web_server { -ListEntitiesIterator::ListEntitiesIterator(WebServer *web_server) : web_server_(web_server) {} +#ifdef USE_ARDUINO +ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es) + : web_server_(ws), events_(es) {} +#endif +#ifdef USE_ESP_IDF +ListEntitiesIterator::ListEntitiesIterator(const WebServer *ws, AsyncEventSource *es) : web_server_(ws), events_(es) {} +#endif +ListEntitiesIterator::~ListEntitiesIterator() {} #ifdef USE_BINARY_SENSOR -bool ListEntitiesIterator::on_binary_sensor(binary_sensor::BinarySensor *binary_sensor) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_binary_sensor(binary_sensor::BinarySensor *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send( - this->web_server_->binary_sensor_json(binary_sensor, binary_sensor->state, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::binary_sensor_all_json_generator); return true; } #endif #ifdef USE_COVER -bool ListEntitiesIterator::on_cover(cover::Cover *cover) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_cover(cover::Cover *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->cover_json(cover, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::cover_all_json_generator); return true; } #endif #ifdef USE_FAN -bool ListEntitiesIterator::on_fan(fan::Fan *fan) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_fan(fan::Fan *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->fan_json(fan, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::fan_all_json_generator); return true; } #endif #ifdef USE_LIGHT -bool ListEntitiesIterator::on_light(light::LightState *light) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_light(light::LightState *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->light_json(light, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::light_all_json_generator); return true; } #endif #ifdef USE_SENSOR -bool ListEntitiesIterator::on_sensor(sensor::Sensor *sensor) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_sensor(sensor::Sensor *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->sensor_json(sensor, sensor->state, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::sensor_all_json_generator); return true; } #endif #ifdef USE_SWITCH -bool ListEntitiesIterator::on_switch(switch_::Switch *a_switch) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_switch(switch_::Switch *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->switch_json(a_switch, a_switch->state, DETAIL_ALL).c_str(), - "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::switch_all_json_generator); return true; } #endif #ifdef USE_BUTTON -bool ListEntitiesIterator::on_button(button::Button *button) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_button(button::Button *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->button_json(button, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::button_all_json_generator); return true; } #endif #ifdef USE_TEXT_SENSOR -bool ListEntitiesIterator::on_text_sensor(text_sensor::TextSensor *text_sensor) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_text_sensor(text_sensor::TextSensor *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send( - this->web_server_->text_sensor_json(text_sensor, text_sensor->state, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::text_sensor_all_json_generator); return true; } #endif #ifdef USE_LOCK -bool ListEntitiesIterator::on_lock(lock::Lock *a_lock) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_lock(lock::Lock *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->lock_json(a_lock, a_lock->state, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::lock_all_json_generator); return true; } #endif #ifdef USE_VALVE -bool ListEntitiesIterator::on_valve(valve::Valve *valve) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_valve(valve::Valve *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->valve_json(valve, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::valve_all_json_generator); return true; } #endif #ifdef USE_CLIMATE -bool ListEntitiesIterator::on_climate(climate::Climate *climate) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_climate(climate::Climate *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->climate_json(climate, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::climate_all_json_generator); return true; } #endif #ifdef USE_NUMBER -bool ListEntitiesIterator::on_number(number::Number *number) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_number(number::Number *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->number_json(number, number->state, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::number_all_json_generator); return true; } #endif #ifdef USE_DATETIME_DATE -bool ListEntitiesIterator::on_date(datetime::DateEntity *date) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_date(datetime::DateEntity *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->date_json(date, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::date_all_json_generator); return true; } #endif #ifdef USE_DATETIME_TIME -bool ListEntitiesIterator::on_time(datetime::TimeEntity *time) { - this->web_server_->events_.send(this->web_server_->time_json(time, DETAIL_ALL).c_str(), "state"); +bool ListEntitiesIterator::on_time(datetime::TimeEntity *obj) { + if (this->events_->count() == 0) + return true; + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::time_all_json_generator); return true; } #endif #ifdef USE_DATETIME_DATETIME -bool ListEntitiesIterator::on_datetime(datetime::DateTimeEntity *datetime) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_datetime(datetime::DateTimeEntity *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->datetime_json(datetime, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::datetime_all_json_generator); return true; } #endif #ifdef USE_TEXT -bool ListEntitiesIterator::on_text(text::Text *text) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_text(text::Text *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->text_json(text, text->state, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::text_all_json_generator); return true; } #endif #ifdef USE_SELECT -bool ListEntitiesIterator::on_select(select::Select *select) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_select(select::Select *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->select_json(select, select->state, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::select_all_json_generator); return true; } #endif #ifdef USE_ALARM_CONTROL_PANEL -bool ListEntitiesIterator::on_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_alarm_control_panel(alarm_control_panel::AlarmControlPanel *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send( - this->web_server_->alarm_control_panel_json(a_alarm_control_panel, a_alarm_control_panel->get_state(), DETAIL_ALL) - .c_str(), - "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::alarm_control_panel_all_json_generator); return true; } #endif #ifdef USE_EVENT -bool ListEntitiesIterator::on_event(event::Event *event) { +bool ListEntitiesIterator::on_event(event::Event *obj) { + if (this->events_->count() == 0) + return true; // Null event type, since we are just iterating over entities - const std::string null_event_type = ""; - this->web_server_->events_.send(this->web_server_->event_json(event, null_event_type, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::event_all_json_generator); return true; } #endif #ifdef USE_UPDATE -bool ListEntitiesIterator::on_update(update::UpdateEntity *update) { - if (this->web_server_->events_.count() == 0) +bool ListEntitiesIterator::on_update(update::UpdateEntity *obj) { + if (this->events_->count() == 0) return true; - this->web_server_->events_.send(this->web_server_->update_json(update, DETAIL_ALL).c_str(), "state"); + this->events_->deferrable_send_state(obj, "state_detail_all", WebServer::update_all_json_generator); return true; } #endif diff --git a/esphome/components/web_server/list_entities.h b/esphome/components/web_server/list_entities.h index 53e5bc3355..ba81c70c86 100644 --- a/esphome/components/web_server/list_entities.h +++ b/esphome/components/web_server/list_entities.h @@ -5,76 +5,97 @@ #include "esphome/core/component.h" #include "esphome/core/component_iterator.h" namespace esphome { +#ifdef USE_ESP_IDF +namespace web_server_idf { +class AsyncEventSource; +} +#endif namespace web_server { +#ifdef USE_ARDUINO +class DeferredUpdateEventSource; +#endif class WebServer; class ListEntitiesIterator : public ComponentIterator { public: - ListEntitiesIterator(WebServer *web_server); +#ifdef USE_ARDUINO + ListEntitiesIterator(const WebServer *ws, DeferredUpdateEventSource *es); +#endif +#ifdef USE_ESP_IDF + ListEntitiesIterator(const WebServer *ws, esphome::web_server_idf::AsyncEventSource *es); +#endif + virtual ~ListEntitiesIterator(); #ifdef USE_BINARY_SENSOR - bool on_binary_sensor(binary_sensor::BinarySensor *binary_sensor) override; + bool on_binary_sensor(binary_sensor::BinarySensor *obj) override; #endif #ifdef USE_COVER - bool on_cover(cover::Cover *cover) override; + bool on_cover(cover::Cover *obj) override; #endif #ifdef USE_FAN - bool on_fan(fan::Fan *fan) override; + bool on_fan(fan::Fan *obj) override; #endif #ifdef USE_LIGHT - bool on_light(light::LightState *light) override; + bool on_light(light::LightState *obj) override; #endif #ifdef USE_SENSOR - bool on_sensor(sensor::Sensor *sensor) override; + bool on_sensor(sensor::Sensor *obj) override; #endif #ifdef USE_SWITCH - bool on_switch(switch_::Switch *a_switch) override; + bool on_switch(switch_::Switch *obj) override; #endif #ifdef USE_BUTTON - bool on_button(button::Button *button) override; + bool on_button(button::Button *obj) override; #endif #ifdef USE_TEXT_SENSOR - bool on_text_sensor(text_sensor::TextSensor *text_sensor) override; + bool on_text_sensor(text_sensor::TextSensor *obj) override; #endif #ifdef USE_CLIMATE - bool on_climate(climate::Climate *climate) override; + bool on_climate(climate::Climate *obj) override; #endif #ifdef USE_NUMBER - bool on_number(number::Number *number) override; + bool on_number(number::Number *obj) override; #endif #ifdef USE_DATETIME_DATE - bool on_date(datetime::DateEntity *date) override; + bool on_date(datetime::DateEntity *obj) override; #endif #ifdef USE_DATETIME_TIME - bool on_time(datetime::TimeEntity *time) override; + bool on_time(datetime::TimeEntity *obj) override; #endif #ifdef USE_DATETIME_DATETIME - bool on_datetime(datetime::DateTimeEntity *datetime) override; + bool on_datetime(datetime::DateTimeEntity *obj) override; #endif #ifdef USE_TEXT - bool on_text(text::Text *text) override; + bool on_text(text::Text *obj) override; #endif #ifdef USE_SELECT - bool on_select(select::Select *select) override; + bool on_select(select::Select *obj) override; #endif #ifdef USE_LOCK - bool on_lock(lock::Lock *a_lock) override; + bool on_lock(lock::Lock *obj) override; #endif #ifdef USE_VALVE - bool on_valve(valve::Valve *valve) override; + bool on_valve(valve::Valve *obj) override; #endif #ifdef USE_ALARM_CONTROL_PANEL - bool on_alarm_control_panel(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) override; + bool on_alarm_control_panel(alarm_control_panel::AlarmControlPanel *obj) override; #endif #ifdef USE_EVENT - bool on_event(event::Event *event) override; + bool on_event(event::Event *obj) override; #endif #ifdef USE_UPDATE - bool on_update(update::UpdateEntity *update) override; + bool on_update(update::UpdateEntity *obj) override; #endif + bool completed() { return this->state_ == IteratorState::NONE; } protected: - WebServer *web_server_; + const WebServer *web_server_; +#ifdef USE_ARDUINO + DeferredUpdateEventSource *events_; +#endif +#ifdef USE_ESP_IDF + esphome::web_server_idf::AsyncEventSource *events_; +#endif }; } // namespace web_server diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index 8c09d607a7..63c1d5d4fd 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -72,8 +72,146 @@ UrlMatch match_url(const std::string &url, bool only_domain = false) { return match; } -WebServer::WebServer(web_server_base::WebServerBase *base) - : base_(base), entities_iterator_(ListEntitiesIterator(this)) { +#ifdef USE_ARDUINO +// helper for allowing only unique entries in the queue +void DeferredUpdateEventSource::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) { + DeferredEvent item(source, message_generator); + + auto iter = std::find_if(this->deferred_queue_.begin(), this->deferred_queue_.end(), + [&item](const DeferredEvent &test) -> bool { return test == item; }); + + if (iter != this->deferred_queue_.end()) { + (*iter) = item; + } else { + this->deferred_queue_.push_back(item); + } +} + +void DeferredUpdateEventSource::process_deferred_queue_() { + while (!deferred_queue_.empty()) { + DeferredEvent &de = deferred_queue_.front(); + std::string message = de.message_generator_(web_server_, de.source_); + if (this->try_send(message.c_str(), "state")) { + // O(n) but memory efficiency is more important than speed here which is why std::vector was chosen + deferred_queue_.erase(deferred_queue_.begin()); + } else { + break; + } + } +} + +void DeferredUpdateEventSource::loop() { + process_deferred_queue_(); + if (!this->entities_iterator_.completed()) + this->entities_iterator_.advance(); +} + +void DeferredUpdateEventSource::deferrable_send_state(void *source, const char *event_type, + message_generator_t *message_generator) { + // allow all json "details_all" to go through before publishing bare state events, this avoids unnamed entries showing + // up in the web GUI and reduces event load during initial connect + if (!entities_iterator_.completed() && 0 != strcmp(event_type, "state_detail_all")) + return; + + if (source == nullptr) + return; + if (event_type == nullptr) + return; + if (message_generator == nullptr) + return; + + if (0 != strcmp(event_type, "state_detail_all") && 0 != strcmp(event_type, "state")) { + ESP_LOGE(TAG, "Can't defer non-state event"); + } + + if (!deferred_queue_.empty()) + process_deferred_queue_(); + if (!deferred_queue_.empty()) { + // deferred queue still not empty which means downstream event queue full, no point trying to send first + deq_push_back_with_dedup_(source, message_generator); + } else { + std::string message = message_generator(web_server_, source); + if (!this->try_send(message.c_str(), "state")) { + deq_push_back_with_dedup_(source, message_generator); + } + } +} + +// used for logs plus the initial ping/config +void DeferredUpdateEventSource::try_send_nodefer(const char *message, const char *event, uint32_t id, + uint32_t reconnect) { + this->send(message, event, id, reconnect); +} + +void DeferredUpdateEventSourceList::loop() { + for (DeferredUpdateEventSource *dues : *this) { + dues->loop(); + } +} + +void DeferredUpdateEventSourceList::deferrable_send_state(void *source, const char *event_type, + message_generator_t *message_generator) { + for (DeferredUpdateEventSource *dues : *this) { + dues->deferrable_send_state(source, event_type, message_generator); + } +} + +void DeferredUpdateEventSourceList::try_send_nodefer(const char *message, const char *event, uint32_t id, + uint32_t reconnect) { + for (DeferredUpdateEventSource *dues : *this) { + dues->try_send_nodefer(message, event, id, reconnect); + } +} + +void DeferredUpdateEventSourceList::add_new_client(WebServer *ws, AsyncWebServerRequest *request) { + DeferredUpdateEventSource *es = new DeferredUpdateEventSource(ws, "/events"); + this->push_back(es); + + es->onConnect([this, ws, es](AsyncEventSourceClient *client) { + ws->defer([this, ws, es]() { this->on_client_connect_(ws, es); }); + }); + + es->onDisconnect([this, ws](AsyncEventSource *source, AsyncEventSourceClient *client) { + ws->defer([this, source]() { this->on_client_disconnect_((DeferredUpdateEventSource *) source); }); + }); + + es->handleRequest(request); +} + +void DeferredUpdateEventSourceList::on_client_connect_(WebServer *ws, DeferredUpdateEventSource *source) { + // Configure reconnect timeout and send config + // this should always go through since the AsyncEventSourceClient event queue is empty on connect + std::string message = ws->get_config_json(); + source->try_send_nodefer(message.c_str(), "ping", millis(), 30000); + + for (auto &group : ws->sorting_groups_) { + message = json::build_json([group](JsonObject root) { + root["name"] = group.second.name; + root["sorting_weight"] = group.second.weight; + }); + + // up to 31 groups should be able to be queued initially without defer + source->try_send_nodefer(message.c_str(), "sorting_group"); + } + + source->entities_iterator_.begin(ws->include_internal_); + + // just dump them all up-front and take advantage of the deferred queue + // on second thought that takes too long, but leaving the commented code here for debug purposes + // while(!source->entities_iterator_.completed()) { + // source->entities_iterator_.advance(); + //} +} + +void DeferredUpdateEventSourceList::on_client_disconnect_(DeferredUpdateEventSource *source) { + // This method was called via WebServer->defer() and is no longer executing in the + // context of the network callback. The object is now dead and can be safely deleted. + this->remove(source); + delete source; // NOLINT +} +#endif + +WebServer::WebServer(web_server_base::WebServerBase *base) : base_(base) { #ifdef USE_ESP32 to_schedule_lock_ = xSemaphoreCreateMutex(); #endif @@ -101,34 +239,27 @@ void WebServer::setup() { this->setup_controller(this->include_internal_); this->base_->init(); - this->events_.onConnect([this](AsyncEventSourceClient *client) { - // Configure reconnect timeout and send config - client->send(this->get_config_json().c_str(), "ping", millis(), 30000); - - for (auto &group : this->sorting_groups_) { - client->send(json::build_json([group](JsonObject root) { - root["name"] = group.second.name; - root["sorting_weight"] = group.second.weight; - }).c_str(), - "sorting_group"); - } - - this->entities_iterator_.begin(this->include_internal_); - }); - #ifdef USE_LOGGER if (logger::global_logger != nullptr && this->expose_log_) { logger::global_logger->add_on_log_callback( - [this](int level, const char *tag, const char *message) { this->events_.send(message, "log", millis()); }); + // logs are not deferred, the memory overhead would be too large + [this](int level, const char *tag, const char *message) { + this->events_.try_send_nodefer(message, "log", millis()); + }); } #endif + +#ifdef USE_ESP_IDF this->base_->add_handler(&this->events_); +#endif this->base_->add_handler(this); if (this->allow_ota_) this->base_->add_ota_handler(); - this->set_interval(10000, [this]() { this->events_.send("", "ping", millis(), 30000); }); + // doesn't need defer functionality - if the queue is full, the client JS knows it's alive because it's clearly + // getting a lot of events + this->set_interval(10000, [this]() { this->events_.try_send_nodefer("", "ping", millis(), 30000); }); } void WebServer::loop() { #ifdef USE_ESP32 @@ -147,7 +278,8 @@ void WebServer::loop() { } } #endif - this->entities_iterator_.advance(); + + this->events_.loop(); } void WebServer::dump_config() { ESP_LOGCONFIG(TAG, "Web Server:"); @@ -219,9 +351,9 @@ void WebServer::handle_js_request(AsyncWebServerRequest *request) { #ifdef USE_SENSOR void WebServer::on_sensor_update(sensor::Sensor *obj, float state) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->sensor_json(obj, state, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", sensor_state_json_generator); } void WebServer::handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (sensor::Sensor *obj : App.get_sensors()) { @@ -240,6 +372,12 @@ void WebServer::handle_sensor_request(AsyncWebServerRequest *request, const UrlM } request->send(404); } +std::string WebServer::sensor_state_json_generator(WebServer *web_server, void *source) { + return web_server->sensor_json((sensor::Sensor *) (source), ((sensor::Sensor *) (source))->state, DETAIL_STATE); +} +std::string WebServer::sensor_all_json_generator(WebServer *web_server, void *source) { + return web_server->sensor_json((sensor::Sensor *) (source), ((sensor::Sensor *) (source))->state, DETAIL_ALL); +} std::string WebServer::sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config) { return json::build_json([this, obj, value, start_config](JsonObject root) { std::string state; @@ -267,9 +405,9 @@ std::string WebServer::sensor_json(sensor::Sensor *obj, float value, JsonDetail #ifdef USE_TEXT_SENSOR void WebServer::on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->text_sensor_json(obj, state, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", text_sensor_state_json_generator); } void WebServer::handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (text_sensor::TextSensor *obj : App.get_text_sensors()) { @@ -288,6 +426,14 @@ void WebServer::handle_text_sensor_request(AsyncWebServerRequest *request, const } request->send(404); } +std::string WebServer::text_sensor_state_json_generator(WebServer *web_server, void *source) { + return web_server->text_sensor_json((text_sensor::TextSensor *) (source), + ((text_sensor::TextSensor *) (source))->state, DETAIL_STATE); +} +std::string WebServer::text_sensor_all_json_generator(WebServer *web_server, void *source) { + return web_server->text_sensor_json((text_sensor::TextSensor *) (source), + ((text_sensor::TextSensor *) (source))->state, DETAIL_ALL); +} std::string WebServer::text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config) { return json::build_json([this, obj, value, start_config](JsonObject root) { @@ -306,9 +452,9 @@ std::string WebServer::text_sensor_json(text_sensor::TextSensor *obj, const std: #ifdef USE_SWITCH void WebServer::on_switch_update(switch_::Switch *obj, bool state) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->switch_json(obj, state, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", switch_state_json_generator); } void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (switch_::Switch *obj : App.get_switches()) { @@ -339,6 +485,12 @@ void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlM } request->send(404); } +std::string WebServer::switch_state_json_generator(WebServer *web_server, void *source) { + return web_server->switch_json((switch_::Switch *) (source), ((switch_::Switch *) (source))->state, DETAIL_STATE); +} +std::string WebServer::switch_all_json_generator(WebServer *web_server, void *source) { + return web_server->switch_json((switch_::Switch *) (source), ((switch_::Switch *) (source))->state, DETAIL_ALL); +} std::string WebServer::switch_json(switch_::Switch *obj, bool value, JsonDetail start_config) { return json::build_json([this, obj, value, start_config](JsonObject root) { set_json_icon_state_value(root, obj, "switch-" + obj->get_object_id(), value ? "ON" : "OFF", value, start_config); @@ -379,6 +531,12 @@ void WebServer::handle_button_request(AsyncWebServerRequest *request, const UrlM } request->send(404); } +std::string WebServer::button_state_json_generator(WebServer *web_server, void *source) { + return web_server->button_json((button::Button *) (source), DETAIL_STATE); +} +std::string WebServer::button_all_json_generator(WebServer *web_server, void *source) { + return web_server->button_json((button::Button *) (source), DETAIL_ALL); +} std::string WebServer::button_json(button::Button *obj, JsonDetail start_config) { return json::build_json([this, obj, start_config](JsonObject root) { set_json_id(root, obj, "button-" + obj->get_object_id(), start_config); @@ -396,9 +554,9 @@ std::string WebServer::button_json(button::Button *obj, JsonDetail start_config) #ifdef USE_BINARY_SENSOR void WebServer::on_binary_sensor_update(binary_sensor::BinarySensor *obj, bool state) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->binary_sensor_json(obj, state, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", binary_sensor_state_json_generator); } void WebServer::handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (binary_sensor::BinarySensor *obj : App.get_binary_sensors()) { @@ -417,6 +575,14 @@ void WebServer::handle_binary_sensor_request(AsyncWebServerRequest *request, con } request->send(404); } +std::string WebServer::binary_sensor_state_json_generator(WebServer *web_server, void *source) { + return web_server->binary_sensor_json((binary_sensor::BinarySensor *) (source), + ((binary_sensor::BinarySensor *) (source))->state, DETAIL_STATE); +} +std::string WebServer::binary_sensor_all_json_generator(WebServer *web_server, void *source) { + return web_server->binary_sensor_json((binary_sensor::BinarySensor *) (source), + ((binary_sensor::BinarySensor *) (source))->state, DETAIL_ALL); +} std::string WebServer::binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config) { return json::build_json([this, obj, value, start_config](JsonObject root) { set_json_icon_state_value(root, obj, "binary_sensor-" + obj->get_object_id(), value ? "ON" : "OFF", value, @@ -435,9 +601,9 @@ std::string WebServer::binary_sensor_json(binary_sensor::BinarySensor *obj, bool #ifdef USE_FAN void WebServer::on_fan_update(fan::Fan *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->fan_json(obj, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", fan_state_json_generator); } void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (fan::Fan *obj : App.get_fans()) { @@ -494,6 +660,12 @@ void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatc } request->send(404); } +std::string WebServer::fan_state_json_generator(WebServer *web_server, void *source) { + return web_server->fan_json((fan::Fan *) (source), DETAIL_STATE); +} +std::string WebServer::fan_all_json_generator(WebServer *web_server, void *source) { + return web_server->fan_json((fan::Fan *) (source), DETAIL_ALL); +} std::string WebServer::fan_json(fan::Fan *obj, JsonDetail start_config) { return json::build_json([this, obj, start_config](JsonObject root) { set_json_icon_state_value(root, obj, "fan-" + obj->get_object_id(), obj->state ? "ON" : "OFF", obj->state, @@ -519,9 +691,9 @@ std::string WebServer::fan_json(fan::Fan *obj, JsonDetail start_config) { #ifdef USE_LIGHT void WebServer::on_light_update(light::LightState *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->light_json(obj, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", light_state_json_generator); } void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (light::LightState *obj : App.get_lights()) { @@ -613,6 +785,12 @@ void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMa } request->send(404); } +std::string WebServer::light_state_json_generator(WebServer *web_server, void *source) { + return web_server->light_json((light::LightState *) (source), DETAIL_STATE); +} +std::string WebServer::light_all_json_generator(WebServer *web_server, void *source) { + return web_server->light_json((light::LightState *) (source), DETAIL_ALL); +} std::string WebServer::light_json(light::LightState *obj, JsonDetail start_config) { return json::build_json([this, obj, start_config](JsonObject root) { set_json_id(root, obj, "light-" + obj->get_object_id(), start_config); @@ -638,9 +816,9 @@ std::string WebServer::light_json(light::LightState *obj, JsonDetail start_confi #ifdef USE_COVER void WebServer::on_cover_update(cover::Cover *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->cover_json(obj, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", cover_state_json_generator); } void WebServer::handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (cover::Cover *obj : App.get_covers()) { @@ -698,6 +876,12 @@ void WebServer::handle_cover_request(AsyncWebServerRequest *request, const UrlMa } request->send(404); } +std::string WebServer::cover_state_json_generator(WebServer *web_server, void *source) { + return web_server->cover_json((cover::Cover *) (source), DETAIL_STATE); +} +std::string WebServer::cover_all_json_generator(WebServer *web_server, void *source) { + return web_server->cover_json((cover::Cover *) (source), DETAIL_STATE); +} std::string WebServer::cover_json(cover::Cover *obj, JsonDetail start_config) { return json::build_json([this, obj, start_config](JsonObject root) { set_json_icon_state_value(root, obj, "cover-" + obj->get_object_id(), obj->is_fully_closed() ? "CLOSED" : "OPEN", @@ -722,9 +906,9 @@ std::string WebServer::cover_json(cover::Cover *obj, JsonDetail start_config) { #ifdef USE_NUMBER void WebServer::on_number_update(number::Number *obj, float state) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->number_json(obj, state, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", number_state_json_generator); } void WebServer::handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (auto *obj : App.get_numbers()) { @@ -760,6 +944,12 @@ void WebServer::handle_number_request(AsyncWebServerRequest *request, const UrlM request->send(404); } +std::string WebServer::number_state_json_generator(WebServer *web_server, void *source) { + return web_server->number_json((number::Number *) (source), ((number::Number *) (source))->state, DETAIL_STATE); +} +std::string WebServer::number_all_json_generator(WebServer *web_server, void *source) { + return web_server->number_json((number::Number *) (source), ((number::Number *) (source))->state, DETAIL_ALL); +} std::string WebServer::number_json(number::Number *obj, float value, JsonDetail start_config) { return json::build_json([this, obj, value, start_config](JsonObject root) { set_json_id(root, obj, "number-" + obj->get_object_id(), start_config); @@ -796,9 +986,9 @@ std::string WebServer::number_json(number::Number *obj, float value, JsonDetail #ifdef USE_DATETIME_DATE void WebServer::on_date_update(datetime::DateEntity *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->date_json(obj, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", date_state_json_generator); } void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (auto *obj : App.get_dates()) { @@ -827,7 +1017,7 @@ void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMat } if (request->hasParam("value")) { - std::string value = request->getParam("value")->value().c_str(); + std::string value = request->getParam("value")->value().c_str(); // NOLINT call.set_date(value); } @@ -838,6 +1028,12 @@ void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMat request->send(404); } +std::string WebServer::date_state_json_generator(WebServer *web_server, void *source) { + return web_server->date_json((datetime::DateEntity *) (source), DETAIL_STATE); +} +std::string WebServer::date_all_json_generator(WebServer *web_server, void *source) { + return web_server->date_json((datetime::DateEntity *) (source), DETAIL_ALL); +} std::string WebServer::date_json(datetime::DateEntity *obj, JsonDetail start_config) { return json::build_json([this, obj, start_config](JsonObject root) { set_json_id(root, obj, "date-" + obj->get_object_id(), start_config); @@ -858,9 +1054,9 @@ std::string WebServer::date_json(datetime::DateEntity *obj, JsonDetail start_con #ifdef USE_DATETIME_TIME void WebServer::on_time_update(datetime::TimeEntity *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->time_json(obj, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", time_state_json_generator); } void WebServer::handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (auto *obj : App.get_times()) { @@ -889,7 +1085,7 @@ void WebServer::handle_time_request(AsyncWebServerRequest *request, const UrlMat } if (request->hasParam("value")) { - std::string value = request->getParam("value")->value().c_str(); + std::string value = request->getParam("value")->value().c_str(); // NOLINT call.set_time(value); } @@ -899,6 +1095,12 @@ void WebServer::handle_time_request(AsyncWebServerRequest *request, const UrlMat } request->send(404); } +std::string WebServer::time_state_json_generator(WebServer *web_server, void *source) { + return web_server->time_json((datetime::TimeEntity *) (source), DETAIL_STATE); +} +std::string WebServer::time_all_json_generator(WebServer *web_server, void *source) { + return web_server->time_json((datetime::TimeEntity *) (source), DETAIL_ALL); +} std::string WebServer::time_json(datetime::TimeEntity *obj, JsonDetail start_config) { return json::build_json([this, obj, start_config](JsonObject root) { set_json_id(root, obj, "time-" + obj->get_object_id(), start_config); @@ -919,9 +1121,9 @@ std::string WebServer::time_json(datetime::TimeEntity *obj, JsonDetail start_con #ifdef USE_DATETIME_DATETIME void WebServer::on_datetime_update(datetime::DateTimeEntity *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->datetime_json(obj, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", datetime_state_json_generator); } void WebServer::handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (auto *obj : App.get_datetimes()) { @@ -950,7 +1152,7 @@ void WebServer::handle_datetime_request(AsyncWebServerRequest *request, const Ur } if (request->hasParam("value")) { - std::string value = request->getParam("value")->value().c_str(); + std::string value = request->getParam("value")->value().c_str(); // NOLINT call.set_datetime(value); } @@ -960,6 +1162,12 @@ void WebServer::handle_datetime_request(AsyncWebServerRequest *request, const Ur } request->send(404); } +std::string WebServer::datetime_state_json_generator(WebServer *web_server, void *source) { + return web_server->datetime_json((datetime::DateTimeEntity *) (source), DETAIL_STATE); +} +std::string WebServer::datetime_all_json_generator(WebServer *web_server, void *source) { + return web_server->datetime_json((datetime::DateTimeEntity *) (source), DETAIL_ALL); +} std::string WebServer::datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config) { return json::build_json([this, obj, start_config](JsonObject root) { set_json_id(root, obj, "datetime-" + obj->get_object_id(), start_config); @@ -981,9 +1189,9 @@ std::string WebServer::datetime_json(datetime::DateTimeEntity *obj, JsonDetail s #ifdef USE_TEXT void WebServer::on_text_update(text::Text *obj, const std::string &state) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->text_json(obj, state, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", text_state_json_generator); } void WebServer::handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (auto *obj : App.get_texts()) { @@ -1008,7 +1216,7 @@ void WebServer::handle_text_request(AsyncWebServerRequest *request, const UrlMat auto call = obj->make_call(); if (request->hasParam("value")) { String value = request->getParam("value")->value(); - call.set_value(value.c_str()); + call.set_value(value.c_str()); // NOLINT } this->defer([call]() mutable { call.perform(); }); @@ -1018,6 +1226,12 @@ void WebServer::handle_text_request(AsyncWebServerRequest *request, const UrlMat request->send(404); } +std::string WebServer::text_state_json_generator(WebServer *web_server, void *source) { + return web_server->text_json((text::Text *) (source), ((text::Text *) (source))->state, DETAIL_STATE); +} +std::string WebServer::text_all_json_generator(WebServer *web_server, void *source) { + return web_server->text_json((text::Text *) (source), ((text::Text *) (source))->state, DETAIL_ALL); +} std::string WebServer::text_json(text::Text *obj, const std::string &value, JsonDetail start_config) { return json::build_json([this, obj, value, start_config](JsonObject root) { set_json_id(root, obj, "text-" + obj->get_object_id(), start_config); @@ -1045,9 +1259,9 @@ std::string WebServer::text_json(text::Text *obj, const std::string &value, Json #ifdef USE_SELECT void WebServer::on_select_update(select::Select *obj, const std::string &state, size_t index) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->select_json(obj, state, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", select_state_json_generator); } void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (auto *obj : App.get_selects()) { @@ -1074,7 +1288,7 @@ void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlM if (request->hasParam("option")) { auto option = request->getParam("option")->value(); - call.set_option(option.c_str()); // NOLINT(clang-diagnostic-deprecated-declarations) + call.set_option(option.c_str()); // NOLINT } this->schedule_([call]() mutable { call.perform(); }); @@ -1083,6 +1297,12 @@ void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlM } request->send(404); } +std::string WebServer::select_state_json_generator(WebServer *web_server, void *source) { + return web_server->select_json((select::Select *) (source), ((select::Select *) (source))->state, DETAIL_STATE); +} +std::string WebServer::select_all_json_generator(WebServer *web_server, void *source) { + return web_server->select_json((select::Select *) (source), ((select::Select *) (source))->state, DETAIL_ALL); +} std::string WebServer::select_json(select::Select *obj, const std::string &value, JsonDetail start_config) { return json::build_json([this, obj, value, start_config](JsonObject root) { set_json_icon_state_value(root, obj, "select-" + obj->get_object_id(), value, value, start_config); @@ -1107,9 +1327,9 @@ std::string WebServer::select_json(select::Select *obj, const std::string &value #ifdef USE_CLIMATE void WebServer::on_climate_update(climate::Climate *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->climate_json(obj, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", climate_state_json_generator); } void WebServer::handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (auto *obj : App.get_climates()) { @@ -1126,6 +1346,7 @@ void WebServer::handle_climate_request(AsyncWebServerRequest *request, const Url request->send(200, "application/json", data.c_str()); return; } + if (match.method != "set") { request->send(404); return; @@ -1135,17 +1356,17 @@ void WebServer::handle_climate_request(AsyncWebServerRequest *request, const Url if (request->hasParam("mode")) { auto mode = request->getParam("mode")->value(); - call.set_mode(mode.c_str()); + call.set_mode(mode.c_str()); // NOLINT } if (request->hasParam("fan_mode")) { auto mode = request->getParam("fan_mode")->value(); - call.set_fan_mode(mode.c_str()); + call.set_fan_mode(mode.c_str()); // NOLINT } if (request->hasParam("swing_mode")) { auto mode = request->getParam("swing_mode")->value(); - call.set_swing_mode(mode.c_str()); + call.set_swing_mode(mode.c_str()); // NOLINT } if (request->hasParam("target_temperature_high")) { @@ -1172,6 +1393,12 @@ void WebServer::handle_climate_request(AsyncWebServerRequest *request, const Url } request->send(404); } +std::string WebServer::climate_state_json_generator(WebServer *web_server, void *source) { + return web_server->climate_json((climate::Climate *) (source), DETAIL_STATE); +} +std::string WebServer::climate_all_json_generator(WebServer *web_server, void *source) { + return web_server->climate_json((climate::Climate *) (source), DETAIL_ALL); +} std::string WebServer::climate_json(climate::Climate *obj, JsonDetail start_config) { return json::build_json([this, obj, start_config](JsonObject root) { set_json_id(root, obj, "climate-" + obj->get_object_id(), start_config); @@ -1268,9 +1495,9 @@ std::string WebServer::climate_json(climate::Climate *obj, JsonDetail start_conf #ifdef USE_LOCK void WebServer::on_lock_update(lock::Lock *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->lock_json(obj, obj->state, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", lock_state_json_generator); } void WebServer::handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (lock::Lock *obj : App.get_locks()) { @@ -1301,6 +1528,12 @@ void WebServer::handle_lock_request(AsyncWebServerRequest *request, const UrlMat } request->send(404); } +std::string WebServer::lock_state_json_generator(WebServer *web_server, void *source) { + return web_server->lock_json((lock::Lock *) (source), ((lock::Lock *) (source))->state, DETAIL_STATE); +} +std::string WebServer::lock_all_json_generator(WebServer *web_server, void *source) { + return web_server->lock_json((lock::Lock *) (source), ((lock::Lock *) (source))->state, DETAIL_ALL); +} std::string WebServer::lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config) { return json::build_json([this, obj, value, start_config](JsonObject root) { set_json_icon_state_value(root, obj, "lock-" + obj->get_object_id(), lock::lock_state_to_string(value), value, @@ -1319,9 +1552,9 @@ std::string WebServer::lock_json(lock::Lock *obj, lock::LockState value, JsonDet #ifdef USE_VALVE void WebServer::on_valve_update(valve::Valve *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->valve_json(obj, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", valve_state_json_generator); } void WebServer::handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (valve::Valve *obj : App.get_valves()) { @@ -1372,6 +1605,12 @@ void WebServer::handle_valve_request(AsyncWebServerRequest *request, const UrlMa } request->send(404); } +std::string WebServer::valve_state_json_generator(WebServer *web_server, void *source) { + return web_server->valve_json((valve::Valve *) (source), DETAIL_STATE); +} +std::string WebServer::valve_all_json_generator(WebServer *web_server, void *source) { + return web_server->valve_json((valve::Valve *) (source), DETAIL_ALL); +} std::string WebServer::valve_json(valve::Valve *obj, JsonDetail start_config) { return json::build_json([this, obj, start_config](JsonObject root) { set_json_icon_state_value(root, obj, "valve-" + obj->get_object_id(), obj->is_fully_closed() ? "CLOSED" : "OPEN", @@ -1394,9 +1633,9 @@ std::string WebServer::valve_json(valve::Valve *obj, JsonDetail start_config) { #ifdef USE_ALARM_CONTROL_PANEL void WebServer::on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->alarm_control_panel_json(obj, obj->get_state(), DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", alarm_control_panel_state_json_generator); } void WebServer::handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (alarm_control_panel::AlarmControlPanel *obj : App.get_alarm_control_panels()) { @@ -1416,7 +1655,7 @@ void WebServer::handle_alarm_control_panel_request(AsyncWebServerRequest *reques auto call = obj->make_call(); if (request->hasParam("code")) { - call.set_code(request->getParam("code")->value().c_str()); + call.set_code(request->getParam("code")->value().c_str()); // NOLINT } if (match.method == "disarm") { @@ -1440,6 +1679,16 @@ void WebServer::handle_alarm_control_panel_request(AsyncWebServerRequest *reques } request->send(404); } +std::string WebServer::alarm_control_panel_state_json_generator(WebServer *web_server, void *source) { + return web_server->alarm_control_panel_json((alarm_control_panel::AlarmControlPanel *) (source), + ((alarm_control_panel::AlarmControlPanel *) (source))->get_state(), + DETAIL_STATE); +} +std::string WebServer::alarm_control_panel_all_json_generator(WebServer *web_server, void *source) { + return web_server->alarm_control_panel_json((alarm_control_panel::AlarmControlPanel *) (source), + ((alarm_control_panel::AlarmControlPanel *) (source))->get_state(), + DETAIL_ALL); +} std::string WebServer::alarm_control_panel_json(alarm_control_panel::AlarmControlPanel *obj, alarm_control_panel::AlarmControlPanelState value, JsonDetail start_config) { @@ -1461,8 +1710,9 @@ std::string WebServer::alarm_control_panel_json(alarm_control_panel::AlarmContro #ifdef USE_EVENT void WebServer::on_event(event::Event *obj, const std::string &event_type) { - this->events_.send(this->event_json(obj, event_type, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", event_state_json_generator); } + void WebServer::handle_event_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (event::Event *obj : App.get_events()) { if (obj->get_object_id() != match.id) @@ -1481,6 +1731,14 @@ void WebServer::handle_event_request(AsyncWebServerRequest *request, const UrlMa } request->send(404); } + +std::string WebServer::event_state_json_generator(WebServer *web_server, void *source) { + return web_server->event_json((event::Event *) (source), *(((event::Event *) (source))->last_event_type), + DETAIL_STATE); +} +std::string WebServer::event_all_json_generator(WebServer *web_server, void *source) { + return web_server->event_json((event::Event *) (source), *(((event::Event *) (source))->last_event_type), DETAIL_ALL); +} std::string WebServer::event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config) { return json::build_json([this, obj, event_type, start_config](JsonObject root) { set_json_id(root, obj, "event-" + obj->get_object_id(), start_config); @@ -1506,9 +1764,9 @@ std::string WebServer::event_json(event::Event *obj, const std::string &event_ty #ifdef USE_UPDATE void WebServer::on_update(update::UpdateEntity *obj) { - if (this->events_.count() == 0) + if (this->events_.empty()) return; - this->events_.send(this->update_json(obj, DETAIL_STATE).c_str(), "state"); + this->events_.deferrable_send_state(obj, "state", update_state_json_generator); } void WebServer::handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match) { for (update::UpdateEntity *obj : App.get_updates()) { @@ -1537,6 +1795,12 @@ void WebServer::handle_update_request(AsyncWebServerRequest *request, const UrlM } request->send(404); } +std::string WebServer::update_state_json_generator(WebServer *web_server, void *source) { + return web_server->update_json((update::UpdateEntity *) (source), DETAIL_STATE); +} +std::string WebServer::update_all_json_generator(WebServer *web_server, void *source) { + return web_server->update_json((update::UpdateEntity *) (source), DETAIL_STATE); +} std::string WebServer::update_json(update::UpdateEntity *obj, JsonDetail start_config) { return json::build_json([this, obj, start_config](JsonObject root) { set_json_id(root, obj, "update-" + obj->get_object_id(), start_config); @@ -1575,6 +1839,12 @@ bool WebServer::canHandle(AsyncWebServerRequest *request) { if (request->url() == "/") return true; +#ifdef USE_ARDUINO + if (request->url() == "/events") { + return true; + } +#endif + #ifdef USE_WEBSERVER_CSS_INCLUDE if (request->url() == "/0.css") return true; @@ -1597,7 +1867,7 @@ bool WebServer::canHandle(AsyncWebServerRequest *request) { } #endif - UrlMatch match = match_url(request->url().c_str(), true); + UrlMatch match = match_url(request->url().c_str(), true); // NOLINT if (!match.valid) return false; #ifdef USE_SENSOR @@ -1708,6 +1978,13 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) { return; } +#ifdef USE_ARDUINO + if (request->url() == "/events") { + this->events_.add_new_client(this, request); + return; + } +#endif + #ifdef USE_WEBSERVER_CSS_INCLUDE if (request->url() == "/0.css") { this->handle_css_request(request); @@ -1729,7 +2006,7 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) { } #endif - UrlMatch match = match_url(request->url().c_str()); + UrlMatch match = match_url(request->url().c_str()); // NOLINT #ifdef USE_SENSOR if (match.domain == "sensor") { this->handle_sensor_request(request, match); diff --git a/esphome/components/web_server/web_server.h b/esphome/components/web_server/web_server.h index 8edb678169..e4f044c50b 100644 --- a/esphome/components/web_server/web_server.h +++ b/esphome/components/web_server/web_server.h @@ -8,7 +8,11 @@ #include "esphome/core/controller.h" #include "esphome/core/entity_base.h" +#include +#include #include +#include +#include #include #ifdef USE_ESP32 #include @@ -54,6 +58,85 @@ struct SortingGroup { enum JsonDetail { DETAIL_ALL, DETAIL_STATE }; +/* + In order to defer updates in arduino mode, we need to create one AsyncEventSource per incoming request to /events. + This is because only minimal changes were made to the ESPAsyncWebServer lib_dep, it was undesirable to put deferred + update logic into that library. We need one deferred queue per connection so instead of one AsyncEventSource with + multiple clients, we have multiple event sources with one client each. This is slightly awkward which is why it's + implemented in a more straightforward way for ESP-IDF. Arudino platform will eventually go away and this workaround + can be forgotten. +*/ +#ifdef USE_ARDUINO +using message_generator_t = std::string(WebServer *, void *); + +class DeferredUpdateEventSourceList; +class DeferredUpdateEventSource : public AsyncEventSource { + friend class DeferredUpdateEventSourceList; + + /* + This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function + that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for + the same component are backed up, and take up only 8 bytes of memory. The entry in the deferred queue (a + std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only 8 bytes per + entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing + because of dedup) would take up only 0.8 kB. + */ + struct DeferredEvent { + friend class DeferredUpdateEventSource; + + protected: + void *source_; + message_generator_t *message_generator_; + + public: + DeferredEvent(void *source, message_generator_t *message_generator) + : source_(source), message_generator_(message_generator) {} + bool operator==(const DeferredEvent &test) const { + return (source_ == test.source_ && message_generator_ == test.message_generator_); + } + } __attribute__((packed)); + + protected: + // surface a couple methods from the base class + using AsyncEventSource::handleRequest; + using AsyncEventSource::try_send; + + ListEntitiesIterator entities_iterator_; + // vector is used very specifically for its zero memory overhead even though items are popped from the front (memory + // footprint is more important than speed here) + std::vector deferred_queue_; + WebServer *web_server_; + + // helper for allowing only unique entries in the queue + void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator); + + void process_deferred_queue_(); + + public: + DeferredUpdateEventSource(WebServer *ws, const String &url) + : AsyncEventSource(url), entities_iterator_(ListEntitiesIterator(ws, this)), web_server_(ws) {} + + void loop(); + + void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator); + void try_send_nodefer(const char *message, const char *event = nullptr, uint32_t id = 0, uint32_t reconnect = 0); +}; + +class DeferredUpdateEventSourceList : public std::list { + protected: + void on_client_connect_(WebServer *ws, DeferredUpdateEventSource *source); + void on_client_disconnect_(DeferredUpdateEventSource *source); + + public: + void loop(); + + void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator); + void try_send_nodefer(const char *message, const char *event = nullptr, uint32_t id = 0, uint32_t reconnect = 0); + + void add_new_client(WebServer *ws, AsyncWebServerRequest *request); +}; +#endif + /** This class allows users to create a web server with their ESP nodes. * * Behind the scenes it's using AsyncWebServer to set up the server. It exposes 3 things: @@ -64,6 +147,10 @@ enum JsonDetail { DETAIL_ALL, DETAIL_STATE }; * can be found under https://esphome.io/web-api/index.html. */ class WebServer : public Controller, public Component, public AsyncWebHandler { +#ifdef USE_ARDUINO + friend class DeferredUpdateEventSourceList; +#endif + public: WebServer(web_server_base::WebServerBase *base); @@ -153,6 +240,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a sensor request under '/sensor/'. void handle_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string sensor_state_json_generator(WebServer *web_server, void *source); + static std::string sensor_all_json_generator(WebServer *web_server, void *source); /// Dump the sensor state with its value as a JSON string. std::string sensor_json(sensor::Sensor *obj, float value, JsonDetail start_config); #endif @@ -163,6 +252,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a switch request under '/switch//'. void handle_switch_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string switch_state_json_generator(WebServer *web_server, void *source); + static std::string switch_all_json_generator(WebServer *web_server, void *source); /// Dump the switch state with its value as a JSON string. std::string switch_json(switch_::Switch *obj, bool value, JsonDetail start_config); #endif @@ -171,6 +262,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a button request under '/button//press'. void handle_button_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string button_state_json_generator(WebServer *web_server, void *source); + static std::string button_all_json_generator(WebServer *web_server, void *source); /// Dump the button details with its value as a JSON string. std::string button_json(button::Button *obj, JsonDetail start_config); #endif @@ -181,6 +274,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a binary sensor request under '/binary_sensor/'. void handle_binary_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string binary_sensor_state_json_generator(WebServer *web_server, void *source); + static std::string binary_sensor_all_json_generator(WebServer *web_server, void *source); /// Dump the binary sensor state with its value as a JSON string. std::string binary_sensor_json(binary_sensor::BinarySensor *obj, bool value, JsonDetail start_config); #endif @@ -191,6 +286,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a fan request under '/fan//'. void handle_fan_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string fan_state_json_generator(WebServer *web_server, void *source); + static std::string fan_all_json_generator(WebServer *web_server, void *source); /// Dump the fan state as a JSON string. std::string fan_json(fan::Fan *obj, JsonDetail start_config); #endif @@ -201,6 +298,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a light request under '/light//'. void handle_light_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string light_state_json_generator(WebServer *web_server, void *source); + static std::string light_all_json_generator(WebServer *web_server, void *source); /// Dump the light state as a JSON string. std::string light_json(light::LightState *obj, JsonDetail start_config); #endif @@ -211,6 +310,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a text sensor request under '/text_sensor/'. void handle_text_sensor_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string text_sensor_state_json_generator(WebServer *web_server, void *source); + static std::string text_sensor_all_json_generator(WebServer *web_server, void *source); /// Dump the text sensor state with its value as a JSON string. std::string text_sensor_json(text_sensor::TextSensor *obj, const std::string &value, JsonDetail start_config); #endif @@ -221,6 +322,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a cover request under '/cover//'. void handle_cover_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string cover_state_json_generator(WebServer *web_server, void *source); + static std::string cover_all_json_generator(WebServer *web_server, void *source); /// Dump the cover state as a JSON string. std::string cover_json(cover::Cover *obj, JsonDetail start_config); #endif @@ -230,6 +333,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a number request under '/number/'. void handle_number_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string number_state_json_generator(WebServer *web_server, void *source); + static std::string number_all_json_generator(WebServer *web_server, void *source); /// Dump the number state with its value as a JSON string. std::string number_json(number::Number *obj, float value, JsonDetail start_config); #endif @@ -239,6 +344,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a date request under '/date/'. void handle_date_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string date_state_json_generator(WebServer *web_server, void *source); + static std::string date_all_json_generator(WebServer *web_server, void *source); /// Dump the date state with its value as a JSON string. std::string date_json(datetime::DateEntity *obj, JsonDetail start_config); #endif @@ -248,6 +355,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a time request under '/time/'. void handle_time_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string time_state_json_generator(WebServer *web_server, void *source); + static std::string time_all_json_generator(WebServer *web_server, void *source); /// Dump the time state with its value as a JSON string. std::string time_json(datetime::TimeEntity *obj, JsonDetail start_config); #endif @@ -257,6 +366,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a datetime request under '/datetime/'. void handle_datetime_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string datetime_state_json_generator(WebServer *web_server, void *source); + static std::string datetime_all_json_generator(WebServer *web_server, void *source); /// Dump the datetime state with its value as a JSON string. std::string datetime_json(datetime::DateTimeEntity *obj, JsonDetail start_config); #endif @@ -266,6 +377,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a text input request under '/text/'. void handle_text_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string text_state_json_generator(WebServer *web_server, void *source); + static std::string text_all_json_generator(WebServer *web_server, void *source); /// Dump the text state with its value as a JSON string. std::string text_json(text::Text *obj, const std::string &value, JsonDetail start_config); #endif @@ -275,6 +388,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a select request under '/select/'. void handle_select_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string select_state_json_generator(WebServer *web_server, void *source); + static std::string select_all_json_generator(WebServer *web_server, void *source); /// Dump the select state with its value as a JSON string. std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config); #endif @@ -284,6 +399,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a climate request under '/climate/'. void handle_climate_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string climate_state_json_generator(WebServer *web_server, void *source); + static std::string climate_all_json_generator(WebServer *web_server, void *source); /// Dump the climate details std::string climate_json(climate::Climate *obj, JsonDetail start_config); #endif @@ -294,6 +411,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a lock request under '/lock//'. void handle_lock_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string lock_state_json_generator(WebServer *web_server, void *source); + static std::string lock_all_json_generator(WebServer *web_server, void *source); /// Dump the lock state with its value as a JSON string. std::string lock_json(lock::Lock *obj, lock::LockState value, JsonDetail start_config); #endif @@ -304,6 +423,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a valve request under '/valve//'. void handle_valve_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string valve_state_json_generator(WebServer *web_server, void *source); + static std::string valve_all_json_generator(WebServer *web_server, void *source); /// Dump the valve state as a JSON string. std::string valve_json(valve::Valve *obj, JsonDetail start_config); #endif @@ -314,6 +435,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a alarm_control_panel request under '/alarm_control_panel/'. void handle_alarm_control_panel_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string alarm_control_panel_state_json_generator(WebServer *web_server, void *source); + static std::string alarm_control_panel_all_json_generator(WebServer *web_server, void *source); /// Dump the alarm_control_panel state with its value as a JSON string. std::string alarm_control_panel_json(alarm_control_panel::AlarmControlPanel *obj, alarm_control_panel::AlarmControlPanelState value, JsonDetail start_config); @@ -322,6 +445,9 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { #ifdef USE_EVENT void on_event(event::Event *obj, const std::string &event_type) override; + static std::string event_state_json_generator(WebServer *web_server, void *source); + static std::string event_all_json_generator(WebServer *web_server, void *source); + /// Handle a event request under '/event'. void handle_event_request(AsyncWebServerRequest *request, const UrlMatch &match); @@ -335,6 +461,8 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { /// Handle a update request under '/update/'. void handle_update_request(AsyncWebServerRequest *request, const UrlMatch &match); + static std::string update_state_json_generator(WebServer *web_server, void *source); + static std::string update_all_json_generator(WebServer *web_server, void *source); /// Dump the update state with its value as a JSON string. std::string update_json(update::UpdateEntity *obj, JsonDetail start_config); #endif @@ -349,14 +477,19 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { void add_entity_config(EntityBase *entity, float weight, uint64_t group); void add_sorting_group(uint64_t group_id, const std::string &group_name, float weight); - protected: - void schedule_(std::function &&f); - friend ListEntitiesIterator; - web_server_base::WebServerBase *base_; - AsyncEventSource events_{"/events"}; - ListEntitiesIterator entities_iterator_; std::map sorting_entitys_; std::map sorting_groups_; + bool include_internal_{false}; + + protected: + void schedule_(std::function &&f); + web_server_base::WebServerBase *base_; +#ifdef USE_ARDUINO + DeferredUpdateEventSourceList events_; +#endif +#ifdef USE_ESP_IDF + AsyncEventSource events_{"/events", this}; +#endif #if USE_WEBSERVER_VERSION == 1 const char *css_url_{nullptr}; @@ -368,7 +501,6 @@ class WebServer : public Controller, public Component, public AsyncWebHandler { #ifdef USE_WEBSERVER_JS_INCLUDE const char *js_include_{nullptr}; #endif - bool include_internal_{false}; bool allow_ota_{true}; bool expose_log_{true}; #ifdef USE_ESP32 diff --git a/esphome/components/web_server_base/__init__.py b/esphome/components/web_server_base/__init__.py index 4f894619b0..115f521d04 100644 --- a/esphome/components/web_server_base/__init__.py +++ b/esphome/components/web_server_base/__init__.py @@ -37,4 +37,4 @@ async def to_code(config): cg.add_library("FS", None) cg.add_library("Update", None) # https://github.com/esphome/ESPAsyncWebServer/blob/master/library.json - cg.add_library("esphome/ESPAsyncWebServer-esphome", "3.2.2") + cg.add_library("esphome/ESPAsyncWebServer-esphome", "3.3.0") diff --git a/esphome/components/web_server_idf/__init__.py b/esphome/components/web_server_idf/__init__.py index bd3db24bc6..a84d9bb663 100644 --- a/esphome/components/web_server_idf/__init__.py +++ b/esphome/components/web_server_idf/__init__.py @@ -8,6 +8,8 @@ CONFIG_SCHEMA = cv.All( cv.only_with_esp_idf, ) +AUTO_LOAD = ["web_server"] + async def to_code(config): # Increase the maximum supported size of headers section in HTTP request packet to be processed by the server diff --git a/esphome/components/web_server_idf/web_server_idf.cpp b/esphome/components/web_server_idf/web_server_idf.cpp index cf187cd647..428bd262e8 100644 --- a/esphome/components/web_server_idf/web_server_idf.cpp +++ b/esphome/components/web_server_idf/web_server_idf.cpp @@ -8,6 +8,10 @@ #include "esp_tls_crypto.h" #include "utils.h" + +#include "esphome/components/web_server/web_server.h" +#include "esphome/components/web_server/list_entities.h" + #include "web_server_idf.h" namespace esphome { @@ -276,21 +280,37 @@ AsyncEventSource::~AsyncEventSource() { } void AsyncEventSource::handleRequest(AsyncWebServerRequest *request) { - auto *rsp = new AsyncEventSourceResponse(request, this); // NOLINT(cppcoreguidelines-owning-memory) + auto *rsp = // NOLINT(cppcoreguidelines-owning-memory) + new AsyncEventSourceResponse(request, this, this->web_server_); if (this->on_connect_) { this->on_connect_(rsp); } this->sessions_.insert(rsp); } -void AsyncEventSource::send(const char *message, const char *event, uint32_t id, uint32_t reconnect) { +void AsyncEventSource::loop() { for (auto *ses : this->sessions_) { - ses->send(message, event, id, reconnect); + ses->loop(); } } -AsyncEventSourceResponse::AsyncEventSourceResponse(const AsyncWebServerRequest *request, AsyncEventSource *server) - : server_(server) { +void AsyncEventSource::try_send_nodefer(const char *message, const char *event, uint32_t id, uint32_t reconnect) { + for (auto *ses : this->sessions_) { + ses->try_send_nodefer(message, event, id, reconnect); + } +} + +void AsyncEventSource::deferrable_send_state(void *source, const char *event_type, + message_generator_t *message_generator) { + for (auto *ses : this->sessions_) { + ses->deferrable_send_state(source, event_type, message_generator); + } +} + +AsyncEventSourceResponse::AsyncEventSourceResponse(const AsyncWebServerRequest *request, + esphome::web_server_idf::AsyncEventSource *server, + esphome::web_server::WebServer *ws) + : server_(server), web_server_(ws), entities_iterator_(new esphome::web_server::ListEntitiesIterator(ws, server)) { httpd_req_t *req = *request; httpd_resp_set_status(req, HTTPD_200); @@ -309,6 +329,30 @@ AsyncEventSourceResponse::AsyncEventSourceResponse(const AsyncWebServerRequest * this->hd_ = req->handle; this->fd_ = httpd_req_to_sockfd(req); + + // Configure reconnect timeout and send config + // this should always go through since the tcp send buffer is empty on connect + std::string message = ws->get_config_json(); + this->try_send_nodefer(message.c_str(), "ping", millis(), 30000); + + for (auto &group : ws->sorting_groups_) { + message = json::build_json([group](JsonObject root) { + root["name"] = group.second.name; + root["sorting_weight"] = group.second.weight; + }); + + // a (very) large number of these should be able to be queued initially without defer + // since the only thing in the send buffer at this point is the initial ping/config + this->try_send_nodefer(message.c_str(), "sorting_group"); + } + + this->entities_iterator_->begin(ws->include_internal_); + + // just dump them all up-front and take advantage of the deferred queue + // on second thought that takes too long, but leaving the commented code here for debug purposes + // while(!this->entities_iterator_->completed()) { + // this->entities_iterator_->advance(); + //} } void AsyncEventSourceResponse::destroy(void *ptr) { @@ -317,52 +361,155 @@ void AsyncEventSourceResponse::destroy(void *ptr) { delete rsp; // NOLINT(cppcoreguidelines-owning-memory) } -void AsyncEventSourceResponse::send(const char *message, const char *event, uint32_t id, uint32_t reconnect) { - if (this->fd_ == 0) { +// helper for allowing only unique entries in the queue +void AsyncEventSourceResponse::deq_push_back_with_dedup_(void *source, message_generator_t *message_generator) { + DeferredEvent item(source, message_generator); + + auto iter = std::find_if(this->deferred_queue_.begin(), this->deferred_queue_.end(), + [&item](const DeferredEvent &test) -> bool { return test == item; }); + + if (iter != this->deferred_queue_.end()) { + (*iter) = item; + } else { + this->deferred_queue_.push_back(item); + } +} + +void AsyncEventSourceResponse::process_deferred_queue_() { + while (!deferred_queue_.empty()) { + DeferredEvent &de = deferred_queue_.front(); + std::string message = de.message_generator_(web_server_, de.source_); + if (this->try_send_nodefer(message.c_str(), "state")) { + // O(n) but memory efficiency is more important than speed here which is why std::vector was chosen + deferred_queue_.erase(deferred_queue_.begin()); + } else { + break; + } + } +} + +void AsyncEventSourceResponse::process_buffer_() { + if (event_buffer_.empty()) { + return; + } + if (event_bytes_sent_ == event_buffer_.size()) { + event_buffer_.resize(0); + event_bytes_sent_ = 0; return; } - std::string ev; + int bytes_sent = httpd_socket_send(this->hd_, this->fd_, event_buffer_.c_str() + event_bytes_sent_, + event_buffer_.size() - event_bytes_sent_, 0); + if (bytes_sent == HTTPD_SOCK_ERR_TIMEOUT || bytes_sent == HTTPD_SOCK_ERR_FAIL) { + return; + } + event_bytes_sent_ += bytes_sent; + + if (event_bytes_sent_ == event_buffer_.size()) { + event_buffer_.resize(0); + event_bytes_sent_ = 0; + } +} + +void AsyncEventSourceResponse::loop() { + process_buffer_(); + process_deferred_queue_(); + if (!this->entities_iterator_->completed()) + this->entities_iterator_->advance(); +} + +bool AsyncEventSourceResponse::try_send_nodefer(const char *message, const char *event, uint32_t id, + uint32_t reconnect) { + if (this->fd_ == 0) { + return false; + } + + process_buffer_(); + if (!event_buffer_.empty()) { + // there is still pending event data to send first + return false; + } + + // 8 spaces are standing in for the hexidecimal chunk length to print later + const char chunk_len_header[] = " " CRLF_STR; + const int chunk_len_header_len = sizeof(chunk_len_header) - 1; + + event_buffer_.append(chunk_len_header); if (reconnect) { - ev.append("retry: ", sizeof("retry: ") - 1); - ev.append(to_string(reconnect)); - ev.append(CRLF_STR, CRLF_LEN); + event_buffer_.append("retry: ", sizeof("retry: ") - 1); + event_buffer_.append(to_string(reconnect)); + event_buffer_.append(CRLF_STR, CRLF_LEN); } if (id) { - ev.append("id: ", sizeof("id: ") - 1); - ev.append(to_string(id)); - ev.append(CRLF_STR, CRLF_LEN); + event_buffer_.append("id: ", sizeof("id: ") - 1); + event_buffer_.append(to_string(id)); + event_buffer_.append(CRLF_STR, CRLF_LEN); } if (event && *event) { - ev.append("event: ", sizeof("event: ") - 1); - ev.append(event); - ev.append(CRLF_STR, CRLF_LEN); + event_buffer_.append("event: ", sizeof("event: ") - 1); + event_buffer_.append(event); + event_buffer_.append(CRLF_STR, CRLF_LEN); } if (message && *message) { - ev.append("data: ", sizeof("data: ") - 1); - ev.append(message); - ev.append(CRLF_STR, CRLF_LEN); + event_buffer_.append("data: ", sizeof("data: ") - 1); + event_buffer_.append(message); + event_buffer_.append(CRLF_STR, CRLF_LEN); } - if (ev.empty()) { + if (event_buffer_.empty()) { + return true; + } + + event_buffer_.append(CRLF_STR, CRLF_LEN); + event_buffer_.append(CRLF_STR, CRLF_LEN); + + // chunk length header itself and the final chunk terminating CRLF are not counted as part of the chunk + int chunk_len = event_buffer_.size() - CRLF_LEN - chunk_len_header_len; + char chunk_len_str[9]; + snprintf(chunk_len_str, 9, "%08x", chunk_len); + std::memcpy(&event_buffer_[0], chunk_len_str, 8); + + event_bytes_sent_ = 0; + process_buffer_(); + + return true; +} + +void AsyncEventSourceResponse::deferrable_send_state(void *source, const char *event_type, + message_generator_t *message_generator) { + // allow all json "details_all" to go through before publishing bare state events, this avoids unnamed entries showing + // up in the web GUI and reduces event load during initial connect + if (!entities_iterator_->completed() && 0 != strcmp(event_type, "state_detail_all")) return; + + if (source == nullptr) + return; + if (event_type == nullptr) + return; + if (message_generator == nullptr) + return; + + if (0 != strcmp(event_type, "state_detail_all") && 0 != strcmp(event_type, "state")) { + ESP_LOGE(TAG, "Can't defer non-state event"); } - ev.append(CRLF_STR, CRLF_LEN); + process_buffer_(); + process_deferred_queue_(); - // Sending chunked content prelude - auto cs = str_snprintf("%x" CRLF_STR, 4 * sizeof(ev.size()) + CRLF_LEN, ev.size()); - httpd_socket_send(this->hd_, this->fd_, cs.c_str(), cs.size(), 0); - - // Sendiing content chunk - httpd_socket_send(this->hd_, this->fd_, ev.c_str(), ev.size(), 0); - - // Indicate end of chunk - httpd_socket_send(this->hd_, this->fd_, CRLF_STR, CRLF_LEN, 0); + if (!event_buffer_.empty() || !deferred_queue_.empty()) { + // outgoing event buffer or deferred queue still not empty which means downstream tcp send buffer full, no point + // trying to send first + deq_push_back_with_dedup_(source, message_generator); + } else { + std::string message = message_generator(web_server_, source); + if (!this->try_send_nodefer(message.c_str(), "state")) { + deq_push_back_with_dedup_(source, message_generator); + } + } } } // namespace web_server_idf diff --git a/esphome/components/web_server_idf/web_server_idf.h b/esphome/components/web_server_idf/web_server_idf.h index 2ead5e3f03..13a3ef168d 100644 --- a/esphome/components/web_server_idf/web_server_idf.h +++ b/esphome/components/web_server_idf/web_server_idf.h @@ -4,12 +4,18 @@ #include #include +#include #include #include #include +#include #include namespace esphome { +namespace web_server { +class WebServer; +class ListEntitiesIterator; +}; // namespace web_server namespace web_server_idf { #define F(string_literal) (string_literal) @@ -215,19 +221,58 @@ class AsyncWebHandler { }; class AsyncEventSource; +class AsyncEventSourceResponse; + +using message_generator_t = std::string(esphome::web_server::WebServer *, void *); + +/* + This class holds a pointer to the source component that wants to publish a state event, and a pointer to a function + that will lazily generate that event. The two pointers allow dedup in the deferred queue if multiple publishes for + the same component are backed up, and take up only 8 bytes of memory. The entry in the deferred queue (a + std::vector) is the DeferredEvent instance itself (not a pointer to one elsewhere in heap) so still only 8 bytes per + entry (and no heap fragmentation). Even 100 backed up events (you'd have to have at least 100 sensors publishing + because of dedup) would take up only 0.8 kB. +*/ +struct DeferredEvent { + friend class AsyncEventSourceResponse; + + protected: + void *source_; + message_generator_t *message_generator_; + + public: + DeferredEvent(void *source, message_generator_t *message_generator) + : source_(source), message_generator_(message_generator) {} + bool operator==(const DeferredEvent &test) const { + return (source_ == test.source_ && message_generator_ == test.message_generator_); + } +} __attribute__((packed)); class AsyncEventSourceResponse { friend class AsyncEventSource; public: - void send(const char *message, const char *event = nullptr, uint32_t id = 0, uint32_t reconnect = 0); + bool try_send_nodefer(const char *message, const char *event = nullptr, uint32_t id = 0, uint32_t reconnect = 0); + void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator); + void loop(); protected: - AsyncEventSourceResponse(const AsyncWebServerRequest *request, AsyncEventSource *server); + AsyncEventSourceResponse(const AsyncWebServerRequest *request, esphome::web_server_idf::AsyncEventSource *server, + esphome::web_server::WebServer *ws); + + void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator); + void process_deferred_queue_(); + void process_buffer_(); + static void destroy(void *p); AsyncEventSource *server_; httpd_handle_t hd_{}; int fd_{}; + std::vector deferred_queue_; + esphome::web_server::WebServer *web_server_; + std::unique_ptr entities_iterator_; + std::string event_buffer_{""}; + size_t event_bytes_sent_; }; using AsyncEventSourceClient = AsyncEventSourceResponse; @@ -237,7 +282,7 @@ class AsyncEventSource : public AsyncWebHandler { using connect_handler_t = std::function; public: - AsyncEventSource(std::string url) : url_(std::move(url)) {} + AsyncEventSource(std::string url, esphome::web_server::WebServer *ws) : url_(std::move(url)), web_server_(ws) {} ~AsyncEventSource() override; // NOLINTNEXTLINE(readability-identifier-naming) @@ -249,7 +294,10 @@ class AsyncEventSource : public AsyncWebHandler { // NOLINTNEXTLINE(readability-identifier-naming) void onConnect(connect_handler_t cb) { this->on_connect_ = std::move(cb); } - void send(const char *message, const char *event = nullptr, uint32_t id = 0, uint32_t reconnect = 0); + void try_send_nodefer(const char *message, const char *event = nullptr, uint32_t id = 0, uint32_t reconnect = 0); + void deferrable_send_state(void *source, const char *event_type, message_generator_t *message_generator); + void loop(); + bool empty() { return this->count() == 0; } size_t count() const { return this->sessions_.size(); } @@ -257,6 +305,7 @@ class AsyncEventSource : public AsyncWebHandler { std::string url_; std::set sessions_; connect_handler_t on_connect_{}; + esphome::web_server::WebServer *web_server_; }; class DefaultHeaders { diff --git a/esphome/config_validation.py b/esphome/config_validation.py index 27d11e4ded..858c6e197c 100644 --- a/esphome/config_validation.py +++ b/esphome/config_validation.py @@ -1223,8 +1223,7 @@ def subscribe_topic(value): if index != len(value) - 1: # If there are multiple wildcards, this will also trigger raise Invalid( - "Multi-level wildcard must be the last " - "character in the topic filter." + "Multi-level wildcard must be the last character in the topic filter." ) if len(value) > 1 and value[index - 1] != "/": raise Invalid("Multi-level wildcard must be after a topic level separator.") @@ -1642,140 +1641,39 @@ class GenerateID(Optional): super().__init__(key, default=lambda: None) -def _get_priority_default(*args): - for arg in args: - if arg is not vol.UNDEFINED: - return arg - return vol.UNDEFINED +def _get_default_key(*args): + return ["_".join([CORE.target_platform] + list(args))] class SplitDefault(Optional): """Mark this key to have a split default for ESP8266/ESP32.""" - def __init__( - self, - key, - esp8266=vol.UNDEFINED, - esp32=vol.UNDEFINED, - esp32_arduino=vol.UNDEFINED, - esp32_idf=vol.UNDEFINED, - esp32_s2=vol.UNDEFINED, - esp32_s2_arduino=vol.UNDEFINED, - esp32_s2_idf=vol.UNDEFINED, - esp32_s3=vol.UNDEFINED, - esp32_s3_arduino=vol.UNDEFINED, - esp32_s3_idf=vol.UNDEFINED, - esp32_c3=vol.UNDEFINED, - esp32_c3_arduino=vol.UNDEFINED, - esp32_c3_idf=vol.UNDEFINED, - esp32_c6=vol.UNDEFINED, - esp32_c6_arduino=vol.UNDEFINED, - esp32_c6_idf=vol.UNDEFINED, - esp32_h2=vol.UNDEFINED, - esp32_h2_arduino=vol.UNDEFINED, - esp32_h2_idf=vol.UNDEFINED, - rp2040=vol.UNDEFINED, - bk72xx=vol.UNDEFINED, - rtl87xx=vol.UNDEFINED, - host=vol.UNDEFINED, - ): + def __init__(self, key, **kwargs): super().__init__(key) - self._esp8266_default = vol.default_factory(esp8266) - self._esp32_arduino_default = vol.default_factory( - _get_priority_default(esp32_arduino, esp32) - ) - self._esp32_idf_default = vol.default_factory( - _get_priority_default(esp32_idf, esp32) - ) - self._esp32_s2_arduino_default = vol.default_factory( - _get_priority_default(esp32_s2_arduino, esp32_s2, esp32_arduino, esp32) - ) - self._esp32_s2_idf_default = vol.default_factory( - _get_priority_default(esp32_s2_idf, esp32_s2, esp32_idf, esp32) - ) - self._esp32_s3_arduino_default = vol.default_factory( - _get_priority_default(esp32_s3_arduino, esp32_s3, esp32_arduino, esp32) - ) - self._esp32_s3_idf_default = vol.default_factory( - _get_priority_default(esp32_s3_idf, esp32_s3, esp32_idf, esp32) - ) - self._esp32_c3_arduino_default = vol.default_factory( - _get_priority_default(esp32_c3_arduino, esp32_c3, esp32_arduino, esp32) - ) - self._esp32_c3_idf_default = vol.default_factory( - _get_priority_default(esp32_c3_idf, esp32_c3, esp32_idf, esp32) - ) - self._esp32_c6_arduino_default = vol.default_factory( - _get_priority_default(esp32_c6_arduino, esp32_c6, esp32_arduino, esp32) - ) - self._esp32_c6_idf_default = vol.default_factory( - _get_priority_default(esp32_c6_idf, esp32_c6, esp32_idf, esp32) - ) - self._esp32_h2_arduino_default = vol.default_factory( - _get_priority_default(esp32_h2_arduino, esp32_h2, esp32_arduino, esp32) - ) - self._esp32_h2_idf_default = vol.default_factory( - _get_priority_default(esp32_h2_idf, esp32_h2, esp32_idf, esp32) - ) - self._rp2040_default = vol.default_factory(rp2040) - self._bk72xx_default = vol.default_factory(bk72xx) - self._rtl87xx_default = vol.default_factory(rtl87xx) - self._host_default = vol.default_factory(host) + + self._defaults = {} + + for platform_key, value in kwargs.items(): + self._defaults[platform_key] = vol.default_factory(value) @property def default(self): - if CORE.is_esp8266: - return self._esp8266_default + keys = [] if CORE.is_esp32: from esphome.components.esp32 import get_esp32_variant - from esphome.components.esp32.const import ( - VARIANT_ESP32C3, - VARIANT_ESP32C6, - VARIANT_ESP32H2, - VARIANT_ESP32S2, - VARIANT_ESP32S3, - ) + from esphome.components.esp32.const import VARIANT_ESP32 - variant = get_esp32_variant() - if variant == VARIANT_ESP32S2: - if CORE.using_arduino: - return self._esp32_s2_arduino_default - if CORE.using_esp_idf: - return self._esp32_s2_idf_default - elif variant == VARIANT_ESP32S3: - if CORE.using_arduino: - return self._esp32_s3_arduino_default - if CORE.using_esp_idf: - return self._esp32_s3_idf_default - elif variant == VARIANT_ESP32C3: - if CORE.using_arduino: - return self._esp32_c3_arduino_default - if CORE.using_esp_idf: - return self._esp32_c3_idf_default - elif variant == VARIANT_ESP32C6: - if CORE.using_arduino: - return self._esp32_c6_arduino_default - if CORE.using_esp_idf: - return self._esp32_c6_idf_default - elif variant == VARIANT_ESP32H2: - if CORE.using_arduino: - return self._esp32_h2_arduino_default - if CORE.using_esp_idf: - return self._esp32_h2_idf_default - else: - if CORE.using_arduino: - return self._esp32_arduino_default - if CORE.using_esp_idf: - return self._esp32_idf_default - if CORE.is_rp2040: - return self._rp2040_default - if CORE.is_bk72xx: - return self._bk72xx_default - if CORE.is_rtl87xx: - return self._rtl87xx_default - if CORE.is_host: - return self._host_default - raise NotImplementedError + variant = get_esp32_variant().replace(VARIANT_ESP32, "").lower() + framework = CORE.target_framework.replace("esp-", "") + if variant: + keys += _get_default_key(variant, framework) + keys += _get_default_key(variant) + keys += _get_default_key(framework) + keys += _get_default_key() + for key in keys: + if self._defaults.get(key) is not None: + return self._defaults[key] + return vol.default_factory(vol.UNDEFINED) @default.setter def default(self, value): diff --git a/esphome/const.py b/esphome/const.py index cc26600860..c460192940 100644 --- a/esphome/const.py +++ b/esphome/const.py @@ -1,6 +1,6 @@ """Constants used by esphome.""" -__version__ = "2025.2.2" +__version__ = "2025.3.0" ALLOWED_NAME_CHARS = "abcdefghijklmnopqrstuvwxyz0123456789-_" VALID_SUBSTITUTIONS_CHARACTERS = ( @@ -546,6 +546,9 @@ CONF_OFF_SPEED_CYCLE = "off_speed_cycle" CONF_OFFSET = "offset" CONF_OFFSET_HEIGHT = "offset_height" CONF_OFFSET_WIDTH = "offset_width" +CONF_OFFSET_X = "offset_x" +CONF_OFFSET_Y = "offset_y" +CONF_OFFSET_Z = "offset_z" CONF_ON = "on" CONF_ON_BLE_ADVERTISE = "on_ble_advertise" CONF_ON_BLE_MANUFACTURER_DATA_ADVERTISE = "on_ble_manufacturer_data_advertise" @@ -927,6 +930,7 @@ CONF_VALUE = "value" CONF_VALUE_FONT = "value_font" CONF_VARIABLES = "variables" CONF_VARIANT = "variant" +CONF_VARS = "vars" CONF_VERSION = "version" CONF_VIBRATIONS = "vibrations" CONF_VISIBLE = "visible" diff --git a/esphome/core/config.py b/esphome/core/config.py index 2077af02a7..f2b8585143 100644 --- a/esphome/core/config.py +++ b/esphome/core/config.py @@ -1,5 +1,4 @@ import logging -import multiprocessing import os from pathlib import Path @@ -94,10 +93,19 @@ def valid_project_name(value: str): return value +def get_usable_cpu_count() -> int: + """Return the number of CPUs that can be used for processes. + On Python 3.13+ this is the number of CPUs that can be used for processes. + On older Python versions this is the number of CPUs. + """ + return ( + os.process_cpu_count() if hasattr(os, "process_cpu_count") else os.cpu_count() + ) + + if "ESPHOME_DEFAULT_COMPILE_PROCESS_LIMIT" in os.environ: _compile_process_limit_default = min( - int(os.environ["ESPHOME_DEFAULT_COMPILE_PROCESS_LIMIT"]), - multiprocessing.cpu_count(), + int(os.environ["ESPHOME_DEFAULT_COMPILE_PROCESS_LIMIT"]), get_usable_cpu_count() ) else: _compile_process_limit_default = cv.UNDEFINED @@ -156,7 +164,7 @@ CONFIG_SCHEMA = cv.All( ), cv.Optional( CONF_COMPILE_PROCESS_LIMIT, default=_compile_process_limit_default - ): cv.int_range(min=1, max=multiprocessing.cpu_count()), + ): cv.int_range(min=1, max=get_usable_cpu_count()), } ), validate_hostname, @@ -181,10 +189,11 @@ def _is_target_platform(name): from esphome.loader import get_component try: - if get_component(name, True).is_target_platform: - return True + return get_component(name, True).is_target_platform except KeyError: pass + except ImportError: + pass return False diff --git a/esphome/core/defines.h b/esphome/core/defines.h index dc0ac3c1e8..7bc84a22dc 100644 --- a/esphome/core/defines.h +++ b/esphome/core/defines.h @@ -71,7 +71,7 @@ #define USE_OTA #define USE_OTA_PASSWORD #define USE_OTA_STATE_CALLBACK -#define USE_OTA_VERSION 1 +#define USE_OTA_VERSION 2 #define USE_OUTPUT #define USE_POWER_SUPPLY #define USE_QR_CODE diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index 82b0fe07f8..7866eaa9bd 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -3,11 +3,11 @@ #include #include #include +#include #include #include #include #include -#include #include "esphome/core/optional.h" @@ -700,8 +700,10 @@ template class RAMAllocator { } template constexpr RAMAllocator(const RAMAllocator &other) : flags_{other.flags_} {} - T *allocate(size_t n) { - size_t size = n * sizeof(T); + T *allocate(size_t n) { return this->allocate(n, sizeof(T)); } + + T *allocate(size_t n, size_t manual_size) { + size_t size = n * manual_size; T *ptr = nullptr; #ifdef USE_ESP32 if (this->flags_ & Flags::ALLOC_EXTERNAL) { @@ -717,6 +719,25 @@ template class RAMAllocator { return ptr; } + T *reallocate(T *p, size_t n) { return this->reallocate(p, n, sizeof(T)); } + + T *reallocate(T *p, size_t n, size_t manual_size) { + size_t size = n * sizeof(T); + T *ptr = nullptr; +#ifdef USE_ESP32 + if (this->flags_ & Flags::ALLOC_EXTERNAL) { + ptr = static_cast(heap_caps_realloc(p, size, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT)); + } + if (ptr == nullptr && this->flags_ & Flags::ALLOC_INTERNAL) { + ptr = static_cast(heap_caps_realloc(p, size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)); + } +#else + // Ignore ALLOC_EXTERNAL/ALLOC_INTERNAL flags if external allocation is not supported + ptr = static_cast(realloc(p, size)); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc) +#endif + return ptr; + } + void deallocate(T *p, size_t n) { free(p); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc) } diff --git a/esphome/core/time.cpp b/esphome/core/time.cpp index 66a0e1c0a7..672f5b98bf 100644 --- a/esphome/core/time.cpp +++ b/esphome/core/time.cpp @@ -197,6 +197,7 @@ void ESPTime::recalc_timestamp_local() { tm.tm_hour = this->hour; tm.tm_min = this->minute; tm.tm_sec = this->second; + tm.tm_isdst = -1; this->timestamp = mktime(&tm); } diff --git a/esphome/cpp_generator.py b/esphome/cpp_generator.py index 4e283868e1..eb0bd25d1d 100644 --- a/esphome/cpp_generator.py +++ b/esphome/cpp_generator.py @@ -506,9 +506,9 @@ def with_local_variable(id_: ID, rhs: SafeExpType, callback: Callable, *args) -> """ # throw if the callback is async: - assert not inspect.iscoroutinefunction( - callback - ), "with_local_variable() callback cannot be async!" + assert not inspect.iscoroutinefunction(callback), ( + "with_local_variable() callback cannot be async!" + ) CORE.add(RawStatement("{")) # output opening curly brace obj = variable(id_, rhs, None, True) diff --git a/esphome/dashboard/core.py b/esphome/dashboard/core.py index f53cb7ffb1..416442c426 100644 --- a/esphome/dashboard/core.py +++ b/esphome/dashboard/core.py @@ -9,7 +9,7 @@ import json import logging from pathlib import Path import threading -from typing import TYPE_CHECKING, Any, Callable +from typing import Any, Callable from esphome.storage_json import ignored_devices_storage_path @@ -17,15 +17,15 @@ from ..zeroconf import DiscoveredImport from .dns import DNSCache from .entries import DashboardEntries from .settings import DashboardSettings - -if TYPE_CHECKING: - from .status.mdns import MDNSStatus - +from .status.mdns import MDNSStatus +from .status.ping import PingStatus _LOGGER = logging.getLogger(__name__) IGNORED_DEVICES_STORAGE_PATH = "ignored-devices.json" +MDNS_BOOTSTRAP_TIME = 7.5 + @dataclass class Event: @@ -81,6 +81,7 @@ class ESPHomeDashboard: "dns_cache", "_background_tasks", "ignored_devices", + "_ping_status_task", ) def __init__(self) -> None: @@ -97,6 +98,7 @@ class ESPHomeDashboard: self.dns_cache = DNSCache() self._background_tasks: set[asyncio.Task] = set() self.ignored_devices: set[str] = set() + self._ping_status_task: asyncio.Task | None = None async def async_setup(self) -> None: """Setup the dashboard.""" @@ -121,41 +123,48 @@ class ESPHomeDashboard: {"ignored_devices": sorted(self.ignored_devices)}, indent=2, fp=f_handle ) + def _async_start_ping_status(self, ping_status: PingStatus) -> None: + self._ping_status_task = asyncio.create_task(ping_status.async_run()) + async def async_run(self) -> None: """Run the dashboard.""" settings = self.settings mdns_task: asyncio.Task | None = None - ping_status_task: asyncio.Task | None = None await self.entries.async_update_entries() - if settings.status_use_ping: - from .status.ping import PingStatus + mdns_status = MDNSStatus(self) + ping_status = PingStatus(self) + start_ping_timer: asyncio.TimerHandle | None = None - ping_status = PingStatus() - ping_status_task = asyncio.create_task(ping_status.async_run()) - else: - from .status.mdns import MDNSStatus - - mdns_status = MDNSStatus() - await mdns_status.async_refresh_hosts() - self.mdns_status = mdns_status + self.mdns_status = mdns_status + if mdns_status.async_setup(): mdns_task = asyncio.create_task(mdns_status.async_run()) + # Start ping MDNS_BOOTSTRAP_TIME seconds after startup to ensure + # MDNS has had a chance to resolve the devices + start_ping_timer = self.loop.call_later( + MDNS_BOOTSTRAP_TIME, self._async_start_ping_status, ping_status + ) + else: + # If mDNS is not available, start the ping status immediately + self._async_start_ping_status(ping_status) if settings.status_use_mqtt: from .status.mqtt import MqttStatusThread - status_thread_mqtt = MqttStatusThread() + status_thread_mqtt = MqttStatusThread(self) status_thread_mqtt.start() - shutdown_event = asyncio.Event() try: - await shutdown_event.wait() + await asyncio.Event().wait() finally: _LOGGER.info("Shutting down...") self.stop_event.set() self.ping_request.set() - if ping_status_task: - ping_status_task.cancel() + if start_ping_timer: + start_ping_timer.cancel() + if self._ping_status_task: + self._ping_status_task.cancel() + self._ping_status_task = None if mdns_task: mdns_task.cancel() if settings.status_use_mqtt: diff --git a/esphome/dashboard/dns.py b/esphome/dashboard/dns.py index b78a909220..ea85d338bf 100644 --- a/esphome/dashboard/dns.py +++ b/esphome/dashboard/dns.py @@ -1,6 +1,8 @@ from __future__ import annotations import asyncio +from contextlib import suppress +from ipaddress import ip_address import sys from icmplib import NameLookupError, async_resolve @@ -10,11 +12,15 @@ if sys.version_info >= (3, 11): else: from async_timeout import timeout as async_timeout +RESOLVE_TIMEOUT = 3.0 + async def _async_resolve_wrapper(hostname: str) -> list[str] | Exception: """Wrap the icmplib async_resolve function.""" + with suppress(ValueError): + return [str(ip_address(hostname))] try: - async with async_timeout(2): + async with async_timeout(RESOLVE_TIMEOUT): return await async_resolve(hostname) except (asyncio.TimeoutError, NameLookupError, UnicodeError) as ex: return ex diff --git a/esphome/dashboard/entries.py b/esphome/dashboard/entries.py index cb0d4a3772..e4825298f7 100644 --- a/esphome/dashboard/entries.py +++ b/esphome/dashboard/entries.py @@ -2,6 +2,8 @@ from __future__ import annotations import asyncio from collections import defaultdict +from dataclasses import dataclass +from functools import lru_cache import logging import os from typing import TYPE_CHECKING, Any @@ -27,37 +29,53 @@ _LOGGER = logging.getLogger(__name__) DashboardCacheKeyType = tuple[int, int, float, int] -# Currently EntryState is a simple -# online/offline/unknown enum, but in the future -# it may be expanded to include more states + +@dataclass(frozen=True) +class EntryState: + """Represents the state of an entry.""" + + reachable: ReachableState + source: EntryStateSource -class EntryState(StrEnum): - ONLINE = "online" - OFFLINE = "offline" +class EntryStateSource(StrEnum): + MDNS = "mdns" + PING = "ping" + MQTT = "mqtt" UNKNOWN = "unknown" -_BOOL_TO_ENTRY_STATE = { - True: EntryState.ONLINE, - False: EntryState.OFFLINE, - None: EntryState.UNKNOWN, -} -_ENTRY_STATE_TO_BOOL = { - EntryState.ONLINE: True, - EntryState.OFFLINE: False, - EntryState.UNKNOWN: None, -} +class ReachableState(StrEnum): + ONLINE = "online" + OFFLINE = "offline" + DNS_FAILURE = "dns_failure" + UNKNOWN = "unknown" -def bool_to_entry_state(value: bool) -> EntryState: +_BOOL_TO_REACHABLE_STATE = { + True: ReachableState.ONLINE, + False: ReachableState.OFFLINE, + None: ReachableState.UNKNOWN, +} +_REACHABLE_STATE_TO_BOOL = { + ReachableState.ONLINE: True, + ReachableState.OFFLINE: False, + ReachableState.DNS_FAILURE: False, + ReachableState.UNKNOWN: None, +} + +UNKNOWN_STATE = EntryState(ReachableState.UNKNOWN, EntryStateSource.UNKNOWN) + + +@lru_cache # creating frozen dataclass instances is expensive, so we cache them +def bool_to_entry_state(value: bool | None, source: EntryStateSource) -> EntryState: """Convert a bool to an entry state.""" - return _BOOL_TO_ENTRY_STATE[value] + return EntryState(_BOOL_TO_REACHABLE_STATE[value], source) def entry_state_to_bool(value: EntryState) -> bool | None: """Convert an entry state to a bool.""" - return _ENTRY_STATE_TO_BOOL[value] + return _REACHABLE_STATE_TO_BOOL[value.reachable] class DashboardEntries: @@ -119,6 +137,55 @@ class DashboardEntries: """Set the state for an entry.""" self.async_set_state(entry, state) + def set_state_if_online_or_source( + self, entry: DashboardEntry, state: EntryState + ) -> None: + """Set the state for an entry if its online or provided by the source or unknown.""" + asyncio.run_coroutine_threadsafe( + self._async_set_state_if_online_or_source(entry, state), self._loop + ).result() + + async def _async_set_state_if_online_or_source( + self, entry: DashboardEntry, state: EntryState + ) -> None: + """Set the state for an entry if its online or provided by the source or unknown.""" + self.async_set_state_if_online_or_source(entry, state) + + def async_set_state_if_online_or_source( + self, entry: DashboardEntry, state: EntryState + ) -> None: + """Set the state for an entry if its online or provided by the source or unknown.""" + if ( + state.reachable is ReachableState.ONLINE + and entry.state.reachable is not ReachableState.ONLINE + ) or entry.state.source in ( + EntryStateSource.UNKNOWN, + state.source, + ): + self.async_set_state(entry, state) + + def set_state_if_source(self, entry: DashboardEntry, state: EntryState) -> None: + """Set the state for an entry if provided by the source or unknown.""" + asyncio.run_coroutine_threadsafe( + self._async_set_state_if_source(entry, state), self._loop + ).result() + + async def _async_set_state_if_source( + self, entry: DashboardEntry, state: EntryState + ) -> None: + """Set the state for an entry if rovided by the source or unknown.""" + self.async_set_state_if_source(entry, state) + + def async_set_state_if_source( + self, entry: DashboardEntry, state: EntryState + ) -> None: + """Set the state for an entry if provided by the source or unknown.""" + if entry.state.source in ( + EntryStateSource.UNKNOWN, + state.source, + ): + self.async_set_state(entry, state) + def async_set_state(self, entry: DashboardEntry, state: EntryState) -> None: """Set the state for an entry.""" if entry.state == state: @@ -269,7 +336,7 @@ class DashboardEntry: self._storage_path = ext_storage_path(self.filename) self.cache_key = cache_key self.storage: StorageJSON | None = None - self.state = EntryState.UNKNOWN + self.state = UNKNOWN_STATE self._to_dict: dict[str, Any] | None = None def __repr__(self) -> str: diff --git a/esphome/dashboard/settings.py b/esphome/dashboard/settings.py index 1f05abab4c..fa39b55016 100644 --- a/esphome/dashboard/settings.py +++ b/esphome/dashboard/settings.py @@ -54,10 +54,6 @@ class DashboardSettings: def relative_url(self) -> str: return os.getenv("ESPHOME_DASHBOARD_RELATIVE_URL") or "/" - @property - def status_use_ping(self): - return get_bool_env("ESPHOME_DASHBOARD_USE_PING") - @property def status_use_mqtt(self) -> bool: return get_bool_env("ESPHOME_DASHBOARD_USE_MQTT") diff --git a/esphome/dashboard/status/mdns.py b/esphome/dashboard/status/mdns.py index 9f6399ca8b..f9ac7b4289 100644 --- a/esphome/dashboard/status/mdns.py +++ b/esphome/dashboard/status/mdns.py @@ -1,6 +1,8 @@ from __future__ import annotations import asyncio +import logging +import typing from esphome.zeroconf import ( ESPHOME_SERVICE_TYPE, @@ -11,20 +13,36 @@ from esphome.zeroconf import ( ) from ..const import SENTINEL -from ..core import DASHBOARD -from ..entries import DashboardEntry, bool_to_entry_state +from ..entries import DashboardEntry, EntryStateSource, bool_to_entry_state + +if typing.TYPE_CHECKING: + from ..core import ESPHomeDashboard + +_LOGGER = logging.getLogger(__name__) class MDNSStatus: """Class that updates the mdns status.""" - def __init__(self) -> None: + def __init__(self, dashboard: ESPHomeDashboard) -> None: """Initialize the MDNSStatus class.""" super().__init__() self.aiozc: AsyncEsphomeZeroconf | None = None # This is the current mdns state for each host (True, False, None) self.host_mdns_state: dict[str, bool | None] = {} self._loop = asyncio.get_running_loop() + self.dashboard = dashboard + + def async_setup(self) -> bool: + """Set up the MDNSStatus class.""" + try: + self.aiozc = AsyncEsphomeZeroconf() + except OSError as e: + _LOGGER.warning( + "Failed to initialize zeroconf, will fallback to ping: %s", e + ) + return False + return True async def async_resolve_host(self, host_name: str) -> list[str] | None: """Resolve a host name to an address in a thread-safe manner.""" @@ -32,9 +50,9 @@ class MDNSStatus: return await aiozc.async_resolve_host(host_name) return None - async def async_refresh_hosts(self): + async def async_refresh_hosts(self) -> None: """Refresh the hosts to track.""" - dashboard = DASHBOARD + dashboard = self.dashboard host_mdns_state = self.host_mdns_state entries = dashboard.entries poll_names: dict[str, set[DashboardEntry]] = {} @@ -49,7 +67,7 @@ class MDNSStatus: # the device won't respond to a request to ._esphomelib._tcp.local. poll_names.setdefault(entry.name, set()).add(entry) elif (online := host_mdns_state.get(entry.name, SENTINEL)) != SENTINEL: - entries.async_set_state(entry, bool_to_entry_state(online)) + self._async_set_state(entry, online) if poll_names and self.aiozc: results = await asyncio.gather( *(self.aiozc.async_resolve_host(name) for name in poll_names) @@ -58,13 +76,25 @@ class MDNSStatus: result = bool(address_list) host_mdns_state[name] = result for entry in poll_names[name]: - entries.async_set_state(entry, bool_to_entry_state(result)) + self._async_set_state(entry, result) + + def _async_set_state(self, entry: DashboardEntry, result: bool | None) -> None: + """Set the state of an entry.""" + state = bool_to_entry_state(result, EntryStateSource.MDNS) + if result: + # If we can reach it via mDNS, we always set it online + # since its the fastest source if its working + self.dashboard.entries.async_set_state(entry, state) + else: + # However if we can't reach it via mDNS + # we only set it to offline if the state is unknown + # or from mDNS + self.dashboard.entries.async_set_state_if_source(entry, state) async def async_run(self) -> None: - dashboard = DASHBOARD + """Run the mdns status.""" + dashboard = self.dashboard entries = dashboard.entries - aiozc = AsyncEsphomeZeroconf() - self.aiozc = aiozc host_mdns_state = self.host_mdns_state def on_update(dat: dict[str, bool | None]) -> None: @@ -73,15 +103,14 @@ class MDNSStatus: host_mdns_state[name] = result if matching_entries := entries.get_by_name(name): for entry in matching_entries: - if not entry.no_mdns: - entries.async_set_state(entry, bool_to_entry_state(result)) + self._async_set_state(entry, result) stat = DashboardStatus(on_update) imports = DashboardImportDiscovery() dashboard.import_result = imports.import_state browser = DashboardBrowser( - aiozc.zeroconf, + self.aiozc.zeroconf, ESPHOME_SERVICE_TYPE, [stat.browser_callback, imports.browser_callback], ) @@ -93,5 +122,5 @@ class MDNSStatus: ping_request.clear() await browser.async_cancel() - await aiozc.async_close() + await self.aiozc.async_close() self.aiozc = None diff --git a/esphome/dashboard/status/mqtt.py b/esphome/dashboard/status/mqtt.py index 8c35dd2535..70eb0b58b5 100644 --- a/esphome/dashboard/status/mqtt.py +++ b/esphome/dashboard/status/mqtt.py @@ -4,19 +4,27 @@ import binascii import json import os import threading +import typing from esphome import mqtt -from ..core import DASHBOARD -from ..entries import EntryState +from ..entries import EntryStateSource, bool_to_entry_state + +if typing.TYPE_CHECKING: + from ..core import ESPHomeDashboard class MqttStatusThread(threading.Thread): """Status thread to get the status of the devices via MQTT.""" + def __init__(self, dashboard: ESPHomeDashboard) -> None: + """Initialize the status thread.""" + super().__init__() + self.dashboard = dashboard + def run(self) -> None: """Run the status thread.""" - dashboard = DASHBOARD + dashboard = self.dashboard entries = dashboard.entries current_entries = entries.all() @@ -31,10 +39,13 @@ class MqttStatusThread(threading.Thread): data = json.loads(payload) if "name" not in data: return - for entry in current_entries: - if entry.name == data["name"]: - entries.set_state(entry, EntryState.ONLINE) - return + if matching_entries := entries.get_by_name(data["name"]): + for entry in matching_entries: + # Only override state if we don't have a state from another source + # or we have a state from MQTT and the device is reachable + entries.set_state_if_online_or_source( + entry, bool_to_entry_state(True, EntryStateSource.MQTT) + ) def on_connect(client, userdata, flags, return_code): client.publish("esphome/discover", None, retain=False) @@ -56,8 +67,10 @@ class MqttStatusThread(threading.Thread): current_entries = entries.all() # will be set to true on on_message for entry in current_entries: - if entry.no_mdns: - entries.set_state(entry, EntryState.OFFLINE) + # Only override state if we don't have a state from another source + entries.set_state_if_source( + entry, bool_to_entry_state(False, EntryStateSource.MQTT) + ) client.publish("esphome/discover", None, retain=False) dashboard.mqtt_ping_request.wait() diff --git a/esphome/dashboard/status/ping.py b/esphome/dashboard/status/ping.py index 6630f03c9d..b4f106d21a 100644 --- a/esphome/dashboard/status/ping.py +++ b/esphome/dashboard/status/ping.py @@ -3,29 +3,44 @@ from __future__ import annotations import asyncio import logging import time +import typing from typing import cast from icmplib import Host, SocketPermissionError, async_ping from ..const import MAX_EXECUTOR_WORKERS -from ..core import DASHBOARD -from ..entries import DashboardEntry, EntryState, bool_to_entry_state +from ..entries import ( + DashboardEntry, + EntryState, + EntryStateSource, + ReachableState, + bool_to_entry_state, +) from ..util.itertools import chunked +if typing.TYPE_CHECKING: + from ..core import ESPHomeDashboard + + _LOGGER = logging.getLogger(__name__) GROUP_SIZE = int(MAX_EXECUTOR_WORKERS / 2) +DNS_FAILURE_STATE = EntryState(ReachableState.DNS_FAILURE, EntryStateSource.PING) + +MIN_PING_INTERVAL = 5 # ensure we don't ping too often + class PingStatus: - def __init__(self) -> None: + def __init__(self, dashboard: ESPHomeDashboard) -> None: """Initialize the PingStatus class.""" super().__init__() self._loop = asyncio.get_running_loop() + self.dashboard = dashboard async def async_run(self) -> None: """Run the ping status.""" - dashboard = DASHBOARD + dashboard = self.dashboard entries = dashboard.entries privileged = await _can_use_icmp_lib_with_privilege() if privileged is None: @@ -36,10 +51,24 @@ class PingStatus: # Only ping if the dashboard is open await dashboard.ping_request.wait() dashboard.ping_request.clear() + iteration_start = time.monotonic() current_entries = dashboard.entries.async_all() - to_ping: list[DashboardEntry] = [ - entry for entry in current_entries if entry.address is not None - ] + to_ping: list[DashboardEntry] = [] + + for entry in current_entries: + if entry.address is None: + # No address or we already have a state from another source + # so no need to ping + continue + if ( + entry.state.reachable is ReachableState.ONLINE + and entry.state.source + not in (EntryStateSource.PING, EntryStateSource.UNKNOWN) + ): + # If we already have a state from another source and + # it's online, we don't need to ping + continue + to_ping.append(entry) # Resolve DNS for all entries entries_with_addresses: dict[DashboardEntry, list[str]] = {} @@ -56,7 +85,10 @@ class PingStatus: for entry, result in zip(ping_group, dns_results): if isinstance(result, Exception): - entries.async_set_state(entry, EntryState.UNKNOWN) + # Only update state if its unknown or from ping + # so we don't mark it as offline if we have a state + # from mDNS or MQTT + entries.async_set_state_if_source(entry, DNS_FAILURE_STATE) continue if isinstance(result, BaseException): raise result @@ -82,8 +114,20 @@ class PingStatus: else: host: Host = result ping_result = host.is_alive - entry, _ = entry_addresses - entries.async_set_state(entry, bool_to_entry_state(ping_result)) + entry: DashboardEntry = entry_addresses[0] + # If we can reach it via ping, we always set it + # online, however if we can't reach it via ping + # we only set it to offline if the state is unknown + # or from ping + entries.async_set_state_if_online_or_source( + entry, + bool_to_entry_state(ping_result, EntryStateSource.PING), + ) + + if not dashboard.stop_event.is_set(): + iteration_duration = time.monotonic() - iteration_start + if iteration_duration < MIN_PING_INTERVAL: + await asyncio.sleep(MIN_PING_INTERVAL - iteration_duration) async def _can_use_icmp_lib_with_privilege() -> None | bool: diff --git a/esphome/dashboard/web_server.py b/esphome/dashboard/web_server.py index b8562aaccb..9c20cf4f58 100644 --- a/esphome/dashboard/web_server.py +++ b/esphome/dashboard/web_server.py @@ -33,18 +33,24 @@ import tornado.process import tornado.queues import tornado.web import tornado.websocket +import voluptuous as vol import yaml from yaml.nodes import Node from esphome import const, platformio_api, yaml_util from esphome.helpers import get_bool_env, mkdir_p -from esphome.storage_json import StorageJSON, ext_storage_path, trash_storage_path +from esphome.storage_json import ( + StorageJSON, + archive_storage_path, + ext_storage_path, + trash_storage_path, +) from esphome.util import get_serial_ports, shlex_quote from esphome.yaml_util import FastestAvailableSafeLoader from .const import DASHBOARD_COMMAND from .core import DASHBOARD -from .entries import EntryState, entry_state_to_bool +from .entries import UNKNOWN_STATE, entry_state_to_bool from .util.file import write_file from .util.subprocess import async_run_system_command from .util.text import friendly_name_slugify @@ -52,7 +58,6 @@ from .util.text import friendly_name_slugify if TYPE_CHECKING: from requests import Response - _LOGGER = logging.getLogger(__name__) ENV_DEV = "ESPHOME_DASHBOARD_DEV" @@ -381,7 +386,7 @@ class EsphomeRenameHandler(EsphomeCommandWebSocket): # Remove the old ping result from the cache entries = DASHBOARD.entries if entry := entries.get(self.old_name): - entries.async_set_state(entry, EntryState.UNKNOWN) + entries.async_set_state(entry, UNKNOWN_STATE) class EsphomeUploadHandler(EsphomePortCommandWebSocket): @@ -592,16 +597,39 @@ class IgnoreDeviceRequestHandler(BaseHandler): class DownloadListRequestHandler(BaseHandler): @authenticated @bind_config - def get(self, configuration: str | None = None) -> None: + async def get(self, configuration: str | None = None) -> None: + loop = asyncio.get_running_loop() + try: + downloads_json = await loop.run_in_executor(None, self._get, configuration) + except vol.Invalid: + self.send_error(404) + return + if downloads_json is None: + self.send_error(404) + return + self.set_status(200) + self.set_header("content-type", "application/json") + self.write(downloads_json) + self.finish() + + def _get(self, configuration: str | None = None) -> dict[str, Any] | None: storage_path = ext_storage_path(configuration) storage_json = StorageJSON.load(storage_path) if storage_json is None: - self.send_error(404) - return + return None + + config = yaml_util.load_yaml(settings.rel_path(configuration)) + + if const.CONF_EXTERNAL_COMPONENTS in config: + from esphome.components.external_components import ( + do_external_components_pass, + ) + + do_external_components_pass(config) from esphome.components.esp32 import VARIANTS as ESP32_VARIANTS - downloads = [] + downloads: list[dict[str, Any]] = [] platform: str = storage_json.target_platform.lower() if platform.upper() in ESP32_VARIANTS: @@ -615,12 +643,7 @@ class DownloadListRequestHandler(BaseHandler): except AttributeError as exc: raise ValueError(f"Unknown platform {platform}") from exc downloads = get_download_types(storage_json) - - self.set_status(200) - self.set_header("content-type", "application/json") - self.write(json.dumps(downloads)) - self.finish() - return + return json.dumps(downloads) class DownloadBinaryRequestHandler(BaseHandler): @@ -918,16 +941,16 @@ class EditRequestHandler(BaseHandler): self.set_status(200) -class DeleteRequestHandler(BaseHandler): +class ArchiveRequestHandler(BaseHandler): @authenticated @bind_config def post(self, configuration: str | None = None) -> None: config_file = settings.rel_path(configuration) storage_path = ext_storage_path(configuration) - trash_path = trash_storage_path() - mkdir_p(trash_path) - shutil.move(config_file, os.path.join(trash_path, configuration)) + archive_path = archive_storage_path() + mkdir_p(archive_path) + shutil.move(config_file, os.path.join(archive_path, configuration)) storage_json = StorageJSON.load(storage_path) if storage_json is not None: @@ -935,16 +958,16 @@ class DeleteRequestHandler(BaseHandler): name = storage_json.name build_folder = os.path.join(settings.config_dir, name) if build_folder is not None: - shutil.rmtree(build_folder, os.path.join(trash_path, name)) + shutil.rmtree(build_folder, os.path.join(archive_path, name)) -class UndoDeleteRequestHandler(BaseHandler): +class UnArchiveRequestHandler(BaseHandler): @authenticated @bind_config def post(self, configuration: str | None = None) -> None: config_file = settings.rel_path(configuration) - trash_path = trash_storage_path() - shutil.move(os.path.join(trash_path, configuration), config_file) + archive_path = archive_storage_path() + shutil.move(os.path.join(archive_path, configuration), config_file) class LoginHandler(BaseHandler): @@ -1185,8 +1208,10 @@ def make_app(debug=get_bool_env(ENV_DEV)) -> tornado.web.Application: (f"{rel}download.bin", DownloadBinaryRequestHandler), (f"{rel}serial-ports", SerialPortRequestHandler), (f"{rel}ping", PingRequestHandler), - (f"{rel}delete", DeleteRequestHandler), - (f"{rel}undo-delete", UndoDeleteRequestHandler), + (f"{rel}delete", ArchiveRequestHandler), + (f"{rel}undo-delete", UnArchiveRequestHandler), + (f"{rel}archive", ArchiveRequestHandler), + (f"{rel}unarchive", UnArchiveRequestHandler), (f"{rel}wizard", WizardRequestHandler), (f"{rel}static/(.*)", StaticFileHandler, {"path": get_static_path()}), (f"{rel}devices", ListDevicesHandler), @@ -1211,6 +1236,13 @@ def start_web_server( config_dir: str, ) -> None: """Start the web server listener.""" + + trash_path = trash_storage_path() + if os.path.exists(trash_path): + _LOGGER.info("Renaming 'trash' folder to 'archive'") + archive_path = archive_storage_path() + shutil.move(trash_path, archive_path) + if socket is None: _LOGGER.info( "Starting dashboard web server on http://%s:%s and configuration dir %s...", diff --git a/esphome/idf_component.yml b/esphome/idf_component.yml index bd5bcda2fe..32ba414ba3 100644 --- a/esphome/idf_component.yml +++ b/esphome/idf_component.yml @@ -7,7 +7,7 @@ dependencies: version: v2.0.9 mdns: git: https://github.com/espressif/esp-protocols.git - version: mdns-v1.5.1 + version: mdns-v1.8.0 path: components/mdns rules: - if: "idf_version >=5.0" diff --git a/esphome/storage_json.py b/esphome/storage_json.py index 97cf9ceadd..fa9fe43d4d 100644 --- a/esphome/storage_json.py +++ b/esphome/storage_json.py @@ -36,6 +36,10 @@ def trash_storage_path() -> str: return CORE.relative_config_path("trash") +def archive_storage_path() -> str: + return CORE.relative_config_path("archive") + + class StorageJSON: def __init__( self, diff --git a/esphome/wizard.py b/esphome/wizard.py index eecbbdb172..7fdf245c76 100644 --- a/esphome/wizard.py +++ b/esphome/wizard.py @@ -144,17 +144,17 @@ def wizard_file(**kwargs): # Configure API if "password" in kwargs: - config += f" password: \"{kwargs['password']}\"\n" + config += f' password: "{kwargs["password"]}"\n' if "api_encryption_key" in kwargs: - config += f" encryption:\n key: \"{kwargs['api_encryption_key']}\"\n" + config += f' encryption:\n key: "{kwargs["api_encryption_key"]}"\n' # Configure OTA config += "\nota:\n" config += " - platform: esphome\n" if "ota_password" in kwargs: - config += f" password: \"{kwargs['ota_password']}\"" + config += f' password: "{kwargs["ota_password"]}"' elif "password" in kwargs: - config += f" password: \"{kwargs['password']}\"" + config += f' password: "{kwargs["password"]}"' # Configuring wifi config += "\n\nwifi:\n" @@ -181,18 +181,14 @@ def wizard_file(**kwargs): password: "{fallback_psk}" captive_portal: - """.format( - **kwargs - ) + """.format(**kwargs) else: config += """ # Enable fallback hotspot in case wifi connection fails ap: ssid: "{fallback_name}" password: "{fallback_psk}" - """.format( - **kwargs - ) + """.format(**kwargs) return config @@ -388,19 +384,19 @@ def wizard(path): safe_print() # Don't sleep because user needs to copy link if platform == "ESP32": - safe_print(f"For example \"{color(Fore.BOLD_WHITE, 'nodemcu-32s')}\".") + safe_print(f'For example "{color(Fore.BOLD_WHITE, "nodemcu-32s")}".') boards_list = esp32_boards.BOARDS.items() elif platform == "ESP8266": - safe_print(f"For example \"{color(Fore.BOLD_WHITE, 'nodemcuv2')}\".") + safe_print(f'For example "{color(Fore.BOLD_WHITE, "nodemcuv2")}".') boards_list = esp8266_boards.BOARDS.items() elif platform == "BK72XX": - safe_print(f"For example \"{color(Fore.BOLD_WHITE, 'cb2s')}\".") + safe_print(f'For example "{color(Fore.BOLD_WHITE, "cb2s")}".') boards_list = bk72xx_boards.BOARDS.items() elif platform == "RTL87XX": - safe_print(f"For example \"{color(Fore.BOLD_WHITE, 'wr3')}\".") + safe_print(f'For example "{color(Fore.BOLD_WHITE, "wr3")}".') boards_list = rtl87xx_boards.BOARDS.items() elif platform == "RP2040": - safe_print(f"For example \"{color(Fore.BOLD_WHITE, 'rpipicow')}\".") + safe_print(f'For example "{color(Fore.BOLD_WHITE, "rpipicow")}".') boards_list = rp2040_boards.BOARDS.items() else: @@ -439,7 +435,7 @@ def wizard(path): f"First, what's the {color(Fore.GREEN, 'SSID')} (the name) of the WiFi network {name} should connect to?" ) sleep(1.5) - safe_print(f"For example \"{color(Fore.BOLD_WHITE, 'Abraham Linksys')}\".") + safe_print(f'For example "{color(Fore.BOLD_WHITE, "Abraham Linksys")}".') while True: ssid = safe_input(color(Fore.BOLD_WHITE, "(ssid): ")) try: @@ -465,7 +461,7 @@ def wizard(path): f"Now please state the {color(Fore.GREEN, 'password')} of the WiFi network so that I can connect to it (Leave empty for no password)" ) safe_print() - safe_print(f"For example \"{color(Fore.BOLD_WHITE, 'PASSWORD42')}\"") + safe_print(f'For example "{color(Fore.BOLD_WHITE, "PASSWORD42")}"') sleep(0.5) psk = safe_input(color(Fore.BOLD_WHITE, "(PSK): ")) safe_print( diff --git a/esphome/writer.py b/esphome/writer.py index 90446ae4b1..39423db64c 100644 --- a/esphome/writer.py +++ b/esphome/writer.py @@ -212,9 +212,7 @@ def write_platformio_project(): write_platformio_ini(content) -DEFINES_H_FORMAT = ( - ESPHOME_H_FORMAT -) = """\ +DEFINES_H_FORMAT = ESPHOME_H_FORMAT = """\ #pragma once #include "esphome/core/macros.h" {} diff --git a/esphome/yaml_util.py b/esphome/yaml_util.py index b27ce4c3e3..431f397e38 100644 --- a/esphome/yaml_util.py +++ b/esphome/yaml_util.py @@ -273,48 +273,18 @@ class ESPHomeLoaderMixin: @_add_data_ref def construct_include(self, node): + from esphome.const import CONF_VARS + def extract_file_vars(node): fields = self.construct_yaml_map(node) file = fields.get("file") if file is None: raise yaml.MarkedYAMLError("Must include 'file'", node.start_mark) - vars = fields.get("vars") + vars = fields.get(CONF_VARS) if vars: vars = {k: str(v) for k, v in vars.items()} return file, vars - def substitute_vars(config, vars): - from esphome.components import substitutions - from esphome.const import CONF_DEFAULTS, CONF_SUBSTITUTIONS - - org_subs = None - result = config - if not isinstance(config, dict): - # when the included yaml contains a list or a scalar - # wrap it into an OrderedDict because do_substitution_pass expects it - result = OrderedDict([("yaml", config)]) - elif CONF_SUBSTITUTIONS in result: - org_subs = result.pop(CONF_SUBSTITUTIONS) - - defaults = {} - if CONF_DEFAULTS in result: - defaults = result.pop(CONF_DEFAULTS) - - result[CONF_SUBSTITUTIONS] = vars - for k, v in defaults.items(): - if k not in result[CONF_SUBSTITUTIONS]: - result[CONF_SUBSTITUTIONS][k] = v - - # Ignore missing vars that refer to the top level substitutions - substitutions.do_substitution_pass(result, None, ignore_missing=True) - result.pop(CONF_SUBSTITUTIONS) - - if not isinstance(config, dict): - result = result["yaml"] # unwrap the result - elif org_subs: - result[CONF_SUBSTITUTIONS] = org_subs - return result - if isinstance(node, yaml.nodes.MappingNode): file, vars = extract_file_vars(node) else: @@ -432,6 +402,39 @@ def parse_yaml(file_name: str, file_handle: TextIOWrapper) -> Any: ) +def substitute_vars(config, vars): + from esphome.components import substitutions + from esphome.const import CONF_DEFAULTS, CONF_SUBSTITUTIONS + + org_subs = None + result = config + if not isinstance(config, dict): + # when the included yaml contains a list or a scalar + # wrap it into an OrderedDict because do_substitution_pass expects it + result = OrderedDict([("yaml", config)]) + elif CONF_SUBSTITUTIONS in result: + org_subs = result.pop(CONF_SUBSTITUTIONS) + + defaults = {} + if CONF_DEFAULTS in result: + defaults = result.pop(CONF_DEFAULTS) + + result[CONF_SUBSTITUTIONS] = vars + for k, v in defaults.items(): + if k not in result[CONF_SUBSTITUTIONS]: + result[CONF_SUBSTITUTIONS][k] = v + + # Ignore missing vars that refer to the top level substitutions + substitutions.do_substitution_pass(result, None, ignore_missing=True) + result.pop(CONF_SUBSTITUTIONS) + + if not isinstance(config, dict): + result = result["yaml"] # unwrap the result + elif org_subs: + result[CONF_SUBSTITUTIONS] = org_subs + return result + + def _load_yaml_internal(fname: str) -> Any: """Load a YAML file.""" try: diff --git a/esphome/zeroconf.py b/esphome/zeroconf.py index 5a92a4ed7c..b235f06786 100644 --- a/esphome/zeroconf.py +++ b/esphome/zeroconf.py @@ -5,7 +5,13 @@ from dataclasses import dataclass import logging from typing import Callable -from zeroconf import IPVersion, ServiceInfo, ServiceStateChange, Zeroconf +from zeroconf import ( + AddressResolver, + IPVersion, + ServiceInfo, + ServiceStateChange, + Zeroconf, +) from zeroconf.asyncio import AsyncServiceBrowser, AsyncServiceInfo, AsyncZeroconf from esphome.storage_json import StorageJSON, ext_storage_path @@ -16,15 +22,6 @@ _LOGGER = logging.getLogger(__name__) _BACKGROUND_TASKS: set[asyncio.Task] = set() -class HostResolver(ServiceInfo): - """Resolve a host name to an IP address.""" - - @property - def _is_complete(self) -> bool: - """The ServiceInfo has all expected properties.""" - return bool(self._ipv4_addresses) - - class DashboardStatus: def __init__(self, on_update: Callable[[dict[str, bool | None], []]]) -> None: """Initialize the dashboard status.""" @@ -166,19 +163,10 @@ class DashboardImportDiscovery: ) -def _make_host_resolver(host: str) -> HostResolver: - """Create a new HostResolver for the given host name.""" - name = host.partition(".")[0] - info = HostResolver( - ESPHOME_SERVICE_TYPE, f"{name}.{ESPHOME_SERVICE_TYPE}", server=f"{name}.local." - ) - return info - - class EsphomeZeroconf(Zeroconf): def resolve_host(self, host: str, timeout: float = 3.0) -> list[str] | None: """Resolve a host name to an IP address.""" - info = _make_host_resolver(host) + info = AddressResolver(f"{host.partition('.')[0]}.local.") if ( info.load_from_cache(self) or (timeout and info.request(self, timeout * 1000)) @@ -192,7 +180,7 @@ class AsyncEsphomeZeroconf(AsyncZeroconf): self, host: str, timeout: float = 3.0 ) -> list[str] | None: """Resolve a host name to an IP address.""" - info = _make_host_resolver(host) + info = AddressResolver(f"{host.partition('.')[0]}.local.") if ( info.load_from_cache(self.zeroconf) or (timeout and await info.async_request(self.zeroconf, timeout * 1000)) diff --git a/platformio.ini b/platformio.ini index 4153310480..88e7c3b331 100644 --- a/platformio.ini +++ b/platformio.ini @@ -62,14 +62,14 @@ lib_deps = SPI ; spi (Arduino built-in) Wire ; i2c (Arduino built-int) heman/AsyncMqttClient-esphome@1.0.0 ; mqtt - esphome/ESPAsyncWebServer-esphome@3.2.2 ; web_server_base + esphome/ESPAsyncWebServer-esphome@3.3.0 ; web_server_base fastled/FastLED@3.3.2 ; fastled_base mikalhart/TinyGPSPlus@1.0.2 ; gps freekode/TM1651@1.0.1 ; tm1651 glmnet/Dsmr@0.7 ; dsmr rweather/Crypto@0.4.0 ; dsmr dudanov/MideaUART@1.1.9 ; midea - tonia/HeatpumpIR@1.0.27 ; heatpumpir + tonia/HeatpumpIR@1.0.32 ; heatpumpir build_flags = ${common.build_flags} -DUSE_ARDUINO @@ -128,7 +128,7 @@ lib_deps = DNSServer ; captive_portal (Arduino built-in) esphome/ESP32-audioI2S@2.0.7 ; i2s_audio droscy/esp_wireguard@0.4.2 ; wireguard - esphome/esp-audio-libs@1.1.1 ; audio + esphome/esp-audio-libs@1.1.3 ; audio build_flags = ${common:arduino.build_flags} @@ -149,7 +149,7 @@ lib_deps = ${common:idf.lib_deps} droscy/esp_wireguard@0.4.2 ; wireguard kahrendt/ESPMicroSpeechFeatures@1.1.0 ; micro_wake_word - esphome/esp-audio-libs@1.1.1 ; audio + esphome/esp-audio-libs@1.1.3 ; audio build_flags = ${common:idf.build_flags} -Wno-nonnull-compare diff --git a/pyproject.toml b/pyproject.toml index 7789f6d645..69b36cd14a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,18 +43,16 @@ include-package-data = true [tool.setuptools.dynamic] dependencies = {file = ["requirements.txt"]} -optional-dependencies.dev = { file = ["requirements_dev.txt"] } -optional-dependencies.test = { file = ["requirements_test.txt"] } -optional-dependencies.displays = { file = ["requirements_optional.txt"] } version = {attr = "esphome.const.__version__"} +[tool.setuptools.dynamic.optional-dependencies] +dev = { file = ["requirements_dev.txt"] } +test = { file = ["requirements_test.txt"] } +displays = { file = ["requirements_optional.txt"] } + [tool.setuptools.packages.find] include = ["esphome*"] -[tool.black] -target-version = ["py39", "py310"] -exclude = 'generated' - [tool.pytest.ini_options] testpaths = [ "tests", @@ -108,6 +106,8 @@ expected-line-ending-format = "LF" [tool.ruff] required-version = ">=0.5.0" +target-version = "py39" +exclude = ['generated'] [tool.ruff.lint] select = [ diff --git a/requirements.txt b/requirements.txt index 2aef8b34f5..46746b08cf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,20 +1,20 @@ async_timeout==4.0.3; python_version <= "3.10" -cryptography==43.0.0 +cryptography==44.0.2 voluptuous==0.14.2 PyYAML==6.0.2 paho-mqtt==1.6.1 colorama==0.4.6 icmplib==3.0.4 -tornado==6.4 +tornado==6.4.2 tzlocal==5.2 # from time tzdata>=2021.1 # from time pyserial==3.5 platformio==6.1.16 # When updating platformio, also update Dockerfile -esptool==4.7.0 +esptool==4.8.1 click==8.1.7 esphome-dashboard==20250212.0 -aioesphomeapi==29.3.2 -zeroconf==0.145.1 +aioesphomeapi==29.6.0 +zeroconf==0.146.1 puremagic==1.27 ruamel.yaml==0.18.6 # dashboard_import esphome-glyphsets==0.1.0 diff --git a/requirements_test.txt b/requirements_test.txt index 5d94f7f640..d836efc148 100644 --- a/requirements_test.txt +++ b/requirements_test.txt @@ -1,6 +1,6 @@ pylint==3.2.7 flake8==7.0.0 # also change in .pre-commit-config.yaml when updating -black==24.4.2 # also change in .pre-commit-config.yaml when updating +ruff==0.9.2 # also change in .pre-commit-config.yaml when updating pyupgrade==3.15.2 # also change in .pre-commit-config.yaml when updating pre-commit diff --git a/script/clang-format b/script/clang-format index d922c5b6f1..b1e84a56b7 100755 --- a/script/clang-format +++ b/script/clang-format @@ -1,7 +1,6 @@ #!/usr/bin/env python3 import argparse -import multiprocessing import os import queue import re @@ -11,7 +10,13 @@ import threading import click import colorama -from helpers import filter_changed, get_binary, git_ls_files, print_error_for_file +from helpers import ( + filter_changed, + get_binary, + get_usable_cpu_count, + git_ls_files, + print_error_for_file, +) def run_format(executable, args, queue, lock, failed_files): @@ -25,7 +30,9 @@ def run_format(executable, args, queue, lock, failed_files): invocation.extend(["--dry-run", "-Werror"]) invocation.append(path) - proc = subprocess.run(invocation, capture_output=True, encoding="utf-8") + proc = subprocess.run( + invocation, capture_output=True, encoding="utf-8", check=False + ) if proc.returncode != 0: with lock: print_error_for_file(path, proc.stderr) @@ -45,7 +52,7 @@ def main(): "-j", "--jobs", type=int, - default=multiprocessing.cpu_count(), + default=get_usable_cpu_count(), help="number of format instances to be run in parallel.", ) parser.add_argument( @@ -80,7 +87,8 @@ def main(): lock = threading.Lock() for _ in range(args.jobs): t = threading.Thread( - target=run_format, args=(executable, args, task_queue, lock, failed_files) + target=run_format, + args=(executable, args, task_queue, lock, failed_files), ) t.daemon = True t.start() @@ -95,7 +103,7 @@ def main(): # Wait for all threads to be done. task_queue.join() - except FileNotFoundError as ex: + except FileNotFoundError: return 1 except KeyboardInterrupt: print() @@ -103,7 +111,7 @@ def main(): # Kill subprocesses (and ourselves!) # No simple, clean alternative appears to be available. os.kill(0, 9) - return 2 # Will not execute. + return 2 # Will not execute. return len(failed_files) diff --git a/script/clang-tidy b/script/clang-tidy index 5c19f81043..51705f955b 100755 --- a/script/clang-tidy +++ b/script/clang-tidy @@ -1,7 +1,6 @@ #!/usr/bin/env python3 import argparse -import multiprocessing import os import queue import re @@ -19,6 +18,7 @@ from helpers import ( filter_changed, filter_grep, get_binary, + get_usable_cpu_count, git_ls_files, load_idedata, print_error_for_file, @@ -170,7 +170,7 @@ def main(): "-j", "--jobs", type=int, - default=multiprocessing.cpu_count(), + default=get_usable_cpu_count(), help="number of tidy instances to be run in parallel.", ) parser.add_argument( diff --git a/script/helpers.py b/script/helpers.py index 6f36faaeb1..6148371e32 100644 --- a/script/helpers.py +++ b/script/helpers.py @@ -188,3 +188,14 @@ def get_binary(name: str, version: str) -> str: """ ) raise + + +def get_usable_cpu_count() -> int: + """Return the number of CPUs that can be used for processes. + + On Python 3.13+ this is the number of CPUs that can be used for processes. + On older Python versions this is the number of CPUs. + """ + return ( + os.process_cpu_count() if hasattr(os, "process_cpu_count") else os.cpu_count() + ) diff --git a/script/lint-python b/script/lint-python index 01e5e76190..c9f1789160 100755 --- a/script/lint-python +++ b/script/lint-python @@ -19,7 +19,7 @@ curfile = None def print_error(file, lineno, msg): - global curfile + global curfile # noqa: PLW0603 if curfile != file: print_error_for_file(file, None) @@ -31,6 +31,22 @@ def print_error(file, lineno, msg): print(f"{styled(colorama.Style.BRIGHT, f'{file}:')} {msg}") +def split_args_platform_compatible(args): + if os.name == "posix": + return [args] + + char_length = 0 + argsets = [] + for index, arg in enumerate(args): + # Windows is techincally 8191, but we need to leave some room for the command itself + if char_length + len(arg) > 8000: + argsets.append(args[:index]) + args = args[index:] + char_length = 0 + char_length += len(arg) + return argsets + + def main(): colorama.init() @@ -69,61 +85,70 @@ def main(): errors = 0 - cmd = ["black", "--verbose"] + ([] if args.apply else ["--check"]) + files - print("Running black...") - print() - log = get_err(*cmd) - for line in log.splitlines(): - WOULD_REFORMAT = "would reformat" - if line.startswith(WOULD_REFORMAT): - file_ = line[len(WOULD_REFORMAT) + 1 :] - print_error(file_, None, "Please format this file with the black formatter") - errors += 1 + # Needed to get around command-line string limits in Windows. + filesets = split_args_platform_compatible(files) + + print("Running ruff...") + print() + for fileset in filesets: + cmd = ["ruff", "format"] + ([] if args.apply else ["--check"]) + fileset + log = get_err(*cmd) + for line in log.splitlines(): + WOULD_REFORMAT = "would reformat" + if line.startswith(WOULD_REFORMAT): + file_ = line[len(WOULD_REFORMAT) + 1 :] + print_error( + file_, None, "Please format this file with the ruff formatter" + ) + errors += 1 - cmd = ["flake8"] + files print() print("Running flake8...") print() - log = get_output(*cmd) - for line in log.splitlines(): - line = line.split(":", 4) - if len(line) < 4: - continue - file_ = line[0] - linno = line[1] - msg = (":".join(line[3:])).strip() - print_error(file_, linno, msg) - errors += 1 + for files in filesets: + cmd = ["flake8"] + files + log = get_output(*cmd) + for line in log.splitlines(): + line = line.split(":", 4) + if len(line) < 4: + continue + file_ = line[0] + linno = line[1] + msg = (":".join(line[3:])).strip() + print_error(file_, linno, msg) + errors += 1 - cmd = ["pylint", "-f", "parseable", "--persistent=n"] + files print() print("Running pylint...") print() - log = get_output(*cmd) - for line in log.splitlines(): - line = line.split(":", 3) - if len(line) < 3: - continue - file_ = line[0] - linno = line[1] - msg = (":".join(line[2:])).strip() - print_error(file_, linno, msg) - errors += 1 + for files in filesets: + cmd = ["pylint", "-f", "parseable", "--persistent=n"] + files + log = get_output(*cmd) + for line in log.splitlines(): + line = line.split(":", 3) + if len(line) < 3: + continue + file_ = line[0] + linno = line[1] + msg = (":".join(line[2:])).strip() + print_error(file_, linno, msg) + errors += 1 - PYUPGRADE_TARGET = "--py39-plus" - cmd = ["pyupgrade", PYUPGRADE_TARGET] + files print() print("Running pyupgrade...") print() - log = get_err(*cmd) - for line in log.splitlines(): - REWRITING = "Rewriting" - if line.startswith(REWRITING): - file_ = line[len(REWRITING) + 1 :] - print_error( - file_, None, f"Please run pyupgrade {PYUPGRADE_TARGET} on this file" - ) - errors += 1 + PYUPGRADE_TARGET = "--py39-plus" + for files in filesets: + cmd = ["pyupgrade", PYUPGRADE_TARGET] + files + log = get_err(*cmd) + for line in log.splitlines(): + REWRITING = "Rewriting" + if line.startswith(REWRITING): + file_ = line[len(REWRITING) + 1 :] + print_error( + file_, None, f"Please run pyupgrade {PYUPGRADE_TARGET} on this file" + ) + errors += 1 sys.exit(errors) diff --git a/tests/component_tests/binary_sensor/test_binary_sensor.py b/tests/component_tests/binary_sensor/test_binary_sensor.py index 514bc6ee5f..32d74027ba 100644 --- a/tests/component_tests/binary_sensor/test_binary_sensor.py +++ b/tests/component_tests/binary_sensor/test_binary_sensor.py @@ -47,3 +47,19 @@ def test_binary_sensor_config_value_internal_set(generate_main): # Then assert "bs_1->set_internal(true);" in main_cpp assert "bs_2->set_internal(false);" in main_cpp + + +def test_binary_sensor_config_value_use_raw_set(generate_main): + """ + Test that the "use_raw" config value is correctly set + """ + # Given + + # When + main_cpp = generate_main( + "tests/component_tests/binary_sensor/test_binary_sensor.yaml" + ) + + # Then + assert "bs_3->set_use_raw(true);" in main_cpp + assert "bs_4->set_use_raw(false);" in main_cpp diff --git a/tests/component_tests/binary_sensor/test_binary_sensor.yaml b/tests/component_tests/binary_sensor/test_binary_sensor.yaml index 8842dda837..60f9472cfb 100644 --- a/tests/component_tests/binary_sensor/test_binary_sensor.yaml +++ b/tests/component_tests/binary_sensor/test_binary_sensor.yaml @@ -2,8 +2,30 @@ esphome: name: test -esp8266: - board: d1_mini_lite +esp32: + board: m5stack-core2 + +i2c: + sda: GPIO21 + scl: GPIO22 + +spi: + clk_pin: GPIO18 + mosi_pin: GPIO23 + miso_pin: GPIO19 + +display: + platform: ili9xxx + id: lcd + model: M5STACK + dc_pin: GPIO15 + cs_pin: GPIO5 + invert_colors: true + +touchscreen: + platform: ft63x6 + id: touch + interrupt_pin: GPIO39 binary_sensor: - platform: gpio @@ -11,10 +33,25 @@ binary_sensor: name: test bs1 internal: true pin: - number: D0 + number: GPIO32 - platform: gpio id: bs_2 name: test bs2 internal: false pin: - number: D1 + number: GPIO33 + - platform: touchscreen + id: bs_3 + name: test bs3 + x_min: 100 + x_max: 200 + y_min: 300 + y_max: 400 + use_raw: true + - platform: touchscreen + id: bs_4 + name: test bs4 + x_min: 100 + x_max: 200 + y_min: 300 + y_max: 400 diff --git a/tests/component_tests/packages/test_packages.py b/tests/component_tests/packages/test_packages.py index 01cf55872c..3fbbf49afd 100644 --- a/tests/component_tests/packages/test_packages.py +++ b/tests/component_tests/packages/test_packages.py @@ -1,11 +1,18 @@ """Tests for the packages component.""" +from pathlib import Path +from unittest.mock import MagicMock, patch + import pytest - +from esphome.components.packages import do_packages_pass +from esphome.config_helpers import Extend, Remove +import esphome.config_validation as cv from esphome.const import ( + CONF_DEFAULTS, CONF_DOMAIN, CONF_ESPHOME, + CONF_FILES, CONF_FILTERS, CONF_ID, CONF_MULTIPLY, @@ -13,15 +20,18 @@ from esphome.const import ( CONF_OFFSET, CONF_PACKAGES, CONF_PASSWORD, + CONF_PATH, CONF_PLATFORM, + CONF_REF, + CONF_REFRESH, CONF_SENSOR, CONF_SSID, CONF_UPDATE_INTERVAL, + CONF_URL, + CONF_VARS, CONF_WIFI, ) -from esphome.components.packages import do_packages_pass -from esphome.config_helpers import Extend, Remove -import esphome.config_validation as cv +from esphome.util import OrderedDict # Test strings TEST_DEVICE_NAME = "test_device_name" @@ -34,9 +44,11 @@ TEST_SENSOR_PLATFORM_1 = "test_sensor_platform_1" TEST_SENSOR_PLATFORM_2 = "test_sensor_platform_2" TEST_SENSOR_NAME_1 = "test_sensor_name_1" TEST_SENSOR_NAME_2 = "test_sensor_name_2" +TEST_SENSOR_NAME_3 = "test_sensor_name_3" TEST_SENSOR_ID_1 = "test_sensor_id_1" TEST_SENSOR_ID_2 = "test_sensor_id_2" TEST_SENSOR_UPDATE_INTERVAL = "test_sensor_update_interval" +TEST_YAML_FILENAME = "sensor1.yaml" @pytest.fixture(name="basic_wifi") @@ -188,17 +200,35 @@ def test_package_list_merge(): } }, CONF_SENSOR: [ - {CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, CONF_NAME: TEST_SENSOR_NAME_1}, - {CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, CONF_NAME: TEST_SENSOR_NAME_2}, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, + CONF_NAME: TEST_SENSOR_NAME_1, + }, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, + CONF_NAME: TEST_SENSOR_NAME_2, + }, ], } expected = { CONF_SENSOR: [ - {CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, CONF_NAME: TEST_SENSOR_NAME_1}, - {CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, CONF_NAME: TEST_SENSOR_NAME_2}, - {CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, CONF_NAME: TEST_SENSOR_NAME_1}, - {CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, CONF_NAME: TEST_SENSOR_NAME_2}, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: TEST_SENSOR_NAME_1, + }, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: TEST_SENSOR_NAME_2, + }, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, + CONF_NAME: TEST_SENSOR_NAME_1, + }, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, + CONF_NAME: TEST_SENSOR_NAME_2, + }, ] } @@ -252,7 +282,10 @@ def test_package_list_merge_by_id(): CONF_UPDATE_INTERVAL: TEST_SENSOR_UPDATE_INTERVAL, }, {CONF_ID: Extend(TEST_SENSOR_ID_2), CONF_NAME: TEST_SENSOR_NAME_1}, - {CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, CONF_NAME: TEST_SENSOR_NAME_2}, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, + CONF_NAME: TEST_SENSOR_NAME_2, + }, ], } @@ -270,7 +303,10 @@ def test_package_list_merge_by_id(): CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, CONF_NAME: TEST_SENSOR_NAME_1, }, - {CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, CONF_NAME: TEST_SENSOR_NAME_2}, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_2, + CONF_NAME: TEST_SENSOR_NAME_2, + }, ] } @@ -289,12 +325,18 @@ def test_package_merge_by_id_with_list(): CONF_PACKAGES: { "sensors": { CONF_SENSOR: [ - {CONF_ID: TEST_SENSOR_ID_1, CONF_FILTERS: [{CONF_MULTIPLY: 42.0}]} + { + CONF_ID: TEST_SENSOR_ID_1, + CONF_FILTERS: [{CONF_MULTIPLY: 42.0}], + } ] } }, CONF_SENSOR: [ - {CONF_ID: Extend(TEST_SENSOR_ID_1), CONF_FILTERS: [{CONF_OFFSET: 146.0}]} + { + CONF_ID: Extend(TEST_SENSOR_ID_1), + CONF_FILTERS: [{CONF_OFFSET: 146.0}], + } ], } @@ -320,13 +362,19 @@ def test_package_merge_by_missing_id(): CONF_PACKAGES: { "sensors": { CONF_SENSOR: [ - {CONF_ID: TEST_SENSOR_ID_1, CONF_FILTERS: [{CONF_MULTIPLY: 42.0}]}, + { + CONF_ID: TEST_SENSOR_ID_1, + CONF_FILTERS: [{CONF_MULTIPLY: 42.0}], + }, ] } }, CONF_SENSOR: [ {CONF_ID: TEST_SENSOR_ID_1, CONF_FILTERS: [{CONF_MULTIPLY: 10.0}]}, - {CONF_ID: Extend(TEST_SENSOR_ID_2), CONF_FILTERS: [{CONF_OFFSET: 146.0}]}, + { + CONF_ID: Extend(TEST_SENSOR_ID_2), + CONF_FILTERS: [{CONF_OFFSET: 146.0}], + }, ], } @@ -451,8 +499,6 @@ def test_multiple_package_list_remove_by_id(): def test_package_dict_remove_by_id(basic_wifi, basic_esphome): """ Ensures that components with missing IDs are removed from dict. - """ - """ Ensures that the top-level configuration takes precedence over duplicate keys defined in a package. In this test, CONF_SSID should be overwritten by that defined in the top-level config. @@ -480,14 +526,20 @@ def test_package_remove_by_missing_id(): CONF_PACKAGES: { "sensors": { CONF_SENSOR: [ - {CONF_ID: TEST_SENSOR_ID_1, CONF_FILTERS: [{CONF_MULTIPLY: 42.0}]}, + { + CONF_ID: TEST_SENSOR_ID_1, + CONF_FILTERS: [{CONF_MULTIPLY: 42.0}], + }, ] } }, "missing_key": Remove(), CONF_SENSOR: [ {CONF_ID: TEST_SENSOR_ID_1, CONF_FILTERS: [{CONF_MULTIPLY: 10.0}]}, - {CONF_ID: Remove(TEST_SENSOR_ID_2), CONF_FILTERS: [{CONF_OFFSET: 146.0}]}, + { + CONF_ID: Remove(TEST_SENSOR_ID_2), + CONF_FILTERS: [{CONF_OFFSET: 146.0}], + }, ], } @@ -511,3 +563,171 @@ def test_package_remove_by_missing_id(): actual = do_packages_pass(config) assert actual == expected + + +@patch("esphome.yaml_util.load_yaml") +@patch("pathlib.Path.is_file") +@patch("esphome.git.clone_or_update") +def test_remote_packages_with_files_list( + mock_clone_or_update, mock_is_file, mock_load_yaml +): + """ + Ensures that packages are loaded as mixed list of dictionary and strings + """ + # Mock the response from git.clone_or_update + mock_revert = MagicMock() + mock_clone_or_update.return_value = (Path("/tmp/noexists"), mock_revert) + + # Mock the response from pathlib.Path.is_file + mock_is_file.return_value = True + + # Mock the response from esphome.yaml_util.load_yaml + mock_load_yaml.side_effect = [ + OrderedDict( + { + CONF_SENSOR: [ + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: TEST_SENSOR_NAME_1, + } + ] + } + ), + OrderedDict( + { + CONF_SENSOR: [ + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: TEST_SENSOR_NAME_2, + } + ] + } + ), + ] + + # Define the input config + config = { + CONF_PACKAGES: { + "package1": { + CONF_URL: "https://github.com/esphome/non-existant-repo", + CONF_REF: "main", + CONF_FILES: [ + {CONF_PATH: TEST_YAML_FILENAME}, + "sensor2.yaml", + ], + CONF_REFRESH: "1d", + } + } + } + + expected = { + CONF_SENSOR: [ + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: TEST_SENSOR_NAME_1, + }, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: TEST_SENSOR_NAME_2, + }, + ] + } + + actual = do_packages_pass(config) + assert actual == expected + + +@patch("esphome.yaml_util.load_yaml") +@patch("pathlib.Path.is_file") +@patch("esphome.git.clone_or_update") +def test_remote_packages_with_files_and_vars( + mock_clone_or_update, mock_is_file, mock_load_yaml +): + """ + Ensures that packages are loaded as mixed list of dictionary and strings with vars + """ + # Mock the response from git.clone_or_update + mock_revert = MagicMock() + mock_clone_or_update.return_value = (Path("/tmp/noexists"), mock_revert) + + # Mock the response from pathlib.Path.is_file + mock_is_file.return_value = True + + # Mock the response from esphome.yaml_util.load_yaml + mock_load_yaml.side_effect = [ + OrderedDict( + { + CONF_DEFAULTS: {CONF_NAME: TEST_SENSOR_NAME_1}, + CONF_SENSOR: [ + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: "${name}", + } + ], + } + ), + OrderedDict( + { + CONF_DEFAULTS: {CONF_NAME: TEST_SENSOR_NAME_1}, + CONF_SENSOR: [ + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: "${name}", + } + ], + } + ), + OrderedDict( + { + CONF_DEFAULTS: {CONF_NAME: TEST_SENSOR_NAME_1}, + CONF_SENSOR: [ + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: "${name}", + } + ], + } + ), + ] + + # Define the input config + config = { + CONF_PACKAGES: { + "package1": { + CONF_URL: "https://github.com/esphome/non-existant-repo", + CONF_REF: "main", + CONF_FILES: [ + { + CONF_PATH: TEST_YAML_FILENAME, + CONF_VARS: {CONF_NAME: TEST_SENSOR_NAME_2}, + }, + { + CONF_PATH: TEST_YAML_FILENAME, + CONF_VARS: {CONF_NAME: TEST_SENSOR_NAME_3}, + }, + {CONF_PATH: TEST_YAML_FILENAME}, + ], + CONF_REFRESH: "1d", + } + } + } + + expected = { + CONF_SENSOR: [ + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: TEST_SENSOR_NAME_2, + }, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: TEST_SENSOR_NAME_3, + }, + { + CONF_PLATFORM: TEST_SENSOR_PLATFORM_1, + CONF_NAME: TEST_SENSOR_NAME_1, + }, + ] + } + + actual = do_packages_pass(config) + assert actual == expected diff --git a/tests/components/chsc6x/test.esp32-ard.yaml b/tests/components/chsc6x/test.esp32-ard.yaml new file mode 100644 index 0000000000..9bc58b66f6 --- /dev/null +++ b/tests/components/chsc6x/test.esp32-ard.yaml @@ -0,0 +1,25 @@ +i2c: + - id: i2c_chsc6x + scl: 3 + sda: 21 + +spi: + clk_pin: 16 + mosi_pin: 17 + +display: + - platform: ili9xxx + id: ili9xxx_display + model: GC9A01A + invert_colors: True + cs_pin: 18 + dc_pin: 19 + pages: + - id: page1 + lambda: |- + it.rectangle(0, 0, it.get_width(), it.get_height()); + +touchscreen: + - platform: chsc6x + display: ili9xxx_display + interrupt_pin: 20 diff --git a/tests/components/chsc6x/test.esp32-c3-ard.yaml b/tests/components/chsc6x/test.esp32-c3-ard.yaml new file mode 100644 index 0000000000..b0f55eb2e6 --- /dev/null +++ b/tests/components/chsc6x/test.esp32-c3-ard.yaml @@ -0,0 +1,25 @@ +i2c: + - id: i2c_chsc6x + scl: 3 + sda: 9 + +spi: + clk_pin: 5 + mosi_pin: 4 + +display: + - platform: ili9xxx + id: ili9xxx_display + model: GC9A01A + invert_colors: True + cs_pin: 18 + dc_pin: 19 + pages: + - id: page1 + lambda: |- + it.rectangle(0, 0, it.get_width(), it.get_height()); + +touchscreen: + - platform: chsc6x + display: ili9xxx_display + interrupt_pin: 20 diff --git a/tests/components/chsc6x/test.esp32-c3-idf.yaml b/tests/components/chsc6x/test.esp32-c3-idf.yaml new file mode 100644 index 0000000000..b0f55eb2e6 --- /dev/null +++ b/tests/components/chsc6x/test.esp32-c3-idf.yaml @@ -0,0 +1,25 @@ +i2c: + - id: i2c_chsc6x + scl: 3 + sda: 9 + +spi: + clk_pin: 5 + mosi_pin: 4 + +display: + - platform: ili9xxx + id: ili9xxx_display + model: GC9A01A + invert_colors: True + cs_pin: 18 + dc_pin: 19 + pages: + - id: page1 + lambda: |- + it.rectangle(0, 0, it.get_width(), it.get_height()); + +touchscreen: + - platform: chsc6x + display: ili9xxx_display + interrupt_pin: 20 diff --git a/tests/components/chsc6x/test.esp32-idf.yaml b/tests/components/chsc6x/test.esp32-idf.yaml new file mode 100644 index 0000000000..9bc58b66f6 --- /dev/null +++ b/tests/components/chsc6x/test.esp32-idf.yaml @@ -0,0 +1,25 @@ +i2c: + - id: i2c_chsc6x + scl: 3 + sda: 21 + +spi: + clk_pin: 16 + mosi_pin: 17 + +display: + - platform: ili9xxx + id: ili9xxx_display + model: GC9A01A + invert_colors: True + cs_pin: 18 + dc_pin: 19 + pages: + - id: page1 + lambda: |- + it.rectangle(0, 0, it.get_width(), it.get_height()); + +touchscreen: + - platform: chsc6x + display: ili9xxx_display + interrupt_pin: 20 diff --git a/tests/components/chsc6x/test.rp2040-ard.yaml b/tests/components/chsc6x/test.rp2040-ard.yaml new file mode 100644 index 0000000000..dbd0d59fc4 --- /dev/null +++ b/tests/components/chsc6x/test.rp2040-ard.yaml @@ -0,0 +1,25 @@ +i2c: + - id: i2c_chsc6x + scl: 1 + sda: 0 + +spi: + clk_pin: 2 + mosi_pin: 3 + +display: + - platform: ili9xxx + id: ili9xxx_display + model: GC9A01A + invert_colors: True + cs_pin: 18 + dc_pin: 19 + pages: + - id: page1 + lambda: |- + it.rectangle(0, 0, it.get_width(), it.get_height()); + +touchscreen: + - platform: chsc6x + display: ili9xxx_display + interrupt_pin: 20 diff --git a/tests/components/cst816/common.yaml b/tests/components/cst816/common.yaml index a4ac4aafec..9750de15db 100644 --- a/tests/components/cst816/common.yaml +++ b/tests/components/cst816/common.yaml @@ -35,5 +35,11 @@ touchscreen: swap_xy: false binary_sensor: - - platform: cst816 + - platform: touchscreen name: Home Button + use_raw: true + x_min: 0 + x_max: 480 + y_min: 320 + y_max: 360 + diff --git a/tests/components/ft63x6/common.yaml b/tests/components/ft63x6/common.yaml index e91266123e..1fae6da5f4 100644 --- a/tests/components/ft63x6/common.yaml +++ b/tests/components/ft63x6/common.yaml @@ -34,3 +34,19 @@ touchscreen: on_release: - logger.log: format: to released + calibration: + x_min: 0 + x_max: 320 + y_min: 0 + y_max: 400 + +binary_sensor: + - platform: touchscreen + name: Bottom Left Touch + use_raw: true + x_min: 0 + x_max: 100 + y_min: 400 + y_max: 480 + on_press: + logger.log: Left pressed diff --git a/tests/components/ld2450/common.yaml b/tests/components/ld2450/common.yaml new file mode 100644 index 0000000000..2e62efb0f5 --- /dev/null +++ b/tests/components/ld2450/common.yaml @@ -0,0 +1,168 @@ +uart: + - id: ld2450_uart + tx_pin: ${tx_pin} + rx_pin: ${rx_pin} + baud_rate: 256000 + parity: NONE + stop_bits: 1 + +ld2450: + - id: ld2450_radar + uart_id: ld2450_uart + throttle: 1000ms + +button: + - platform: ld2450 + ld2450_id: ld2450_radar + factory_reset: + name: LD2450 Factory Reset + entity_category: config + restart: + name: LD2450 Restart + entity_category: config + +sensor: + - platform: ld2450 + ld2450_id: ld2450_radar + target_count: + name: Presence Target Count + still_target_count: + name: Still Target Count + moving_target_count: + name: Moving Target Count + target_1: + x: + name: Target-1 X + y: + name: Target-1 Y + speed: + name: Target-1 Speed + angle: + name: Target-1 Angle + distance: + name: Target-1 Distance + resolution: + name: Target-1 Resolution + target_2: + x: + name: Target-2 X + y: + name: Target-2 Y + speed: + name: Target-2 Speed + angle: + name: Target-2 Angle + distance: + name: Target-2 Distance + resolution: + name: Target-2 Resolution + target_3: + x: + name: Target-3 X + y: + name: Target-3 Y + speed: + name: Target-3 Speed + angle: + name: Target-3 Angle + distance: + name: Target-3 Distance + resolution: + name: Target-3 Resolution + zone_1: + target_count: + name: Zone-1 All Target Count + still_target_count: + name: Zone-1 Still Target Count + moving_target_count: + name: Zone-1 Moving Target Count + zone_2: + target_count: + name: Zone-2 All Target Count + still_target_count: + name: Zone-2 Still Target Count + moving_target_count: + name: Zone-2 Moving Target Count + zone_3: + target_count: + name: Zone-3 All Target Count + still_target_count: + name: Zone-3 Still Target Count + moving_target_count: + name: Zone-3 Moving Target Count + +binary_sensor: + - platform: ld2450 + ld2450_id: ld2450_radar + has_target: + name: Presence + has_moving_target: + name: Moving Target + has_still_target: + name: Still Target + +switch: + - platform: ld2450 + ld2450_id: ld2450_radar + bluetooth: + name: Bluetooth + multi_target: + name: Multi Target Tracking + +text_sensor: + - platform: ld2450 + ld2450_id: ld2450_radar + version: + name: LD2450 Firmware + mac_address: + name: LD2450 BT MAC + target_1: + direction: + name: Target-1 Direction + target_2: + direction: + name: Target-2 Direction + target_3: + direction: + name: Target-3 Direction + +number: + - platform: ld2450 + ld2450_id: ld2450_radar + presence_timeout: + name: Timeout + zone_1: + x1: + name: Zone-1 X1 + y1: + name: Zone-1 Y1 + x2: + name: Zone-1 X2 + y2: + name: Zone-1 Y2 + zone_2: + x1: + name: Zone-2 X1 + y1: + name: Zone-2 Y1 + x2: + name: Zone-2 X2 + y2: + name: Zone-2 Y2 + zone_3: + x1: + name: Zone-3 X1 + y1: + name: Zone-3 Y1 + x2: + name: Zone-3 X2 + y2: + name: Zone-3 Y2 + +select: + - platform: ld2450 + ld2450_id: ld2450_radar + baud_rate: + name: Baud Rate + zone_type: + name: Zone Type diff --git a/tests/components/ld2450/test.esp32-ard.yaml b/tests/components/ld2450/test.esp32-ard.yaml new file mode 100644 index 0000000000..f486544afa --- /dev/null +++ b/tests/components/ld2450/test.esp32-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO17 + rx_pin: GPIO16 + +<<: !include common.yaml diff --git a/tests/components/ld2450/test.esp32-c3-ard.yaml b/tests/components/ld2450/test.esp32-c3-ard.yaml new file mode 100644 index 0000000000..b516342f3b --- /dev/null +++ b/tests/components/ld2450/test.esp32-c3-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO4 + rx_pin: GPIO5 + +<<: !include common.yaml diff --git a/tests/components/ld2450/test.esp32-c3-idf.yaml b/tests/components/ld2450/test.esp32-c3-idf.yaml new file mode 100644 index 0000000000..b516342f3b --- /dev/null +++ b/tests/components/ld2450/test.esp32-c3-idf.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO4 + rx_pin: GPIO5 + +<<: !include common.yaml diff --git a/tests/components/ld2450/test.esp32-idf.yaml b/tests/components/ld2450/test.esp32-idf.yaml new file mode 100644 index 0000000000..f486544afa --- /dev/null +++ b/tests/components/ld2450/test.esp32-idf.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO17 + rx_pin: GPIO16 + +<<: !include common.yaml diff --git a/tests/components/ld2450/test.esp8266-ard.yaml b/tests/components/ld2450/test.esp8266-ard.yaml new file mode 100644 index 0000000000..b516342f3b --- /dev/null +++ b/tests/components/ld2450/test.esp8266-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO4 + rx_pin: GPIO5 + +<<: !include common.yaml diff --git a/tests/components/ld2450/test.rp2040-ard.yaml b/tests/components/ld2450/test.rp2040-ard.yaml new file mode 100644 index 0000000000..b516342f3b --- /dev/null +++ b/tests/components/ld2450/test.rp2040-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO4 + rx_pin: GPIO5 + +<<: !include common.yaml diff --git a/tests/components/mlx90393/common.yaml b/tests/components/mlx90393/common.yaml index a7ab0867cc..0b074f9be3 100644 --- a/tests/components/mlx90393/common.yaml +++ b/tests/components/mlx90393/common.yaml @@ -7,14 +7,17 @@ sensor: - platform: mlx90393 oversampling: 1 filter: 0 - gain: 3X + gain: 1X + temperature_compensation: true x_axis: name: mlxxaxis + resolution: DIV_2 y_axis: name: mlxyaxis + resolution: DIV_1 z_axis: name: mlxzaxis - resolution: 17BIT + resolution: DIV_2 temperature: name: mlxtemp oversampling: 2 diff --git a/tests/components/mlx90393/test.esp32-s3-ard.yaml b/tests/components/mlx90393/test.esp32-s3-ard.yaml new file mode 100644 index 0000000000..ee2c29ca4e --- /dev/null +++ b/tests/components/mlx90393/test.esp32-s3-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + scl_pin: GPIO5 + sda_pin: GPIO4 + +<<: !include common.yaml diff --git a/tests/components/mlx90393/test.esp32-s3-idf.yaml b/tests/components/mlx90393/test.esp32-s3-idf.yaml new file mode 100644 index 0000000000..ee2c29ca4e --- /dev/null +++ b/tests/components/mlx90393/test.esp32-s3-idf.yaml @@ -0,0 +1,5 @@ +substitutions: + scl_pin: GPIO5 + sda_pin: GPIO4 + +<<: !include common.yaml diff --git a/tests/components/modbus_controller/common.yaml b/tests/components/modbus_controller/common.yaml index 93d8391ff5..7fa9f8dae3 100644 --- a/tests/components/modbus_controller/common.yaml +++ b/tests/components/modbus_controller/common.yaml @@ -33,3 +33,73 @@ modbus_controller: read_lambda: |- return 42.3; max_cmd_retries: 0 + +binary_sensor: + - platform: modbus_controller + modbus_controller_id: modbus_controller1 + id: modbus_binary_sensor1 + name: Test Binary Sensor + register_type: read + address: 0x3200 + bitmask: 0x80 + +number: + - platform: modbus_controller + modbus_controller_id: modbus_controller1 + id: modbus_number1 + name: Test Number + address: 0x9001 + value_type: U_WORD + multiply: 1.0 + +output: + - platform: modbus_controller + modbus_controller_id: modbus_controller1 + id: modbus_output1 + address: 2048 + register_type: holding + value_type: U_WORD + multiply: 1000 + +select: + - platform: modbus_controller + modbus_controller_id: modbus_controller1 + id: modbus_select1 + name: Test Select + address: 1000 + value_type: U_WORD + optionsmap: + "Zero": 0 + "One": 1 + "Two": 2 + "Three": 3 + +sensor: + - platform: modbus_controller + modbus_controller_id: modbus_controller1 + id: modbus_sensor1 + name: Test Sensor + register_type: holding + address: 0x9001 + unit_of_measurement: "AH" + value_type: U_WORD + +switch: + - platform: modbus_controller + modbus_controller_id: modbus_controller1 + id: modbus_switch1 + name: Test Switch + register_type: coil + address: 0x15 + bitmask: 1 + +text_sensor: + - platform: modbus_controller + modbus_controller_id: modbus_controller1 + id: modbus_text_sensor1 + name: Test Text Sensor + register_type: holding + address: 0x9013 + register_count: 3 + raw_encode: HEXBYTES + response_size: 6 diff --git a/tests/components/msa3xx/common.yaml b/tests/components/msa3xx/common.yaml new file mode 100644 index 0000000000..8de6a8a89a --- /dev/null +++ b/tests/components/msa3xx/common.yaml @@ -0,0 +1,48 @@ +msa3xx: + i2c_id: i2c_msa3xx + type: msa301 + range: 4G + resolution: 14 + update_interval: 10s + calibration: + offset_x: -0.250 + offset_y: -0.400 + offset_z: -0.800 + transform: + mirror_x: false + mirror_y: true + mirror_z: true + swap_xy: false + on_tap: + - then: + - logger.log: "Tapped" + on_double_tap: + - then: + - logger.log: "Double tapped" + on_active: + - then: + - logger.log: "Activity detected" + on_orientation: + - then: + - logger.log: "Orientation changed" + +sensor: + - platform: msa3xx + acceleration_x: Accel X + acceleration_y: Accel Y + acceleration_z: Accel Z + +text_sensor: + - platform: msa3xx + orientation_xy: Orientation XY + orientation_z: Orientation Z + +binary_sensor: + - platform: msa3xx + tap: Single tap + double_tap: + name: Double tap + active: + name: Active + filters: + - delayed_off: 5000ms diff --git a/tests/components/msa3xx/test.esp32-ard.yaml b/tests/components/msa3xx/test.esp32-ard.yaml new file mode 100644 index 0000000000..7202e7b9bf --- /dev/null +++ b/tests/components/msa3xx/test.esp32-ard.yaml @@ -0,0 +1,6 @@ +i2c: + - id: i2c_msa3xx + scl: GPIO16 + sda: GPIO17 + +<<: !include common.yaml diff --git a/tests/components/msa3xx/test.esp32-c3-ard.yaml b/tests/components/msa3xx/test.esp32-c3-ard.yaml new file mode 100644 index 0000000000..b972ce8cdb --- /dev/null +++ b/tests/components/msa3xx/test.esp32-c3-ard.yaml @@ -0,0 +1,6 @@ +i2c: + - id: i2c_msa3xx + scl: GPIO5 + sda: GPIO4 + +<<: !include common.yaml diff --git a/tests/components/msa3xx/test.esp32-c3-idf.yaml b/tests/components/msa3xx/test.esp32-c3-idf.yaml new file mode 100644 index 0000000000..b972ce8cdb --- /dev/null +++ b/tests/components/msa3xx/test.esp32-c3-idf.yaml @@ -0,0 +1,6 @@ +i2c: + - id: i2c_msa3xx + scl: GPIO5 + sda: GPIO4 + +<<: !include common.yaml diff --git a/tests/components/msa3xx/test.esp32-idf.yaml b/tests/components/msa3xx/test.esp32-idf.yaml new file mode 100644 index 0000000000..7202e7b9bf --- /dev/null +++ b/tests/components/msa3xx/test.esp32-idf.yaml @@ -0,0 +1,6 @@ +i2c: + - id: i2c_msa3xx + scl: GPIO16 + sda: GPIO17 + +<<: !include common.yaml diff --git a/tests/components/msa3xx/test.esp8266-ard.yaml b/tests/components/msa3xx/test.esp8266-ard.yaml new file mode 100644 index 0000000000..b972ce8cdb --- /dev/null +++ b/tests/components/msa3xx/test.esp8266-ard.yaml @@ -0,0 +1,6 @@ +i2c: + - id: i2c_msa3xx + scl: GPIO5 + sda: GPIO4 + +<<: !include common.yaml diff --git a/tests/components/msa3xx/test.rp2040-ard.yaml b/tests/components/msa3xx/test.rp2040-ard.yaml new file mode 100644 index 0000000000..b972ce8cdb --- /dev/null +++ b/tests/components/msa3xx/test.rp2040-ard.yaml @@ -0,0 +1,6 @@ +i2c: + - id: i2c_msa3xx + scl: GPIO5 + sda: GPIO4 + +<<: !include common.yaml diff --git a/tests/components/tormatic/common.yaml b/tests/components/tormatic/common.yaml new file mode 100644 index 0000000000..0f1b33ac12 --- /dev/null +++ b/tests/components/tormatic/common.yaml @@ -0,0 +1,13 @@ +uart: + - id: uart_tormatic + tx_pin: ${tx_pin} + rx_pin: ${rx_pin} + baud_rate: 9600 + +cover: + - platform: tormatic + uart_id: uart_tormatic + id: tormatic_garage_door + name: Tormatic Garage Door + open_duration: 15s + close_duration: 22s diff --git a/tests/components/tormatic/test.esp32-ard.yaml b/tests/components/tormatic/test.esp32-ard.yaml new file mode 100644 index 0000000000..f486544afa --- /dev/null +++ b/tests/components/tormatic/test.esp32-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO17 + rx_pin: GPIO16 + +<<: !include common.yaml diff --git a/tests/components/tormatic/test.esp32-c3-ard.yaml b/tests/components/tormatic/test.esp32-c3-ard.yaml new file mode 100644 index 0000000000..b516342f3b --- /dev/null +++ b/tests/components/tormatic/test.esp32-c3-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO4 + rx_pin: GPIO5 + +<<: !include common.yaml diff --git a/tests/components/tormatic/test.esp32-c3-idf.yaml b/tests/components/tormatic/test.esp32-c3-idf.yaml new file mode 100644 index 0000000000..b516342f3b --- /dev/null +++ b/tests/components/tormatic/test.esp32-c3-idf.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO4 + rx_pin: GPIO5 + +<<: !include common.yaml diff --git a/tests/components/tormatic/test.esp32-idf.yaml b/tests/components/tormatic/test.esp32-idf.yaml new file mode 100644 index 0000000000..f486544afa --- /dev/null +++ b/tests/components/tormatic/test.esp32-idf.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO17 + rx_pin: GPIO16 + +<<: !include common.yaml diff --git a/tests/components/tormatic/test.esp8266-ard.yaml b/tests/components/tormatic/test.esp8266-ard.yaml new file mode 100644 index 0000000000..b516342f3b --- /dev/null +++ b/tests/components/tormatic/test.esp8266-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO4 + rx_pin: GPIO5 + +<<: !include common.yaml diff --git a/tests/components/tormatic/test.rp2040-ard.yaml b/tests/components/tormatic/test.rp2040-ard.yaml new file mode 100644 index 0000000000..b516342f3b --- /dev/null +++ b/tests/components/tormatic/test.rp2040-ard.yaml @@ -0,0 +1,5 @@ +substitutions: + tx_pin: GPIO4 + rx_pin: GPIO5 + +<<: !include common.yaml diff --git a/tests/unit_tests/test_config_validation.py b/tests/unit_tests/test_config_validation.py index 93ae67754a..3b2c72af2c 100644 --- a/tests/unit_tests/test_config_validation.py +++ b/tests/unit_tests/test_config_validation.py @@ -5,7 +5,24 @@ from hypothesis.strategies import builds, integers, ip_addresses, one_of, text import pytest from esphome import config_validation +from esphome.components.esp32.const import ( + VARIANT_ESP32, + VARIANT_ESP32C2, + VARIANT_ESP32C3, + VARIANT_ESP32C6, + VARIANT_ESP32H2, + VARIANT_ESP32S2, + VARIANT_ESP32S3, +) from esphome.config_validation import Invalid +from esphome.const import ( + PLATFORM_BK72XX, + PLATFORM_ESP32, + PLATFORM_ESP8266, + PLATFORM_HOST, + PLATFORM_RP2040, + PLATFORM_RTL87XX, +) from esphome.core import CORE, HexInt, Lambda @@ -174,3 +191,96 @@ def hex_int__valid(value): assert isinstance(actual, HexInt) assert actual == value + + +@pytest.mark.parametrize( + "framework, platform, variant, full, idf, arduino, simple", + [ + ("arduino", PLATFORM_ESP8266, None, "1", "1", "1", "1"), + ("arduino", PLATFORM_ESP32, VARIANT_ESP32, "3", "2", "3", "2"), + ("esp-idf", PLATFORM_ESP32, VARIANT_ESP32, "4", "4", "2", "2"), + ("arduino", PLATFORM_ESP32, VARIANT_ESP32C2, "3", "2", "3", "2"), + ("esp-idf", PLATFORM_ESP32, VARIANT_ESP32C2, "4", "4", "2", "2"), + ("arduino", PLATFORM_ESP32, VARIANT_ESP32S2, "6", "5", "6", "5"), + ("esp-idf", PLATFORM_ESP32, VARIANT_ESP32S2, "7", "7", "5", "5"), + ("arduino", PLATFORM_ESP32, VARIANT_ESP32S3, "9", "8", "9", "8"), + ("esp-idf", PLATFORM_ESP32, VARIANT_ESP32S3, "10", "10", "8", "8"), + ("arduino", PLATFORM_ESP32, VARIANT_ESP32C3, "12", "11", "12", "11"), + ("esp-idf", PLATFORM_ESP32, VARIANT_ESP32C3, "13", "13", "11", "11"), + ("arduino", PLATFORM_ESP32, VARIANT_ESP32C6, "15", "14", "15", "14"), + ("esp-idf", PLATFORM_ESP32, VARIANT_ESP32C6, "16", "16", "14", "14"), + ("arduino", PLATFORM_ESP32, VARIANT_ESP32H2, "18", "17", "18", "17"), + ("esp-idf", PLATFORM_ESP32, VARIANT_ESP32H2, "19", "19", "17", "17"), + ("arduino", PLATFORM_RP2040, None, "20", "20", "20", "20"), + ("arduino", PLATFORM_BK72XX, None, "21", "21", "21", "21"), + ("arduino", PLATFORM_RTL87XX, None, "22", "22", "22", "22"), + ("host", PLATFORM_HOST, None, "23", "23", "23", "23"), + ], +) +def test_split_default(framework, platform, variant, full, idf, arduino, simple): + from esphome.components.esp32.const import KEY_ESP32 + from esphome.const import ( + KEY_CORE, + KEY_TARGET_FRAMEWORK, + KEY_TARGET_PLATFORM, + KEY_VARIANT, + ) + + CORE.data[KEY_CORE] = {} + CORE.data[KEY_CORE][KEY_TARGET_PLATFORM] = platform + CORE.data[KEY_CORE][KEY_TARGET_FRAMEWORK] = framework + if platform == PLATFORM_ESP32: + CORE.data[KEY_ESP32] = {} + CORE.data[KEY_ESP32][KEY_VARIANT] = variant + + common_mappings = { + "esp8266": "1", + "esp32": "2", + "esp32_s2": "5", + "esp32_s3": "8", + "esp32_c3": "11", + "esp32_c6": "14", + "esp32_h2": "17", + "rp2040": "20", + "bk72xx": "21", + "rtl87xx": "22", + "host": "23", + } + + idf_mappings = { + "esp32_idf": "4", + "esp32_s2_idf": "7", + "esp32_s3_idf": "10", + "esp32_c3_idf": "13", + "esp32_c6_idf": "16", + "esp32_h2_idf": "19", + } + + arduino_mappings = { + "esp32_arduino": "3", + "esp32_s2_arduino": "6", + "esp32_s3_arduino": "9", + "esp32_c3_arduino": "12", + "esp32_c6_arduino": "15", + "esp32_h2_arduino": "18", + } + + schema = config_validation.Schema( + { + config_validation.SplitDefault( + "full", **common_mappings, **idf_mappings, **arduino_mappings + ): str, + config_validation.SplitDefault( + "idf", **common_mappings, **idf_mappings + ): str, + config_validation.SplitDefault( + "arduino", **common_mappings, **arduino_mappings + ): str, + config_validation.SplitDefault("simple", **common_mappings): str, + } + ) + + assert schema({}).get("full") == full + assert schema({}).get("idf") == idf + assert schema({}).get("arduino") == arduino + assert schema({}).get("simple") == simple diff --git a/tests/unit_tests/test_cpp_generator.py b/tests/unit_tests/test_cpp_generator.py index 6f4b5a40bc..95633ca0c6 100644 --- a/tests/unit_tests/test_cpp_generator.py +++ b/tests/unit_tests/test_cpp_generator.py @@ -1,11 +1,9 @@ from collections.abc import Iterator - import math import pytest -from esphome import cpp_generator as cg -from esphome import cpp_types as ct +from esphome import cpp_generator as cg, cpp_types as ct class TestExpressions: @@ -156,10 +154,7 @@ class TestLambdaExpression: actual = str(target) assert actual == ( - "[=](int32_t foo, float bar) {\n" - " if ((foo == 5) && (bar < 10))) {\n" - " }\n" - "}" + "[=](int32_t foo, float bar) {\n if ((foo == 5) && (bar < 10))) {\n }\n}" ) def test_str__with_return(self):