mirror of
				https://github.com/esphome/esphome.git
				synced 2025-11-04 09:01:49 +00:00 
			
		
		
		
	Compare commits
	
		
			2 Commits
		
	
	
		
			2025.8.0b4
			...
			copilot/fi
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					5fa6cd3dae | ||
| 
						 | 
					4ec9ab944d | 
							
								
								
									
										2
									
								
								.github/actions/restore-python/action.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/actions/restore-python/action.yml
									
									
									
									
										vendored
									
									
								
							@@ -22,7 +22,7 @@ runs:
 | 
			
		||||
        python-version: ${{ inputs.python-version }}
 | 
			
		||||
    - name: Restore Python virtual environment
 | 
			
		||||
      id: cache-venv
 | 
			
		||||
      uses: actions/cache/restore@v4.2.4
 | 
			
		||||
      uses: actions/cache/restore@v4.2.3
 | 
			
		||||
      with:
 | 
			
		||||
        path: venv
 | 
			
		||||
        # yamllint disable-line rule:line-length
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/auto-label-pr.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/auto-label-pr.yml
									
									
									
									
										vendored
									
									
								
							@@ -22,7 +22,7 @@ jobs:
 | 
			
		||||
    if: github.event.action != 'labeled' || github.event.sender.type != 'Bot'
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
 | 
			
		||||
      - name: Generate a token
 | 
			
		||||
        id: generate-token
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/ci-api-proto.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/ci-api-proto.yml
									
									
									
									
										vendored
									
									
								
							@@ -21,7 +21,7 @@ jobs:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        with:
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/ci-clang-tidy-hash.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/ci-clang-tidy-hash.yml
									
									
									
									
										vendored
									
									
								
							@@ -20,7 +20,7 @@ jobs:
 | 
			
		||||
    runs-on: ubuntu-latest
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/ci-docker.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/ci-docker.yml
									
									
									
									
										vendored
									
									
								
							@@ -43,7 +43,7 @@ jobs:
 | 
			
		||||
          - "docker"
 | 
			
		||||
          # - "lint"
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v5.0.0
 | 
			
		||||
      - uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        with:
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										32
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										32
									
								
								.github/workflows/ci.yml
									
									
									
									
										vendored
									
									
								
							@@ -36,7 +36,7 @@ jobs:
 | 
			
		||||
      cache-key: ${{ steps.cache-key.outputs.key }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Generate cache-key
 | 
			
		||||
        id: cache-key
 | 
			
		||||
        run: echo key="${{ hashFiles('requirements.txt', 'requirements_test.txt', '.pre-commit-config.yaml') }}" >> $GITHUB_OUTPUT
 | 
			
		||||
@@ -47,7 +47,7 @@ jobs:
 | 
			
		||||
          python-version: ${{ env.DEFAULT_PYTHON }}
 | 
			
		||||
      - name: Restore Python virtual environment
 | 
			
		||||
        id: cache-venv
 | 
			
		||||
        uses: actions/cache@v4.2.4
 | 
			
		||||
        uses: actions/cache@v4.2.3
 | 
			
		||||
        with:
 | 
			
		||||
          path: venv
 | 
			
		||||
          # yamllint disable-line rule:line-length
 | 
			
		||||
@@ -70,7 +70,7 @@ jobs:
 | 
			
		||||
    if: needs.determine-jobs.outputs.python-linters == 'true'
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
@@ -91,7 +91,7 @@ jobs:
 | 
			
		||||
      - common
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
@@ -136,7 +136,7 @@ jobs:
 | 
			
		||||
      - common
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        id: restore-python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
@@ -161,7 +161,7 @@ jobs:
 | 
			
		||||
          token: ${{ secrets.CODECOV_TOKEN }}
 | 
			
		||||
      - name: Save Python virtual environment cache
 | 
			
		||||
        if: github.ref == 'refs/heads/dev'
 | 
			
		||||
        uses: actions/cache/save@v4.2.4
 | 
			
		||||
        uses: actions/cache/save@v4.2.3
 | 
			
		||||
        with:
 | 
			
		||||
          path: venv
 | 
			
		||||
          key: ${{ runner.os }}-${{ steps.restore-python.outputs.python-version }}-venv-${{ needs.common.outputs.cache-key }}
 | 
			
		||||
@@ -179,7 +179,7 @@ jobs:
 | 
			
		||||
      component-test-count: ${{ steps.determine.outputs.component-test-count }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        with:
 | 
			
		||||
          # Fetch enough history to find the merge base
 | 
			
		||||
          fetch-depth: 2
 | 
			
		||||
@@ -214,7 +214,7 @@ jobs:
 | 
			
		||||
    if: needs.determine-jobs.outputs.integration-tests == 'true'
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Set up Python 3.13
 | 
			
		||||
        id: python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
@@ -222,7 +222,7 @@ jobs:
 | 
			
		||||
          python-version: "3.13"
 | 
			
		||||
      - name: Restore Python virtual environment
 | 
			
		||||
        id: cache-venv
 | 
			
		||||
        uses: actions/cache@v4.2.4
 | 
			
		||||
        uses: actions/cache@v4.2.3
 | 
			
		||||
        with:
 | 
			
		||||
          path: venv
 | 
			
		||||
          key: ${{ runner.os }}-${{ steps.python.outputs.python-version }}-venv-${{ needs.common.outputs.cache-key }}
 | 
			
		||||
@@ -287,7 +287,7 @@ jobs:
 | 
			
		||||
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        with:
 | 
			
		||||
          # Need history for HEAD~1 to work for checking changed files
 | 
			
		||||
          fetch-depth: 2
 | 
			
		||||
@@ -300,14 +300,14 @@ jobs:
 | 
			
		||||
 | 
			
		||||
      - name: Cache platformio
 | 
			
		||||
        if: github.ref == 'refs/heads/dev'
 | 
			
		||||
        uses: actions/cache@v4.2.4
 | 
			
		||||
        uses: actions/cache@v4.2.3
 | 
			
		||||
        with:
 | 
			
		||||
          path: ~/.platformio
 | 
			
		||||
          key: platformio-${{ matrix.pio_cache_key }}-${{ hashFiles('platformio.ini') }}
 | 
			
		||||
 | 
			
		||||
      - name: Cache platformio
 | 
			
		||||
        if: github.ref != 'refs/heads/dev'
 | 
			
		||||
        uses: actions/cache/restore@v4.2.4
 | 
			
		||||
        uses: actions/cache/restore@v4.2.3
 | 
			
		||||
        with:
 | 
			
		||||
          path: ~/.platformio
 | 
			
		||||
          key: platformio-${{ matrix.pio_cache_key }}-${{ hashFiles('platformio.ini') }}
 | 
			
		||||
@@ -374,7 +374,7 @@ jobs:
 | 
			
		||||
          sudo apt-get install libsdl2-dev
 | 
			
		||||
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
@@ -400,7 +400,7 @@ jobs:
 | 
			
		||||
      matrix: ${{ steps.split.outputs.components }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Split components into 20 groups
 | 
			
		||||
        id: split
 | 
			
		||||
        run: |
 | 
			
		||||
@@ -430,7 +430,7 @@ jobs:
 | 
			
		||||
          sudo apt-get install libsdl2-dev
 | 
			
		||||
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
@@ -459,7 +459,7 @@ jobs:
 | 
			
		||||
    if: github.event_name == 'pull_request' && github.base_ref != 'beta' && github.base_ref != 'release'
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Check out code from GitHub
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Restore Python
 | 
			
		||||
        uses: ./.github/actions/restore-python
 | 
			
		||||
        with:
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								.github/workflows/codeql.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.github/workflows/codeql.yml
									
									
									
									
										vendored
									
									
								
							@@ -54,7 +54,7 @@ jobs:
 | 
			
		||||
            # your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout repository
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4
 | 
			
		||||
 | 
			
		||||
      # Initializes the CodeQL tools for scanning.
 | 
			
		||||
      - name: Initialize CodeQL
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										10
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										10
									
								
								.github/workflows/release.yml
									
									
									
									
										vendored
									
									
								
							@@ -20,7 +20,7 @@ jobs:
 | 
			
		||||
      branch_build: ${{ steps.tag.outputs.branch_build }}
 | 
			
		||||
      deploy_env: ${{ steps.tag.outputs.deploy_env }}
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v5.0.0
 | 
			
		||||
      - uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Get tag
 | 
			
		||||
        id: tag
 | 
			
		||||
        # yamllint disable rule:line-length
 | 
			
		||||
@@ -60,7 +60,7 @@ jobs:
 | 
			
		||||
      contents: read
 | 
			
		||||
      id-token: write
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v5.0.0
 | 
			
		||||
      - uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        with:
 | 
			
		||||
@@ -92,7 +92,7 @@ jobs:
 | 
			
		||||
            os: "ubuntu-24.04-arm"
 | 
			
		||||
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v5.0.0
 | 
			
		||||
      - uses: actions/checkout@v4.2.2
 | 
			
		||||
      - name: Set up Python
 | 
			
		||||
        uses: actions/setup-python@v5.6.0
 | 
			
		||||
        with:
 | 
			
		||||
@@ -168,10 +168,10 @@ jobs:
 | 
			
		||||
          - ghcr
 | 
			
		||||
          - dockerhub
 | 
			
		||||
    steps:
 | 
			
		||||
      - uses: actions/checkout@v5.0.0
 | 
			
		||||
      - uses: actions/checkout@v4.2.2
 | 
			
		||||
 | 
			
		||||
      - name: Download digests
 | 
			
		||||
        uses: actions/download-artifact@v5.0.0
 | 
			
		||||
        uses: actions/download-artifact@v4.3.0
 | 
			
		||||
        with:
 | 
			
		||||
          pattern: digests-*
 | 
			
		||||
          path: /tmp/digests
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										4
									
								
								.github/workflows/sync-device-classes.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								.github/workflows/sync-device-classes.yml
									
									
									
									
										vendored
									
									
								
							@@ -13,10 +13,10 @@ jobs:
 | 
			
		||||
    if: github.repository == 'esphome/esphome'
 | 
			
		||||
    steps:
 | 
			
		||||
      - name: Checkout
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
 | 
			
		||||
      - name: Checkout Home Assistant
 | 
			
		||||
        uses: actions/checkout@v5.0.0
 | 
			
		||||
        uses: actions/checkout@v4.2.2
 | 
			
		||||
        with:
 | 
			
		||||
          repository: home-assistant/core
 | 
			
		||||
          path: lib/home-assistant
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,7 @@ ci:
 | 
			
		||||
repos:
 | 
			
		||||
  - repo: https://github.com/astral-sh/ruff-pre-commit
 | 
			
		||||
    # Ruff version.
 | 
			
		||||
    rev: v0.12.8
 | 
			
		||||
    rev: v0.12.7
 | 
			
		||||
    hooks:
 | 
			
		||||
      # Run the linter.
 | 
			
		||||
      - id: ruff
 | 
			
		||||
 
 | 
			
		||||
@@ -246,7 +246,6 @@ esphome/components/kuntze/* @ssieb
 | 
			
		||||
esphome/components/lc709203f/* @ilikecake
 | 
			
		||||
esphome/components/lcd_menu/* @numo68
 | 
			
		||||
esphome/components/ld2410/* @regevbr @sebcaps
 | 
			
		||||
esphome/components/ld2412/* @Rihan9
 | 
			
		||||
esphome/components/ld2420/* @descipher
 | 
			
		||||
esphome/components/ld2450/* @hareeshmu
 | 
			
		||||
esphome/components/ld24xx/* @kbx81
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2
									
								
								Doxyfile
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								Doxyfile
									
									
									
									
									
								
							@@ -48,7 +48,7 @@ PROJECT_NAME           = ESPHome
 | 
			
		||||
# could be handy for archiving the generated documentation or if some version
 | 
			
		||||
# control system is used.
 | 
			
		||||
 | 
			
		||||
PROJECT_NUMBER         = 2025.8.0b4
 | 
			
		||||
PROJECT_NUMBER         = 2025.8.0-dev
 | 
			
		||||
 | 
			
		||||
# Using the PROJECT_BRIEF tag one can provide an optional one line description
 | 
			
		||||
# for a project that appears at the top of each page and should give viewer a
 | 
			
		||||
 
 | 
			
		||||
@@ -90,7 +90,7 @@ def main():
 | 
			
		||||
    def run_command(*cmd, ignore_error: bool = False):
 | 
			
		||||
        print(f"$ {shlex.join(list(cmd))}")
 | 
			
		||||
        if not args.dry_run:
 | 
			
		||||
            rc = subprocess.call(list(cmd), close_fds=False)
 | 
			
		||||
            rc = subprocess.call(list(cmd))
 | 
			
		||||
            if rc != 0 and not ignore_error:
 | 
			
		||||
                print("Command failed")
 | 
			
		||||
                sys.exit(1)
 | 
			
		||||
 
 | 
			
		||||
@@ -476,7 +476,7 @@ def show_logs(config: ConfigType, args: ArgsProtocol, devices: list[str]) -> int
 | 
			
		||||
        from esphome.components.api.client import run_logs
 | 
			
		||||
 | 
			
		||||
        return run_logs(config, addresses_to_use)
 | 
			
		||||
    if get_port_type(port) in ("NETWORK", "MQTT") and "mqtt" in config:
 | 
			
		||||
    if get_port_type(port) == "MQTT" and "mqtt" in config:
 | 
			
		||||
        from esphome import mqtt
 | 
			
		||||
 | 
			
		||||
        return mqtt.show_logs(
 | 
			
		||||
 
 | 
			
		||||
@@ -5,7 +5,7 @@ from esphome.const import (
 | 
			
		||||
    CONF_EQUATION,
 | 
			
		||||
    CONF_HUMIDITY,
 | 
			
		||||
    CONF_TEMPERATURE,
 | 
			
		||||
    DEVICE_CLASS_ABSOLUTE_HUMIDITY,
 | 
			
		||||
    ICON_WATER,
 | 
			
		||||
    STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    UNIT_GRAMS_PER_CUBIC_METER,
 | 
			
		||||
)
 | 
			
		||||
@@ -27,8 +27,8 @@ EQUATION = {
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    sensor.sensor_schema(
 | 
			
		||||
        unit_of_measurement=UNIT_GRAMS_PER_CUBIC_METER,
 | 
			
		||||
        icon=ICON_WATER,
 | 
			
		||||
        accuracy_decimals=2,
 | 
			
		||||
        device_class=DEVICE_CLASS_ABSOLUTE_HUMIDITY,
 | 
			
		||||
        state_class=STATE_CLASS_MEASUREMENT,
 | 
			
		||||
    )
 | 
			
		||||
    .extend(
 | 
			
		||||
 
 | 
			
		||||
@@ -36,7 +36,6 @@ from esphome.const import (
 | 
			
		||||
    UNIT_WATT,
 | 
			
		||||
    UNIT_WATT_HOURS,
 | 
			
		||||
)
 | 
			
		||||
from esphome.types import ConfigType
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["i2c"]
 | 
			
		||||
 | 
			
		||||
@@ -52,20 +51,6 @@ CONF_POWER_GAIN = "power_gain"
 | 
			
		||||
 | 
			
		||||
CONF_NEUTRAL = "neutral"
 | 
			
		||||
 | 
			
		||||
# Tuple of power channel phases
 | 
			
		||||
POWER_PHASES = (CONF_PHASE_A, CONF_PHASE_B, CONF_PHASE_C)
 | 
			
		||||
 | 
			
		||||
# Tuple of sensor types that can be configured for power channels
 | 
			
		||||
POWER_SENSOR_TYPES = (
 | 
			
		||||
    CONF_CURRENT,
 | 
			
		||||
    CONF_VOLTAGE,
 | 
			
		||||
    CONF_ACTIVE_POWER,
 | 
			
		||||
    CONF_APPARENT_POWER,
 | 
			
		||||
    CONF_POWER_FACTOR,
 | 
			
		||||
    CONF_FORWARD_ACTIVE_ENERGY,
 | 
			
		||||
    CONF_REVERSE_ACTIVE_ENERGY,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
NEUTRAL_CHANNEL_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(NeutralChannel),
 | 
			
		||||
@@ -165,64 +150,7 @@ POWER_CHANNEL_SCHEMA = cv.Schema(
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def prefix_sensor_name(
 | 
			
		||||
    sensor_conf: ConfigType,
 | 
			
		||||
    channel_name: str,
 | 
			
		||||
    channel_config: ConfigType,
 | 
			
		||||
    sensor_type: str,
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Helper to prefix sensor name with channel name.
 | 
			
		||||
 | 
			
		||||
    Args:
 | 
			
		||||
        sensor_conf: The sensor configuration (dict or string)
 | 
			
		||||
        channel_name: The channel name to prefix with
 | 
			
		||||
        channel_config: The channel configuration to update
 | 
			
		||||
        sensor_type: The sensor type key in the channel config
 | 
			
		||||
    """
 | 
			
		||||
    if isinstance(sensor_conf, dict) and CONF_NAME in sensor_conf:
 | 
			
		||||
        sensor_name = sensor_conf[CONF_NAME]
 | 
			
		||||
        if sensor_name and not sensor_name.startswith(channel_name):
 | 
			
		||||
            sensor_conf[CONF_NAME] = f"{channel_name} {sensor_name}"
 | 
			
		||||
    elif isinstance(sensor_conf, str):
 | 
			
		||||
        # Simple value case - convert to dict with prefixed name
 | 
			
		||||
        channel_config[sensor_type] = {CONF_NAME: f"{channel_name} {sensor_conf}"}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def process_channel_sensors(
 | 
			
		||||
    config: ConfigType, channel_key: str, sensor_types: tuple
 | 
			
		||||
) -> None:
 | 
			
		||||
    """Process sensors for a channel and prefix their names.
 | 
			
		||||
 | 
			
		||||
    Args:
 | 
			
		||||
        config: The main configuration
 | 
			
		||||
        channel_key: The channel key (e.g., CONF_PHASE_A, CONF_NEUTRAL)
 | 
			
		||||
        sensor_types: Tuple of sensor types to process for this channel
 | 
			
		||||
    """
 | 
			
		||||
    if not (channel_config := config.get(channel_key)) or not (
 | 
			
		||||
        channel_name := channel_config.get(CONF_NAME)
 | 
			
		||||
    ):
 | 
			
		||||
        return
 | 
			
		||||
 | 
			
		||||
    for sensor_type in sensor_types:
 | 
			
		||||
        if sensor_conf := channel_config.get(sensor_type):
 | 
			
		||||
            prefix_sensor_name(sensor_conf, channel_name, channel_config, sensor_type)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def preprocess_channels(config: ConfigType) -> ConfigType:
 | 
			
		||||
    """Preprocess channel configurations to add channel name prefix to sensor names."""
 | 
			
		||||
    # Process power channels
 | 
			
		||||
    for channel in POWER_PHASES:
 | 
			
		||||
        process_channel_sensors(config, channel, POWER_SENSOR_TYPES)
 | 
			
		||||
 | 
			
		||||
    # Process neutral channel
 | 
			
		||||
    process_channel_sensors(config, CONF_NEUTRAL, (CONF_CURRENT,))
 | 
			
		||||
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    preprocess_channels,
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(ADE7880),
 | 
			
		||||
@@ -239,7 +167,7 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(cv.polling_component_schema("60s"))
 | 
			
		||||
    .extend(i2c.i2c_device_schema(0x38)),
 | 
			
		||||
    .extend(i2c.i2c_device_schema(0x38))
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -260,7 +188,15 @@ async def neutral_channel(config):
 | 
			
		||||
async def power_channel(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
 | 
			
		||||
    for sensor_type in POWER_SENSOR_TYPES:
 | 
			
		||||
    for sensor_type in [
 | 
			
		||||
        CONF_CURRENT,
 | 
			
		||||
        CONF_VOLTAGE,
 | 
			
		||||
        CONF_ACTIVE_POWER,
 | 
			
		||||
        CONF_APPARENT_POWER,
 | 
			
		||||
        CONF_POWER_FACTOR,
 | 
			
		||||
        CONF_FORWARD_ACTIVE_ENERGY,
 | 
			
		||||
        CONF_REVERSE_ACTIVE_ENERGY,
 | 
			
		||||
    ]:
 | 
			
		||||
        if conf := config.get(sensor_type):
 | 
			
		||||
            sens = await sensor.new_sensor(conf)
 | 
			
		||||
            cg.add(getattr(var, f"set_{sensor_type}")(sens))
 | 
			
		||||
@@ -280,6 +216,44 @@ async def power_channel(config):
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def final_validate(config):
 | 
			
		||||
    for channel in [CONF_PHASE_A, CONF_PHASE_B, CONF_PHASE_C]:
 | 
			
		||||
        if channel := config.get(channel):
 | 
			
		||||
            channel_name = channel.get(CONF_NAME)
 | 
			
		||||
 | 
			
		||||
            for sensor_type in [
 | 
			
		||||
                CONF_CURRENT,
 | 
			
		||||
                CONF_VOLTAGE,
 | 
			
		||||
                CONF_ACTIVE_POWER,
 | 
			
		||||
                CONF_APPARENT_POWER,
 | 
			
		||||
                CONF_POWER_FACTOR,
 | 
			
		||||
                CONF_FORWARD_ACTIVE_ENERGY,
 | 
			
		||||
                CONF_REVERSE_ACTIVE_ENERGY,
 | 
			
		||||
            ]:
 | 
			
		||||
                if conf := channel.get(sensor_type):
 | 
			
		||||
                    sensor_name = conf.get(CONF_NAME)
 | 
			
		||||
                    if (
 | 
			
		||||
                        sensor_name
 | 
			
		||||
                        and channel_name
 | 
			
		||||
                        and not sensor_name.startswith(channel_name)
 | 
			
		||||
                    ):
 | 
			
		||||
                        conf[CONF_NAME] = f"{channel_name} {sensor_name}"
 | 
			
		||||
 | 
			
		||||
    if channel := config.get(CONF_NEUTRAL):
 | 
			
		||||
        channel_name = channel.get(CONF_NAME)
 | 
			
		||||
        if conf := channel.get(CONF_CURRENT):
 | 
			
		||||
            sensor_name = conf.get(CONF_NAME)
 | 
			
		||||
            if (
 | 
			
		||||
                sensor_name
 | 
			
		||||
                and channel_name
 | 
			
		||||
                and not sensor_name.startswith(channel_name)
 | 
			
		||||
            ):
 | 
			
		||||
                conf[CONF_NAME] = f"{channel_name} {sensor_name}"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
FINAL_VALIDATE_SCHEMA = final_validate
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
 
 | 
			
		||||
@@ -1438,11 +1438,11 @@ message BluetoothLERawAdvertisementsResponse {
 | 
			
		||||
  option (ifdef) = "USE_BLUETOOTH_PROXY";
 | 
			
		||||
  option (no_delay) = true;
 | 
			
		||||
 | 
			
		||||
  repeated BluetoothLERawAdvertisement advertisements = 1 [(fixed_array_with_length_define) = "BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE"];
 | 
			
		||||
  repeated BluetoothLERawAdvertisement advertisements = 1;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
enum BluetoothDeviceRequestType {
 | 
			
		||||
  BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT = 0 [deprecated = true]; // V1 removed, use V3 variants
 | 
			
		||||
  BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT = 0;
 | 
			
		||||
  BLUETOOTH_DEVICE_REQUEST_TYPE_DISCONNECT = 1;
 | 
			
		||||
  BLUETOOTH_DEVICE_REQUEST_TYPE_PAIR = 2;
 | 
			
		||||
  BLUETOOTH_DEVICE_REQUEST_TYPE_UNPAIR = 3;
 | 
			
		||||
 
 | 
			
		||||
@@ -156,9 +156,7 @@ APIError APIFrameHelper::write_raw_(const struct iovec *iov, int iovcnt, uint16_
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Try to send directly if no buffered data
 | 
			
		||||
  // Optimize for single iovec case (common for plaintext API)
 | 
			
		||||
  ssize_t sent =
 | 
			
		||||
      (iovcnt == 1) ? this->socket_->write(iov[0].iov_base, iov[0].iov_len) : this->socket_->writev(iov, iovcnt);
 | 
			
		||||
  ssize_t sent = this->socket_->writev(iov, iovcnt);
 | 
			
		||||
 | 
			
		||||
  if (sent == -1) {
 | 
			
		||||
    APIError err = this->handle_socket_write_error_();
 | 
			
		||||
 
 | 
			
		||||
@@ -30,7 +30,6 @@ extend google.protobuf.FieldOptions {
 | 
			
		||||
    optional bool no_zero_copy = 50008 [default=false];
 | 
			
		||||
    optional bool fixed_array_skip_zero = 50009 [default=false];
 | 
			
		||||
    optional string fixed_array_size_define = 50010;
 | 
			
		||||
    optional string fixed_array_with_length_define = 50011;
 | 
			
		||||
 | 
			
		||||
    // container_pointer: Zero-copy optimization for repeated fields.
 | 
			
		||||
    //
 | 
			
		||||
 
 | 
			
		||||
@@ -1843,14 +1843,12 @@ void BluetoothLERawAdvertisement::calculate_size(ProtoSize &size) const {
 | 
			
		||||
  size.add_length(1, this->data_len);
 | 
			
		||||
}
 | 
			
		||||
void BluetoothLERawAdvertisementsResponse::encode(ProtoWriteBuffer buffer) const {
 | 
			
		||||
  for (uint16_t i = 0; i < this->advertisements_len; i++) {
 | 
			
		||||
    buffer.encode_message(1, this->advertisements[i], true);
 | 
			
		||||
  for (auto &it : this->advertisements) {
 | 
			
		||||
    buffer.encode_message(1, it, true);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
void BluetoothLERawAdvertisementsResponse::calculate_size(ProtoSize &size) const {
 | 
			
		||||
  for (uint16_t i = 0; i < this->advertisements_len; i++) {
 | 
			
		||||
    size.add_message_object_force(1, this->advertisements[i]);
 | 
			
		||||
  }
 | 
			
		||||
  size.add_repeated_message(1, this->advertisements);
 | 
			
		||||
}
 | 
			
		||||
bool BluetoothDeviceRequest::decode_varint(uint32_t field_id, ProtoVarInt value) {
 | 
			
		||||
  switch (field_id) {
 | 
			
		||||
 
 | 
			
		||||
@@ -1788,12 +1788,11 @@ class BluetoothLERawAdvertisement : public ProtoMessage {
 | 
			
		||||
class BluetoothLERawAdvertisementsResponse : public ProtoMessage {
 | 
			
		||||
 public:
 | 
			
		||||
  static constexpr uint8_t MESSAGE_TYPE = 93;
 | 
			
		||||
  static constexpr uint8_t ESTIMATED_SIZE = 136;
 | 
			
		||||
  static constexpr uint8_t ESTIMATED_SIZE = 34;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
  const char *message_name() const override { return "bluetooth_le_raw_advertisements_response"; }
 | 
			
		||||
#endif
 | 
			
		||||
  std::array<BluetoothLERawAdvertisement, BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE> advertisements{};
 | 
			
		||||
  uint16_t advertisements_len{0};
 | 
			
		||||
  std::vector<BluetoothLERawAdvertisement> advertisements{};
 | 
			
		||||
  void encode(ProtoWriteBuffer buffer) const override;
 | 
			
		||||
  void calculate_size(ProtoSize &size) const override;
 | 
			
		||||
#ifdef HAS_PROTO_MESSAGE_DUMP
 | 
			
		||||
 
 | 
			
		||||
@@ -1534,9 +1534,9 @@ void BluetoothLERawAdvertisement::dump_to(std::string &out) const {
 | 
			
		||||
}
 | 
			
		||||
void BluetoothLERawAdvertisementsResponse::dump_to(std::string &out) const {
 | 
			
		||||
  MessageDumpHelper helper(out, "BluetoothLERawAdvertisementsResponse");
 | 
			
		||||
  for (uint16_t i = 0; i < this->advertisements_len; i++) {
 | 
			
		||||
  for (const auto &it : this->advertisements) {
 | 
			
		||||
    out.append("  advertisements: ");
 | 
			
		||||
    this->advertisements[i].dump_to(out);
 | 
			
		||||
    it.dump_to(out);
 | 
			
		||||
    out.append("\n");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -371,8 +371,21 @@ void APIServer::set_batch_delay(uint16_t batch_delay) { this->batch_delay_ = bat
 | 
			
		||||
 | 
			
		||||
#ifdef USE_API_HOMEASSISTANT_SERVICES
 | 
			
		||||
void APIServer::send_homeassistant_service_call(const HomeassistantServiceResponse &call) {
 | 
			
		||||
  for (auto &client : this->clients_) {
 | 
			
		||||
    client->send_homeassistant_service_call(call);
 | 
			
		||||
  if (call.is_event) {
 | 
			
		||||
    // For events, send to only one client to prevent duplicates
 | 
			
		||||
    // Events represent "something that happened" and should only be sent once total
 | 
			
		||||
    for (auto &client : this->clients_) {
 | 
			
		||||
      if (client->is_authenticated() && client->flags_.service_call_subscription) {
 | 
			
		||||
        client->send_homeassistant_service_call(call);
 | 
			
		||||
        return;  // Send to only the first authenticated client with service call subscription
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    // For service calls, send to all clients (existing behavior)
 | 
			
		||||
    // Service calls represent "actions to take" and may need to be sent to multiple Home Assistant instances
 | 
			
		||||
    for (auto &client : this->clients_) {
 | 
			
		||||
      client->send_homeassistant_service_call(call);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -15,23 +15,6 @@
 | 
			
		||||
 | 
			
		||||
namespace esphome::api {
 | 
			
		||||
 | 
			
		||||
// Helper functions for ZigZag encoding/decoding
 | 
			
		||||
inline constexpr uint32_t encode_zigzag32(int32_t value) {
 | 
			
		||||
  return (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline constexpr uint64_t encode_zigzag64(int64_t value) {
 | 
			
		||||
  return (static_cast<uint64_t>(value) << 1) ^ (static_cast<uint64_t>(value >> 63));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline constexpr int32_t decode_zigzag32(uint32_t value) {
 | 
			
		||||
  return (value & 1) ? static_cast<int32_t>(~(value >> 1)) : static_cast<int32_t>(value >> 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
inline constexpr int64_t decode_zigzag64(uint64_t value) {
 | 
			
		||||
  return (value & 1) ? static_cast<int64_t>(~(value >> 1)) : static_cast<int64_t>(value >> 1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * StringRef Ownership Model for API Protocol Messages
 | 
			
		||||
 * ===================================================
 | 
			
		||||
@@ -104,25 +87,33 @@ class ProtoVarInt {
 | 
			
		||||
    return {};  // Incomplete or invalid varint
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  constexpr uint16_t as_uint16() const { return this->value_; }
 | 
			
		||||
  constexpr uint32_t as_uint32() const { return this->value_; }
 | 
			
		||||
  constexpr uint64_t as_uint64() const { return this->value_; }
 | 
			
		||||
  constexpr bool as_bool() const { return this->value_; }
 | 
			
		||||
  constexpr int32_t as_int32() const {
 | 
			
		||||
  uint16_t as_uint16() const { return this->value_; }
 | 
			
		||||
  uint32_t as_uint32() const { return this->value_; }
 | 
			
		||||
  uint64_t as_uint64() const { return this->value_; }
 | 
			
		||||
  bool as_bool() const { return this->value_; }
 | 
			
		||||
  int32_t as_int32() const {
 | 
			
		||||
    // Not ZigZag encoded
 | 
			
		||||
    return static_cast<int32_t>(this->as_int64());
 | 
			
		||||
  }
 | 
			
		||||
  constexpr int64_t as_int64() const {
 | 
			
		||||
  int64_t as_int64() const {
 | 
			
		||||
    // Not ZigZag encoded
 | 
			
		||||
    return static_cast<int64_t>(this->value_);
 | 
			
		||||
  }
 | 
			
		||||
  constexpr int32_t as_sint32() const {
 | 
			
		||||
  int32_t as_sint32() const {
 | 
			
		||||
    // with ZigZag encoding
 | 
			
		||||
    return decode_zigzag32(static_cast<uint32_t>(this->value_));
 | 
			
		||||
    if (this->value_ & 1) {
 | 
			
		||||
      return static_cast<int32_t>(~(this->value_ >> 1));
 | 
			
		||||
    } else {
 | 
			
		||||
      return static_cast<int32_t>(this->value_ >> 1);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  constexpr int64_t as_sint64() const {
 | 
			
		||||
  int64_t as_sint64() const {
 | 
			
		||||
    // with ZigZag encoding
 | 
			
		||||
    return decode_zigzag64(this->value_);
 | 
			
		||||
    if (this->value_ & 1) {
 | 
			
		||||
      return static_cast<int64_t>(~(this->value_ >> 1));
 | 
			
		||||
    } else {
 | 
			
		||||
      return static_cast<int64_t>(this->value_ >> 1);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  /**
 | 
			
		||||
   * Encode the varint value to a pre-allocated buffer without bounds checking.
 | 
			
		||||
@@ -318,10 +309,22 @@ class ProtoWriteBuffer {
 | 
			
		||||
    this->encode_uint64(field_id, static_cast<uint64_t>(value), force);
 | 
			
		||||
  }
 | 
			
		||||
  void encode_sint32(uint32_t field_id, int32_t value, bool force = false) {
 | 
			
		||||
    this->encode_uint32(field_id, encode_zigzag32(value), force);
 | 
			
		||||
    uint32_t uvalue;
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
      uvalue = ~(value << 1);
 | 
			
		||||
    } else {
 | 
			
		||||
      uvalue = value << 1;
 | 
			
		||||
    }
 | 
			
		||||
    this->encode_uint32(field_id, uvalue, force);
 | 
			
		||||
  }
 | 
			
		||||
  void encode_sint64(uint32_t field_id, int64_t value, bool force = false) {
 | 
			
		||||
    this->encode_uint64(field_id, encode_zigzag64(value), force);
 | 
			
		||||
    uint64_t uvalue;
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
      uvalue = ~(value << 1);
 | 
			
		||||
    } else {
 | 
			
		||||
      uvalue = value << 1;
 | 
			
		||||
    }
 | 
			
		||||
    this->encode_uint64(field_id, uvalue, force);
 | 
			
		||||
  }
 | 
			
		||||
  void encode_message(uint32_t field_id, const ProtoMessage &value, bool force = false);
 | 
			
		||||
  std::vector<uint8_t> *get_buffer() const { return buffer_; }
 | 
			
		||||
@@ -392,7 +395,7 @@ class ProtoSize {
 | 
			
		||||
   * @param value The uint32_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static constexpr uint32_t varint(uint32_t value) {
 | 
			
		||||
  static inline uint32_t varint(uint32_t value) {
 | 
			
		||||
    // Optimized varint size calculation using leading zeros
 | 
			
		||||
    // Each 7 bits requires one byte in the varint encoding
 | 
			
		||||
    if (value < 128)
 | 
			
		||||
@@ -416,7 +419,7 @@ class ProtoSize {
 | 
			
		||||
   * @param value The uint64_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static constexpr uint32_t varint(uint64_t value) {
 | 
			
		||||
  static inline uint32_t varint(uint64_t value) {
 | 
			
		||||
    // Handle common case of values fitting in uint32_t (vast majority of use cases)
 | 
			
		||||
    if (value <= UINT32_MAX) {
 | 
			
		||||
      return varint(static_cast<uint32_t>(value));
 | 
			
		||||
@@ -447,7 +450,7 @@ class ProtoSize {
 | 
			
		||||
   * @param value The int32_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static constexpr uint32_t varint(int32_t value) {
 | 
			
		||||
  static inline uint32_t varint(int32_t value) {
 | 
			
		||||
    // Negative values are sign-extended to 64 bits in protocol buffers,
 | 
			
		||||
    // which always results in a 10-byte varint for negative int32
 | 
			
		||||
    if (value < 0) {
 | 
			
		||||
@@ -463,7 +466,7 @@ class ProtoSize {
 | 
			
		||||
   * @param value The int64_t value to calculate size for
 | 
			
		||||
   * @return The number of bytes needed to encode the value
 | 
			
		||||
   */
 | 
			
		||||
  static constexpr uint32_t varint(int64_t value) {
 | 
			
		||||
  static inline uint32_t varint(int64_t value) {
 | 
			
		||||
    // For int64_t, we convert to uint64_t and calculate the size
 | 
			
		||||
    // This works because the bit pattern determines the encoding size,
 | 
			
		||||
    // and we've handled negative int32 values as a special case above
 | 
			
		||||
@@ -477,7 +480,7 @@ class ProtoSize {
 | 
			
		||||
   * @param type The wire type value (from the WireType enum in the protobuf spec)
 | 
			
		||||
   * @return The number of bytes needed to encode the field ID and wire type
 | 
			
		||||
   */
 | 
			
		||||
  static constexpr uint32_t field(uint32_t field_id, uint32_t type) {
 | 
			
		||||
  static inline uint32_t field(uint32_t field_id, uint32_t type) {
 | 
			
		||||
    uint32_t tag = (field_id << 3) | (type & 0b111);
 | 
			
		||||
    return varint(tag);
 | 
			
		||||
  }
 | 
			
		||||
@@ -604,8 +607,9 @@ class ProtoSize {
 | 
			
		||||
   */
 | 
			
		||||
  inline void add_sint32_force(uint32_t field_id_size, int32_t value) {
 | 
			
		||||
    // Always calculate size when force is true
 | 
			
		||||
    // ZigZag encoding for sint32
 | 
			
		||||
    total_size_ += field_id_size + varint(encode_zigzag32(value));
 | 
			
		||||
    // ZigZag encoding for sint32: (n << 1) ^ (n >> 31)
 | 
			
		||||
    uint32_t zigzag = (static_cast<uint32_t>(value) << 1) ^ (static_cast<uint32_t>(value >> 31));
 | 
			
		||||
    total_size_ += field_id_size + varint(zigzag);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,6 @@ from esphome.const import (
 | 
			
		||||
    CONF_DIRECTION,
 | 
			
		||||
    CONF_HYSTERESIS,
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_POWER_MODE,
 | 
			
		||||
    CONF_RANGE,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -58,6 +57,7 @@ FAST_FILTER = {
 | 
			
		||||
CONF_RAW_ANGLE = "raw_angle"
 | 
			
		||||
CONF_RAW_POSITION = "raw_position"
 | 
			
		||||
CONF_WATCHDOG = "watchdog"
 | 
			
		||||
CONF_POWER_MODE = "power_mode"
 | 
			
		||||
CONF_SLOW_FILTER = "slow_filter"
 | 
			
		||||
CONF_FAST_FILTER = "fast_filter"
 | 
			
		||||
CONF_START_POSITION = "start_position"
 | 
			
		||||
 
 | 
			
		||||
@@ -24,6 +24,7 @@ AS5600Sensor = as5600_ns.class_("AS5600Sensor", sensor.Sensor, cg.PollingCompone
 | 
			
		||||
CONF_RAW_ANGLE = "raw_angle"
 | 
			
		||||
CONF_RAW_POSITION = "raw_position"
 | 
			
		||||
CONF_WATCHDOG = "watchdog"
 | 
			
		||||
CONF_POWER_MODE = "power_mode"
 | 
			
		||||
CONF_SLOW_FILTER = "slow_filter"
 | 
			
		||||
CONF_FAST_FILTER = "fast_filter"
 | 
			
		||||
CONF_PWM_FREQUENCY = "pwm_frequency"
 | 
			
		||||
 
 | 
			
		||||
@@ -110,8 +110,6 @@ void ATM90E32Component::update() {
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::setup() {
 | 
			
		||||
  this->spi_setup();
 | 
			
		||||
  this->cs_summary_ = this->cs_->dump_summary();
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
 | 
			
		||||
  uint16_t mmode0 = 0x87;  // 3P4W 50Hz
 | 
			
		||||
  uint16_t high_thresh = 0;
 | 
			
		||||
@@ -132,9 +130,9 @@ void ATM90E32Component::setup() {
 | 
			
		||||
    mmode0 |= 0 << 1;  // sets 1st bit to 0, phase b is not counted into the all-phase sum energy/power (P/Q/S)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A, false);  // Perform soft reset
 | 
			
		||||
  delay(6);                                                    // Wait for the minimum 5ms + 1ms
 | 
			
		||||
  this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);       // enable register config access
 | 
			
		||||
  this->write16_(ATM90E32_REGISTER_SOFTRESET, 0x789A);    // Perform soft reset
 | 
			
		||||
  delay(6);                                               // Wait for the minimum 5ms + 1ms
 | 
			
		||||
  this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x55AA);  // enable register config access
 | 
			
		||||
  if (!this->validate_spi_read_(0x55AA, "setup()")) {
 | 
			
		||||
    ESP_LOGW(TAG, "Could not initialize ATM90E32 IC, check SPI settings");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
@@ -158,17 +156,16 @@ void ATM90E32Component::setup() {
 | 
			
		||||
 | 
			
		||||
  if (this->enable_offset_calibration_) {
 | 
			
		||||
    // Initialize flash storage for offset calibrations
 | 
			
		||||
    uint32_t o_hash = fnv1_hash(std::string("_offset_calibration_") + this->cs_summary_);
 | 
			
		||||
    uint32_t o_hash = fnv1_hash(std::string("_offset_calibration_") + this->cs_->dump_summary());
 | 
			
		||||
    this->offset_pref_ = global_preferences->make_preference<OffsetCalibration[3]>(o_hash, true);
 | 
			
		||||
    this->restore_offset_calibrations_();
 | 
			
		||||
 | 
			
		||||
    // Initialize flash storage for power offset calibrations
 | 
			
		||||
    uint32_t po_hash = fnv1_hash(std::string("_power_offset_calibration_") + this->cs_summary_);
 | 
			
		||||
    uint32_t po_hash = fnv1_hash(std::string("_power_offset_calibration_") + this->cs_->dump_summary());
 | 
			
		||||
    this->power_offset_pref_ = global_preferences->make_preference<PowerOffsetCalibration[3]>(po_hash, true);
 | 
			
		||||
    this->restore_power_offset_calibrations_();
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
 | 
			
		||||
             cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Power & Voltage/Current offset calibration is disabled. Using config file values.");
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      this->write16_(this->voltage_offset_registers[phase],
 | 
			
		||||
                     static_cast<uint16_t>(this->offset_phase_[phase].voltage_offset_));
 | 
			
		||||
@@ -183,18 +180,21 @@ void ATM90E32Component::setup() {
 | 
			
		||||
 | 
			
		||||
  if (this->enable_gain_calibration_) {
 | 
			
		||||
    // Initialize flash storage for gain calibration
 | 
			
		||||
    uint32_t g_hash = fnv1_hash(std::string("_gain_calibration_") + this->cs_summary_);
 | 
			
		||||
    uint32_t g_hash = fnv1_hash(std::string("_gain_calibration_") + this->cs_->dump_summary());
 | 
			
		||||
    this->gain_calibration_pref_ = global_preferences->make_preference<GainCalibration[3]>(g_hash, true);
 | 
			
		||||
    this->restore_gain_calibrations_();
 | 
			
		||||
 | 
			
		||||
    if (!this->using_saved_calibrations_) {
 | 
			
		||||
    if (this->using_saved_calibrations_) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION] Successfully restored gain calibration from memory.");
 | 
			
		||||
    } else {
 | 
			
		||||
      for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
        this->write16_(voltage_gain_registers[phase], this->phase_[phase].voltage_gain_);
 | 
			
		||||
        this->write16_(current_gain_registers[phase], this->phase_[phase].ct_gain_);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Gain calibration is disabled. Using config file values.");
 | 
			
		||||
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      this->write16_(voltage_gain_registers[phase], this->phase_[phase].voltage_gain_);
 | 
			
		||||
      this->write16_(current_gain_registers[phase], this->phase_[phase].ct_gain_);
 | 
			
		||||
@@ -213,122 +213,6 @@ void ATM90E32Component::setup() {
 | 
			
		||||
  this->write16_(ATM90E32_REGISTER_CFGREGACCEN, 0x0000);  // end configuration
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::log_calibration_status_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
 | 
			
		||||
  bool offset_mismatch = false;
 | 
			
		||||
  bool power_mismatch = false;
 | 
			
		||||
  bool gain_mismatch = false;
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
    offset_mismatch |= this->offset_calibration_mismatch_[phase];
 | 
			
		||||
    power_mismatch |= this->power_offset_calibration_mismatch_[phase];
 | 
			
		||||
    gain_mismatch |= this->gain_calibration_mismatch_[phase];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (offset_mismatch) {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ===================== Offset mismatch: using flash values =====================", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] | Phase |   offset_voltage   |   offset_current   |", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] |       |  config  |  flash  |  config  |  flash  |", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] |   %c   |  %6d  | %6d  |  %6d  | %6d  |", cs, 'A' + phase,
 | 
			
		||||
               this->config_offset_phase_[phase].voltage_offset_, this->offset_phase_[phase].voltage_offset_,
 | 
			
		||||
               this->config_offset_phase_[phase].current_offset_, this->offset_phase_[phase].current_offset_);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ===============================================================================", cs);
 | 
			
		||||
  }
 | 
			
		||||
  if (power_mismatch) {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ================= Power offset mismatch: using flash values =================", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] | Phase | offset_active_power|offset_reactive_power|", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] |       |  config  |  flash  |  config  |  flash  |", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] |   %c   |  %6d  | %6d  |  %6d  | %6d  |", cs, 'A' + phase,
 | 
			
		||||
               this->config_power_offset_phase_[phase].active_power_offset,
 | 
			
		||||
               this->power_offset_phase_[phase].active_power_offset,
 | 
			
		||||
               this->config_power_offset_phase_[phase].reactive_power_offset,
 | 
			
		||||
               this->power_offset_phase_[phase].reactive_power_offset);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ===============================================================================", cs);
 | 
			
		||||
  }
 | 
			
		||||
  if (gain_mismatch) {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ====================== Gain mismatch: using flash values =====================", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] | Phase |    voltage_gain    |    current_gain    |", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] |       |  config  |  flash  |  config  |  flash  |", cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------------------",
 | 
			
		||||
             cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] |   %c   |  %6u  | %6u  |  %6u  | %6u  |", cs, 'A' + phase,
 | 
			
		||||
               this->config_gain_phase_[phase].voltage_gain, this->gain_phase_[phase].voltage_gain,
 | 
			
		||||
               this->config_gain_phase_[phase].current_gain, this->gain_phase_[phase].current_gain);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] ===============================================================================", cs);
 | 
			
		||||
  }
 | 
			
		||||
  if (!this->enable_offset_calibration_) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Power & Voltage/Current offset calibration is disabled. Using config file values.",
 | 
			
		||||
             cs);
 | 
			
		||||
  } else if (this->restored_offset_calibration_ && !offset_mismatch) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ============== Restored offset calibration from memory ==============", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |     %6d      |     %6d      |", cs, 'A' + phase,
 | 
			
		||||
               this->offset_phase_[phase].voltage_offset_, this->offset_phase_[phase].current_offset_);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ==============================================================\\n", cs);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->restored_power_offset_calibration_ && !power_mismatch) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ============ Restored power offset calibration from memory ============", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |       %6d        |        %6d        |", cs, 'A' + phase,
 | 
			
		||||
               this->power_offset_phase_[phase].active_power_offset,
 | 
			
		||||
               this->power_offset_phase_[phase].reactive_power_offset);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
 | 
			
		||||
  }
 | 
			
		||||
  if (!this->enable_gain_calibration_) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration is disabled. Using config file values.", cs);
 | 
			
		||||
  } else if (this->restored_gain_calibration_ && !gain_mismatch) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ============ Restoring saved gain calibrations to registers ============", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |    %6u    |    %6u    |", cs, 'A' + phase,
 | 
			
		||||
               this->gain_phase_[phase].voltage_gain, this->gain_phase_[phase].current_gain);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\\n", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration loaded and verified successfully.\n", cs);
 | 
			
		||||
  }
 | 
			
		||||
  this->calibration_message_printed_ = true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG("", "ATM90E32:");
 | 
			
		||||
  LOG_PIN("  CS Pin: ", this->cs_);
 | 
			
		||||
@@ -371,10 +255,6 @@ void ATM90E32Component::dump_config() {
 | 
			
		||||
  LOG_SENSOR("  ", "Peak Current C", this->phase_[PHASEC].peak_current_sensor_);
 | 
			
		||||
  LOG_SENSOR("  ", "Frequency", this->freq_sensor_);
 | 
			
		||||
  LOG_SENSOR("  ", "Chip Temp", this->chip_temperature_sensor_);
 | 
			
		||||
  if (this->restored_offset_calibration_ || this->restored_power_offset_calibration_ ||
 | 
			
		||||
      this->restored_gain_calibration_ || !this->enable_offset_calibration_ || !this->enable_gain_calibration_) {
 | 
			
		||||
    this->log_calibration_status_();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float ATM90E32Component::get_setup_priority() const { return setup_priority::IO; }
 | 
			
		||||
@@ -383,17 +263,19 @@ float ATM90E32Component::get_setup_priority() const { return setup_priority::IO;
 | 
			
		||||
// Peakdetect period: 05H. Bit 15:8 are PeakDet_period in ms. 7:0 are Sag_period
 | 
			
		||||
// Default is 143FH (20ms, 63ms)
 | 
			
		||||
uint16_t ATM90E32Component::read16_(uint16_t a_register) {
 | 
			
		||||
  this->enable();
 | 
			
		||||
  delay_microseconds_safe(1);  // min delay between CS low and first SCK is 200ns - 1us is plenty
 | 
			
		||||
  uint8_t addrh = (1 << 7) | ((a_register >> 8) & 0x03);
 | 
			
		||||
  uint8_t addrl = (a_register & 0xFF);
 | 
			
		||||
  uint8_t data[4] = {addrh, addrl, 0x00, 0x00};
 | 
			
		||||
  this->transfer_array(data, 4);
 | 
			
		||||
  uint16_t output = encode_uint16(data[2], data[3]);
 | 
			
		||||
  ESP_LOGVV(TAG, "read16_ 0x%04" PRIX16 " output 0x%04" PRIX16, a_register, output);
 | 
			
		||||
  delay_microseconds_safe(1);  // allow the last clock to propagate before releasing CS
 | 
			
		||||
  uint8_t data[2];
 | 
			
		||||
  uint16_t output;
 | 
			
		||||
  this->enable();
 | 
			
		||||
  delay_microseconds_safe(1);  // min delay between CS low and first SCK is 200ns - 1ms is plenty
 | 
			
		||||
  this->write_byte(addrh);
 | 
			
		||||
  this->write_byte(addrl);
 | 
			
		||||
  this->read_array(data, 2);
 | 
			
		||||
  this->disable();
 | 
			
		||||
  delay_microseconds_safe(1);  // meet minimum CS high time before next transaction
 | 
			
		||||
 | 
			
		||||
  output = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF);
 | 
			
		||||
  ESP_LOGVV(TAG, "read16_ 0x%04" PRIX16 " output 0x%04" PRIX16, a_register, output);
 | 
			
		||||
  return output;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -410,19 +292,13 @@ int ATM90E32Component::read32_(uint16_t addr_h, uint16_t addr_l) {
 | 
			
		||||
  return val;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::write16_(uint16_t a_register, uint16_t val, bool validate) {
 | 
			
		||||
void ATM90E32Component::write16_(uint16_t a_register, uint16_t val) {
 | 
			
		||||
  ESP_LOGVV(TAG, "write16_ 0x%04" PRIX16 " val 0x%04" PRIX16, a_register, val);
 | 
			
		||||
  uint8_t addrh = ((a_register >> 8) & 0x03);
 | 
			
		||||
  uint8_t addrl = (a_register & 0xFF);
 | 
			
		||||
  uint8_t data[4] = {addrh, addrl, uint8_t((val >> 8) & 0xFF), uint8_t(val & 0xFF)};
 | 
			
		||||
  this->enable();
 | 
			
		||||
  delay_microseconds_safe(1);  // ensure CS setup time
 | 
			
		||||
  this->write_array(data, 4);
 | 
			
		||||
  delay_microseconds_safe(1);  // allow clock to settle before raising CS
 | 
			
		||||
  this->write_byte16(a_register);
 | 
			
		||||
  this->write_byte16(val);
 | 
			
		||||
  this->disable();
 | 
			
		||||
  delay_microseconds_safe(1);  // ensure minimum CS high time
 | 
			
		||||
  if (validate)
 | 
			
		||||
    this->validate_spi_read_(val, "write16()");
 | 
			
		||||
  this->validate_spi_read_(val, "write16()");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float ATM90E32Component::get_local_phase_voltage_(uint8_t phase) { return this->phase_[phase].voltage_; }
 | 
			
		||||
@@ -565,10 +441,8 @@ float ATM90E32Component::get_chip_temperature_() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->enable_gain_calibration_) {
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] Gain calibration is disabled! Enable it first with enable_gain_calibration: true",
 | 
			
		||||
             cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION] Gain calibration is disabled! Enable it first with enable_gain_calibration: true");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -580,14 +454,12 @@ void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
  float ref_currents[3] = {this->get_reference_current(0), this->get_reference_current(1),
 | 
			
		||||
                           this->get_reference_current(2)};
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ========================= Gain Calibration  =========================", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(
 | 
			
		||||
      TAG,
 | 
			
		||||
      "[CALIBRATION][%s] | Phase | V_meas (V) | I_meas (A) | V_ref | I_ref  | V_gain (old→new) | I_gain (old→new) |",
 | 
			
		||||
      cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] ");
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] ========================= Gain Calibration  =========================");
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] ---------------------------------------------------------------------");
 | 
			
		||||
  ESP_LOGI(TAG,
 | 
			
		||||
           "[CALIBRATION] | Phase | V_meas (V) | I_meas (A) | V_ref | I_ref  | V_gain (old→new) | I_gain (old→new) |");
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] ---------------------------------------------------------------------");
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    float measured_voltage = this->get_phase_voltage_avg_(phase);
 | 
			
		||||
@@ -604,22 +476,22 @@ void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
 | 
			
		||||
    // Voltage calibration
 | 
			
		||||
    if (ref_voltage <= 0.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping voltage calibration: reference voltage is 0.", cs,
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping voltage calibration: reference voltage is 0.",
 | 
			
		||||
               phase_labels[phase]);
 | 
			
		||||
    } else if (measured_voltage == 0.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping voltage calibration: measured voltage is 0.", cs,
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping voltage calibration: measured voltage is 0.",
 | 
			
		||||
               phase_labels[phase]);
 | 
			
		||||
    } else {
 | 
			
		||||
      uint32_t new_voltage_gain = static_cast<uint16_t>((ref_voltage / measured_voltage) * current_voltage_gain);
 | 
			
		||||
      if (new_voltage_gain == 0) {
 | 
			
		||||
        ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Voltage gain would be 0. Check reference and measured voltage.", cs,
 | 
			
		||||
        ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Voltage gain would be 0. Check reference and measured voltage.",
 | 
			
		||||
                 phase_labels[phase]);
 | 
			
		||||
      } else {
 | 
			
		||||
        if (new_voltage_gain >= 65535) {
 | 
			
		||||
          ESP_LOGW(TAG,
 | 
			
		||||
                   "[CALIBRATION][%s] Phase %s - Voltage gain exceeds 65535. You may need a higher output voltage "
 | 
			
		||||
                   "transformer.",
 | 
			
		||||
                   cs, phase_labels[phase]);
 | 
			
		||||
          ESP_LOGW(
 | 
			
		||||
              TAG,
 | 
			
		||||
              "[CALIBRATION] Phase %s - Voltage gain exceeds 65535. You may need a higher output voltage transformer.",
 | 
			
		||||
              phase_labels[phase]);
 | 
			
		||||
          new_voltage_gain = 65535;
 | 
			
		||||
        }
 | 
			
		||||
        this->gain_phase_[phase].voltage_gain = static_cast<uint16_t>(new_voltage_gain);
 | 
			
		||||
@@ -629,20 +501,20 @@ void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
 | 
			
		||||
    // Current calibration
 | 
			
		||||
    if (ref_current == 0.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping current calibration: reference current is 0.", cs,
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping current calibration: reference current is 0.",
 | 
			
		||||
               phase_labels[phase]);
 | 
			
		||||
    } else if (measured_current == 0.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Skipping current calibration: measured current is 0.", cs,
 | 
			
		||||
      ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Skipping current calibration: measured current is 0.",
 | 
			
		||||
               phase_labels[phase]);
 | 
			
		||||
    } else {
 | 
			
		||||
      uint32_t new_current_gain = static_cast<uint16_t>((ref_current / measured_current) * current_current_gain);
 | 
			
		||||
      if (new_current_gain == 0) {
 | 
			
		||||
        ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Current gain would be 0. Check reference and measured current.", cs,
 | 
			
		||||
        ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Current gain would be 0. Check reference and measured current.",
 | 
			
		||||
                 phase_labels[phase]);
 | 
			
		||||
      } else {
 | 
			
		||||
        if (new_current_gain >= 65535) {
 | 
			
		||||
          ESP_LOGW(TAG, "[CALIBRATION][%s] Phase %s - Current gain exceeds 65535. You may need to turn up pga gain.",
 | 
			
		||||
                   cs, phase_labels[phase]);
 | 
			
		||||
          ESP_LOGW(TAG, "[CALIBRATION] Phase %s - Current gain exceeds 65535. You may need to turn up pga gain.",
 | 
			
		||||
                   phase_labels[phase]);
 | 
			
		||||
          new_current_gain = 65535;
 | 
			
		||||
        }
 | 
			
		||||
        this->gain_phase_[phase].current_gain = static_cast<uint16_t>(new_current_gain);
 | 
			
		||||
@@ -651,13 +523,13 @@ void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Final row output
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |  %9.2f |  %9.4f | %5.2f | %6.4f |  %5u → %-5u  |  %5u → %-5u  |", cs,
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] |   %c   |  %9.2f |  %9.4f | %5.2f | %6.4f |  %5u → %-5u  |  %5u → %-5u  |",
 | 
			
		||||
             'A' + phase, measured_voltage, measured_current, ref_voltage, ref_current, current_voltage_gain,
 | 
			
		||||
             did_voltage ? this->gain_phase_[phase].voltage_gain : current_voltage_gain, current_current_gain,
 | 
			
		||||
             did_current ? this->gain_phase_[phase].current_gain : current_current_gain);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] =====================================================================\n");
 | 
			
		||||
 | 
			
		||||
  this->save_gain_calibration_to_memory_();
 | 
			
		||||
  this->write_gains_to_registers_();
 | 
			
		||||
@@ -665,108 +537,54 @@ void ATM90E32Component::run_gain_calibrations() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::save_gain_calibration_to_memory_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  bool success = this->gain_calibration_pref_.save(&this->gain_phase_);
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
  if (success) {
 | 
			
		||||
    this->using_saved_calibrations_ = true;
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Gain calibration saved to memory.", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Gain calibration saved to memory.");
 | 
			
		||||
  } else {
 | 
			
		||||
    this->using_saved_calibrations_ = false;
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to save gain calibration to memory!", cs);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::save_offset_calibration_to_memory_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  bool success = this->offset_pref_.save(&this->offset_phase_);
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
  if (success) {
 | 
			
		||||
    this->using_saved_calibrations_ = true;
 | 
			
		||||
    this->restored_offset_calibration_ = true;
 | 
			
		||||
    for (bool &phase : this->offset_calibration_mismatch_)
 | 
			
		||||
      phase = false;
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Offset calibration saved to memory.", cs);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->using_saved_calibrations_ = false;
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to save offset calibration to memory!", cs);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::save_power_offset_calibration_to_memory_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  bool success = this->power_offset_pref_.save(&this->power_offset_phase_);
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
  if (success) {
 | 
			
		||||
    this->using_saved_calibrations_ = true;
 | 
			
		||||
    this->restored_power_offset_calibration_ = true;
 | 
			
		||||
    for (bool &phase : this->power_offset_calibration_mismatch_)
 | 
			
		||||
      phase = false;
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] Power offset calibration saved to memory.", cs);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->using_saved_calibrations_ = false;
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to save power offset calibration to memory!", cs);
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION] Failed to save gain calibration to memory!");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::run_offset_calibrations() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->enable_offset_calibration_) {
 | 
			
		||||
    ESP_LOGW(TAG,
 | 
			
		||||
             "[CALIBRATION][%s] Offset calibration is disabled! Enable it first with enable_offset_calibration: true",
 | 
			
		||||
             cs);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION] Offset calibration is disabled! Enable it first with enable_offset_calibration: true");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ======================== Offset Calibration ========================", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ------------------------------------------------------------------", cs);
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    int16_t voltage_offset = calibrate_offset(phase, true);
 | 
			
		||||
    int16_t current_offset = calibrate_offset(phase, false);
 | 
			
		||||
 | 
			
		||||
    this->write_offsets_to_registers_(phase, voltage_offset, current_offset);
 | 
			
		||||
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |     %6d      |     %6d      |", cs, 'A' + phase, voltage_offset,
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_voltage: %d, offset_current: %d", 'A' + phase, voltage_offset,
 | 
			
		||||
             current_offset);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ==================================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  this->save_offset_calibration_to_memory_();
 | 
			
		||||
  this->offset_pref_.save(&this->offset_phase_);  // Save to flash
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::run_power_offset_calibrations() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->enable_offset_calibration_) {
 | 
			
		||||
    ESP_LOGW(
 | 
			
		||||
        TAG,
 | 
			
		||||
        "[CALIBRATION][%s] Offset power calibration is disabled! Enable it first with enable_offset_calibration: true",
 | 
			
		||||
        cs);
 | 
			
		||||
        "[CALIBRATION] Offset power calibration is disabled! Enable it first with enable_offset_calibration: true");
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ===================== Power Offset Calibration =====================", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
    int16_t active_offset = calibrate_power_offset(phase, false);
 | 
			
		||||
    int16_t reactive_offset = calibrate_power_offset(phase, true);
 | 
			
		||||
 | 
			
		||||
    this->write_power_offsets_to_registers_(phase, active_offset, reactive_offset);
 | 
			
		||||
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |       %6d        |        %6d        |", cs, 'A' + phase, active_offset,
 | 
			
		||||
             reactive_offset);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_active_power: %d, offset_reactive_power: %d", 'A' + phase,
 | 
			
		||||
             active_offset, reactive_offset);
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  this->save_power_offset_calibration_to_memory_();
 | 
			
		||||
  this->power_offset_pref_.save(&this->power_offset_phase_);  // Save to flash
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::write_gains_to_registers_() {
 | 
			
		||||
@@ -813,276 +631,102 @@ void ATM90E32Component::write_power_offsets_to_registers_(uint8_t phase, int16_t
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::restore_gain_calibrations_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  for (uint8_t i = 0; i < 3; ++i) {
 | 
			
		||||
    this->config_gain_phase_[i].voltage_gain = this->phase_[i].voltage_gain_;
 | 
			
		||||
    this->config_gain_phase_[i].current_gain = this->phase_[i].ct_gain_;
 | 
			
		||||
    this->gain_phase_[i] = this->config_gain_phase_[i];
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->gain_calibration_pref_.load(&this->gain_phase_)) {
 | 
			
		||||
    bool all_zero = true;
 | 
			
		||||
    bool same_as_config = true;
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      const auto &cfg = this->config_gain_phase_[phase];
 | 
			
		||||
      const auto &saved = this->gain_phase_[phase];
 | 
			
		||||
      if (saved.voltage_gain != 0 || saved.current_gain != 0)
 | 
			
		||||
        all_zero = false;
 | 
			
		||||
      if (saved.voltage_gain != cfg.voltage_gain || saved.current_gain != cfg.current_gain)
 | 
			
		||||
        same_as_config = false;
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Restoring saved gain calibrations to registers:");
 | 
			
		||||
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      uint16_t v_gain = this->gain_phase_[phase].voltage_gain;
 | 
			
		||||
      uint16_t i_gain = this->gain_phase_[phase].current_gain;
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION]   Phase %c - Voltage Gain: %u, Current Gain: %u", 'A' + phase, v_gain, i_gain);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (!all_zero && !same_as_config) {
 | 
			
		||||
      for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
        bool mismatch = false;
 | 
			
		||||
        if (this->has_config_voltage_gain_[phase] &&
 | 
			
		||||
            this->gain_phase_[phase].voltage_gain != this->config_gain_phase_[phase].voltage_gain)
 | 
			
		||||
          mismatch = true;
 | 
			
		||||
        if (this->has_config_current_gain_[phase] &&
 | 
			
		||||
            this->gain_phase_[phase].current_gain != this->config_gain_phase_[phase].current_gain)
 | 
			
		||||
          mismatch = true;
 | 
			
		||||
        if (mismatch)
 | 
			
		||||
          this->gain_calibration_mismatch_[phase] = true;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      this->write_gains_to_registers_();
 | 
			
		||||
 | 
			
		||||
      if (this->verify_gain_writes_()) {
 | 
			
		||||
        this->using_saved_calibrations_ = true;
 | 
			
		||||
        this->restored_gain_calibration_ = true;
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
    this->write_gains_to_registers_();
 | 
			
		||||
 | 
			
		||||
    if (this->verify_gain_writes_()) {
 | 
			
		||||
      this->using_saved_calibrations_ = true;
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION] Gain calibration loaded and verified successfully.");
 | 
			
		||||
    } else {
 | 
			
		||||
      this->using_saved_calibrations_ = false;
 | 
			
		||||
      ESP_LOGE(TAG, "[CALIBRATION][%s] Gain verification failed! Calibration may not be applied correctly.", cs);
 | 
			
		||||
      ESP_LOGE(TAG, "[CALIBRATION] Gain verification failed! Calibration may not be applied correctly.");
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    this->using_saved_calibrations_ = false;
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION] No stored gain calibrations found. Using config file values.");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->using_saved_calibrations_ = false;
 | 
			
		||||
  for (uint8_t i = 0; i < 3; ++i)
 | 
			
		||||
    this->gain_phase_[i] = this->config_gain_phase_[i];
 | 
			
		||||
  this->write_gains_to_registers_();
 | 
			
		||||
 | 
			
		||||
  ESP_LOGW(TAG, "[CALIBRATION][%s] No stored gain calibrations found. Using config file values.", cs);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::restore_offset_calibrations_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  for (uint8_t i = 0; i < 3; ++i)
 | 
			
		||||
    this->config_offset_phase_[i] = this->offset_phase_[i];
 | 
			
		||||
  if (this->offset_pref_.load(&this->offset_phase_)) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Successfully restored offset calibration from memory.");
 | 
			
		||||
 | 
			
		||||
  bool have_data = this->offset_pref_.load(&this->offset_phase_);
 | 
			
		||||
  bool all_zero = true;
 | 
			
		||||
  if (have_data) {
 | 
			
		||||
    for (auto &phase : this->offset_phase_) {
 | 
			
		||||
      if (phase.voltage_offset_ != 0 || phase.current_offset_ != 0) {
 | 
			
		||||
        all_zero = false;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (have_data && !all_zero) {
 | 
			
		||||
    this->restored_offset_calibration_ = true;
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      auto &offset = this->offset_phase_[phase];
 | 
			
		||||
      bool mismatch = false;
 | 
			
		||||
      if (this->has_config_voltage_offset_[phase] &&
 | 
			
		||||
          offset.voltage_offset_ != this->config_offset_phase_[phase].voltage_offset_)
 | 
			
		||||
        mismatch = true;
 | 
			
		||||
      if (this->has_config_current_offset_[phase] &&
 | 
			
		||||
          offset.current_offset_ != this->config_offset_phase_[phase].current_offset_)
 | 
			
		||||
        mismatch = true;
 | 
			
		||||
      if (mismatch)
 | 
			
		||||
        this->offset_calibration_mismatch_[phase] = true;
 | 
			
		||||
      write_offsets_to_registers_(phase, offset.voltage_offset_, offset.current_offset_);
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_voltage:: %d, offset_current: %d", 'A' + phase,
 | 
			
		||||
               offset.voltage_offset_, offset.current_offset_);
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++)
 | 
			
		||||
      this->offset_phase_[phase] = this->config_offset_phase_[phase];
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] No stored offset calibrations found. Using default values.", cs);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    write_offsets_to_registers_(phase, this->offset_phase_[phase].voltage_offset_,
 | 
			
		||||
                                this->offset_phase_[phase].current_offset_);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION] No stored offset calibrations found. Using default values.");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::restore_power_offset_calibrations_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  for (uint8_t i = 0; i < 3; ++i)
 | 
			
		||||
    this->config_power_offset_phase_[i] = this->power_offset_phase_[i];
 | 
			
		||||
  if (this->power_offset_pref_.load(&this->power_offset_phase_)) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Successfully restored power offset calibration from memory.");
 | 
			
		||||
 | 
			
		||||
  bool have_data = this->power_offset_pref_.load(&this->power_offset_phase_);
 | 
			
		||||
  bool all_zero = true;
 | 
			
		||||
  if (have_data) {
 | 
			
		||||
    for (auto &phase : this->power_offset_phase_) {
 | 
			
		||||
      if (phase.active_power_offset != 0 || phase.reactive_power_offset != 0) {
 | 
			
		||||
        all_zero = false;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (have_data && !all_zero) {
 | 
			
		||||
    this->restored_power_offset_calibration_ = true;
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
      auto &offset = this->power_offset_phase_[phase];
 | 
			
		||||
      bool mismatch = false;
 | 
			
		||||
      if (this->has_config_active_power_offset_[phase] &&
 | 
			
		||||
          offset.active_power_offset != this->config_power_offset_phase_[phase].active_power_offset)
 | 
			
		||||
        mismatch = true;
 | 
			
		||||
      if (this->has_config_reactive_power_offset_[phase] &&
 | 
			
		||||
          offset.reactive_power_offset != this->config_power_offset_phase_[phase].reactive_power_offset)
 | 
			
		||||
        mismatch = true;
 | 
			
		||||
      if (mismatch)
 | 
			
		||||
        this->power_offset_calibration_mismatch_[phase] = true;
 | 
			
		||||
      write_power_offsets_to_registers_(phase, offset.active_power_offset, offset.reactive_power_offset);
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION] Phase %c - offset_active_power: %d, offset_reactive_power: %d", 'A' + phase,
 | 
			
		||||
               offset.active_power_offset, offset.reactive_power_offset);
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; ++phase)
 | 
			
		||||
      this->power_offset_phase_[phase] = this->config_power_offset_phase_[phase];
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION][%s] No stored power offsets found. Using default values.", cs);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; ++phase) {
 | 
			
		||||
    write_power_offsets_to_registers_(phase, this->power_offset_phase_[phase].active_power_offset,
 | 
			
		||||
                                      this->power_offset_phase_[phase].reactive_power_offset);
 | 
			
		||||
    ESP_LOGW(TAG, "[CALIBRATION] No stored power offsets found. Using default values.");
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::clear_gain_calibrations() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->using_saved_calibrations_) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] No stored gain calibrations to clear. Current values:", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
 | 
			
		||||
    for (int phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |    %6u    |    %6u    |", cs, 'A' + phase,
 | 
			
		||||
               this->gain_phase_[phase].voltage_gain, this->gain_phase_[phase].current_gain);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ==========================================================\n", cs);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] Clearing stored gain calibrations and restoring config-defined values", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | voltage_gain | current_gain |", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ----------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] Clearing stored gain calibrations and restoring config-defined values");
 | 
			
		||||
 | 
			
		||||
  for (int phase = 0; phase < 3; phase++) {
 | 
			
		||||
    uint16_t voltage_gain = this->phase_[phase].voltage_gain_;
 | 
			
		||||
    uint16_t current_gain = this->phase_[phase].ct_gain_;
 | 
			
		||||
 | 
			
		||||
    this->config_gain_phase_[phase].voltage_gain = voltage_gain;
 | 
			
		||||
    this->config_gain_phase_[phase].current_gain = current_gain;
 | 
			
		||||
    this->gain_phase_[phase].voltage_gain = voltage_gain;
 | 
			
		||||
    this->gain_phase_[phase].current_gain = current_gain;
 | 
			
		||||
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |    %6u    |    %6u    |", cs, 'A' + phase, voltage_gain, current_gain);
 | 
			
		||||
    gain_phase_[phase].voltage_gain = this->phase_[phase].voltage_gain_;
 | 
			
		||||
    gain_phase_[phase].current_gain = this->phase_[phase].ct_gain_;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ==========================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  GainCalibration zero_gains[3]{{0, 0}, {0, 0}, {0, 0}};
 | 
			
		||||
  bool success = this->gain_calibration_pref_.save(&zero_gains);
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
 | 
			
		||||
  bool success = this->gain_calibration_pref_.save(&this->gain_phase_);
 | 
			
		||||
  this->using_saved_calibrations_ = false;
 | 
			
		||||
  this->restored_gain_calibration_ = false;
 | 
			
		||||
  for (bool &phase : this->gain_calibration_mismatch_)
 | 
			
		||||
    phase = false;
 | 
			
		||||
 | 
			
		||||
  if (!success) {
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION][%s] Failed to clear gain calibrations!", cs);
 | 
			
		||||
  if (success) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION] Gain calibrations cleared. Config values restored:");
 | 
			
		||||
    for (int phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION]   Phase %c - Voltage Gain: %u, Current Gain: %u", 'A' + phase,
 | 
			
		||||
               gain_phase_[phase].voltage_gain, gain_phase_[phase].current_gain);
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGE(TAG, "[CALIBRATION] Failed to clear gain calibrations!");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->write_gains_to_registers_();  // Apply them to the chip immediately
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::clear_offset_calibrations() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->restored_offset_calibration_) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] No stored offset calibrations to clear. Current values:", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |     %6d      |     %6d      |", cs, 'A' + phase,
 | 
			
		||||
               this->offset_phase_[phase].voltage_offset_, this->offset_phase_[phase].current_offset_);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ==============================================================\n", cs);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] Clearing stored offset calibrations and restoring config-defined values", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_voltage | offset_current |", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] --------------------------------------------------------------", cs);
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    int16_t voltage_offset =
 | 
			
		||||
        this->has_config_voltage_offset_[phase] ? this->config_offset_phase_[phase].voltage_offset_ : 0;
 | 
			
		||||
    int16_t current_offset =
 | 
			
		||||
        this->has_config_current_offset_[phase] ? this->config_offset_phase_[phase].current_offset_ : 0;
 | 
			
		||||
    this->write_offsets_to_registers_(phase, voltage_offset, current_offset);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |     %6d      |     %6d      |", cs, 'A' + phase, voltage_offset,
 | 
			
		||||
             current_offset);
 | 
			
		||||
    this->write_offsets_to_registers_(phase, 0, 0);
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ==============================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  OffsetCalibration zero_offsets[3]{{0, 0}, {0, 0}, {0, 0}};
 | 
			
		||||
  this->offset_pref_.save(&zero_offsets);  // Clear stored values in flash
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
  this->offset_pref_.save(&this->offset_phase_);  // Save cleared values to flash memory
 | 
			
		||||
 | 
			
		||||
  this->restored_offset_calibration_ = false;
 | 
			
		||||
  for (bool &phase : this->offset_calibration_mismatch_)
 | 
			
		||||
    phase = false;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] Offsets cleared.", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] Offsets cleared.");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ATM90E32Component::clear_power_offset_calibrations() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  if (!this->restored_power_offset_calibration_) {
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] No stored power offsets to clear. Current values:", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
    for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
      ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |       %6d        |        %6d        |", cs, 'A' + phase,
 | 
			
		||||
               this->power_offset_phase_[phase].active_power_offset,
 | 
			
		||||
               this->power_offset_phase_[phase].reactive_power_offset);
 | 
			
		||||
    }
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] Clearing stored power offsets and restoring config-defined values", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] | Phase | offset_active_power | offset_reactive_power |", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] ---------------------------------------------------------------------", cs);
 | 
			
		||||
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    int16_t active_offset =
 | 
			
		||||
        this->has_config_active_power_offset_[phase] ? this->config_power_offset_phase_[phase].active_power_offset : 0;
 | 
			
		||||
    int16_t reactive_offset = this->has_config_reactive_power_offset_[phase]
 | 
			
		||||
                                  ? this->config_power_offset_phase_[phase].reactive_power_offset
 | 
			
		||||
                                  : 0;
 | 
			
		||||
    this->write_power_offsets_to_registers_(phase, active_offset, reactive_offset);
 | 
			
		||||
    ESP_LOGI(TAG, "[CALIBRATION][%s] |   %c   |       %6d        |        %6d        |", cs, 'A' + phase, active_offset,
 | 
			
		||||
             reactive_offset);
 | 
			
		||||
    this->write_power_offsets_to_registers_(phase, 0, 0);
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] =====================================================================\n", cs);
 | 
			
		||||
 | 
			
		||||
  PowerOffsetCalibration zero_power_offsets[3]{{0, 0}, {0, 0}, {0, 0}};
 | 
			
		||||
  this->power_offset_pref_.save(&zero_power_offsets);
 | 
			
		||||
  global_preferences->sync();
 | 
			
		||||
  this->power_offset_pref_.save(&this->power_offset_phase_);
 | 
			
		||||
 | 
			
		||||
  this->restored_power_offset_calibration_ = false;
 | 
			
		||||
  for (bool &phase : this->power_offset_calibration_mismatch_)
 | 
			
		||||
    phase = false;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION][%s] Power offsets cleared.", cs);
 | 
			
		||||
  ESP_LOGI(TAG, "[CALIBRATION] Power offsets cleared.");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int16_t ATM90E32Component::calibrate_offset(uint8_t phase, bool voltage) {
 | 
			
		||||
@@ -1103,21 +747,20 @@ int16_t ATM90E32Component::calibrate_offset(uint8_t phase, bool voltage) {
 | 
			
		||||
 | 
			
		||||
int16_t ATM90E32Component::calibrate_power_offset(uint8_t phase, bool reactive) {
 | 
			
		||||
  const uint8_t num_reads = 5;
 | 
			
		||||
  int64_t total_value = 0;
 | 
			
		||||
  uint64_t total_value = 0;
 | 
			
		||||
 | 
			
		||||
  for (uint8_t i = 0; i < num_reads; ++i) {
 | 
			
		||||
    int32_t reading = reactive ? this->read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase)
 | 
			
		||||
                               : this->read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
 | 
			
		||||
    uint32_t reading = reactive ? this->read32_(ATM90E32_REGISTER_QMEAN + phase, ATM90E32_REGISTER_QMEANLSB + phase)
 | 
			
		||||
                                : this->read32_(ATM90E32_REGISTER_PMEAN + phase, ATM90E32_REGISTER_PMEANLSB + phase);
 | 
			
		||||
    total_value += reading;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  int32_t average_value = total_value / num_reads;
 | 
			
		||||
  int32_t power_offset = -average_value;
 | 
			
		||||
  const uint32_t average_value = total_value / num_reads;
 | 
			
		||||
  const uint32_t power_offset = ~average_value + 1;
 | 
			
		||||
  return static_cast<int16_t>(power_offset);  // Takes the lower 16 bits
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool ATM90E32Component::verify_gain_writes_() {
 | 
			
		||||
  const char *cs = this->cs_summary_.c_str();
 | 
			
		||||
  bool success = true;
 | 
			
		||||
  for (uint8_t phase = 0; phase < 3; phase++) {
 | 
			
		||||
    uint16_t read_voltage = this->read16_(voltage_gain_registers[phase]);
 | 
			
		||||
@@ -1125,7 +768,7 @@ bool ATM90E32Component::verify_gain_writes_() {
 | 
			
		||||
 | 
			
		||||
    if (read_voltage != this->gain_phase_[phase].voltage_gain ||
 | 
			
		||||
        read_current != this->gain_phase_[phase].current_gain) {
 | 
			
		||||
      ESP_LOGE(TAG, "[CALIBRATION][%s] Mismatch detected for Phase %s!", cs, phase_labels[phase]);
 | 
			
		||||
      ESP_LOGE(TAG, "[CALIBRATION] Mismatch detected for Phase %s!", phase_labels[phase]);
 | 
			
		||||
      success = false;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
@@ -1148,16 +791,16 @@ void ATM90E32Component::check_phase_status() {
 | 
			
		||||
      status += "Phase Loss; ";
 | 
			
		||||
 | 
			
		||||
    auto *sensor = this->phase_status_text_sensor_[phase];
 | 
			
		||||
    if (sensor == nullptr)
 | 
			
		||||
      continue;
 | 
			
		||||
 | 
			
		||||
    const char *phase_name = sensor ? sensor->get_name().c_str() : "Unknown Phase";
 | 
			
		||||
    if (!status.empty()) {
 | 
			
		||||
      status.pop_back();  // remove space
 | 
			
		||||
      status.pop_back();  // remove semicolon
 | 
			
		||||
      ESP_LOGW(TAG, "%s: %s", sensor->get_name().c_str(), status.c_str());
 | 
			
		||||
      sensor->publish_state(status);
 | 
			
		||||
      ESP_LOGW(TAG, "%s: %s", phase_name, status.c_str());
 | 
			
		||||
      if (sensor != nullptr)
 | 
			
		||||
        sensor->publish_state(status);
 | 
			
		||||
    } else {
 | 
			
		||||
      sensor->publish_state("Okay");
 | 
			
		||||
      if (sensor != nullptr)
 | 
			
		||||
        sensor->publish_state("Okay");
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -1174,12 +817,9 @@ void ATM90E32Component::check_freq_status() {
 | 
			
		||||
  } else {
 | 
			
		||||
    freq_status = "Normal";
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGW(TAG, "Frequency status: %s", freq_status.c_str());
 | 
			
		||||
 | 
			
		||||
  if (this->freq_status_text_sensor_ != nullptr) {
 | 
			
		||||
    if (freq_status == "Normal") {
 | 
			
		||||
      ESP_LOGD(TAG, "Frequency status: %s", freq_status.c_str());
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGW(TAG, "Frequency status: %s", freq_status.c_str());
 | 
			
		||||
    }
 | 
			
		||||
    this->freq_status_text_sensor_->publish_state(freq_status);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -61,29 +61,15 @@ class ATM90E32Component : public PollingComponent,
 | 
			
		||||
    this->phase_[phase].harmonic_active_power_sensor_ = obj;
 | 
			
		||||
  }
 | 
			
		||||
  void set_peak_current_sensor(int phase, sensor::Sensor *obj) { this->phase_[phase].peak_current_sensor_ = obj; }
 | 
			
		||||
  void set_volt_gain(int phase, uint16_t gain) {
 | 
			
		||||
    this->phase_[phase].voltage_gain_ = gain;
 | 
			
		||||
    this->has_config_voltage_gain_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_ct_gain(int phase, uint16_t gain) {
 | 
			
		||||
    this->phase_[phase].ct_gain_ = gain;
 | 
			
		||||
    this->has_config_current_gain_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_voltage_offset(uint8_t phase, int16_t offset) {
 | 
			
		||||
    this->offset_phase_[phase].voltage_offset_ = offset;
 | 
			
		||||
    this->has_config_voltage_offset_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_current_offset(uint8_t phase, int16_t offset) {
 | 
			
		||||
    this->offset_phase_[phase].current_offset_ = offset;
 | 
			
		||||
    this->has_config_current_offset_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_volt_gain(int phase, uint16_t gain) { this->phase_[phase].voltage_gain_ = gain; }
 | 
			
		||||
  void set_ct_gain(int phase, uint16_t gain) { this->phase_[phase].ct_gain_ = gain; }
 | 
			
		||||
  void set_voltage_offset(uint8_t phase, int16_t offset) { this->offset_phase_[phase].voltage_offset_ = offset; }
 | 
			
		||||
  void set_current_offset(uint8_t phase, int16_t offset) { this->offset_phase_[phase].current_offset_ = offset; }
 | 
			
		||||
  void set_active_power_offset(uint8_t phase, int16_t offset) {
 | 
			
		||||
    this->power_offset_phase_[phase].active_power_offset = offset;
 | 
			
		||||
    this->has_config_active_power_offset_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_reactive_power_offset(uint8_t phase, int16_t offset) {
 | 
			
		||||
    this->power_offset_phase_[phase].reactive_power_offset = offset;
 | 
			
		||||
    this->has_config_reactive_power_offset_[phase] = true;
 | 
			
		||||
  }
 | 
			
		||||
  void set_freq_sensor(sensor::Sensor *freq_sensor) { freq_sensor_ = freq_sensor; }
 | 
			
		||||
  void set_peak_current_signed(bool flag) { peak_current_signed_ = flag; }
 | 
			
		||||
@@ -141,7 +127,7 @@ class ATM90E32Component : public PollingComponent,
 | 
			
		||||
#endif
 | 
			
		||||
  uint16_t read16_(uint16_t a_register);
 | 
			
		||||
  int read32_(uint16_t addr_h, uint16_t addr_l);
 | 
			
		||||
  void write16_(uint16_t a_register, uint16_t val, bool validate = true);
 | 
			
		||||
  void write16_(uint16_t a_register, uint16_t val);
 | 
			
		||||
  float get_local_phase_voltage_(uint8_t phase);
 | 
			
		||||
  float get_local_phase_current_(uint8_t phase);
 | 
			
		||||
  float get_local_phase_active_power_(uint8_t phase);
 | 
			
		||||
@@ -173,15 +159,12 @@ class ATM90E32Component : public PollingComponent,
 | 
			
		||||
  void restore_offset_calibrations_();
 | 
			
		||||
  void restore_power_offset_calibrations_();
 | 
			
		||||
  void restore_gain_calibrations_();
 | 
			
		||||
  void save_offset_calibration_to_memory_();
 | 
			
		||||
  void save_gain_calibration_to_memory_();
 | 
			
		||||
  void save_power_offset_calibration_to_memory_();
 | 
			
		||||
  void write_offsets_to_registers_(uint8_t phase, int16_t voltage_offset, int16_t current_offset);
 | 
			
		||||
  void write_power_offsets_to_registers_(uint8_t phase, int16_t p_offset, int16_t q_offset);
 | 
			
		||||
  void write_gains_to_registers_();
 | 
			
		||||
  bool verify_gain_writes_();
 | 
			
		||||
  bool validate_spi_read_(uint16_t expected, const char *context = nullptr);
 | 
			
		||||
  void log_calibration_status_();
 | 
			
		||||
 | 
			
		||||
  struct ATM90E32Phase {
 | 
			
		||||
    uint16_t voltage_gain_{0};
 | 
			
		||||
@@ -221,33 +204,19 @@ class ATM90E32Component : public PollingComponent,
 | 
			
		||||
    int16_t current_offset_{0};
 | 
			
		||||
  } offset_phase_[3];
 | 
			
		||||
 | 
			
		||||
  OffsetCalibration config_offset_phase_[3];
 | 
			
		||||
 | 
			
		||||
  struct PowerOffsetCalibration {
 | 
			
		||||
    int16_t active_power_offset{0};
 | 
			
		||||
    int16_t reactive_power_offset{0};
 | 
			
		||||
  } power_offset_phase_[3];
 | 
			
		||||
 | 
			
		||||
  PowerOffsetCalibration config_power_offset_phase_[3];
 | 
			
		||||
 | 
			
		||||
  struct GainCalibration {
 | 
			
		||||
    uint16_t voltage_gain{1};
 | 
			
		||||
    uint16_t current_gain{1};
 | 
			
		||||
  } gain_phase_[3];
 | 
			
		||||
 | 
			
		||||
  GainCalibration config_gain_phase_[3];
 | 
			
		||||
 | 
			
		||||
  bool has_config_voltage_offset_[3]{false, false, false};
 | 
			
		||||
  bool has_config_current_offset_[3]{false, false, false};
 | 
			
		||||
  bool has_config_active_power_offset_[3]{false, false, false};
 | 
			
		||||
  bool has_config_reactive_power_offset_[3]{false, false, false};
 | 
			
		||||
  bool has_config_voltage_gain_[3]{false, false, false};
 | 
			
		||||
  bool has_config_current_gain_[3]{false, false, false};
 | 
			
		||||
 | 
			
		||||
  ESPPreferenceObject offset_pref_;
 | 
			
		||||
  ESPPreferenceObject power_offset_pref_;
 | 
			
		||||
  ESPPreferenceObject gain_calibration_pref_;
 | 
			
		||||
  std::string cs_summary_;
 | 
			
		||||
 | 
			
		||||
  sensor::Sensor *freq_sensor_{nullptr};
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
@@ -262,13 +231,6 @@ class ATM90E32Component : public PollingComponent,
 | 
			
		||||
  bool peak_current_signed_{false};
 | 
			
		||||
  bool enable_offset_calibration_{false};
 | 
			
		||||
  bool enable_gain_calibration_{false};
 | 
			
		||||
  bool restored_offset_calibration_{false};
 | 
			
		||||
  bool restored_power_offset_calibration_{false};
 | 
			
		||||
  bool restored_gain_calibration_{false};
 | 
			
		||||
  bool calibration_message_printed_{false};
 | 
			
		||||
  bool offset_calibration_mismatch_[3]{false, false, false};
 | 
			
		||||
  bool power_offset_calibration_mismatch_[3]{false, false, false};
 | 
			
		||||
  bool gain_calibration_mismatch_[3]{false, false, false};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace atm90e32
 | 
			
		||||
 
 | 
			
		||||
@@ -286,7 +286,6 @@ async def remove_bond_to_code(config, action_id, template_arg, args):
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    # Register the loggers this component needs
 | 
			
		||||
    esp32_ble.register_bt_logger(BTLoggers.GATT, BTLoggers.SMP)
 | 
			
		||||
    cg.add_define("USE_ESP32_BLE_UUID")
 | 
			
		||||
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
 
 | 
			
		||||
@@ -118,12 +118,6 @@ async def to_code(config):
 | 
			
		||||
    connection_count = len(config.get(CONF_CONNECTIONS, []))
 | 
			
		||||
    cg.add_define("BLUETOOTH_PROXY_MAX_CONNECTIONS", connection_count)
 | 
			
		||||
 | 
			
		||||
    # Define batch size for BLE advertisements
 | 
			
		||||
    # Each advertisement is up to 80 bytes when packaged (including protocol overhead)
 | 
			
		||||
    # 16 advertisements × 80 bytes (worst case) = 1280 bytes out of ~1320 bytes usable payload
 | 
			
		||||
    # This achieves ~97% WiFi MTU utilization while staying under the limit
 | 
			
		||||
    cg.add_define("BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE", 16)
 | 
			
		||||
 | 
			
		||||
    for connection_conf in config.get(CONF_CONNECTIONS, []):
 | 
			
		||||
        connection_var = cg.new_Pvariable(connection_conf[CONF_ID])
 | 
			
		||||
        await cg.register_component(connection_var, connection_conf)
 | 
			
		||||
 
 | 
			
		||||
@@ -12,30 +12,16 @@ namespace esphome::bluetooth_proxy {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bluetooth_proxy.connection";
 | 
			
		||||
 | 
			
		||||
// This function is allocation-free and directly packs UUIDs into the output array
 | 
			
		||||
// using precalculated constants for the Bluetooth base UUID
 | 
			
		||||
static void fill_128bit_uuid_array(std::array<uint64_t, 2> &out, esp_bt_uuid_t uuid_source) {
 | 
			
		||||
  // Bluetooth base UUID: 00000000-0000-1000-8000-00805F9B34FB
 | 
			
		||||
  // out[0] = bytes 8-15 (big-endian)
 | 
			
		||||
  // - For 128-bit UUIDs: use bytes 8-15 as-is
 | 
			
		||||
  // - For 16/32-bit UUIDs: insert into bytes 12-15, use 0x00001000 for bytes 8-11
 | 
			
		||||
  out[0] = uuid_source.len == ESP_UUID_LEN_128
 | 
			
		||||
               ? (((uint64_t) uuid_source.uuid.uuid128[15] << 56) | ((uint64_t) uuid_source.uuid.uuid128[14] << 48) |
 | 
			
		||||
                  ((uint64_t) uuid_source.uuid.uuid128[13] << 40) | ((uint64_t) uuid_source.uuid.uuid128[12] << 32) |
 | 
			
		||||
                  ((uint64_t) uuid_source.uuid.uuid128[11] << 24) | ((uint64_t) uuid_source.uuid.uuid128[10] << 16) |
 | 
			
		||||
                  ((uint64_t) uuid_source.uuid.uuid128[9] << 8) | ((uint64_t) uuid_source.uuid.uuid128[8]))
 | 
			
		||||
               : (((uint64_t) (uuid_source.len == ESP_UUID_LEN_16 ? uuid_source.uuid.uuid16 : uuid_source.uuid.uuid32)
 | 
			
		||||
                   << 32) |
 | 
			
		||||
                  0x00001000ULL);  // Base UUID bytes 8-11
 | 
			
		||||
  // out[1] = bytes 0-7 (big-endian)
 | 
			
		||||
  // - For 128-bit UUIDs: use bytes 0-7 as-is
 | 
			
		||||
  // - For 16/32-bit UUIDs: use precalculated base UUID constant
 | 
			
		||||
  out[1] = uuid_source.len == ESP_UUID_LEN_128
 | 
			
		||||
               ? ((uint64_t) uuid_source.uuid.uuid128[7] << 56) | ((uint64_t) uuid_source.uuid.uuid128[6] << 48) |
 | 
			
		||||
                     ((uint64_t) uuid_source.uuid.uuid128[5] << 40) | ((uint64_t) uuid_source.uuid.uuid128[4] << 32) |
 | 
			
		||||
                     ((uint64_t) uuid_source.uuid.uuid128[3] << 24) | ((uint64_t) uuid_source.uuid.uuid128[2] << 16) |
 | 
			
		||||
                     ((uint64_t) uuid_source.uuid.uuid128[1] << 8) | ((uint64_t) uuid_source.uuid.uuid128[0])
 | 
			
		||||
               : 0x800000805F9B34FBULL;  // Base UUID bytes 0-7: 80-00-00-80-5F-9B-34-FB
 | 
			
		||||
  esp_bt_uuid_t uuid = espbt::ESPBTUUID::from_uuid(uuid_source).as_128bit().get_uuid();
 | 
			
		||||
  out[0] = ((uint64_t) uuid.uuid.uuid128[15] << 56) | ((uint64_t) uuid.uuid.uuid128[14] << 48) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[13] << 40) | ((uint64_t) uuid.uuid.uuid128[12] << 32) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[11] << 24) | ((uint64_t) uuid.uuid.uuid128[10] << 16) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[9] << 8) | ((uint64_t) uuid.uuid.uuid128[8]);
 | 
			
		||||
  out[1] = ((uint64_t) uuid.uuid.uuid128[7] << 56) | ((uint64_t) uuid.uuid.uuid128[6] << 48) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[5] << 40) | ((uint64_t) uuid.uuid.uuid128[4] << 32) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[3] << 24) | ((uint64_t) uuid.uuid.uuid128[2] << 16) |
 | 
			
		||||
           ((uint64_t) uuid.uuid.uuid128[1] << 8) | ((uint64_t) uuid.uuid.uuid128[0]);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Helper to fill UUID in the appropriate format based on client support and UUID type
 | 
			
		||||
@@ -94,11 +80,9 @@ void BluetoothConnection::dump_config() {
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::update_allocated_slot_(uint64_t find_value, uint64_t set_value) {
 | 
			
		||||
  auto &allocated = this->proxy_->connections_free_response_.allocated;
 | 
			
		||||
  for (auto &slot : allocated) {
 | 
			
		||||
    if (slot == find_value) {
 | 
			
		||||
      slot = set_value;
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
  auto *it = std::find(allocated.begin(), allocated.end(), find_value);
 | 
			
		||||
  if (it != allocated.end()) {
 | 
			
		||||
    *it = set_value;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -121,24 +105,13 @@ void BluetoothConnection::set_address(uint64_t address) {
 | 
			
		||||
void BluetoothConnection::loop() {
 | 
			
		||||
  BLEClientBase::loop();
 | 
			
		||||
 | 
			
		||||
  // Early return if no active connection
 | 
			
		||||
  if (this->address_ == 0) {
 | 
			
		||||
  // Early return if no active connection or not in service discovery phase
 | 
			
		||||
  if (this->address_ == 0 || this->send_service_ < 0 || this->send_service_ > this->service_count_) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Handle service discovery if in valid range
 | 
			
		||||
  if (this->send_service_ >= 0 && this->send_service_ <= this->service_count_) {
 | 
			
		||||
    this->send_service_for_discovery_();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Check if we should disable the loop
 | 
			
		||||
  // - For V3_WITH_CACHE: Services are never sent, disable after INIT state
 | 
			
		||||
  // - For V3_WITHOUT_CACHE: Disable only after service discovery is complete
 | 
			
		||||
  //   (send_service_ == DONE_SENDING_SERVICES, which is only set after services are sent)
 | 
			
		||||
  if (this->state_ != espbt::ClientState::INIT && (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE ||
 | 
			
		||||
                                                   this->send_service_ == DONE_SENDING_SERVICES)) {
 | 
			
		||||
    this->disable_loop();
 | 
			
		||||
  }
 | 
			
		||||
  // Handle service discovery
 | 
			
		||||
  this->send_service_for_discovery_();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::reset_connection_(esp_err_t reason) {
 | 
			
		||||
@@ -152,7 +125,7 @@ void BluetoothConnection::reset_connection_(esp_err_t reason) {
 | 
			
		||||
  // to detect incomplete service discovery rather than relying on us to
 | 
			
		||||
  // tell them about a partial list.
 | 
			
		||||
  this->set_address(0);
 | 
			
		||||
  this->send_service_ = INIT_SENDING_SERVICES;
 | 
			
		||||
  this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
  this->proxy_->send_connections_free();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -160,7 +133,10 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
  if (this->send_service_ >= this->service_count_) {
 | 
			
		||||
    this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
    this->proxy_->send_gatt_services_done(this->address_);
 | 
			
		||||
    this->release_services();
 | 
			
		||||
    if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE ||
 | 
			
		||||
        this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) {
 | 
			
		||||
      this->release_services();
 | 
			
		||||
    }
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -209,7 +185,8 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
                                     service_result.start_handle, service_result.end_handle, 0, &total_char_count);
 | 
			
		||||
 | 
			
		||||
    if (char_count_status != ESP_GATT_OK) {
 | 
			
		||||
      this->log_connection_error_("esp_ble_gattc_get_attr_count", char_count_status);
 | 
			
		||||
      ESP_LOGE(TAG, "[%d] [%s] Error getting characteristic count, status=%d", this->connection_index_,
 | 
			
		||||
               this->address_str().c_str(), char_count_status);
 | 
			
		||||
      this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
@@ -243,7 +220,8 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
          break;
 | 
			
		||||
        }
 | 
			
		||||
        if (char_status != ESP_GATT_OK) {
 | 
			
		||||
          this->log_connection_error_("esp_ble_gattc_get_all_char", char_status);
 | 
			
		||||
          ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->connection_index_,
 | 
			
		||||
                   this->address_str().c_str(), char_status);
 | 
			
		||||
          this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
@@ -266,7 +244,8 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
            this->gattc_if_, this->conn_id_, ESP_GATT_DB_DESCRIPTOR, 0, 0, char_result.char_handle, &total_desc_count);
 | 
			
		||||
 | 
			
		||||
        if (desc_count_status != ESP_GATT_OK) {
 | 
			
		||||
          this->log_connection_error_("esp_ble_gattc_get_attr_count", desc_count_status);
 | 
			
		||||
          ESP_LOGE(TAG, "[%d] [%s] Error getting descriptor count for char handle %d, status=%d",
 | 
			
		||||
                   this->connection_index_, this->address_str().c_str(), char_result.char_handle, desc_count_status);
 | 
			
		||||
          this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
@@ -287,7 +266,8 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
            break;
 | 
			
		||||
          }
 | 
			
		||||
          if (desc_status != ESP_GATT_OK) {
 | 
			
		||||
            this->log_connection_error_("esp_ble_gattc_get_all_descr", desc_status);
 | 
			
		||||
            ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_get_all_descr error, status=%d", this->connection_index_,
 | 
			
		||||
                     this->address_str().c_str(), desc_status);
 | 
			
		||||
            this->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
            return;
 | 
			
		||||
          }
 | 
			
		||||
@@ -341,33 +321,6 @@ void BluetoothConnection::send_service_for_discovery_() {
 | 
			
		||||
  api_conn->send_message(resp, api::BluetoothGATTGetServicesResponse::MESSAGE_TYPE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::log_connection_error_(const char *operation, esp_gatt_status_t status) {
 | 
			
		||||
  ESP_LOGE(TAG, "[%d] [%s] %s error, status=%d", this->connection_index_, this->address_str().c_str(), operation,
 | 
			
		||||
           status);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::log_connection_warning_(const char *operation, esp_err_t err) {
 | 
			
		||||
  ESP_LOGW(TAG, "[%d] [%s] %s failed, err=%d", this->connection_index_, this->address_str().c_str(), operation, err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::log_gatt_not_connected_(const char *action, const char *type) {
 | 
			
		||||
  ESP_LOGW(TAG, "[%d] [%s] Cannot %s GATT %s, not connected.", this->connection_index_, this->address_str().c_str(),
 | 
			
		||||
           action, type);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothConnection::log_gatt_operation_error_(const char *operation, uint16_t handle, esp_gatt_status_t status) {
 | 
			
		||||
  ESP_LOGW(TAG, "[%d] [%s] Error %s for handle 0x%2X, status=%d", this->connection_index_, this->address_str().c_str(),
 | 
			
		||||
           operation, handle, status);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::check_and_log_error_(const char *operation, esp_err_t err) {
 | 
			
		||||
  if (err != ESP_OK) {
 | 
			
		||||
    this->log_connection_warning_(operation, err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if,
 | 
			
		||||
                                              esp_ble_gattc_cb_param_t *param) {
 | 
			
		||||
  if (!BLEClientBase::gattc_event_handler(event, gattc_if, param))
 | 
			
		||||
@@ -375,19 +328,10 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
 | 
			
		||||
 | 
			
		||||
  switch (event) {
 | 
			
		||||
    case ESP_GATTC_DISCONNECT_EVT: {
 | 
			
		||||
      // Don't reset connection yet - wait for CLOSE_EVT to ensure controller has freed resources
 | 
			
		||||
      // This prevents race condition where we mark slot as free before controller cleanup is complete
 | 
			
		||||
      ESP_LOGD(TAG, "[%d] [%s] Disconnect, reason=0x%02x", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
               param->disconnect.reason);
 | 
			
		||||
      // Send disconnection notification but don't free the slot yet
 | 
			
		||||
      this->proxy_->send_device_connection(this->address_, false, 0, param->disconnect.reason);
 | 
			
		||||
      this->reset_connection_(param->disconnect.reason);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_CLOSE_EVT: {
 | 
			
		||||
      ESP_LOGD(TAG, "[%d] [%s] Close, reason=0x%02x, freeing slot", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
               param->close.reason);
 | 
			
		||||
      // Now the GATT connection is fully closed and controller resources are freed
 | 
			
		||||
      // Safe to mark the connection slot as available
 | 
			
		||||
      this->reset_connection_(param->close.reason);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
@@ -417,7 +361,8 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
 | 
			
		||||
    case ESP_GATTC_READ_DESCR_EVT:
 | 
			
		||||
    case ESP_GATTC_READ_CHAR_EVT: {
 | 
			
		||||
      if (param->read.status != ESP_GATT_OK) {
 | 
			
		||||
        this->log_gatt_operation_error_("reading char/descriptor", param->read.handle, param->read.status);
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Error reading char/descriptor at handle 0x%2X, status=%d", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), param->read.handle, param->read.status);
 | 
			
		||||
        this->proxy_->send_gatt_error(this->address_, param->read.handle, param->read.status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
@@ -431,7 +376,8 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
 | 
			
		||||
    case ESP_GATTC_WRITE_CHAR_EVT:
 | 
			
		||||
    case ESP_GATTC_WRITE_DESCR_EVT: {
 | 
			
		||||
      if (param->write.status != ESP_GATT_OK) {
 | 
			
		||||
        this->log_gatt_operation_error_("writing char/descriptor", param->write.handle, param->write.status);
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Error writing char/descriptor at handle 0x%2X, status=%d", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), param->write.handle, param->write.status);
 | 
			
		||||
        this->proxy_->send_gatt_error(this->address_, param->write.handle, param->write.status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
@@ -443,8 +389,9 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_UNREG_FOR_NOTIFY_EVT: {
 | 
			
		||||
      if (param->unreg_for_notify.status != ESP_GATT_OK) {
 | 
			
		||||
        this->log_gatt_operation_error_("unregistering notifications", param->unreg_for_notify.handle,
 | 
			
		||||
                                        param->unreg_for_notify.status);
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Error unregistering notifications for handle 0x%2X, status=%d",
 | 
			
		||||
                 this->connection_index_, this->address_str_.c_str(), param->unreg_for_notify.handle,
 | 
			
		||||
                 param->unreg_for_notify.status);
 | 
			
		||||
        this->proxy_->send_gatt_error(this->address_, param->unreg_for_notify.handle, param->unreg_for_notify.status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
@@ -456,8 +403,8 @@ bool BluetoothConnection::gattc_event_handler(esp_gattc_cb_event_t event, esp_ga
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
 | 
			
		||||
      if (param->reg_for_notify.status != ESP_GATT_OK) {
 | 
			
		||||
        this->log_gatt_operation_error_("registering notifications", param->reg_for_notify.handle,
 | 
			
		||||
                                        param->reg_for_notify.status);
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Error registering notifications for handle 0x%2X, status=%d", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), param->reg_for_notify.handle, param->reg_for_notify.status);
 | 
			
		||||
        this->proxy_->send_gatt_error(this->address_, param->reg_for_notify.handle, param->reg_for_notify.status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
@@ -503,7 +450,8 @@ void BluetoothConnection::gap_event_handler(esp_gap_ble_cb_event_t event, esp_bl
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::read_characteristic(uint16_t handle) {
 | 
			
		||||
  if (!this->connected()) {
 | 
			
		||||
    this->log_gatt_not_connected_("read", "characteristic");
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] Cannot read GATT characteristic, not connected.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    return ESP_GATT_NOT_CONNECTED;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -511,12 +459,18 @@ esp_err_t BluetoothConnection::read_characteristic(uint16_t handle) {
 | 
			
		||||
           handle);
 | 
			
		||||
 | 
			
		||||
  esp_err_t err = esp_ble_gattc_read_char(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
  return this->check_and_log_error_("esp_ble_gattc_read_char", err);
 | 
			
		||||
  if (err != ERR_OK) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_read_char error, err=%d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const std::string &data, bool response) {
 | 
			
		||||
  if (!this->connected()) {
 | 
			
		||||
    this->log_gatt_not_connected_("write", "characteristic");
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] Cannot write GATT characteristic, not connected.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    return ESP_GATT_NOT_CONNECTED;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGV(TAG, "[%d] [%s] Writing GATT characteristic handle %d", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
@@ -525,24 +479,36 @@ esp_err_t BluetoothConnection::write_characteristic(uint16_t handle, const std::
 | 
			
		||||
  esp_err_t err =
 | 
			
		||||
      esp_ble_gattc_write_char(this->gattc_if_, this->conn_id_, handle, data.size(), (uint8_t *) data.data(),
 | 
			
		||||
                               response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
  return this->check_and_log_error_("esp_ble_gattc_write_char", err);
 | 
			
		||||
  if (err != ERR_OK) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_write_char error, err=%d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::read_descriptor(uint16_t handle) {
 | 
			
		||||
  if (!this->connected()) {
 | 
			
		||||
    this->log_gatt_not_connected_("read", "descriptor");
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] Cannot read GATT descriptor, not connected.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    return ESP_GATT_NOT_CONNECTED;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGV(TAG, "[%d] [%s] Reading GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
           handle);
 | 
			
		||||
 | 
			
		||||
  esp_err_t err = esp_ble_gattc_read_char_descr(this->gattc_if_, this->conn_id_, handle, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
  return this->check_and_log_error_("esp_ble_gattc_read_char_descr", err);
 | 
			
		||||
  if (err != ERR_OK) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_read_char_descr error, err=%d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::string &data, bool response) {
 | 
			
		||||
  if (!this->connected()) {
 | 
			
		||||
    this->log_gatt_not_connected_("write", "descriptor");
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] Cannot write GATT descriptor, not connected.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    return ESP_GATT_NOT_CONNECTED;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGV(TAG, "[%d] [%s] Writing GATT descriptor handle %d", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
@@ -551,12 +517,18 @@ esp_err_t BluetoothConnection::write_descriptor(uint16_t handle, const std::stri
 | 
			
		||||
  esp_err_t err = esp_ble_gattc_write_char_descr(
 | 
			
		||||
      this->gattc_if_, this->conn_id_, handle, data.size(), (uint8_t *) data.data(),
 | 
			
		||||
      response ? ESP_GATT_WRITE_TYPE_RSP : ESP_GATT_WRITE_TYPE_NO_RSP, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
  return this->check_and_log_error_("esp_ble_gattc_write_char_descr", err);
 | 
			
		||||
  if (err != ERR_OK) {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_write_char_descr error, err=%d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), err);
 | 
			
		||||
    return err;
 | 
			
		||||
  }
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enable) {
 | 
			
		||||
  if (!this->connected()) {
 | 
			
		||||
    this->log_gatt_not_connected_("notify", "characteristic");
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] Cannot notify GATT characteristic, not connected.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    return ESP_GATT_NOT_CONNECTED;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -564,13 +536,22 @@ esp_err_t BluetoothConnection::notify_characteristic(uint16_t handle, bool enabl
 | 
			
		||||
    ESP_LOGV(TAG, "[%d] [%s] Registering for GATT characteristic notifications handle %d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), handle);
 | 
			
		||||
    esp_err_t err = esp_ble_gattc_register_for_notify(this->gattc_if_, this->remote_bda_, handle);
 | 
			
		||||
    return this->check_and_log_error_("esp_ble_gattc_register_for_notify", err);
 | 
			
		||||
    if (err != ESP_OK) {
 | 
			
		||||
      ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_register_for_notify failed, err=%d", this->connection_index_,
 | 
			
		||||
               this->address_str_.c_str(), err);
 | 
			
		||||
      return err;
 | 
			
		||||
    }
 | 
			
		||||
  } else {
 | 
			
		||||
    ESP_LOGV(TAG, "[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), handle);
 | 
			
		||||
    esp_err_t err = esp_ble_gattc_unregister_for_notify(this->gattc_if_, this->remote_bda_, handle);
 | 
			
		||||
    if (err != ESP_OK) {
 | 
			
		||||
      ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_unregister_for_notify failed, err=%d", this->connection_index_,
 | 
			
		||||
               this->address_str_.c_str(), err);
 | 
			
		||||
      return err;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGV(TAG, "[%d] [%s] Unregistering for GATT characteristic notifications handle %d", this->connection_index_,
 | 
			
		||||
           this->address_str_.c_str(), handle);
 | 
			
		||||
  esp_err_t err = esp_ble_gattc_unregister_for_notify(this->gattc_if_, this->remote_bda_, handle);
 | 
			
		||||
  return this->check_and_log_error_("esp_ble_gattc_unregister_for_notify", err);
 | 
			
		||||
  return ESP_OK;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp32_ble_tracker::AdvertisementParserType BluetoothConnection::get_advertisement_parser_type() {
 | 
			
		||||
 
 | 
			
		||||
@@ -33,18 +33,13 @@ class BluetoothConnection : public esp32_ble_client::BLEClientBase {
 | 
			
		||||
  void send_service_for_discovery_();
 | 
			
		||||
  void reset_connection_(esp_err_t reason);
 | 
			
		||||
  void update_allocated_slot_(uint64_t find_value, uint64_t set_value);
 | 
			
		||||
  void log_connection_error_(const char *operation, esp_gatt_status_t status);
 | 
			
		||||
  void log_connection_warning_(const char *operation, esp_err_t err);
 | 
			
		||||
  void log_gatt_not_connected_(const char *action, const char *type);
 | 
			
		||||
  void log_gatt_operation_error_(const char *operation, uint16_t handle, esp_gatt_status_t status);
 | 
			
		||||
  esp_err_t check_and_log_error_(const char *operation, esp_err_t err);
 | 
			
		||||
 | 
			
		||||
  // Memory optimized layout for 32-bit systems
 | 
			
		||||
  // Group 1: Pointers (4 bytes each, naturally aligned)
 | 
			
		||||
  BluetoothProxy *proxy_;
 | 
			
		||||
 | 
			
		||||
  // Group 2: 2-byte types
 | 
			
		||||
  int16_t send_service_{-3};  // -3 = INIT_SENDING_SERVICES, -2 = DONE_SENDING_SERVICES, >=0 = service index
 | 
			
		||||
  int16_t send_service_{-2};  // Needs to handle negative values and service count
 | 
			
		||||
 | 
			
		||||
  // Group 3: 1-byte types
 | 
			
		||||
  bool seen_mtu_or_services_{false};
 | 
			
		||||
 
 | 
			
		||||
@@ -11,8 +11,12 @@ namespace esphome::bluetooth_proxy {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "bluetooth_proxy";
 | 
			
		||||
 | 
			
		||||
// BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE is defined during code generation
 | 
			
		||||
// It sets the batch size for BLE advertisements to maximize WiFi efficiency
 | 
			
		||||
// Batch size for BLE advertisements to maximize WiFi efficiency
 | 
			
		||||
// Each advertisement is up to 80 bytes when packaged (including protocol overhead)
 | 
			
		||||
// Most advertisements are 20-30 bytes, allowing even more to fit per packet
 | 
			
		||||
// 16 advertisements × 80 bytes (worst case) = 1280 bytes out of ~1320 bytes usable payload
 | 
			
		||||
// This achieves ~97% WiFi MTU utilization while staying under the limit
 | 
			
		||||
static constexpr size_t FLUSH_BATCH_SIZE = 16;
 | 
			
		||||
 | 
			
		||||
// Verify BLE advertisement data array size matches the BLE specification (31 bytes adv + 31 bytes scan response)
 | 
			
		||||
static_assert(sizeof(((api::BluetoothLERawAdvertisement *) nullptr)->data) == 62,
 | 
			
		||||
@@ -21,6 +25,16 @@ static_assert(sizeof(((api::BluetoothLERawAdvertisement *) nullptr)->data) == 62
 | 
			
		||||
BluetoothProxy::BluetoothProxy() { global_bluetooth_proxy = this; }
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::setup() {
 | 
			
		||||
  // Pre-allocate response object
 | 
			
		||||
  this->response_ = std::make_unique<api::BluetoothLERawAdvertisementsResponse>();
 | 
			
		||||
 | 
			
		||||
  // Reserve capacity but start with size 0
 | 
			
		||||
  // Reserve 50% since we'll grow naturally and flush at FLUSH_BATCH_SIZE
 | 
			
		||||
  this->response_->advertisements.reserve(FLUSH_BATCH_SIZE / 2);
 | 
			
		||||
 | 
			
		||||
  // Don't pre-allocate pool - let it grow only if needed in busy environments
 | 
			
		||||
  // Many devices in quiet areas will never need the overflow pool
 | 
			
		||||
 | 
			
		||||
  this->connections_free_response_.limit = BLUETOOTH_PROXY_MAX_CONNECTIONS;
 | 
			
		||||
  this->connections_free_response_.free = BLUETOOTH_PROXY_MAX_CONNECTIONS;
 | 
			
		||||
 | 
			
		||||
@@ -39,26 +53,6 @@ void BluetoothProxy::send_bluetooth_scanner_state_(esp32_ble_tracker::ScannerSta
 | 
			
		||||
  this->api_connection_->send_message(resp, api::BluetoothScannerStateResponse::MESSAGE_TYPE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::log_connection_request_ignored_(BluetoothConnection *connection, espbt::ClientState state) {
 | 
			
		||||
  ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, state: %s", connection->get_connection_index(),
 | 
			
		||||
           connection->address_str().c_str(), espbt::client_state_to_string(state));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::log_connection_info_(BluetoothConnection *connection, const char *message) {
 | 
			
		||||
  ESP_LOGI(TAG, "[%d] [%s] Connecting %s", connection->get_connection_index(), connection->address_str().c_str(),
 | 
			
		||||
           message);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::log_not_connected_gatt_(const char *action, const char *type) {
 | 
			
		||||
  ESP_LOGW(TAG, "Cannot %s GATT %s, not connected", action, type);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::handle_gatt_not_connected_(uint64_t address, uint16_t handle, const char *action,
 | 
			
		||||
                                                const char *type) {
 | 
			
		||||
  this->log_not_connected_gatt_(action, type);
 | 
			
		||||
  this->send_gatt_error(address, handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
bool BluetoothProxy::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
 | 
			
		||||
  // This method should never be called since bluetooth_proxy always uses raw advertisements
 | 
			
		||||
@@ -71,27 +65,39 @@ bool BluetoothProxy::parse_devices(const esp32_ble::BLEScanResult *scan_results,
 | 
			
		||||
  if (!api::global_api_server->is_connected() || this->api_connection_ == nullptr)
 | 
			
		||||
    return false;
 | 
			
		||||
 | 
			
		||||
  auto &advertisements = this->response_.advertisements;
 | 
			
		||||
  auto &advertisements = this->response_->advertisements;
 | 
			
		||||
 | 
			
		||||
  for (size_t i = 0; i < count; i++) {
 | 
			
		||||
    auto &result = scan_results[i];
 | 
			
		||||
    uint8_t length = result.adv_data_len + result.scan_rsp_len;
 | 
			
		||||
 | 
			
		||||
    // Check if we need to expand the vector
 | 
			
		||||
    if (this->advertisement_count_ >= advertisements.size()) {
 | 
			
		||||
      if (this->advertisement_pool_.empty()) {
 | 
			
		||||
        // No room in pool, need to allocate
 | 
			
		||||
        advertisements.emplace_back();
 | 
			
		||||
      } else {
 | 
			
		||||
        // Pull from pool
 | 
			
		||||
        advertisements.push_back(std::move(this->advertisement_pool_.back()));
 | 
			
		||||
        this->advertisement_pool_.pop_back();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Fill in the data directly at current position
 | 
			
		||||
    auto &adv = advertisements[this->response_.advertisements_len];
 | 
			
		||||
    auto &adv = advertisements[this->advertisement_count_];
 | 
			
		||||
    adv.address = esp32_ble::ble_addr_to_uint64(result.bda);
 | 
			
		||||
    adv.rssi = result.rssi;
 | 
			
		||||
    adv.address_type = result.ble_addr_type;
 | 
			
		||||
    adv.data_len = length;
 | 
			
		||||
    std::memcpy(adv.data, result.ble_adv, length);
 | 
			
		||||
 | 
			
		||||
    this->response_.advertisements_len++;
 | 
			
		||||
    this->advertisement_count_++;
 | 
			
		||||
 | 
			
		||||
    ESP_LOGV(TAG, "Queuing raw packet from %02X:%02X:%02X:%02X:%02X:%02X, length %d. RSSI: %d dB", result.bda[0],
 | 
			
		||||
             result.bda[1], result.bda[2], result.bda[3], result.bda[4], result.bda[5], length, result.rssi);
 | 
			
		||||
 | 
			
		||||
    // Flush if we have reached BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE
 | 
			
		||||
    if (this->response_.advertisements_len >= BLUETOOTH_PROXY_ADVERTISEMENT_BATCH_SIZE) {
 | 
			
		||||
    // Flush if we have reached FLUSH_BATCH_SIZE
 | 
			
		||||
    if (this->advertisement_count_ >= FLUSH_BATCH_SIZE) {
 | 
			
		||||
      this->flush_pending_advertisements();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
@@ -100,22 +106,32 @@ bool BluetoothProxy::parse_devices(const esp32_ble::BLEScanResult *scan_results,
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::flush_pending_advertisements() {
 | 
			
		||||
  if (this->response_.advertisements_len == 0 || !api::global_api_server->is_connected() ||
 | 
			
		||||
      this->api_connection_ == nullptr)
 | 
			
		||||
  if (this->advertisement_count_ == 0 || !api::global_api_server->is_connected() || this->api_connection_ == nullptr)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  auto &advertisements = this->response_->advertisements;
 | 
			
		||||
 | 
			
		||||
  // Return any items beyond advertisement_count_ to the pool
 | 
			
		||||
  if (advertisements.size() > this->advertisement_count_) {
 | 
			
		||||
    // Move unused items back to pool
 | 
			
		||||
    this->advertisement_pool_.insert(this->advertisement_pool_.end(),
 | 
			
		||||
                                     std::make_move_iterator(advertisements.begin() + this->advertisement_count_),
 | 
			
		||||
                                     std::make_move_iterator(advertisements.end()));
 | 
			
		||||
 | 
			
		||||
    // Resize to actual count
 | 
			
		||||
    advertisements.resize(this->advertisement_count_);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Send the message
 | 
			
		||||
  this->api_connection_->send_message(this->response_, api::BluetoothLERawAdvertisementsResponse::MESSAGE_TYPE);
 | 
			
		||||
  this->api_connection_->send_message(*this->response_, api::BluetoothLERawAdvertisementsResponse::MESSAGE_TYPE);
 | 
			
		||||
 | 
			
		||||
  ESP_LOGV(TAG, "Sent batch of %u BLE advertisements", this->response_.advertisements_len);
 | 
			
		||||
 | 
			
		||||
  // Reset the length for the next batch
 | 
			
		||||
  this->response_.advertisements_len = 0;
 | 
			
		||||
  // Reset count - existing items will be overwritten in next batch
 | 
			
		||||
  this->advertisement_count_ = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Bluetooth Proxy:");
 | 
			
		||||
  ESP_LOGCONFIG(TAG,
 | 
			
		||||
                "Bluetooth Proxy:\n"
 | 
			
		||||
                "  Active: %s\n"
 | 
			
		||||
                "  Connections: %d",
 | 
			
		||||
                YESNO(this->active_), this->connection_count_);
 | 
			
		||||
@@ -159,7 +175,7 @@ BluetoothConnection *BluetoothProxy::get_connection_(uint64_t address, bool rese
 | 
			
		||||
  for (uint8_t i = 0; i < this->connection_count_; i++) {
 | 
			
		||||
    auto *connection = this->connections_[i];
 | 
			
		||||
    if (connection->get_address() == 0) {
 | 
			
		||||
      connection->send_service_ = INIT_SENDING_SERVICES;
 | 
			
		||||
      connection->send_service_ = DONE_SENDING_SERVICES;
 | 
			
		||||
      connection->set_address(address);
 | 
			
		||||
      // All connections must start at INIT
 | 
			
		||||
      // We only set the state if we allocate the connection
 | 
			
		||||
@@ -176,7 +192,8 @@ BluetoothConnection *BluetoothProxy::get_connection_(uint64_t address, bool rese
 | 
			
		||||
void BluetoothProxy::bluetooth_device_request(const api::BluetoothDeviceRequest &msg) {
 | 
			
		||||
  switch (msg.request_type) {
 | 
			
		||||
    case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITH_CACHE:
 | 
			
		||||
    case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITHOUT_CACHE: {
 | 
			
		||||
    case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITHOUT_CACHE:
 | 
			
		||||
    case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT: {
 | 
			
		||||
      auto *connection = this->get_connection_(msg.address, true);
 | 
			
		||||
      if (connection == nullptr) {
 | 
			
		||||
        ESP_LOGW(TAG, "No free connections available");
 | 
			
		||||
@@ -185,10 +202,23 @@ void BluetoothProxy::bluetooth_device_request(const api::BluetoothDeviceRequest
 | 
			
		||||
      }
 | 
			
		||||
      if (connection->state() == espbt::ClientState::CONNECTED ||
 | 
			
		||||
          connection->state() == espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
        this->log_connection_request_ignored_(connection, connection->state());
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection already established", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
        this->send_device_connection(msg.address, true);
 | 
			
		||||
        this->send_connections_free();
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() == espbt::ClientState::SEARCHING) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, already searching for device",
 | 
			
		||||
                 connection->get_connection_index(), connection->address_str().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() == espbt::ClientState::DISCOVERED) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, device already discovered",
 | 
			
		||||
                 connection->get_connection_index(), connection->address_str().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() == espbt::ClientState::READY_TO_CONNECT) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, waiting in line to connect",
 | 
			
		||||
                 connection->get_connection_index(), connection->address_str().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() == espbt::ClientState::CONNECTING) {
 | 
			
		||||
        if (connection->disconnect_pending()) {
 | 
			
		||||
          ESP_LOGW(TAG, "[%d] [%s] Connection request while pending disconnect, cancelling pending disconnect",
 | 
			
		||||
@@ -196,18 +226,29 @@ void BluetoothProxy::bluetooth_device_request(const api::BluetoothDeviceRequest
 | 
			
		||||
          connection->cancel_pending_disconnect();
 | 
			
		||||
          return;
 | 
			
		||||
        }
 | 
			
		||||
        this->log_connection_request_ignored_(connection, connection->state());
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, already connecting", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() == espbt::ClientState::DISCONNECTING) {
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection request ignored, device is disconnecting",
 | 
			
		||||
                 connection->get_connection_index(), connection->address_str().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
      } else if (connection->state() != espbt::ClientState::INIT) {
 | 
			
		||||
        this->log_connection_request_ignored_(connection, connection->state());
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection already in progress", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
        return;
 | 
			
		||||
      }
 | 
			
		||||
      if (msg.request_type == api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITH_CACHE) {
 | 
			
		||||
        connection->set_connection_type(espbt::ConnectionType::V3_WITH_CACHE);
 | 
			
		||||
        this->log_connection_info_(connection, "v3 with cache");
 | 
			
		||||
      } else {  // BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITHOUT_CACHE
 | 
			
		||||
        ESP_LOGI(TAG, "[%d] [%s] Connecting v3 with cache", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
      } else if (msg.request_type == api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT_V3_WITHOUT_CACHE) {
 | 
			
		||||
        connection->set_connection_type(espbt::ConnectionType::V3_WITHOUT_CACHE);
 | 
			
		||||
        this->log_connection_info_(connection, "v3 without cache");
 | 
			
		||||
        ESP_LOGI(TAG, "[%d] [%s] Connecting v3 without cache", connection->get_connection_index(),
 | 
			
		||||
                 connection->address_str().c_str());
 | 
			
		||||
      } else {
 | 
			
		||||
        connection->set_connection_type(espbt::ConnectionType::V1);
 | 
			
		||||
        ESP_LOGI(TAG, "[%d] [%s] Connecting v1", connection->get_connection_index(), connection->address_str().c_str());
 | 
			
		||||
      }
 | 
			
		||||
      if (msg.has_address_type) {
 | 
			
		||||
        uint64_to_bd_addr(msg.address, connection->remote_bda_);
 | 
			
		||||
@@ -269,18 +310,14 @@ void BluetoothProxy::bluetooth_device_request(const api::BluetoothDeviceRequest
 | 
			
		||||
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case api::enums::BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT: {
 | 
			
		||||
      ESP_LOGE(TAG, "V1 connections removed");
 | 
			
		||||
      this->send_device_connection(msg.address, false);
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_read(const api::BluetoothGATTReadRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr) {
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, msg.handle, "read", "characteristic");
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot read GATT characteristic, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, msg.handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -293,7 +330,8 @@ void BluetoothProxy::bluetooth_gatt_read(const api::BluetoothGATTReadRequest &ms
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_write(const api::BluetoothGATTWriteRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr) {
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, msg.handle, "write", "characteristic");
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot write GATT characteristic, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, msg.handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -306,7 +344,8 @@ void BluetoothProxy::bluetooth_gatt_write(const api::BluetoothGATTWriteRequest &
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_read_descriptor(const api::BluetoothGATTReadDescriptorRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr) {
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, msg.handle, "read", "descriptor");
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot read GATT descriptor, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, msg.handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -319,7 +358,8 @@ void BluetoothProxy::bluetooth_gatt_read_descriptor(const api::BluetoothGATTRead
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_write_descriptor(const api::BluetoothGATTWriteDescriptorRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr) {
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, msg.handle, "write", "descriptor");
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot write GATT descriptor, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, msg.handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -332,7 +372,8 @@ void BluetoothProxy::bluetooth_gatt_write_descriptor(const api::BluetoothGATTWri
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_send_services(const api::BluetoothGATTGetServicesRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr || !connection->connected()) {
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, 0, "get", "services");
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot get GATT services, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, 0, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (!connection->service_count_) {
 | 
			
		||||
@@ -340,14 +381,16 @@ void BluetoothProxy::bluetooth_gatt_send_services(const api::BluetoothGATTGetSer
 | 
			
		||||
    this->send_gatt_services_done(msg.address);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (connection->send_service_ == INIT_SENDING_SERVICES)  // Start sending services if not started yet
 | 
			
		||||
  if (connection->send_service_ ==
 | 
			
		||||
      DONE_SENDING_SERVICES)  // Only start sending services if we're not already sending them
 | 
			
		||||
    connection->send_service_ = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BluetoothProxy::bluetooth_gatt_notify(const api::BluetoothGATTNotifyRequest &msg) {
 | 
			
		||||
  auto *connection = this->get_connection_(msg.address, false);
 | 
			
		||||
  if (connection == nullptr) {
 | 
			
		||||
    this->handle_gatt_not_connected_(msg.address, msg.handle, "notify", "characteristic");
 | 
			
		||||
    ESP_LOGW(TAG, "Cannot notify GATT characteristic, not connected");
 | 
			
		||||
    this->send_gatt_error(msg.address, msg.handle, ESP_GATT_NOT_CONNECTED);
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,6 @@ namespace esphome::bluetooth_proxy {
 | 
			
		||||
 | 
			
		||||
static const esp_err_t ESP_GATT_NOT_CONNECTED = -1;
 | 
			
		||||
static const int DONE_SENDING_SERVICES = -2;
 | 
			
		||||
static const int INIT_SENDING_SERVICES = -3;
 | 
			
		||||
 | 
			
		||||
using namespace esp32_ble_client;
 | 
			
		||||
 | 
			
		||||
@@ -137,10 +136,6 @@ class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Com
 | 
			
		||||
  void send_bluetooth_scanner_state_(esp32_ble_tracker::ScannerState state);
 | 
			
		||||
 | 
			
		||||
  BluetoothConnection *get_connection_(uint64_t address, bool reserve);
 | 
			
		||||
  void log_connection_request_ignored_(BluetoothConnection *connection, espbt::ClientState state);
 | 
			
		||||
  void log_connection_info_(BluetoothConnection *connection, const char *message);
 | 
			
		||||
  void log_not_connected_gatt_(const char *action, const char *type);
 | 
			
		||||
  void handle_gatt_not_connected_(uint64_t address, uint16_t handle, const char *action, const char *type);
 | 
			
		||||
 | 
			
		||||
  // Memory optimized layout for 32-bit systems
 | 
			
		||||
  // Group 1: Pointers (4 bytes each, naturally aligned)
 | 
			
		||||
@@ -150,7 +145,8 @@ class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Com
 | 
			
		||||
  std::array<BluetoothConnection *, BLUETOOTH_PROXY_MAX_CONNECTIONS> connections_{};
 | 
			
		||||
 | 
			
		||||
  // BLE advertisement batching
 | 
			
		||||
  api::BluetoothLERawAdvertisementsResponse response_;
 | 
			
		||||
  std::vector<api::BluetoothLERawAdvertisement> advertisement_pool_;
 | 
			
		||||
  std::unique_ptr<api::BluetoothLERawAdvertisementsResponse> response_;
 | 
			
		||||
 | 
			
		||||
  // Group 3: 4-byte types
 | 
			
		||||
  uint32_t last_advertisement_flush_time_{0};
 | 
			
		||||
@@ -160,8 +156,9 @@ class BluetoothProxy : public esp32_ble_tracker::ESPBTDeviceListener, public Com
 | 
			
		||||
 | 
			
		||||
  // Group 4: 1-byte types grouped together
 | 
			
		||||
  bool active_;
 | 
			
		||||
  uint8_t advertisement_count_{0};
 | 
			
		||||
  uint8_t connection_count_{0};
 | 
			
		||||
  // 2 bytes used, 2 bytes padding
 | 
			
		||||
  // 3 bytes used, 1 byte padding
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
extern BluetoothProxy *global_bluetooth_proxy;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
 | 
			
		||||
 
 | 
			
		||||
@@ -7,8 +7,6 @@
 | 
			
		||||
#include <esphome/components/sensor/sensor.h>
 | 
			
		||||
#include <esphome/core/component.h>
 | 
			
		||||
 | 
			
		||||
#define BME280_ERROR_WRONG_CHIP_ID "Wrong chip ID"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bme280_base {
 | 
			
		||||
 | 
			
		||||
@@ -100,18 +98,18 @@ void BME280Component::setup() {
 | 
			
		||||
 | 
			
		||||
  if (!this->read_byte(BME280_REGISTER_CHIPID, &chip_id)) {
 | 
			
		||||
    this->error_code_ = COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed(ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (chip_id != 0x60) {
 | 
			
		||||
    this->error_code_ = WRONG_CHIP_ID;
 | 
			
		||||
    this->mark_failed(BME280_ERROR_WRONG_CHIP_ID);
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Send a soft reset.
 | 
			
		||||
  if (!this->write_byte(BME280_REGISTER_RESET, BME280_SOFT_RESET)) {
 | 
			
		||||
    this->mark_failed("Reset failed");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  // Wait until the NVM data has finished loading.
 | 
			
		||||
@@ -120,12 +118,14 @@ void BME280Component::setup() {
 | 
			
		||||
  do {  // NOLINT
 | 
			
		||||
    delay(2);
 | 
			
		||||
    if (!this->read_byte(BME280_REGISTER_STATUS, &status)) {
 | 
			
		||||
      this->mark_failed("Error reading status register");
 | 
			
		||||
      ESP_LOGW(TAG, "Error reading status register.");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
  } while ((status & BME280_STATUS_IM_UPDATE) && (--retry));
 | 
			
		||||
  if (status & BME280_STATUS_IM_UPDATE) {
 | 
			
		||||
    this->mark_failed("Timeout loading NVM");
 | 
			
		||||
    ESP_LOGW(TAG, "Timeout loading NVM.");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -153,26 +153,26 @@ void BME280Component::setup() {
 | 
			
		||||
 | 
			
		||||
  uint8_t humid_control_val = 0;
 | 
			
		||||
  if (!this->read_byte(BME280_REGISTER_CONTROLHUMID, &humid_control_val)) {
 | 
			
		||||
    this->mark_failed("Read humidity control");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  humid_control_val &= ~0b00000111;
 | 
			
		||||
  humid_control_val |= this->humidity_oversampling_ & 0b111;
 | 
			
		||||
  if (!this->write_byte(BME280_REGISTER_CONTROLHUMID, humid_control_val)) {
 | 
			
		||||
    this->mark_failed("Write humidity control");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uint8_t config_register = 0;
 | 
			
		||||
  if (!this->read_byte(BME280_REGISTER_CONFIG, &config_register)) {
 | 
			
		||||
    this->mark_failed("Read config");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  config_register &= ~0b11111100;
 | 
			
		||||
  config_register |= 0b101 << 5;  // 1000 ms standby time
 | 
			
		||||
  config_register |= (this->iir_filter_ & 0b111) << 2;
 | 
			
		||||
  if (!this->write_byte(BME280_REGISTER_CONFIG, config_register)) {
 | 
			
		||||
    this->mark_failed("Write config");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -183,7 +183,7 @@ void BME280Component::dump_config() {
 | 
			
		||||
      ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
      break;
 | 
			
		||||
    case WRONG_CHIP_ID:
 | 
			
		||||
      ESP_LOGE(TAG, BME280_ERROR_WRONG_CHIP_ID);
 | 
			
		||||
      ESP_LOGE(TAG, "BME280 has wrong chip ID! Is it a BME280?");
 | 
			
		||||
      break;
 | 
			
		||||
    case NONE:
 | 
			
		||||
    default:
 | 
			
		||||
@@ -223,21 +223,21 @@ void BME280Component::update() {
 | 
			
		||||
  this->set_timeout("data", uint32_t(ceilf(meas_time)), [this]() {
 | 
			
		||||
    uint8_t data[8];
 | 
			
		||||
    if (!this->read_bytes(BME280_REGISTER_MEASUREMENTS, data, 8)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Error reading registers");
 | 
			
		||||
      ESP_LOGW(TAG, "Error reading registers.");
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    int32_t t_fine = 0;
 | 
			
		||||
    float const temperature = this->read_temperature_(data, &t_fine);
 | 
			
		||||
    if (std::isnan(temperature)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid temperature");
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid temperature, cannot read pressure & humidity values.");
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    float const pressure = this->read_pressure_(data, t_fine);
 | 
			
		||||
    float const humidity = this->read_humidity_(data, t_fine);
 | 
			
		||||
 | 
			
		||||
    ESP_LOGV(TAG, "Temperature=%.1f°C Pressure=%.1fhPa Humidity=%.1f%%", temperature, pressure, humidity);
 | 
			
		||||
    ESP_LOGV(TAG, "Got temperature=%.1f°C pressure=%.1fhPa humidity=%.1f%%", temperature, pressure, humidity);
 | 
			
		||||
    if (this->temperature_sensor_ != nullptr)
 | 
			
		||||
      this->temperature_sensor_->publish_state(temperature);
 | 
			
		||||
    if (this->pressure_sensor_ != nullptr)
 | 
			
		||||
 
 | 
			
		||||
@@ -28,7 +28,7 @@ const float BME680_GAS_LOOKUP_TABLE_1[16] PROGMEM = {0.0, 0.0, 0.0,  0.0,  0.0,
 | 
			
		||||
const float BME680_GAS_LOOKUP_TABLE_2[16] PROGMEM = {0.0,  0.0, 0.0, 0.0, 0.1, 0.7, 0.0, -0.8,
 | 
			
		||||
                                                     -0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
 | 
			
		||||
 | 
			
		||||
[[maybe_unused]] static const char *oversampling_to_str(BME680Oversampling oversampling) {
 | 
			
		||||
static const char *oversampling_to_str(BME680Oversampling oversampling) {
 | 
			
		||||
  switch (oversampling) {
 | 
			
		||||
    case BME680_OVERSAMPLING_NONE:
 | 
			
		||||
      return "None";
 | 
			
		||||
@@ -47,7 +47,7 @@ const float BME680_GAS_LOOKUP_TABLE_2[16] PROGMEM = {0.0,  0.0, 0.0, 0.0, 0.1, 0
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
[[maybe_unused]] static const char *iir_filter_to_str(BME680IIRFilter filter) {
 | 
			
		||||
static const char *iir_filter_to_str(BME680IIRFilter filter) {
 | 
			
		||||
  switch (filter) {
 | 
			
		||||
    case BME680_IIR_FILTER_OFF:
 | 
			
		||||
      return "OFF";
 | 
			
		||||
 
 | 
			
		||||
@@ -2,8 +2,6 @@
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
#define BMP280_ERROR_WRONG_CHIP_ID "Wrong chip ID"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace bmp280_base {
 | 
			
		||||
 | 
			
		||||
@@ -65,23 +63,23 @@ void BMP280Component::setup() {
 | 
			
		||||
  // https://community.st.com/t5/stm32-mcus-products/issue-with-reading-bmp280-chip-id-using-spi/td-p/691855
 | 
			
		||||
  if (!this->read_byte(0xD0, &chip_id)) {
 | 
			
		||||
    this->error_code_ = COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed(ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (!this->read_byte(0xD0, &chip_id)) {
 | 
			
		||||
    this->error_code_ = COMMUNICATION_FAILED;
 | 
			
		||||
    this->mark_failed(ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (chip_id != 0x58) {
 | 
			
		||||
    this->error_code_ = WRONG_CHIP_ID;
 | 
			
		||||
    this->mark_failed(BMP280_ERROR_WRONG_CHIP_ID);
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Send a soft reset.
 | 
			
		||||
  if (!this->write_byte(BMP280_REGISTER_RESET, BMP280_SOFT_RESET)) {
 | 
			
		||||
    this->mark_failed("Reset failed");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  // Wait until the NVM data has finished loading.
 | 
			
		||||
@@ -90,12 +88,14 @@ void BMP280Component::setup() {
 | 
			
		||||
  do {
 | 
			
		||||
    delay(2);
 | 
			
		||||
    if (!this->read_byte(BMP280_REGISTER_STATUS, &status)) {
 | 
			
		||||
      this->mark_failed("Error reading status register");
 | 
			
		||||
      ESP_LOGW(TAG, "Error reading status register.");
 | 
			
		||||
      this->mark_failed();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
  } while ((status & BMP280_STATUS_IM_UPDATE) && (--retry));
 | 
			
		||||
  if (status & BMP280_STATUS_IM_UPDATE) {
 | 
			
		||||
    this->mark_failed("Timeout loading NVM");
 | 
			
		||||
    ESP_LOGW(TAG, "Timeout loading NVM.");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -116,14 +116,14 @@ void BMP280Component::setup() {
 | 
			
		||||
 | 
			
		||||
  uint8_t config_register = 0;
 | 
			
		||||
  if (!this->read_byte(BMP280_REGISTER_CONFIG, &config_register)) {
 | 
			
		||||
    this->mark_failed("Read config");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  config_register &= ~0b11111100;
 | 
			
		||||
  config_register |= 0b000 << 5;  // 0.5 ms standby time
 | 
			
		||||
  config_register |= (this->iir_filter_ & 0b111) << 2;
 | 
			
		||||
  if (!this->write_byte(BMP280_REGISTER_CONFIG, config_register)) {
 | 
			
		||||
    this->mark_failed("Write config");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -134,7 +134,7 @@ void BMP280Component::dump_config() {
 | 
			
		||||
      ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
 | 
			
		||||
      break;
 | 
			
		||||
    case WRONG_CHIP_ID:
 | 
			
		||||
      ESP_LOGE(TAG, BMP280_ERROR_WRONG_CHIP_ID);
 | 
			
		||||
      ESP_LOGE(TAG, "BMP280 has wrong chip ID! Is it a BME280?");
 | 
			
		||||
      break;
 | 
			
		||||
    case NONE:
 | 
			
		||||
    default:
 | 
			
		||||
@@ -172,13 +172,13 @@ void BMP280Component::update() {
 | 
			
		||||
    int32_t t_fine = 0;
 | 
			
		||||
    float temperature = this->read_temperature_(&t_fine);
 | 
			
		||||
    if (std::isnan(temperature)) {
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid temperature");
 | 
			
		||||
      ESP_LOGW(TAG, "Invalid temperature, cannot read pressure values.");
 | 
			
		||||
      this->status_set_warning();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    float pressure = this->read_pressure_(t_fine);
 | 
			
		||||
 | 
			
		||||
    ESP_LOGV(TAG, "Temperature=%.1f°C Pressure=%.1fhPa", temperature, pressure);
 | 
			
		||||
    ESP_LOGD(TAG, "Got temperature=%.1f°C pressure=%.1fhPa", temperature, pressure);
 | 
			
		||||
    if (this->temperature_sensor_ != nullptr)
 | 
			
		||||
      this->temperature_sensor_->publish_state(temperature);
 | 
			
		||||
    if (this->pressure_sensor_ != nullptr)
 | 
			
		||||
 
 | 
			
		||||
@@ -99,39 +99,43 @@ const optional<float> &CoverCall::get_tilt() const { return this->tilt_; }
 | 
			
		||||
const optional<bool> &CoverCall::get_toggle() const { return this->toggle_; }
 | 
			
		||||
void CoverCall::validate_() {
 | 
			
		||||
  auto traits = this->parent_->get_traits();
 | 
			
		||||
  const char *name = this->parent_->get_name().c_str();
 | 
			
		||||
 | 
			
		||||
  if (this->position_.has_value()) {
 | 
			
		||||
    auto pos = *this->position_;
 | 
			
		||||
    if (!traits.get_supports_position() && pos != COVER_OPEN && pos != COVER_CLOSED) {
 | 
			
		||||
      ESP_LOGW(TAG, "'%s': position unsupported", name);
 | 
			
		||||
      ESP_LOGW(TAG, "'%s' - This cover device does not support setting position!", this->parent_->get_name().c_str());
 | 
			
		||||
      this->position_.reset();
 | 
			
		||||
    } else if (pos < 0.0f || pos > 1.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "'%s': position %.2f out of range", name, pos);
 | 
			
		||||
      ESP_LOGW(TAG, "'%s' - Position %.2f is out of range [0.0 - 1.0]", this->parent_->get_name().c_str(), pos);
 | 
			
		||||
      this->position_ = clamp(pos, 0.0f, 1.0f);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if (this->tilt_.has_value()) {
 | 
			
		||||
    auto tilt = *this->tilt_;
 | 
			
		||||
    if (!traits.get_supports_tilt()) {
 | 
			
		||||
      ESP_LOGW(TAG, "'%s': tilt unsupported", name);
 | 
			
		||||
      ESP_LOGW(TAG, "'%s' - This cover device does not support tilt!", this->parent_->get_name().c_str());
 | 
			
		||||
      this->tilt_.reset();
 | 
			
		||||
    } else if (tilt < 0.0f || tilt > 1.0f) {
 | 
			
		||||
      ESP_LOGW(TAG, "'%s': tilt %.2f out of range", name, tilt);
 | 
			
		||||
      ESP_LOGW(TAG, "'%s' - Tilt %.2f is out of range [0.0 - 1.0]", this->parent_->get_name().c_str(), tilt);
 | 
			
		||||
      this->tilt_ = clamp(tilt, 0.0f, 1.0f);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if (this->toggle_.has_value()) {
 | 
			
		||||
    if (!traits.get_supports_toggle()) {
 | 
			
		||||
      ESP_LOGW(TAG, "'%s': toggle unsupported", name);
 | 
			
		||||
      ESP_LOGW(TAG, "'%s' - This cover device does not support toggle!", this->parent_->get_name().c_str());
 | 
			
		||||
      this->toggle_.reset();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if (this->stop_) {
 | 
			
		||||
    if (this->position_.has_value() || this->tilt_.has_value() || this->toggle_.has_value()) {
 | 
			
		||||
      ESP_LOGW(TAG, "'%s': cannot position/tilt/toggle when stopping", name);
 | 
			
		||||
    if (this->position_.has_value()) {
 | 
			
		||||
      ESP_LOGW(TAG, "Cannot set position when stopping a cover!");
 | 
			
		||||
      this->position_.reset();
 | 
			
		||||
    }
 | 
			
		||||
    if (this->tilt_.has_value()) {
 | 
			
		||||
      ESP_LOGW(TAG, "Cannot set tilt when stopping a cover!");
 | 
			
		||||
      this->tilt_.reset();
 | 
			
		||||
    }
 | 
			
		||||
    if (this->toggle_.has_value()) {
 | 
			
		||||
      ESP_LOGW(TAG, "Cannot set toggle when stopping a cover!");
 | 
			
		||||
      this->toggle_.reset();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -48,15 +48,6 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    if CORE.using_zephyr:
 | 
			
		||||
        zephyr_add_prj_conf("HWINFO", True)
 | 
			
		||||
        # gdb thread support
 | 
			
		||||
        zephyr_add_prj_conf("DEBUG_THREAD_INFO", True)
 | 
			
		||||
        # RTT
 | 
			
		||||
        zephyr_add_prj_conf("USE_SEGGER_RTT", True)
 | 
			
		||||
        zephyr_add_prj_conf("RTT_CONSOLE", True)
 | 
			
		||||
        zephyr_add_prj_conf("LOG", True)
 | 
			
		||||
        zephyr_add_prj_conf("LOG_BLOCK_IN_THREAD", True)
 | 
			
		||||
        zephyr_add_prj_conf("LOG_BUFFER_SIZE", 4096)
 | 
			
		||||
        zephyr_add_prj_conf("SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL", True)
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,5 +1,4 @@
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#include "driver/gpio.h"
 | 
			
		||||
#include "deep_sleep_component.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
@@ -75,20 +74,11 @@ void DeepSleepComponent::deep_sleep_() {
 | 
			
		||||
  if (this->sleep_duration_.has_value())
 | 
			
		||||
    esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
 | 
			
		||||
  if (this->wakeup_pin_ != nullptr) {
 | 
			
		||||
    const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
 | 
			
		||||
    if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLUP) {
 | 
			
		||||
      gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
 | 
			
		||||
    } else if (this->wakeup_pin_->get_flags() & gpio::FLAG_PULLDOWN) {
 | 
			
		||||
      gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
 | 
			
		||||
    }
 | 
			
		||||
    gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
 | 
			
		||||
    gpio_hold_en(gpio_pin);
 | 
			
		||||
    gpio_deep_sleep_hold_en();
 | 
			
		||||
    bool level = !this->wakeup_pin_->is_inverted();
 | 
			
		||||
    if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_INVERT_WAKEUP && this->wakeup_pin_->digital_read()) {
 | 
			
		||||
      level = !level;
 | 
			
		||||
    }
 | 
			
		||||
    esp_sleep_enable_ext0_wakeup(gpio_pin, level);
 | 
			
		||||
    esp_sleep_enable_ext0_wakeup(gpio_num_t(this->wakeup_pin_->get_pin()), level);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->ext1_wakeup_.has_value()) {
 | 
			
		||||
    esp_sleep_enable_ext1_wakeup(this->ext1_wakeup_->mask, this->ext1_wakeup_->wakeup_mode);
 | 
			
		||||
@@ -112,15 +102,6 @@ void DeepSleepComponent::deep_sleep_() {
 | 
			
		||||
  if (this->sleep_duration_.has_value())
 | 
			
		||||
    esp_sleep_enable_timer_wakeup(*this->sleep_duration_);
 | 
			
		||||
  if (this->wakeup_pin_ != nullptr) {
 | 
			
		||||
    const auto gpio_pin = gpio_num_t(this->wakeup_pin_->get_pin());
 | 
			
		||||
    if (this->wakeup_pin_->get_flags() && gpio::FLAG_PULLUP) {
 | 
			
		||||
      gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLUP_ONLY);
 | 
			
		||||
    } else if (this->wakeup_pin_->get_flags() && gpio::FLAG_PULLDOWN) {
 | 
			
		||||
      gpio_sleep_set_pull_mode(gpio_pin, GPIO_PULLDOWN_ONLY);
 | 
			
		||||
    }
 | 
			
		||||
    gpio_sleep_set_direction(gpio_pin, GPIO_MODE_INPUT);
 | 
			
		||||
    gpio_hold_en(gpio_pin);
 | 
			
		||||
    gpio_deep_sleep_hold_en();
 | 
			
		||||
    bool level = !this->wakeup_pin_->is_inverted();
 | 
			
		||||
    if (this->wakeup_pin_mode_ == WAKEUP_PIN_MODE_INVERT_WAKEUP && this->wakeup_pin_->digital_read()) {
 | 
			
		||||
      level = !level;
 | 
			
		||||
 
 | 
			
		||||
@@ -12,8 +12,6 @@ from esphome.const import (
 | 
			
		||||
    CONF_ROTATION,
 | 
			
		||||
    CONF_TO,
 | 
			
		||||
    CONF_TRIGGER_ID,
 | 
			
		||||
    CONF_UPDATE_INTERVAL,
 | 
			
		||||
    SCHEDULER_DONT_RUN,
 | 
			
		||||
)
 | 
			
		||||
from esphome.core import coroutine_with_priority
 | 
			
		||||
 | 
			
		||||
@@ -69,18 +67,6 @@ BASIC_DISPLAY_SCHEMA = cv.Schema(
 | 
			
		||||
    }
 | 
			
		||||
).extend(cv.polling_component_schema("1s"))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _validate_test_card(config):
 | 
			
		||||
    if (
 | 
			
		||||
        config.get(CONF_SHOW_TEST_CARD, False)
 | 
			
		||||
        and config.get(CONF_UPDATE_INTERVAL, False) == SCHEDULER_DONT_RUN
 | 
			
		||||
    ):
 | 
			
		||||
        raise cv.Invalid(
 | 
			
		||||
            f"`{CONF_SHOW_TEST_CARD}: True` cannot be used with `{CONF_UPDATE_INTERVAL}: never` because this combination will not show a test_card."
 | 
			
		||||
        )
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
FULL_DISPLAY_SCHEMA = BASIC_DISPLAY_SCHEMA.extend(
 | 
			
		||||
    {
 | 
			
		||||
        cv.Optional(CONF_ROTATION): validate_rotation,
 | 
			
		||||
@@ -108,7 +94,6 @@ FULL_DISPLAY_SCHEMA = BASIC_DISPLAY_SCHEMA.extend(
 | 
			
		||||
        cv.Optional(CONF_SHOW_TEST_CARD): cv.boolean,
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
FULL_DISPLAY_SCHEMA.add_extra(_validate_test_card)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def setup_display_core_(var, config):
 | 
			
		||||
@@ -215,6 +200,7 @@ async def display_is_displaying_page_to_code(config, condition_id, template_arg,
 | 
			
		||||
    page = await cg.get_variable(config[CONF_PAGE_ID])
 | 
			
		||||
    var = cg.new_Pvariable(condition_id, template_arg, paren)
 | 
			
		||||
    cg.add(var.set_page(page))
 | 
			
		||||
 | 
			
		||||
    return var
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,6 @@ from esphome.const import (
 | 
			
		||||
    CONF_FRAMEWORK,
 | 
			
		||||
    CONF_IGNORE_EFUSE_CUSTOM_MAC,
 | 
			
		||||
    CONF_IGNORE_EFUSE_MAC_CRC,
 | 
			
		||||
    CONF_LOG_LEVEL,
 | 
			
		||||
    CONF_NAME,
 | 
			
		||||
    CONF_PATH,
 | 
			
		||||
    CONF_PLATFORM_VERSION,
 | 
			
		||||
@@ -80,15 +79,6 @@ CONF_ENABLE_LWIP_ASSERT = "enable_lwip_assert"
 | 
			
		||||
CONF_EXECUTE_FROM_PSRAM = "execute_from_psram"
 | 
			
		||||
CONF_RELEASE = "release"
 | 
			
		||||
 | 
			
		||||
LOG_LEVELS_IDF = [
 | 
			
		||||
    "NONE",
 | 
			
		||||
    "ERROR",
 | 
			
		||||
    "WARN",
 | 
			
		||||
    "INFO",
 | 
			
		||||
    "DEBUG",
 | 
			
		||||
    "VERBOSE",
 | 
			
		||||
]
 | 
			
		||||
 | 
			
		||||
ASSERTION_LEVELS = {
 | 
			
		||||
    "DISABLE": "CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_DISABLE",
 | 
			
		||||
    "ENABLE": "CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE",
 | 
			
		||||
@@ -633,9 +623,6 @@ ESP_IDF_FRAMEWORK_SCHEMA = cv.All(
 | 
			
		||||
            cv.Optional(CONF_SDKCONFIG_OPTIONS, default={}): {
 | 
			
		||||
                cv.string_strict: cv.string_strict
 | 
			
		||||
            },
 | 
			
		||||
            cv.Optional(CONF_LOG_LEVEL, default="ERROR"): cv.one_of(
 | 
			
		||||
                *LOG_LEVELS_IDF, upper=True
 | 
			
		||||
            ),
 | 
			
		||||
            cv.Optional(CONF_ADVANCED, default={}): cv.Schema(
 | 
			
		||||
                {
 | 
			
		||||
                    cv.Optional(CONF_ASSERTION_LEVEL): cv.one_of(
 | 
			
		||||
@@ -824,9 +811,8 @@ async def to_code(config):
 | 
			
		||||
    cg.set_cpp_standard("gnu++20")
 | 
			
		||||
    cg.add_build_flag("-DUSE_ESP32")
 | 
			
		||||
    cg.add_define("ESPHOME_BOARD", config[CONF_BOARD])
 | 
			
		||||
    variant = config[CONF_VARIANT]
 | 
			
		||||
    cg.add_build_flag(f"-DUSE_ESP32_VARIANT_{variant}")
 | 
			
		||||
    cg.add_define("ESPHOME_VARIANT", VARIANT_FRIENDLY[variant])
 | 
			
		||||
    cg.add_build_flag(f"-DUSE_ESP32_VARIANT_{config[CONF_VARIANT]}")
 | 
			
		||||
    cg.add_define("ESPHOME_VARIANT", VARIANT_FRIENDLY[config[CONF_VARIANT]])
 | 
			
		||||
    cg.add_define(ThreadModel.MULTI_ATOMICS)
 | 
			
		||||
 | 
			
		||||
    cg.add_platformio_option("lib_ldf_mode", "off")
 | 
			
		||||
@@ -860,7 +846,6 @@ async def to_code(config):
 | 
			
		||||
        cg.add_platformio_option(
 | 
			
		||||
            "platform_packages", ["espressif/toolchain-esp32ulp@2.35.0-20220830"]
 | 
			
		||||
        )
 | 
			
		||||
        add_idf_sdkconfig_option(f"CONFIG_IDF_TARGET_{variant}", True)
 | 
			
		||||
        add_idf_sdkconfig_option(
 | 
			
		||||
            f"CONFIG_ESPTOOLPY_FLASHSIZE_{config[CONF_FLASH_SIZE]}", True
 | 
			
		||||
        )
 | 
			
		||||
@@ -952,10 +937,6 @@ async def to_code(config):
 | 
			
		||||
            ),
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        add_idf_sdkconfig_option(
 | 
			
		||||
            f"CONFIG_LOG_DEFAULT_LEVEL_{conf[CONF_LOG_LEVEL]}", True
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        for name, value in conf[CONF_SDKCONFIG_OPTIONS].items():
 | 
			
		||||
            add_idf_sdkconfig_option(name, RawSdkconfigValue(value))
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,6 @@ import esphome.final_validate as fv
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["esp32"]
 | 
			
		||||
CODEOWNERS = ["@jesserockz", "@Rapsssito", "@bdraco"]
 | 
			
		||||
DOMAIN = "esp32_ble"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class BTLoggers(Enum):
 | 
			
		||||
@@ -116,11 +115,9 @@ def register_bt_logger(*loggers: BTLoggers) -> None:
 | 
			
		||||
 | 
			
		||||
CONF_BLE_ID = "ble_id"
 | 
			
		||||
CONF_IO_CAPABILITY = "io_capability"
 | 
			
		||||
CONF_ADVERTISING = "advertising"
 | 
			
		||||
CONF_ADVERTISING_CYCLE_TIME = "advertising_cycle_time"
 | 
			
		||||
CONF_DISABLE_BT_LOGS = "disable_bt_logs"
 | 
			
		||||
CONF_CONNECTION_TIMEOUT = "connection_timeout"
 | 
			
		||||
CONF_MAX_NOTIFICATIONS = "max_notifications"
 | 
			
		||||
 | 
			
		||||
NO_BLUETOOTH_VARIANTS = [const.VARIANT_ESP32S2]
 | 
			
		||||
 | 
			
		||||
@@ -165,7 +162,6 @@ CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
            IO_CAPABILITY, lower=True
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_ENABLE_ON_BOOT, default=True): cv.boolean,
 | 
			
		||||
        cv.Optional(CONF_ADVERTISING, default=False): cv.boolean,
 | 
			
		||||
        cv.Optional(
 | 
			
		||||
            CONF_ADVERTISING_CYCLE_TIME, default="10s"
 | 
			
		||||
        ): cv.positive_time_period_milliseconds,
 | 
			
		||||
@@ -177,11 +173,6 @@ CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
            cv.positive_time_period_seconds,
 | 
			
		||||
            cv.Range(min=TimePeriod(seconds=10), max=TimePeriod(seconds=180)),
 | 
			
		||||
        ),
 | 
			
		||||
        cv.SplitDefault(CONF_MAX_NOTIFICATIONS, esp32_idf=12): cv.All(
 | 
			
		||||
            cv.only_with_esp_idf,
 | 
			
		||||
            cv.positive_int,
 | 
			
		||||
            cv.Range(min=1, max=64),
 | 
			
		||||
        ),
 | 
			
		||||
    }
 | 
			
		||||
).extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
 | 
			
		||||
@@ -281,21 +272,8 @@ async def to_code(config):
 | 
			
		||||
                "CONFIG_BT_BLE_ESTAB_LINK_CONN_TOUT", timeout_seconds
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
        # Set the maximum number of notification registrations
 | 
			
		||||
        # This controls how many BLE characteristics can have notifications enabled
 | 
			
		||||
        # across all connections for a single GATT client interface
 | 
			
		||||
        # https://github.com/esphome/issues/issues/6808
 | 
			
		||||
        if CONF_MAX_NOTIFICATIONS in config:
 | 
			
		||||
            add_idf_sdkconfig_option(
 | 
			
		||||
                "CONFIG_BT_GATTC_NOTIF_REG_MAX", config[CONF_MAX_NOTIFICATIONS]
 | 
			
		||||
            )
 | 
			
		||||
 | 
			
		||||
    cg.add_define("USE_ESP32_BLE")
 | 
			
		||||
 | 
			
		||||
    if config[CONF_ADVERTISING]:
 | 
			
		||||
        cg.add_define("USE_ESP32_BLE_ADVERTISING")
 | 
			
		||||
        cg.add_define("USE_ESP32_BLE_UUID")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@automation.register_condition("ble.enabled", BLEEnabledCondition, cv.Schema({}))
 | 
			
		||||
async def ble_enabled_to_code(config, condition_id, template_arg, args):
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,7 @@
 | 
			
		||||
#include "ble.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
 | 
			
		||||
#include "ble.h"
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/application.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
@@ -53,7 +53,6 @@ void ESP32BLE::disable() {
 | 
			
		||||
 | 
			
		||||
bool ESP32BLE::is_active() { return this->state_ == BLE_COMPONENT_STATE_ACTIVE; }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
void ESP32BLE::advertising_start() {
 | 
			
		||||
  this->advertising_init_();
 | 
			
		||||
  if (!this->is_active())
 | 
			
		||||
@@ -89,7 +88,6 @@ void ESP32BLE::advertising_remove_service_uuid(ESPBTUUID uuid) {
 | 
			
		||||
  this->advertising_->remove_service_uuid(uuid);
 | 
			
		||||
  this->advertising_start();
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
bool ESP32BLE::ble_pre_setup_() {
 | 
			
		||||
  esp_err_t err = nvs_flash_init();
 | 
			
		||||
@@ -100,7 +98,6 @@ bool ESP32BLE::ble_pre_setup_() {
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
void ESP32BLE::advertising_init_() {
 | 
			
		||||
  if (this->advertising_ != nullptr)
 | 
			
		||||
    return;
 | 
			
		||||
@@ -110,7 +107,6 @@ void ESP32BLE::advertising_init_() {
 | 
			
		||||
  this->advertising_->set_min_preferred_interval(0x06);
 | 
			
		||||
  this->advertising_->set_appearance(this->appearance_);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
bool ESP32BLE::ble_setup_() {
 | 
			
		||||
  esp_err_t err;
 | 
			
		||||
@@ -306,7 +302,7 @@ void ESP32BLE::loop() {
 | 
			
		||||
      case BLEEvent::GATTS: {
 | 
			
		||||
        esp_gatts_cb_event_t event = ble_event->event_.gatts.gatts_event;
 | 
			
		||||
        esp_gatt_if_t gatts_if = ble_event->event_.gatts.gatts_if;
 | 
			
		||||
        esp_ble_gatts_cb_param_t *param = &ble_event->event_.gatts.gatts_param;
 | 
			
		||||
        esp_ble_gatts_cb_param_t *param = ble_event->event_.gatts.gatts_param;
 | 
			
		||||
        ESP_LOGV(TAG, "gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
 | 
			
		||||
        for (auto *gatts_handler : this->gatts_event_handlers_) {
 | 
			
		||||
          gatts_handler->gatts_event_handler(event, gatts_if, param);
 | 
			
		||||
@@ -316,7 +312,7 @@ void ESP32BLE::loop() {
 | 
			
		||||
      case BLEEvent::GATTC: {
 | 
			
		||||
        esp_gattc_cb_event_t event = ble_event->event_.gattc.gattc_event;
 | 
			
		||||
        esp_gatt_if_t gattc_if = ble_event->event_.gattc.gattc_if;
 | 
			
		||||
        esp_ble_gattc_cb_param_t *param = &ble_event->event_.gattc.gattc_param;
 | 
			
		||||
        esp_ble_gattc_cb_param_t *param = ble_event->event_.gattc.gattc_param;
 | 
			
		||||
        ESP_LOGV(TAG, "gattc_event [esp_gatt_if: %d] - %d", gattc_if, event);
 | 
			
		||||
        for (auto *gattc_handler : this->gattc_event_handlers_) {
 | 
			
		||||
          gattc_handler->gattc_event_handler(event, gattc_if, param);
 | 
			
		||||
@@ -398,11 +394,9 @@ void ESP32BLE::loop() {
 | 
			
		||||
    this->ble_event_pool_.release(ble_event);
 | 
			
		||||
    ble_event = this->ble_events_.pop();
 | 
			
		||||
  }
 | 
			
		||||
#ifdef USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
  if (this->advertising_ != nullptr) {
 | 
			
		||||
    this->advertising_->loop();
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // Log dropped events periodically
 | 
			
		||||
  uint16_t dropped = this->ble_events_.get_and_reset_dropped_count();
 | 
			
		||||
 
 | 
			
		||||
@@ -1,17 +1,14 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/defines.h"  // Must be included before conditional includes
 | 
			
		||||
 | 
			
		||||
#include "ble_advertising.h"
 | 
			
		||||
#include "ble_uuid.h"
 | 
			
		||||
#include "ble_scan_result.h"
 | 
			
		||||
#ifdef USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
#include "ble_advertising.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <functional>
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/automation.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
 | 
			
		||||
#include "ble_event.h"
 | 
			
		||||
@@ -109,7 +106,6 @@ class ESP32BLE : public Component {
 | 
			
		||||
  float get_setup_priority() const override;
 | 
			
		||||
  void set_name(const std::string &name) { this->name_ = name; }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
  void advertising_start();
 | 
			
		||||
  void advertising_set_service_data(const std::vector<uint8_t> &data);
 | 
			
		||||
  void advertising_set_manufacturer_data(const std::vector<uint8_t> &data);
 | 
			
		||||
@@ -117,7 +113,6 @@ class ESP32BLE : public Component {
 | 
			
		||||
  void advertising_add_service_uuid(ESPBTUUID uuid);
 | 
			
		||||
  void advertising_remove_service_uuid(ESPBTUUID uuid);
 | 
			
		||||
  void advertising_register_raw_advertisement_callback(std::function<void(bool)> &&callback);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  void register_gap_event_handler(GAPEventHandler *handler) { this->gap_event_handlers_.push_back(handler); }
 | 
			
		||||
  void register_gap_scan_event_handler(GAPScanEventHandler *handler) {
 | 
			
		||||
@@ -138,9 +133,7 @@ class ESP32BLE : public Component {
 | 
			
		||||
  bool ble_setup_();
 | 
			
		||||
  bool ble_dismantle_();
 | 
			
		||||
  bool ble_pre_setup_();
 | 
			
		||||
#ifdef USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
  void advertising_init_();
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  template<typename... Args> friend void enqueue_ble_event(Args... args);
 | 
			
		||||
@@ -160,9 +153,7 @@ class ESP32BLE : public Component {
 | 
			
		||||
  optional<std::string> name_;
 | 
			
		||||
 | 
			
		||||
  // 4-byte aligned members
 | 
			
		||||
#ifdef USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
  BLEAdvertising *advertising_{};  // 4 bytes (pointer)
 | 
			
		||||
#endif
 | 
			
		||||
  BLEAdvertising *advertising_{};             // 4 bytes (pointer)
 | 
			
		||||
  esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};  // 4 bytes (enum)
 | 
			
		||||
  uint32_t advertising_cycle_time_{};         // 4 bytes
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
#include "ble_advertising.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#ifdef USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
#include <cstring>
 | 
			
		||||
@@ -162,5 +161,4 @@ void BLEAdvertising::register_raw_advertisement_callback(std::function<void(bool
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,10 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
 | 
			
		||||
#include <array>
 | 
			
		||||
#include <functional>
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#ifdef USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
 | 
			
		||||
#include <esp_bt.h>
 | 
			
		||||
#include <esp_gap_ble_api.h>
 | 
			
		||||
@@ -59,5 +56,4 @@ class BLEAdvertising {
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_BLE_ADVERTISING
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -3,7 +3,8 @@
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
 | 
			
		||||
#include <cstddef>  // for offsetof
 | 
			
		||||
#include <cstring>  // for memcpy
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
#include <esp_gap_ble_api.h>
 | 
			
		||||
#include <esp_gattc_api.h>
 | 
			
		||||
#include <esp_gatts_api.h>
 | 
			
		||||
@@ -61,24 +62,10 @@ static_assert(offsetof(esp_ble_gap_cb_param_t, read_rssi_cmpl.rssi) == sizeof(es
 | 
			
		||||
static_assert(offsetof(esp_ble_gap_cb_param_t, read_rssi_cmpl.remote_addr) == sizeof(esp_bt_status_t) + sizeof(int8_t),
 | 
			
		||||
              "remote_addr must follow rssi in read_rssi_cmpl");
 | 
			
		||||
 | 
			
		||||
// Param struct sizes on ESP32
 | 
			
		||||
static constexpr size_t GATTC_PARAM_SIZE = 28;
 | 
			
		||||
static constexpr size_t GATTS_PARAM_SIZE = 32;
 | 
			
		||||
 | 
			
		||||
// Maximum size for inline storage of data
 | 
			
		||||
// GATTC: 80 - 28 (param) - 8 (other fields) = 44 bytes for data
 | 
			
		||||
// GATTS: 80 - 32 (param) - 8 (other fields) = 40 bytes for data
 | 
			
		||||
static constexpr size_t GATTC_INLINE_DATA_SIZE = 44;
 | 
			
		||||
static constexpr size_t GATTS_INLINE_DATA_SIZE = 40;
 | 
			
		||||
 | 
			
		||||
// Verify param struct sizes
 | 
			
		||||
static_assert(sizeof(esp_ble_gattc_cb_param_t) == GATTC_PARAM_SIZE, "GATTC param size unexpected");
 | 
			
		||||
static_assert(sizeof(esp_ble_gatts_cb_param_t) == GATTS_PARAM_SIZE, "GATTS param size unexpected");
 | 
			
		||||
 | 
			
		||||
// Received GAP, GATTC and GATTS events are only queued, and get processed in the main loop().
 | 
			
		||||
// This class stores each event with minimal memory usage.
 | 
			
		||||
// GAP events (99% of traffic) don't have the heap allocation overhead.
 | 
			
		||||
// GATTC/GATTS events use heap allocation for their param and inline storage for small data.
 | 
			
		||||
// GAP events (99% of traffic) don't have the vector overhead.
 | 
			
		||||
// GATTC/GATTS events use heap allocation for their param and data.
 | 
			
		||||
//
 | 
			
		||||
// Event flow:
 | 
			
		||||
// 1. ESP-IDF BLE stack calls our static handlers in the BLE task context
 | 
			
		||||
@@ -125,21 +112,21 @@ class BLEEvent {
 | 
			
		||||
    this->init_gap_data_(e, p);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Constructor for GATTC events - param stored inline, data may use heap
 | 
			
		||||
  // IMPORTANT: We MUST copy the param struct because the pointer from ESP-IDF
 | 
			
		||||
  // is only valid during the callback execution. Since BLE events are processed
 | 
			
		||||
  // asynchronously in the main loop, we store our own copy inline to ensure
 | 
			
		||||
  // the data remains valid until the event is processed.
 | 
			
		||||
  // Constructor for GATTC events - uses heap allocation
 | 
			
		||||
  // IMPORTANT: The heap allocation is REQUIRED and must not be removed as an optimization.
 | 
			
		||||
  // The param pointer from ESP-IDF is only valid during the callback execution.
 | 
			
		||||
  // Since BLE events are processed asynchronously in the main loop, we must create
 | 
			
		||||
  // our own copy to ensure the data remains valid until the event is processed.
 | 
			
		||||
  BLEEvent(esp_gattc_cb_event_t e, esp_gatt_if_t i, esp_ble_gattc_cb_param_t *p) {
 | 
			
		||||
    this->type_ = GATTC;
 | 
			
		||||
    this->init_gattc_data_(e, i, p);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Constructor for GATTS events - param stored inline, data may use heap
 | 
			
		||||
  // IMPORTANT: We MUST copy the param struct because the pointer from ESP-IDF
 | 
			
		||||
  // is only valid during the callback execution. Since BLE events are processed
 | 
			
		||||
  // asynchronously in the main loop, we store our own copy inline to ensure
 | 
			
		||||
  // the data remains valid until the event is processed.
 | 
			
		||||
  // Constructor for GATTS events - uses heap allocation
 | 
			
		||||
  // IMPORTANT: The heap allocation is REQUIRED and must not be removed as an optimization.
 | 
			
		||||
  // The param pointer from ESP-IDF is only valid during the callback execution.
 | 
			
		||||
  // Since BLE events are processed asynchronously in the main loop, we must create
 | 
			
		||||
  // our own copy to ensure the data remains valid until the event is processed.
 | 
			
		||||
  BLEEvent(esp_gatts_cb_event_t e, esp_gatt_if_t i, esp_ble_gatts_cb_param_t *p) {
 | 
			
		||||
    this->type_ = GATTS;
 | 
			
		||||
    this->init_gatts_data_(e, i, p);
 | 
			
		||||
@@ -149,32 +136,25 @@ class BLEEvent {
 | 
			
		||||
  ~BLEEvent() { this->release(); }
 | 
			
		||||
 | 
			
		||||
  // Default constructor for pre-allocation in pool
 | 
			
		||||
  BLEEvent() : event_{}, type_(GAP) {}
 | 
			
		||||
  BLEEvent() : type_(GAP) {}
 | 
			
		||||
 | 
			
		||||
  // Invoked on return to EventPool - clean up any heap-allocated data
 | 
			
		||||
  void release() {
 | 
			
		||||
    switch (this->type_) {
 | 
			
		||||
      case GAP:
 | 
			
		||||
        // GAP events don't have heap allocations
 | 
			
		||||
        break;
 | 
			
		||||
      case GATTC:
 | 
			
		||||
        // Param is now stored inline, only delete heap data if it was heap-allocated
 | 
			
		||||
        if (!this->event_.gattc.is_inline && this->event_.gattc.data.heap_data != nullptr) {
 | 
			
		||||
          delete[] this->event_.gattc.data.heap_data;
 | 
			
		||||
        }
 | 
			
		||||
        // Clear critical fields to prevent issues if type changes
 | 
			
		||||
        this->event_.gattc.is_inline = false;
 | 
			
		||||
        this->event_.gattc.data.heap_data = nullptr;
 | 
			
		||||
        break;
 | 
			
		||||
      case GATTS:
 | 
			
		||||
        // Param is now stored inline, only delete heap data if it was heap-allocated
 | 
			
		||||
        if (!this->event_.gatts.is_inline && this->event_.gatts.data.heap_data != nullptr) {
 | 
			
		||||
          delete[] this->event_.gatts.data.heap_data;
 | 
			
		||||
        }
 | 
			
		||||
        // Clear critical fields to prevent issues if type changes
 | 
			
		||||
        this->event_.gatts.is_inline = false;
 | 
			
		||||
        this->event_.gatts.data.heap_data = nullptr;
 | 
			
		||||
        break;
 | 
			
		||||
    if (this->type_ == GAP) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    if (this->type_ == GATTC) {
 | 
			
		||||
      delete this->event_.gattc.gattc_param;
 | 
			
		||||
      delete this->event_.gattc.data;
 | 
			
		||||
      this->event_.gattc.gattc_param = nullptr;
 | 
			
		||||
      this->event_.gattc.data = nullptr;
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    if (this->type_ == GATTS) {
 | 
			
		||||
      delete this->event_.gatts.gatts_param;
 | 
			
		||||
      delete this->event_.gatts.data;
 | 
			
		||||
      this->event_.gatts.gatts_param = nullptr;
 | 
			
		||||
      this->event_.gatts.data = nullptr;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -226,30 +206,20 @@ class BLEEvent {
 | 
			
		||||
 | 
			
		||||
    // NOLINTNEXTLINE(readability-identifier-naming)
 | 
			
		||||
    struct gattc_event {
 | 
			
		||||
      esp_ble_gattc_cb_param_t gattc_param;  // Stored inline (28 bytes)
 | 
			
		||||
      esp_gattc_cb_event_t gattc_event;      // 4 bytes
 | 
			
		||||
      union {
 | 
			
		||||
        uint8_t *heap_data;                           // 4 bytes when heap-allocated
 | 
			
		||||
        uint8_t inline_data[GATTC_INLINE_DATA_SIZE];  // 44 bytes when stored inline
 | 
			
		||||
      } data;                                         // 44 bytes total
 | 
			
		||||
      uint16_t data_len;                              // 2 bytes
 | 
			
		||||
      esp_gatt_if_t gattc_if;                         // 1 byte
 | 
			
		||||
      bool is_inline;                                 // 1 byte - true when data is stored inline
 | 
			
		||||
    } gattc;                                          // Total: 80 bytes
 | 
			
		||||
      esp_gattc_cb_event_t gattc_event;
 | 
			
		||||
      esp_gatt_if_t gattc_if;
 | 
			
		||||
      esp_ble_gattc_cb_param_t *gattc_param;  // Heap-allocated
 | 
			
		||||
      std::vector<uint8_t> *data;             // Heap-allocated
 | 
			
		||||
    } gattc;                                  // 16 bytes (pointers only)
 | 
			
		||||
 | 
			
		||||
    // NOLINTNEXTLINE(readability-identifier-naming)
 | 
			
		||||
    struct gatts_event {
 | 
			
		||||
      esp_ble_gatts_cb_param_t gatts_param;  // Stored inline (32 bytes)
 | 
			
		||||
      esp_gatts_cb_event_t gatts_event;      // 4 bytes
 | 
			
		||||
      union {
 | 
			
		||||
        uint8_t *heap_data;                           // 4 bytes when heap-allocated
 | 
			
		||||
        uint8_t inline_data[GATTS_INLINE_DATA_SIZE];  // 40 bytes when stored inline
 | 
			
		||||
      } data;                                         // 40 bytes total
 | 
			
		||||
      uint16_t data_len;                              // 2 bytes
 | 
			
		||||
      esp_gatt_if_t gatts_if;                         // 1 byte
 | 
			
		||||
      bool is_inline;                                 // 1 byte - true when data is stored inline
 | 
			
		||||
    } gatts;                                          // Total: 80 bytes
 | 
			
		||||
  } event_;                                           // 80 bytes
 | 
			
		||||
      esp_gatts_cb_event_t gatts_event;
 | 
			
		||||
      esp_gatt_if_t gatts_if;
 | 
			
		||||
      esp_ble_gatts_cb_param_t *gatts_param;  // Heap-allocated
 | 
			
		||||
      std::vector<uint8_t> *data;             // Heap-allocated
 | 
			
		||||
    } gatts;                                  // 16 bytes (pointers only)
 | 
			
		||||
  } event_;                                   // 80 bytes
 | 
			
		||||
 | 
			
		||||
  ble_event_t type_;
 | 
			
		||||
 | 
			
		||||
@@ -263,29 +233,6 @@ class BLEEvent {
 | 
			
		||||
  const esp_ble_sec_t &security() const { return event_.gap.security; }
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  // Helper to copy data with inline storage optimization
 | 
			
		||||
  template<typename EventStruct, size_t InlineSize>
 | 
			
		||||
  void copy_data_with_inline_storage_(EventStruct &event, const uint8_t *src_data, uint16_t len,
 | 
			
		||||
                                      uint8_t **param_value_ptr) {
 | 
			
		||||
    event.data_len = len;
 | 
			
		||||
    if (len > 0) {
 | 
			
		||||
      if (len <= InlineSize) {
 | 
			
		||||
        event.is_inline = true;
 | 
			
		||||
        memcpy(event.data.inline_data, src_data, len);
 | 
			
		||||
        *param_value_ptr = event.data.inline_data;
 | 
			
		||||
      } else {
 | 
			
		||||
        event.is_inline = false;
 | 
			
		||||
        event.data.heap_data = new uint8_t[len];
 | 
			
		||||
        memcpy(event.data.heap_data, src_data, len);
 | 
			
		||||
        *param_value_ptr = event.data.heap_data;
 | 
			
		||||
      }
 | 
			
		||||
    } else {
 | 
			
		||||
      event.is_inline = false;
 | 
			
		||||
      event.data.heap_data = nullptr;
 | 
			
		||||
      *param_value_ptr = nullptr;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Initialize GAP event data
 | 
			
		||||
  void init_gap_data_(esp_gap_ble_cb_event_t e, esp_ble_gap_cb_param_t *p) {
 | 
			
		||||
    this->event_.gap.gap_event = e;
 | 
			
		||||
@@ -370,38 +317,35 @@ class BLEEvent {
 | 
			
		||||
    this->event_.gattc.gattc_if = i;
 | 
			
		||||
 | 
			
		||||
    if (p == nullptr) {
 | 
			
		||||
      // Zero out the param struct when null
 | 
			
		||||
      memset(&this->event_.gattc.gattc_param, 0, sizeof(this->event_.gattc.gattc_param));
 | 
			
		||||
      this->event_.gattc.is_inline = false;
 | 
			
		||||
      this->event_.gattc.data.heap_data = nullptr;
 | 
			
		||||
      this->event_.gattc.data_len = 0;
 | 
			
		||||
      this->event_.gattc.gattc_param = nullptr;
 | 
			
		||||
      this->event_.gattc.data = nullptr;
 | 
			
		||||
      return;  // Invalid event, but we can't log in header file
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Copy param struct inline (no heap allocation!)
 | 
			
		||||
    // GATTC/GATTS events are rare (<1% of events) but we can still store them inline
 | 
			
		||||
    // along with small data payloads, eliminating all heap allocations for typical BLE operations
 | 
			
		||||
    // CRITICAL: This copy is REQUIRED for memory safety - the ESP-IDF param pointer
 | 
			
		||||
    // is only valid during the callback and will be reused/freed after we return
 | 
			
		||||
    this->event_.gattc.gattc_param = *p;
 | 
			
		||||
    // Heap-allocate param and data
 | 
			
		||||
    // Heap allocation is used because GATTC/GATTS events are rare (<1% of events)
 | 
			
		||||
    // while GAP events (99%) are stored inline to minimize memory usage
 | 
			
		||||
    // IMPORTANT: This heap allocation provides clear ownership semantics:
 | 
			
		||||
    // - The BLEEvent owns the allocated memory for its lifetime
 | 
			
		||||
    // - The data remains valid from the BLE callback context until processed in the main loop
 | 
			
		||||
    // - Without this copy, we'd have use-after-free bugs as ESP-IDF reuses the callback memory
 | 
			
		||||
    this->event_.gattc.gattc_param = new esp_ble_gattc_cb_param_t(*p);
 | 
			
		||||
 | 
			
		||||
    // Copy data for events that need it
 | 
			
		||||
    // The param struct contains pointers (e.g., notify.value) that point to temporary buffers.
 | 
			
		||||
    // We must copy this data to ensure it remains valid when the event is processed later.
 | 
			
		||||
    switch (e) {
 | 
			
		||||
      case ESP_GATTC_NOTIFY_EVT:
 | 
			
		||||
        copy_data_with_inline_storage_<decltype(this->event_.gattc), GATTC_INLINE_DATA_SIZE>(
 | 
			
		||||
            this->event_.gattc, p->notify.value, p->notify.value_len, &this->event_.gattc.gattc_param.notify.value);
 | 
			
		||||
        this->event_.gattc.data = new std::vector<uint8_t>(p->notify.value, p->notify.value + p->notify.value_len);
 | 
			
		||||
        this->event_.gattc.gattc_param->notify.value = this->event_.gattc.data->data();
 | 
			
		||||
        break;
 | 
			
		||||
      case ESP_GATTC_READ_CHAR_EVT:
 | 
			
		||||
      case ESP_GATTC_READ_DESCR_EVT:
 | 
			
		||||
        copy_data_with_inline_storage_<decltype(this->event_.gattc), GATTC_INLINE_DATA_SIZE>(
 | 
			
		||||
            this->event_.gattc, p->read.value, p->read.value_len, &this->event_.gattc.gattc_param.read.value);
 | 
			
		||||
        this->event_.gattc.data = new std::vector<uint8_t>(p->read.value, p->read.value + p->read.value_len);
 | 
			
		||||
        this->event_.gattc.gattc_param->read.value = this->event_.gattc.data->data();
 | 
			
		||||
        break;
 | 
			
		||||
      default:
 | 
			
		||||
        this->event_.gattc.is_inline = false;
 | 
			
		||||
        this->event_.gattc.data.heap_data = nullptr;
 | 
			
		||||
        this->event_.gattc.data_len = 0;
 | 
			
		||||
        this->event_.gattc.data = nullptr;
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
@@ -412,33 +356,30 @@ class BLEEvent {
 | 
			
		||||
    this->event_.gatts.gatts_if = i;
 | 
			
		||||
 | 
			
		||||
    if (p == nullptr) {
 | 
			
		||||
      // Zero out the param struct when null
 | 
			
		||||
      memset(&this->event_.gatts.gatts_param, 0, sizeof(this->event_.gatts.gatts_param));
 | 
			
		||||
      this->event_.gatts.is_inline = false;
 | 
			
		||||
      this->event_.gatts.data.heap_data = nullptr;
 | 
			
		||||
      this->event_.gatts.data_len = 0;
 | 
			
		||||
      this->event_.gatts.gatts_param = nullptr;
 | 
			
		||||
      this->event_.gatts.data = nullptr;
 | 
			
		||||
      return;  // Invalid event, but we can't log in header file
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Copy param struct inline (no heap allocation!)
 | 
			
		||||
    // GATTC/GATTS events are rare (<1% of events) but we can still store them inline
 | 
			
		||||
    // along with small data payloads, eliminating all heap allocations for typical BLE operations
 | 
			
		||||
    // CRITICAL: This copy is REQUIRED for memory safety - the ESP-IDF param pointer
 | 
			
		||||
    // is only valid during the callback and will be reused/freed after we return
 | 
			
		||||
    this->event_.gatts.gatts_param = *p;
 | 
			
		||||
    // Heap-allocate param and data
 | 
			
		||||
    // Heap allocation is used because GATTC/GATTS events are rare (<1% of events)
 | 
			
		||||
    // while GAP events (99%) are stored inline to minimize memory usage
 | 
			
		||||
    // IMPORTANT: This heap allocation provides clear ownership semantics:
 | 
			
		||||
    // - The BLEEvent owns the allocated memory for its lifetime
 | 
			
		||||
    // - The data remains valid from the BLE callback context until processed in the main loop
 | 
			
		||||
    // - Without this copy, we'd have use-after-free bugs as ESP-IDF reuses the callback memory
 | 
			
		||||
    this->event_.gatts.gatts_param = new esp_ble_gatts_cb_param_t(*p);
 | 
			
		||||
 | 
			
		||||
    // Copy data for events that need it
 | 
			
		||||
    // The param struct contains pointers (e.g., write.value) that point to temporary buffers.
 | 
			
		||||
    // We must copy this data to ensure it remains valid when the event is processed later.
 | 
			
		||||
    switch (e) {
 | 
			
		||||
      case ESP_GATTS_WRITE_EVT:
 | 
			
		||||
        copy_data_with_inline_storage_<decltype(this->event_.gatts), GATTS_INLINE_DATA_SIZE>(
 | 
			
		||||
            this->event_.gatts, p->write.value, p->write.len, &this->event_.gatts.gatts_param.write.value);
 | 
			
		||||
        this->event_.gatts.data = new std::vector<uint8_t>(p->write.value, p->write.value + p->write.len);
 | 
			
		||||
        this->event_.gatts.gatts_param->write.value = this->event_.gatts.data->data();
 | 
			
		||||
        break;
 | 
			
		||||
      default:
 | 
			
		||||
        this->event_.gatts.is_inline = false;
 | 
			
		||||
        this->event_.gatts.data.heap_data = nullptr;
 | 
			
		||||
        this->event_.gatts.data_len = 0;
 | 
			
		||||
        this->event_.gatts.data = nullptr;
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
@@ -448,15 +389,6 @@ class BLEEvent {
 | 
			
		||||
// The gap member in the union should be 80 bytes (including the gap_event enum)
 | 
			
		||||
static_assert(sizeof(decltype(((BLEEvent *) nullptr)->event_.gap)) <= 80, "gap_event struct has grown beyond 80 bytes");
 | 
			
		||||
 | 
			
		||||
// Verify GATTC and GATTS structs don't exceed GAP struct size
 | 
			
		||||
// This ensures the union size is determined by GAP (the most common event type)
 | 
			
		||||
static_assert(sizeof(decltype(((BLEEvent *) nullptr)->event_.gattc)) <=
 | 
			
		||||
                  sizeof(decltype(((BLEEvent *) nullptr)->event_.gap)),
 | 
			
		||||
              "gattc_event struct exceeds gap_event size - union size would increase");
 | 
			
		||||
static_assert(sizeof(decltype(((BLEEvent *) nullptr)->event_.gatts)) <=
 | 
			
		||||
                  sizeof(decltype(((BLEEvent *) nullptr)->event_.gap)),
 | 
			
		||||
              "gatts_event struct exceeds gap_event size - union size would increase");
 | 
			
		||||
 | 
			
		||||
// Verify esp_ble_sec_t fits within our union
 | 
			
		||||
static_assert(sizeof(esp_ble_sec_t) <= 73, "esp_ble_sec_t is larger than BLEScanResult");
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
#include "ble_uuid.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#ifdef USE_ESP32_BLE_UUID
 | 
			
		||||
 | 
			
		||||
#include <cstring>
 | 
			
		||||
#include <cstdio>
 | 
			
		||||
@@ -191,5 +190,4 @@ std::string ESPBTUUID::to_string() const {
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_BLE_UUID
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -1,11 +1,9 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
#include "esphome/core/hal.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#ifdef USE_ESP32_BLE_UUID
 | 
			
		||||
 | 
			
		||||
#include <string>
 | 
			
		||||
#include <esp_bt_defs.h>
 | 
			
		||||
@@ -44,5 +42,4 @@ class ESPBTUUID {
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_BLE_UUID
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -65,8 +65,6 @@ FINAL_VALIDATE_SCHEMA = esp32_ble.validate_variant
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    cg.add_define("USE_ESP32_BLE_UUID")
 | 
			
		||||
 | 
			
		||||
    uuid = config[CONF_UUID].hex
 | 
			
		||||
    uuid_arr = [
 | 
			
		||||
        cg.RawExpression(f"0x{uuid[i : i + 2]}") for i in range(0, len(uuid), 2)
 | 
			
		||||
@@ -84,8 +82,6 @@ async def to_code(config):
 | 
			
		||||
    cg.add(var.set_measured_power(config[CONF_MEASURED_POWER]))
 | 
			
		||||
    cg.add(var.set_tx_power(config[CONF_TX_POWER]))
 | 
			
		||||
 | 
			
		||||
    cg.add_define("USE_ESP32_BLE_ADVERTISING")
 | 
			
		||||
 | 
			
		||||
    if CORE.using_esp_idf:
 | 
			
		||||
        add_idf_sdkconfig_option("CONFIG_BT_ENABLED", True)
 | 
			
		||||
        add_idf_sdkconfig_option("CONFIG_BT_BLE_42_FEATURES_SUPPORTED", True)
 | 
			
		||||
 
 | 
			
		||||
@@ -5,9 +5,9 @@
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
 | 
			
		||||
namespace esphome::esp32_ble_client {
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace esp32_ble_client {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "esp32_ble_client";
 | 
			
		||||
 | 
			
		||||
@@ -93,7 +93,7 @@ esp_err_t BLECharacteristic::write_value(uint8_t *new_val, int16_t new_val_size)
 | 
			
		||||
  return write_value(new_val, new_val_size, ESP_GATT_WRITE_TYPE_NO_RSP);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble_client
 | 
			
		||||
}  // namespace esp32_ble_client
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_BLE_DEVICE
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,6 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
 | 
			
		||||
 | 
			
		||||
@@ -11,7 +8,8 @@
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
namespace esphome::esp32_ble_client {
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace esp32_ble_client {
 | 
			
		||||
 | 
			
		||||
namespace espbt = esphome::esp32_ble_tracker;
 | 
			
		||||
 | 
			
		||||
@@ -35,7 +33,7 @@ class BLECharacteristic {
 | 
			
		||||
  BLEService *service;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble_client
 | 
			
		||||
}  // namespace esp32_ble_client
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_BLE_DEVICE
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,8 @@
 | 
			
		||||
#include <esp_gap_ble_api.h>
 | 
			
		||||
#include <esp_gatt_defs.h>
 | 
			
		||||
 | 
			
		||||
namespace esphome::esp32_ble_client {
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace esp32_ble_client {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "esp32_ble_client";
 | 
			
		||||
 | 
			
		||||
@@ -78,7 +79,40 @@ void BLEClientBase::dump_config() {
 | 
			
		||||
                "  Address: %s\n"
 | 
			
		||||
                "  Auto-Connect: %s",
 | 
			
		||||
                this->address_str().c_str(), TRUEFALSE(this->auto_connect_));
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  State: %s", espbt::client_state_to_string(this->state()));
 | 
			
		||||
  std::string state_name;
 | 
			
		||||
  switch (this->state()) {
 | 
			
		||||
    case espbt::ClientState::INIT:
 | 
			
		||||
      state_name = "INIT";
 | 
			
		||||
      break;
 | 
			
		||||
    case espbt::ClientState::DISCONNECTING:
 | 
			
		||||
      state_name = "DISCONNECTING";
 | 
			
		||||
      break;
 | 
			
		||||
    case espbt::ClientState::IDLE:
 | 
			
		||||
      state_name = "IDLE";
 | 
			
		||||
      break;
 | 
			
		||||
    case espbt::ClientState::SEARCHING:
 | 
			
		||||
      state_name = "SEARCHING";
 | 
			
		||||
      break;
 | 
			
		||||
    case espbt::ClientState::DISCOVERED:
 | 
			
		||||
      state_name = "DISCOVERED";
 | 
			
		||||
      break;
 | 
			
		||||
    case espbt::ClientState::READY_TO_CONNECT:
 | 
			
		||||
      state_name = "READY_TO_CONNECT";
 | 
			
		||||
      break;
 | 
			
		||||
    case espbt::ClientState::CONNECTING:
 | 
			
		||||
      state_name = "CONNECTING";
 | 
			
		||||
      break;
 | 
			
		||||
    case espbt::ClientState::CONNECTED:
 | 
			
		||||
      state_name = "CONNECTED";
 | 
			
		||||
      break;
 | 
			
		||||
    case espbt::ClientState::ESTABLISHED:
 | 
			
		||||
      state_name = "ESTABLISHED";
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
      state_name = "UNKNOWN_STATE";
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  State: %s", state_name.c_str());
 | 
			
		||||
  if (this->status_ == ESP_GATT_NO_RESOURCES) {
 | 
			
		||||
    ESP_LOGE(TAG, "  Failed due to no resources. Try to reduce number of BLE clients in config.");
 | 
			
		||||
  } else if (this->status_ != ESP_GATT_OK) {
 | 
			
		||||
@@ -107,7 +141,7 @@ bool BLEClientBase::parse_device(const espbt::ESPBTDevice &device) {
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void BLEClientBase::connect() {
 | 
			
		||||
  ESP_LOGI(TAG, "[%d] [%s] 0x%02x Connecting", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
  ESP_LOGI(TAG, "[%d] [%s] 0x%02x Attempting BLE connection", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
           this->remote_addr_type_);
 | 
			
		||||
  this->paired_ = false;
 | 
			
		||||
 | 
			
		||||
@@ -137,13 +171,14 @@ void BLEClientBase::connect() {
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] esp_ble_gap_set_prefer_conn_params failed: %d", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str(), param_ret);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->log_connection_params_(param_type);
 | 
			
		||||
    ESP_LOGD(TAG, "[%d] [%s] Set %s conn params", this->connection_index_, this->address_str_.c_str(), param_type);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Now open the connection
 | 
			
		||||
  auto ret = esp_ble_gattc_open(this->gattc_if_, this->remote_bda_, this->remote_addr_type_, true);
 | 
			
		||||
  if (ret) {
 | 
			
		||||
    this->log_gattc_warning_("esp_ble_gattc_open", ret);
 | 
			
		||||
    ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_open error, status=%d", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
             ret);
 | 
			
		||||
    this->set_state(espbt::ClientState::IDLE);
 | 
			
		||||
  } else {
 | 
			
		||||
    this->set_state(espbt::ClientState::CONNECTING);
 | 
			
		||||
@@ -153,9 +188,14 @@ void BLEClientBase::connect() {
 | 
			
		||||
esp_err_t BLEClientBase::pair() { return esp_ble_set_encryption(this->remote_bda_, ESP_BLE_SEC_ENCRYPT); }
 | 
			
		||||
 | 
			
		||||
void BLEClientBase::disconnect() {
 | 
			
		||||
  if (this->state_ == espbt::ClientState::IDLE || this->state_ == espbt::ClientState::DISCONNECTING) {
 | 
			
		||||
    ESP_LOGI(TAG, "[%d] [%s] Disconnect requested, but already %s", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
             espbt::client_state_to_string(this->state_));
 | 
			
		||||
  if (this->state_ == espbt::ClientState::IDLE) {
 | 
			
		||||
    ESP_LOGI(TAG, "[%d] [%s] Disconnect requested, but already idle.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->state_ == espbt::ClientState::DISCONNECTING) {
 | 
			
		||||
    ESP_LOGI(TAG, "[%d] [%s] Disconnect requested, but already disconnecting.", this->connection_index_,
 | 
			
		||||
             this->address_str_.c_str());
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->state_ == espbt::ClientState::CONNECTING || this->conn_id_ == UNSET_CONN_ID) {
 | 
			
		||||
@@ -190,7 +230,8 @@ void BLEClientBase::unconditional_disconnect() {
 | 
			
		||||
    // In the future we might consider App.reboot() here since
 | 
			
		||||
    // the BLE stack is in an indeterminate state.
 | 
			
		||||
    //
 | 
			
		||||
    this->log_gattc_warning_("esp_ble_gattc_close", err);
 | 
			
		||||
    ESP_LOGE(TAG, "[%d] [%s] esp_ble_gattc_close error, err=%d", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
             err);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (this->state_ == espbt::ClientState::SEARCHING || this->state_ == espbt::ClientState::READY_TO_CONNECT ||
 | 
			
		||||
@@ -203,11 +244,9 @@ void BLEClientBase::unconditional_disconnect() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BLEClientBase::release_services() {
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
  for (auto &svc : this->services_)
 | 
			
		||||
    delete svc;  // NOLINT(cppcoreguidelines-owning-memory)
 | 
			
		||||
  this->services_.clear();
 | 
			
		||||
#endif
 | 
			
		||||
#ifndef CONFIG_BT_GATTC_CACHE_NVS_FLASH
 | 
			
		||||
  esp_ble_gattc_cache_clean(this->remote_bda_);
 | 
			
		||||
#endif
 | 
			
		||||
@@ -217,23 +256,6 @@ void BLEClientBase::log_event_(const char *name) {
 | 
			
		||||
  ESP_LOGD(TAG, "[%d] [%s] %s", this->connection_index_, this->address_str_.c_str(), name);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BLEClientBase::log_gattc_event_(const char *name) {
 | 
			
		||||
  ESP_LOGD(TAG, "[%d] [%s] ESP_GATTC_%s_EVT", this->connection_index_, this->address_str_.c_str(), name);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BLEClientBase::log_gattc_warning_(const char *operation, esp_gatt_status_t status) {
 | 
			
		||||
  ESP_LOGW(TAG, "[%d] [%s] %s error, status=%d", this->connection_index_, this->address_str_.c_str(), operation,
 | 
			
		||||
           status);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BLEClientBase::log_gattc_warning_(const char *operation, esp_err_t err) {
 | 
			
		||||
  ESP_LOGW(TAG, "[%d] [%s] %s error, status=%d", this->connection_index_, this->address_str_.c_str(), operation, err);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BLEClientBase::log_connection_params_(const char *param_type) {
 | 
			
		||||
  ESP_LOGD(TAG, "[%d] [%s] %s conn params", this->connection_index_, this->address_str_.c_str(), param_type);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void BLEClientBase::restore_medium_conn_params_() {
 | 
			
		||||
  // Restore to medium connection parameters after initial connection phase
 | 
			
		||||
  // This balances performance with bandwidth usage for normal operation
 | 
			
		||||
@@ -243,7 +265,7 @@ void BLEClientBase::restore_medium_conn_params_() {
 | 
			
		||||
  conn_params.max_int = MEDIUM_MAX_CONN_INTERVAL;
 | 
			
		||||
  conn_params.latency = 0;
 | 
			
		||||
  conn_params.timeout = MEDIUM_CONN_TIMEOUT;
 | 
			
		||||
  this->log_connection_params_("medium");
 | 
			
		||||
  ESP_LOGD(TAG, "[%d] [%s] Restoring medium conn params", this->connection_index_, this->address_str_.c_str());
 | 
			
		||||
  esp_ble_gap_update_conn_params(&conn_params);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -274,18 +296,30 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
    case ESP_GATTC_OPEN_EVT: {
 | 
			
		||||
      if (!this->check_addr(param->open.remote_bda))
 | 
			
		||||
        return false;
 | 
			
		||||
      this->log_gattc_event_("OPEN");
 | 
			
		||||
      this->log_event_("ESP_GATTC_OPEN_EVT");
 | 
			
		||||
      // conn_id was already set in ESP_GATTC_CONNECT_EVT
 | 
			
		||||
      this->service_count_ = 0;
 | 
			
		||||
      if (this->state_ != espbt::ClientState::CONNECTING) {
 | 
			
		||||
        // This should not happen but lets log it in case it does
 | 
			
		||||
        // because it means we have a bad assumption about how the
 | 
			
		||||
        // ESP BT stack works.
 | 
			
		||||
        ESP_LOGE(TAG, "[%d] [%s] Got ESP_GATTC_OPEN_EVT while in %s state, status=%d", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), espbt::client_state_to_string(this->state_), param->open.status);
 | 
			
		||||
        if (this->state_ == espbt::ClientState::CONNECTED) {
 | 
			
		||||
          ESP_LOGE(TAG, "[%d] [%s] Got ESP_GATTC_OPEN_EVT while already connected, status=%d", this->connection_index_,
 | 
			
		||||
                   this->address_str_.c_str(), param->open.status);
 | 
			
		||||
        } else if (this->state_ == espbt::ClientState::ESTABLISHED) {
 | 
			
		||||
          ESP_LOGE(TAG, "[%d] [%s] Got ESP_GATTC_OPEN_EVT while already established, status=%d",
 | 
			
		||||
                   this->connection_index_, this->address_str_.c_str(), param->open.status);
 | 
			
		||||
        } else if (this->state_ == espbt::ClientState::DISCONNECTING) {
 | 
			
		||||
          ESP_LOGE(TAG, "[%d] [%s] Got ESP_GATTC_OPEN_EVT while disconnecting, status=%d", this->connection_index_,
 | 
			
		||||
                   this->address_str_.c_str(), param->open.status);
 | 
			
		||||
        } else {
 | 
			
		||||
          ESP_LOGE(TAG, "[%d] [%s] Got ESP_GATTC_OPEN_EVT while not in connecting state, status=%d",
 | 
			
		||||
                   this->connection_index_, this->address_str_.c_str(), param->open.status);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
      if (param->open.status != ESP_GATT_OK && param->open.status != ESP_GATT_ALREADY_OPEN) {
 | 
			
		||||
        this->log_gattc_warning_("Connection open", param->open.status);
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] Connection failed, status=%d", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
                 param->open.status);
 | 
			
		||||
        this->set_state(espbt::ClientState::IDLE);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
@@ -301,8 +335,11 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
      this->set_state(espbt::ClientState::CONNECTED);
 | 
			
		||||
      ESP_LOGI(TAG, "[%d] [%s] Connection open", this->connection_index_, this->address_str_.c_str());
 | 
			
		||||
      if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) {
 | 
			
		||||
        ESP_LOGI(TAG, "[%d] [%s] Using cached services", this->connection_index_, this->address_str_.c_str());
 | 
			
		||||
 | 
			
		||||
        // Restore to medium connection parameters for cached connections too
 | 
			
		||||
        this->restore_medium_conn_params_();
 | 
			
		||||
 | 
			
		||||
        // only set our state, subclients might have more stuff to do yet.
 | 
			
		||||
        this->state_ = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
        break;
 | 
			
		||||
@@ -314,7 +351,7 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
    case ESP_GATTC_CONNECT_EVT: {
 | 
			
		||||
      if (!this->check_addr(param->connect.remote_bda))
 | 
			
		||||
        return false;
 | 
			
		||||
      this->log_gattc_event_("CONNECT");
 | 
			
		||||
      this->log_event_("ESP_GATTC_CONNECT_EVT");
 | 
			
		||||
      this->conn_id_ = param->connect.conn_id;
 | 
			
		||||
      // Start MTU negotiation immediately as recommended by ESP-IDF examples
 | 
			
		||||
      // (gatt_client, ble_throughput) which call esp_ble_gattc_send_mtu_req in
 | 
			
		||||
@@ -322,7 +359,8 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
      // This saves ~3ms in the connection process.
 | 
			
		||||
      auto ret = esp_ble_gattc_send_mtu_req(this->gattc_if_, param->connect.conn_id);
 | 
			
		||||
      if (ret) {
 | 
			
		||||
        this->log_gattc_warning_("esp_ble_gattc_send_mtu_req", ret);
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_send_mtu_req failed, status=%x", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), ret);
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
@@ -360,7 +398,7 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
    case ESP_GATTC_CLOSE_EVT: {
 | 
			
		||||
      if (this->conn_id_ != param->close.conn_id)
 | 
			
		||||
        return false;
 | 
			
		||||
      this->log_gattc_event_("CLOSE");
 | 
			
		||||
      this->log_event_("ESP_GATTC_CLOSE_EVT");
 | 
			
		||||
      this->release_services();
 | 
			
		||||
      this->set_state(espbt::ClientState::IDLE);
 | 
			
		||||
      this->conn_id_ = UNSET_CONN_ID;
 | 
			
		||||
@@ -372,74 +410,71 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
      this->service_count_++;
 | 
			
		||||
      if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) {
 | 
			
		||||
        // V3 clients don't need services initialized since
 | 
			
		||||
        // as they use the ESP APIs to get services.
 | 
			
		||||
        // they only request by handle after receiving the services.
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
      BLEService *ble_service = new BLEService();  // NOLINT(cppcoreguidelines-owning-memory)
 | 
			
		||||
      ble_service->uuid = espbt::ESPBTUUID::from_uuid(param->search_res.srvc_id.uuid);
 | 
			
		||||
      ble_service->start_handle = param->search_res.start_handle;
 | 
			
		||||
      ble_service->end_handle = param->search_res.end_handle;
 | 
			
		||||
      ble_service->client = this;
 | 
			
		||||
      this->services_.push_back(ble_service);
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_SEARCH_CMPL_EVT: {
 | 
			
		||||
      if (this->conn_id_ != param->search_cmpl.conn_id)
 | 
			
		||||
        return false;
 | 
			
		||||
      this->log_gattc_event_("SEARCH_CMPL");
 | 
			
		||||
      this->log_event_("ESP_GATTC_SEARCH_CMPL_EVT");
 | 
			
		||||
      for (auto &svc : this->services_) {
 | 
			
		||||
        ESP_LOGV(TAG, "[%d] [%s] Service UUID: %s", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
                 svc->uuid.to_string().c_str());
 | 
			
		||||
        ESP_LOGV(TAG, "[%d] [%s]  start_handle: 0x%x  end_handle: 0x%x", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), svc->start_handle, svc->end_handle);
 | 
			
		||||
      }
 | 
			
		||||
      ESP_LOGI(TAG, "[%d] [%s] Service discovery complete", this->connection_index_, this->address_str_.c_str());
 | 
			
		||||
 | 
			
		||||
      // For V3 connections, restore to medium connection parameters after service discovery
 | 
			
		||||
      // This balances performance with bandwidth usage after the critical discovery phase
 | 
			
		||||
      if (this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE ||
 | 
			
		||||
          this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE) {
 | 
			
		||||
        this->restore_medium_conn_params_();
 | 
			
		||||
      } else {
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
        for (auto &svc : this->services_) {
 | 
			
		||||
          ESP_LOGV(TAG, "[%d] [%s] Service UUID: %s", this->connection_index_, this->address_str_.c_str(),
 | 
			
		||||
                   svc->uuid.to_string().c_str());
 | 
			
		||||
          ESP_LOGV(TAG, "[%d] [%s]  start_handle: 0x%x  end_handle: 0x%x", this->connection_index_,
 | 
			
		||||
                   this->address_str_.c_str(), svc->start_handle, svc->end_handle);
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
      }
 | 
			
		||||
      ESP_LOGI(TAG, "[%d] [%s] Service discovery complete", this->connection_index_, this->address_str_.c_str());
 | 
			
		||||
 | 
			
		||||
      this->state_ = espbt::ClientState::ESTABLISHED;
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_READ_DESCR_EVT: {
 | 
			
		||||
      if (this->conn_id_ != param->write.conn_id)
 | 
			
		||||
        return false;
 | 
			
		||||
      this->log_gattc_event_("READ_DESCR");
 | 
			
		||||
      this->log_event_("ESP_GATTC_READ_DESCR_EVT");
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_WRITE_DESCR_EVT: {
 | 
			
		||||
      if (this->conn_id_ != param->write.conn_id)
 | 
			
		||||
        return false;
 | 
			
		||||
      this->log_gattc_event_("WRITE_DESCR");
 | 
			
		||||
      this->log_event_("ESP_GATTC_WRITE_DESCR_EVT");
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_WRITE_CHAR_EVT: {
 | 
			
		||||
      if (this->conn_id_ != param->write.conn_id)
 | 
			
		||||
        return false;
 | 
			
		||||
      this->log_gattc_event_("WRITE_CHAR");
 | 
			
		||||
      this->log_event_("ESP_GATTC_WRITE_CHAR_EVT");
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_READ_CHAR_EVT: {
 | 
			
		||||
      if (this->conn_id_ != param->read.conn_id)
 | 
			
		||||
        return false;
 | 
			
		||||
      this->log_gattc_event_("READ_CHAR");
 | 
			
		||||
      this->log_event_("ESP_GATTC_READ_CHAR_EVT");
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_NOTIFY_EVT: {
 | 
			
		||||
      if (this->conn_id_ != param->notify.conn_id)
 | 
			
		||||
        return false;
 | 
			
		||||
      this->log_gattc_event_("NOTIFY");
 | 
			
		||||
      this->log_event_("ESP_GATTC_NOTIFY_EVT");
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case ESP_GATTC_REG_FOR_NOTIFY_EVT: {
 | 
			
		||||
      this->log_gattc_event_("REG_FOR_NOTIFY");
 | 
			
		||||
      this->log_event_("ESP_GATTC_REG_FOR_NOTIFY_EVT");
 | 
			
		||||
      if (this->connection_type_ == espbt::ConnectionType::V3_WITH_CACHE ||
 | 
			
		||||
          this->connection_type_ == espbt::ConnectionType::V3_WITHOUT_CACHE) {
 | 
			
		||||
        // Client is responsible for flipping the descriptor value
 | 
			
		||||
@@ -451,7 +486,8 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
      esp_gatt_status_t descr_status = esp_ble_gattc_get_descr_by_char_handle(
 | 
			
		||||
          this->gattc_if_, this->conn_id_, param->reg_for_notify.handle, NOTIFY_DESC_UUID, &desc_result, &count);
 | 
			
		||||
      if (descr_status != ESP_GATT_OK) {
 | 
			
		||||
        this->log_gattc_warning_("esp_ble_gattc_get_descr_by_char_handle", descr_status);
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_get_descr_by_char_handle error, status=%d", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), descr_status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      esp_gattc_char_elem_t char_result;
 | 
			
		||||
@@ -459,7 +495,8 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
          esp_ble_gattc_get_all_char(this->gattc_if_, this->conn_id_, param->reg_for_notify.handle,
 | 
			
		||||
                                     param->reg_for_notify.handle, &char_result, &count, 0);
 | 
			
		||||
      if (char_status != ESP_GATT_OK) {
 | 
			
		||||
        this->log_gattc_warning_("esp_ble_gattc_get_all_char", char_status);
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_get_all_char error, status=%d", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), char_status);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
@@ -473,7 +510,8 @@ bool BLEClientBase::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_
 | 
			
		||||
                                         (uint8_t *) ¬ify_en, ESP_GATT_WRITE_TYPE_RSP, ESP_GATT_AUTH_REQ_NONE);
 | 
			
		||||
      ESP_LOGD(TAG, "Wrote notify descriptor %d, properties=%d", notify_en, char_result.properties);
 | 
			
		||||
      if (status) {
 | 
			
		||||
        this->log_gattc_warning_("esp_ble_gattc_write_char_descr", status);
 | 
			
		||||
        ESP_LOGW(TAG, "[%d] [%s] esp_ble_gattc_write_char_descr error, status=%d", this->connection_index_,
 | 
			
		||||
                 this->address_str_.c_str(), status);
 | 
			
		||||
      }
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
@@ -581,7 +619,6 @@ float BLEClientBase::parse_char_value(uint8_t *value, uint16_t length) {
 | 
			
		||||
  return NAN;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
BLEService *BLEClientBase::get_service(espbt::ESPBTUUID uuid) {
 | 
			
		||||
  for (auto *svc : this->services_) {
 | 
			
		||||
    if (svc->uuid == uuid)
 | 
			
		||||
@@ -658,8 +695,8 @@ BLEDescriptor *BLEClientBase::get_descriptor(uint16_t handle) {
 | 
			
		||||
  }
 | 
			
		||||
  return nullptr;
 | 
			
		||||
}
 | 
			
		||||
#endif  // USE_ESP32_BLE_DEVICE
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble_client
 | 
			
		||||
}  // namespace esp32_ble_client
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -5,9 +5,7 @@
 | 
			
		||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
 | 
			
		||||
#include "esphome/core/component.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
#include "ble_service.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include <array>
 | 
			
		||||
#include <string>
 | 
			
		||||
@@ -18,7 +16,8 @@
 | 
			
		||||
#include <esp_gatt_common_api.h>
 | 
			
		||||
#include <esp_gattc_api.h>
 | 
			
		||||
 | 
			
		||||
namespace esphome::esp32_ble_client {
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace esp32_ble_client {
 | 
			
		||||
 | 
			
		||||
namespace espbt = esphome::esp32_ble_tracker;
 | 
			
		||||
 | 
			
		||||
@@ -69,7 +68,6 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
 | 
			
		||||
  }
 | 
			
		||||
  const std::string &address_str() const { return this->address_str_; }
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
  BLEService *get_service(espbt::ESPBTUUID uuid);
 | 
			
		||||
  BLEService *get_service(uint16_t uuid);
 | 
			
		||||
  BLECharacteristic *get_characteristic(espbt::ESPBTUUID service, espbt::ESPBTUUID chr);
 | 
			
		||||
@@ -80,7 +78,6 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
 | 
			
		||||
  BLEDescriptor *get_descriptor(uint16_t handle);
 | 
			
		||||
  // Get the configuration descriptor for the given characteristic handle.
 | 
			
		||||
  BLEDescriptor *get_config_descriptor(uint16_t handle);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  float parse_char_value(uint8_t *value, uint16_t length);
 | 
			
		||||
 | 
			
		||||
@@ -107,9 +104,7 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
 | 
			
		||||
 | 
			
		||||
  // Group 2: Container types (grouped for memory optimization)
 | 
			
		||||
  std::string address_str_{};
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
  std::vector<BLEService *> services_;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // Group 3: 4-byte types
 | 
			
		||||
  int gattc_if_;
 | 
			
		||||
@@ -132,13 +127,10 @@ class BLEClientBase : public espbt::ESPBTClient, public Component {
 | 
			
		||||
  // 6 bytes used, 2 bytes padding
 | 
			
		||||
 | 
			
		||||
  void log_event_(const char *name);
 | 
			
		||||
  void log_gattc_event_(const char *name);
 | 
			
		||||
  void restore_medium_conn_params_();
 | 
			
		||||
  void log_gattc_warning_(const char *operation, esp_gatt_status_t status);
 | 
			
		||||
  void log_gattc_warning_(const char *operation, esp_err_t err);
 | 
			
		||||
  void log_connection_params_(const char *param_type);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble_client
 | 
			
		||||
}  // namespace esp32_ble_client
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,11 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome::esp32_ble_client {
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace esp32_ble_client {
 | 
			
		||||
 | 
			
		||||
namespace espbt = esphome::esp32_ble_tracker;
 | 
			
		||||
 | 
			
		||||
@@ -21,7 +19,7 @@ class BLEDescriptor {
 | 
			
		||||
  BLECharacteristic *characteristic;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble_client
 | 
			
		||||
}  // namespace esp32_ble_client
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_BLE_DEVICE
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -4,9 +4,9 @@
 | 
			
		||||
#include "esphome/core/log.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
 | 
			
		||||
namespace esphome::esp32_ble_client {
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace esp32_ble_client {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "esp32_ble_client";
 | 
			
		||||
 | 
			
		||||
@@ -71,7 +71,7 @@ void BLEService::parse_characteristics() {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble_client
 | 
			
		||||
}  // namespace esp32_ble_client
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_BLE_DEVICE
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,6 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
 | 
			
		||||
 | 
			
		||||
@@ -11,7 +8,8 @@
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
 | 
			
		||||
namespace esphome::esp32_ble_client {
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace esp32_ble_client {
 | 
			
		||||
 | 
			
		||||
namespace espbt = esphome::esp32_ble_tracker;
 | 
			
		||||
 | 
			
		||||
@@ -32,7 +30,7 @@ class BLEService {
 | 
			
		||||
  BLECharacteristic *get_characteristic(uint16_t uuid);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble_client
 | 
			
		||||
}  // namespace esp32_ble_client
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32_BLE_DEVICE
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -529,7 +529,6 @@ async def to_code_characteristic(service_var, char_conf):
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    # Register the loggers this component needs
 | 
			
		||||
    esp32_ble.register_bt_logger(BTLoggers.GATT, BTLoggers.SMP)
 | 
			
		||||
    cg.add_define("USE_ESP32_BLE_UUID")
 | 
			
		||||
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
 | 
			
		||||
@@ -572,7 +571,6 @@ async def to_code(config):
 | 
			
		||||
            config[CONF_ON_DISCONNECT],
 | 
			
		||||
        )
 | 
			
		||||
    cg.add_define("USE_ESP32_BLE_SERVER")
 | 
			
		||||
    cg.add_define("USE_ESP32_BLE_ADVERTISING")
 | 
			
		||||
    if CORE.using_esp_idf:
 | 
			
		||||
        add_idf_sdkconfig_option("CONFIG_BT_ENABLED", True)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -355,6 +355,11 @@ async def to_code(config):
 | 
			
		||||
        add_idf_sdkconfig_option(
 | 
			
		||||
            "CONFIG_BTDM_CTRL_BLE_MAX_CONN", config[CONF_MAX_CONNECTIONS]
 | 
			
		||||
        )
 | 
			
		||||
        # CONFIG_BT_GATTC_NOTIF_REG_MAX controls the number of
 | 
			
		||||
        # max notifications in 5.x, setting CONFIG_BT_ACL_CONNECTIONS
 | 
			
		||||
        # is enough in 4.x
 | 
			
		||||
        # https://github.com/esphome/issues/issues/6808
 | 
			
		||||
        add_idf_sdkconfig_option("CONFIG_BT_GATTC_NOTIF_REG_MAX", 9)
 | 
			
		||||
 | 
			
		||||
    cg.add_define("USE_OTA_STATE_CALLBACK")  # To be notified when an OTA update starts
 | 
			
		||||
    cg.add_define("USE_ESP32_BLE_CLIENT")
 | 
			
		||||
@@ -373,7 +378,6 @@ async def _add_ble_features():
 | 
			
		||||
    # Add feature-specific defines based on what's needed
 | 
			
		||||
    if BLEFeatures.ESP_BT_DEVICE in _required_features:
 | 
			
		||||
        cg.add_define("USE_ESP32_BLE_DEVICE")
 | 
			
		||||
        cg.add_define("USE_ESP32_BLE_UUID")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
ESP32_BLE_START_SCAN_ACTION_SCHEMA = cv.Schema(
 | 
			
		||||
 
 | 
			
		||||
@@ -41,31 +41,6 @@ static const char *const TAG = "esp32_ble_tracker";
 | 
			
		||||
 | 
			
		||||
ESP32BLETracker *global_esp32_ble_tracker = nullptr;  // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
 | 
			
		||||
 | 
			
		||||
const char *client_state_to_string(ClientState state) {
 | 
			
		||||
  switch (state) {
 | 
			
		||||
    case ClientState::INIT:
 | 
			
		||||
      return "INIT";
 | 
			
		||||
    case ClientState::DISCONNECTING:
 | 
			
		||||
      return "DISCONNECTING";
 | 
			
		||||
    case ClientState::IDLE:
 | 
			
		||||
      return "IDLE";
 | 
			
		||||
    case ClientState::SEARCHING:
 | 
			
		||||
      return "SEARCHING";
 | 
			
		||||
    case ClientState::DISCOVERED:
 | 
			
		||||
      return "DISCOVERED";
 | 
			
		||||
    case ClientState::READY_TO_CONNECT:
 | 
			
		||||
      return "READY_TO_CONNECT";
 | 
			
		||||
    case ClientState::CONNECTING:
 | 
			
		||||
      return "CONNECTING";
 | 
			
		||||
    case ClientState::CONNECTED:
 | 
			
		||||
      return "CONNECTED";
 | 
			
		||||
    case ClientState::ESTABLISHED:
 | 
			
		||||
      return "ESTABLISHED";
 | 
			
		||||
    default:
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
float ESP32BLETracker::get_setup_priority() const { return setup_priority::AFTER_BLUETOOTH; }
 | 
			
		||||
 | 
			
		||||
void ESP32BLETracker::setup() {
 | 
			
		||||
@@ -101,49 +76,58 @@ void ESP32BLETracker::loop() {
 | 
			
		||||
      this->start_scan();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Check for scan timeout - moved here from scheduler to avoid false reboots
 | 
			
		||||
  // when the loop is blocked
 | 
			
		||||
  if (this->scanner_state_ == ScannerState::RUNNING) {
 | 
			
		||||
    switch (this->scan_timeout_state_) {
 | 
			
		||||
      case ScanTimeoutState::MONITORING: {
 | 
			
		||||
        uint32_t now = App.get_loop_component_start_time();
 | 
			
		||||
        uint32_t timeout_ms = this->scan_duration_ * 2000;
 | 
			
		||||
        // Robust time comparison that handles rollover correctly
 | 
			
		||||
        // This works because unsigned arithmetic wraps around predictably
 | 
			
		||||
        if ((now - this->scan_start_time_) > timeout_ms) {
 | 
			
		||||
          // First time we've seen the timeout exceeded - wait one more loop iteration
 | 
			
		||||
          // This ensures all components have had a chance to process pending events
 | 
			
		||||
          // This is because esp32_ble may not have run yet and called
 | 
			
		||||
          // gap_scan_event_handler yet when the loop unblocks
 | 
			
		||||
          ESP_LOGW(TAG, "Scan timeout exceeded");
 | 
			
		||||
          this->scan_timeout_state_ = ScanTimeoutState::EXCEEDED_WAIT;
 | 
			
		||||
        }
 | 
			
		||||
  int connecting = 0;
 | 
			
		||||
  int discovered = 0;
 | 
			
		||||
  int searching = 0;
 | 
			
		||||
  int disconnecting = 0;
 | 
			
		||||
  for (auto *client : this->clients_) {
 | 
			
		||||
    switch (client->state()) {
 | 
			
		||||
      case ClientState::DISCONNECTING:
 | 
			
		||||
        disconnecting++;
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
      case ScanTimeoutState::EXCEEDED_WAIT:
 | 
			
		||||
        // We've waited at least one full loop iteration, and scan is still running
 | 
			
		||||
        ESP_LOGE(TAG, "Scan never terminated, rebooting");
 | 
			
		||||
        App.reboot();
 | 
			
		||||
      case ClientState::DISCOVERED:
 | 
			
		||||
        discovered++;
 | 
			
		||||
        break;
 | 
			
		||||
 | 
			
		||||
      case ScanTimeoutState::INACTIVE:
 | 
			
		||||
        // This case should be unreachable - scanner and timeout states are always synchronized
 | 
			
		||||
      case ClientState::SEARCHING:
 | 
			
		||||
        searching++;
 | 
			
		||||
        break;
 | 
			
		||||
      case ClientState::CONNECTING:
 | 
			
		||||
      case ClientState::READY_TO_CONNECT:
 | 
			
		||||
        connecting++;
 | 
			
		||||
        break;
 | 
			
		||||
      default:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ClientStateCounts counts = this->count_client_states_();
 | 
			
		||||
  if (counts != this->client_state_counts_) {
 | 
			
		||||
    this->client_state_counts_ = counts;
 | 
			
		||||
    ESP_LOGD(TAG, "connecting: %d, discovered: %d, searching: %d, disconnecting: %d",
 | 
			
		||||
             this->client_state_counts_.connecting, this->client_state_counts_.discovered,
 | 
			
		||||
             this->client_state_counts_.searching, this->client_state_counts_.disconnecting);
 | 
			
		||||
  if (connecting != connecting_ || discovered != discovered_ || searching != searching_ ||
 | 
			
		||||
      disconnecting != disconnecting_) {
 | 
			
		||||
    connecting_ = connecting;
 | 
			
		||||
    discovered_ = discovered;
 | 
			
		||||
    searching_ = searching;
 | 
			
		||||
    disconnecting_ = disconnecting;
 | 
			
		||||
    ESP_LOGD(TAG, "connecting: %d, discovered: %d, searching: %d, disconnecting: %d", connecting_, discovered_,
 | 
			
		||||
             searching_, disconnecting_);
 | 
			
		||||
  }
 | 
			
		||||
  bool promote_to_connecting = discovered && !searching && !connecting;
 | 
			
		||||
 | 
			
		||||
  // All scan result processing is now done immediately in gap_scan_event_handler
 | 
			
		||||
  // No ring buffer processing needed here
 | 
			
		||||
  if (this->scanner_state_ == ScannerState::FAILED ||
 | 
			
		||||
      (this->scan_set_param_failed_ && this->scanner_state_ == ScannerState::RUNNING)) {
 | 
			
		||||
    this->handle_scanner_failure_();
 | 
			
		||||
    this->stop_scan_();
 | 
			
		||||
    if (this->scan_start_fail_count_ == std::numeric_limits<uint8_t>::max()) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan could not restart after %d attempts, rebooting to restore stack (IDF)",
 | 
			
		||||
               std::numeric_limits<uint8_t>::max());
 | 
			
		||||
      App.reboot();
 | 
			
		||||
    }
 | 
			
		||||
    if (this->scan_start_failed_) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan start failed: %d", this->scan_start_failed_);
 | 
			
		||||
      this->scan_start_failed_ = ESP_BT_STATUS_SUCCESS;
 | 
			
		||||
    }
 | 
			
		||||
    if (this->scan_set_param_failed_) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan set param failed: %d", this->scan_set_param_failed_);
 | 
			
		||||
      this->scan_set_param_failed_ = ESP_BT_STATUS_SUCCESS;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  /*
 | 
			
		||||
 | 
			
		||||
@@ -158,12 +142,13 @@ void ESP32BLETracker::loop() {
 | 
			
		||||
    https://github.com/espressif/esp-idf/issues/6688
 | 
			
		||||
 | 
			
		||||
  */
 | 
			
		||||
  bool promote_to_connecting = counts.discovered && !counts.searching && !counts.connecting;
 | 
			
		||||
 | 
			
		||||
  if (this->scanner_state_ == ScannerState::IDLE && !counts.connecting && !counts.disconnecting &&
 | 
			
		||||
      !promote_to_connecting) {
 | 
			
		||||
  if (this->scanner_state_ == ScannerState::IDLE && !connecting && !disconnecting && !promote_to_connecting) {
 | 
			
		||||
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
 | 
			
		||||
    this->update_coex_preference_(false);
 | 
			
		||||
    if (this->coex_prefer_ble_) {
 | 
			
		||||
      this->coex_prefer_ble_ = false;
 | 
			
		||||
      ESP_LOGD(TAG, "Setting coexistence preference to balanced.");
 | 
			
		||||
      esp_coex_preference_set(ESP_COEX_PREFER_BALANCE);  // Reset to default
 | 
			
		||||
    }
 | 
			
		||||
#endif
 | 
			
		||||
    if (this->scan_continuous_) {
 | 
			
		||||
      this->start_scan_(false);  // first = false
 | 
			
		||||
@@ -172,12 +157,34 @@ void ESP32BLETracker::loop() {
 | 
			
		||||
  // If there is a discovered client and no connecting
 | 
			
		||||
  // clients and no clients using the scanner to search for
 | 
			
		||||
  // devices, then promote the discovered client to ready to connect.
 | 
			
		||||
  // We check both RUNNING and IDLE states because:
 | 
			
		||||
  // - RUNNING: gap_scan_event_handler initiates stop_scan_() but promotion can happen immediately
 | 
			
		||||
  // - IDLE: Scanner has already stopped (naturally or by gap_scan_event_handler)
 | 
			
		||||
  // Note: Scanning is already stopped by gap_scan_event_handler when
 | 
			
		||||
  // a discovered client is found, so we only need to handle promotion
 | 
			
		||||
  // when the scanner is IDLE.
 | 
			
		||||
  if (promote_to_connecting &&
 | 
			
		||||
      (this->scanner_state_ == ScannerState::RUNNING || this->scanner_state_ == ScannerState::IDLE)) {
 | 
			
		||||
    this->try_promote_discovered_clients_();
 | 
			
		||||
    for (auto *client : this->clients_) {
 | 
			
		||||
      if (client->state() == ClientState::DISCOVERED) {
 | 
			
		||||
        if (this->scanner_state_ == ScannerState::RUNNING) {
 | 
			
		||||
          ESP_LOGD(TAG, "Stopping scan to make connection");
 | 
			
		||||
          this->stop_scan_();
 | 
			
		||||
          // Don't wait for scan stop complete - promote immediately.
 | 
			
		||||
          // This is safe because ESP-IDF processes BLE commands sequentially through its internal mailbox queue.
 | 
			
		||||
          // This guarantees that the stop scan command will be fully processed before any subsequent connect command,
 | 
			
		||||
          // preventing race conditions or overlapping operations.
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ESP_LOGD(TAG, "Promoting client to connect");
 | 
			
		||||
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
 | 
			
		||||
        ESP_LOGD(TAG, "Setting coexistence to Bluetooth to make connection.");
 | 
			
		||||
        if (!this->coex_prefer_ble_) {
 | 
			
		||||
          this->coex_prefer_ble_ = true;
 | 
			
		||||
          esp_coex_preference_set(ESP_COEX_PREFER_BT);  // Prioritize Bluetooth
 | 
			
		||||
        }
 | 
			
		||||
#endif
 | 
			
		||||
        client->set_state(ClientState::READY_TO_CONNECT);
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -193,11 +200,16 @@ void ESP32BLETracker::ble_before_disabled_event_handler() { this->stop_scan_();
 | 
			
		||||
 | 
			
		||||
void ESP32BLETracker::stop_scan_() {
 | 
			
		||||
  if (this->scanner_state_ != ScannerState::RUNNING && this->scanner_state_ != ScannerState::FAILED) {
 | 
			
		||||
    ESP_LOGE(TAG, "Cannot stop scan: %s", this->scanner_state_to_string_(this->scanner_state_));
 | 
			
		||||
    if (this->scanner_state_ == ScannerState::IDLE) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan is already stopped while trying to stop.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::STARTING) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan is starting while trying to stop.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::STOPPING) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan is already stopping while trying to stop.");
 | 
			
		||||
    }
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  // Reset timeout state machine when stopping scan
 | 
			
		||||
  this->scan_timeout_state_ = ScanTimeoutState::INACTIVE;
 | 
			
		||||
  this->cancel_timeout("scan");
 | 
			
		||||
  this->set_scanner_state_(ScannerState::STOPPING);
 | 
			
		||||
  esp_err_t err = esp_ble_gap_stop_scanning();
 | 
			
		||||
  if (err != ESP_OK) {
 | 
			
		||||
@@ -212,7 +224,15 @@ void ESP32BLETracker::start_scan_(bool first) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->scanner_state_ != ScannerState::IDLE) {
 | 
			
		||||
    this->log_unexpected_state_("start scan", ScannerState::IDLE);
 | 
			
		||||
    if (this->scanner_state_ == ScannerState::STARTING) {
 | 
			
		||||
      ESP_LOGE(TAG, "Cannot start scan while already starting.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::RUNNING) {
 | 
			
		||||
      ESP_LOGE(TAG, "Cannot start scan while already running.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::STOPPING) {
 | 
			
		||||
      ESP_LOGE(TAG, "Cannot start scan while already stopping.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::FAILED) {
 | 
			
		||||
      ESP_LOGE(TAG, "Cannot start scan while already failed.");
 | 
			
		||||
    }
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  this->set_scanner_state_(ScannerState::STARTING);
 | 
			
		||||
@@ -221,19 +241,18 @@ void ESP32BLETracker::start_scan_(bool first) {
 | 
			
		||||
    for (auto *listener : this->listeners_)
 | 
			
		||||
      listener->on_scan_end();
 | 
			
		||||
  }
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
  this->already_discovered_.clear();
 | 
			
		||||
#endif
 | 
			
		||||
  this->scan_params_.scan_type = this->scan_active_ ? BLE_SCAN_TYPE_ACTIVE : BLE_SCAN_TYPE_PASSIVE;
 | 
			
		||||
  this->scan_params_.own_addr_type = BLE_ADDR_TYPE_PUBLIC;
 | 
			
		||||
  this->scan_params_.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL;
 | 
			
		||||
  this->scan_params_.scan_interval = this->scan_interval_;
 | 
			
		||||
  this->scan_params_.scan_window = this->scan_window_;
 | 
			
		||||
 | 
			
		||||
  // Start timeout monitoring in loop() instead of using scheduler
 | 
			
		||||
  // This prevents false reboots when the loop is blocked
 | 
			
		||||
  this->scan_start_time_ = App.get_loop_component_start_time();
 | 
			
		||||
  this->scan_timeout_state_ = ScanTimeoutState::MONITORING;
 | 
			
		||||
  // Start timeout before scan is started. Otherwise scan never starts if any error.
 | 
			
		||||
  this->set_timeout("scan", this->scan_duration_ * 2000, []() {
 | 
			
		||||
    ESP_LOGE(TAG, "Scan never terminated, rebooting to restore stack (IDF)");
 | 
			
		||||
    App.reboot();
 | 
			
		||||
  });
 | 
			
		||||
 | 
			
		||||
  esp_err_t err = esp_ble_gap_set_scan_params(&this->scan_params_);
 | 
			
		||||
  if (err != ESP_OK) {
 | 
			
		||||
@@ -318,7 +337,15 @@ void ESP32BLETracker::gap_scan_event_handler(const BLEScanResult &scan_result) {
 | 
			
		||||
  } else if (scan_result.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT) {
 | 
			
		||||
    // Scan finished on its own
 | 
			
		||||
    if (this->scanner_state_ != ScannerState::RUNNING) {
 | 
			
		||||
      this->log_unexpected_state_("scan complete", ScannerState::RUNNING);
 | 
			
		||||
      if (this->scanner_state_ == ScannerState::STOPPING) {
 | 
			
		||||
        ESP_LOGE(TAG, "Scan was not running when scan completed.");
 | 
			
		||||
      } else if (this->scanner_state_ == ScannerState::STARTING) {
 | 
			
		||||
        ESP_LOGE(TAG, "Scan was not started when scan completed.");
 | 
			
		||||
      } else if (this->scanner_state_ == ScannerState::FAILED) {
 | 
			
		||||
        ESP_LOGE(TAG, "Scan was in failed state when scan completed.");
 | 
			
		||||
      } else if (this->scanner_state_ == ScannerState::IDLE) {
 | 
			
		||||
        ESP_LOGE(TAG, "Scan was idle when scan completed.");
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    // Scan completed naturally, perform cleanup and transition to IDLE
 | 
			
		||||
    this->cleanup_scan_state_(false);
 | 
			
		||||
@@ -340,7 +367,15 @@ void ESP32BLETracker::gap_scan_start_complete_(const esp_ble_gap_cb_param_t::ble
 | 
			
		||||
  ESP_LOGV(TAG, "gap_scan_start_complete - status %d", param.status);
 | 
			
		||||
  this->scan_start_failed_ = param.status;
 | 
			
		||||
  if (this->scanner_state_ != ScannerState::STARTING) {
 | 
			
		||||
    this->log_unexpected_state_("start complete", ScannerState::STARTING);
 | 
			
		||||
    if (this->scanner_state_ == ScannerState::RUNNING) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan was already running when start complete.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::STOPPING) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan was stopping when start complete.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::FAILED) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan was in failed state when start complete.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::IDLE) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan was idle when start complete.");
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  if (param.status == ESP_BT_STATUS_SUCCESS) {
 | 
			
		||||
    this->scan_start_fail_count_ = 0;
 | 
			
		||||
@@ -358,7 +393,15 @@ void ESP32BLETracker::gap_scan_stop_complete_(const esp_ble_gap_cb_param_t::ble_
 | 
			
		||||
  // This allows us to safely transition to IDLE state and perform cleanup without race conditions
 | 
			
		||||
  ESP_LOGV(TAG, "gap_scan_stop_complete - status %d", param.status);
 | 
			
		||||
  if (this->scanner_state_ != ScannerState::STOPPING) {
 | 
			
		||||
    this->log_unexpected_state_("stop complete", ScannerState::STOPPING);
 | 
			
		||||
    if (this->scanner_state_ == ScannerState::RUNNING) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan was not running when stop complete.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::STARTING) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan was not started when stop complete.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::FAILED) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan was in failed state when stop complete.");
 | 
			
		||||
    } else if (this->scanner_state_ == ScannerState::IDLE) {
 | 
			
		||||
      ESP_LOGE(TAG, "Scan was idle when stop complete.");
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Perform cleanup and transition to IDLE
 | 
			
		||||
@@ -639,10 +682,25 @@ void ESP32BLETracker::dump_config() {
 | 
			
		||||
                "  Continuous Scanning: %s",
 | 
			
		||||
                this->scan_duration_, this->scan_interval_ * 0.625f, this->scan_window_ * 0.625f,
 | 
			
		||||
                this->scan_active_ ? "ACTIVE" : "PASSIVE", YESNO(this->scan_continuous_));
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Scanner State: %s", this->scanner_state_to_string_(this->scanner_state_));
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Connecting: %d, discovered: %d, searching: %d, disconnecting: %d",
 | 
			
		||||
                this->client_state_counts_.connecting, this->client_state_counts_.discovered,
 | 
			
		||||
                this->client_state_counts_.searching, this->client_state_counts_.disconnecting);
 | 
			
		||||
  switch (this->scanner_state_) {
 | 
			
		||||
    case ScannerState::IDLE:
 | 
			
		||||
      ESP_LOGCONFIG(TAG, "  Scanner State: IDLE");
 | 
			
		||||
      break;
 | 
			
		||||
    case ScannerState::STARTING:
 | 
			
		||||
      ESP_LOGCONFIG(TAG, "  Scanner State: STARTING");
 | 
			
		||||
      break;
 | 
			
		||||
    case ScannerState::RUNNING:
 | 
			
		||||
      ESP_LOGCONFIG(TAG, "  Scanner State: RUNNING");
 | 
			
		||||
      break;
 | 
			
		||||
    case ScannerState::STOPPING:
 | 
			
		||||
      ESP_LOGCONFIG(TAG, "  Scanner State: STOPPING");
 | 
			
		||||
      break;
 | 
			
		||||
    case ScannerState::FAILED:
 | 
			
		||||
      ESP_LOGCONFIG(TAG, "  Scanner State: FAILED");
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "  Connecting: %d, discovered: %d, searching: %d, disconnecting: %d", connecting_, discovered_,
 | 
			
		||||
                searching_, disconnecting_);
 | 
			
		||||
  if (this->scan_start_fail_count_) {
 | 
			
		||||
    ESP_LOGCONFIG(TAG, "  Scan Start Fail Count: %d", this->scan_start_fail_count_);
 | 
			
		||||
  }
 | 
			
		||||
@@ -781,11 +839,8 @@ bool ESP32BLETracker::process_scan_result_(const BLEScanResult &scan_result) {
 | 
			
		||||
 | 
			
		||||
void ESP32BLETracker::cleanup_scan_state_(bool is_stop_complete) {
 | 
			
		||||
  ESP_LOGD(TAG, "Scan %scomplete, set scanner state to IDLE.", is_stop_complete ? "stop " : "");
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
  this->already_discovered_.clear();
 | 
			
		||||
#endif
 | 
			
		||||
  // Reset timeout state machine instead of cancelling scheduler timeout
 | 
			
		||||
  this->scan_timeout_state_ = ScanTimeoutState::INACTIVE;
 | 
			
		||||
  this->cancel_timeout("scan");
 | 
			
		||||
 | 
			
		||||
  for (auto *listener : this->listeners_)
 | 
			
		||||
    listener->on_scan_end();
 | 
			
		||||
@@ -793,84 +848,6 @@ void ESP32BLETracker::cleanup_scan_state_(bool is_stop_complete) {
 | 
			
		||||
  this->set_scanner_state_(ScannerState::IDLE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ESP32BLETracker::handle_scanner_failure_() {
 | 
			
		||||
  this->stop_scan_();
 | 
			
		||||
  if (this->scan_start_fail_count_ == std::numeric_limits<uint8_t>::max()) {
 | 
			
		||||
    ESP_LOGE(TAG, "Scan could not restart after %d attempts, rebooting to restore stack (IDF)",
 | 
			
		||||
             std::numeric_limits<uint8_t>::max());
 | 
			
		||||
    App.reboot();
 | 
			
		||||
  }
 | 
			
		||||
  if (this->scan_start_failed_) {
 | 
			
		||||
    ESP_LOGE(TAG, "Scan start failed: %d", this->scan_start_failed_);
 | 
			
		||||
    this->scan_start_failed_ = ESP_BT_STATUS_SUCCESS;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->scan_set_param_failed_) {
 | 
			
		||||
    ESP_LOGE(TAG, "Scan set param failed: %d", this->scan_set_param_failed_);
 | 
			
		||||
    this->scan_set_param_failed_ = ESP_BT_STATUS_SUCCESS;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ESP32BLETracker::try_promote_discovered_clients_() {
 | 
			
		||||
  // Only promote the first discovered client to avoid multiple simultaneous connections
 | 
			
		||||
  for (auto *client : this->clients_) {
 | 
			
		||||
    if (client->state() != ClientState::DISCOVERED) {
 | 
			
		||||
      continue;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (this->scanner_state_ == ScannerState::RUNNING) {
 | 
			
		||||
      ESP_LOGD(TAG, "Stopping scan to make connection");
 | 
			
		||||
      this->stop_scan_();
 | 
			
		||||
      // Don't wait for scan stop complete - promote immediately.
 | 
			
		||||
      // This is safe because ESP-IDF processes BLE commands sequentially through its internal mailbox queue.
 | 
			
		||||
      // This guarantees that the stop scan command will be fully processed before any subsequent connect command,
 | 
			
		||||
      // preventing race conditions or overlapping operations.
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ESP_LOGD(TAG, "Promoting client to connect");
 | 
			
		||||
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
 | 
			
		||||
    this->update_coex_preference_(true);
 | 
			
		||||
#endif
 | 
			
		||||
    client->set_state(ClientState::READY_TO_CONNECT);
 | 
			
		||||
    break;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const char *ESP32BLETracker::scanner_state_to_string_(ScannerState state) const {
 | 
			
		||||
  switch (state) {
 | 
			
		||||
    case ScannerState::IDLE:
 | 
			
		||||
      return "IDLE";
 | 
			
		||||
    case ScannerState::STARTING:
 | 
			
		||||
      return "STARTING";
 | 
			
		||||
    case ScannerState::RUNNING:
 | 
			
		||||
      return "RUNNING";
 | 
			
		||||
    case ScannerState::STOPPING:
 | 
			
		||||
      return "STOPPING";
 | 
			
		||||
    case ScannerState::FAILED:
 | 
			
		||||
      return "FAILED";
 | 
			
		||||
    default:
 | 
			
		||||
      return "UNKNOWN";
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ESP32BLETracker::log_unexpected_state_(const char *operation, ScannerState expected_state) const {
 | 
			
		||||
  ESP_LOGE(TAG, "Unexpected state: %s on %s, expected: %s", this->scanner_state_to_string_(this->scanner_state_),
 | 
			
		||||
           operation, this->scanner_state_to_string_(expected_state));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
 | 
			
		||||
void ESP32BLETracker::update_coex_preference_(bool force_ble) {
 | 
			
		||||
  if (force_ble && !this->coex_prefer_ble_) {
 | 
			
		||||
    ESP_LOGD(TAG, "Setting coexistence to Bluetooth to make connection.");
 | 
			
		||||
    this->coex_prefer_ble_ = true;
 | 
			
		||||
    esp_coex_preference_set(ESP_COEX_PREFER_BT);  // Prioritize Bluetooth
 | 
			
		||||
  } else if (!force_ble && this->coex_prefer_ble_) {
 | 
			
		||||
    ESP_LOGD(TAG, "Setting coexistence preference to balanced.");
 | 
			
		||||
    this->coex_prefer_ble_ = false;
 | 
			
		||||
    esp_coex_preference_set(ESP_COEX_PREFER_BALANCE);  // Reset to default
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome::esp32_ble_tracker
 | 
			
		||||
 | 
			
		||||
#endif  // USE_ESP32
 | 
			
		||||
 
 | 
			
		||||
@@ -33,12 +33,10 @@ enum AdvertisementParserType {
 | 
			
		||||
  RAW_ADVERTISEMENTS,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_UUID
 | 
			
		||||
struct ServiceData {
 | 
			
		||||
  ESPBTUUID uuid;
 | 
			
		||||
  adv_data_t data;
 | 
			
		||||
};
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
class ESPBLEiBeacon {
 | 
			
		||||
@@ -138,20 +136,6 @@ class ESPBTDeviceListener {
 | 
			
		||||
  ESP32BLETracker *parent_{nullptr};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct ClientStateCounts {
 | 
			
		||||
  uint8_t connecting = 0;
 | 
			
		||||
  uint8_t discovered = 0;
 | 
			
		||||
  uint8_t searching = 0;
 | 
			
		||||
  uint8_t disconnecting = 0;
 | 
			
		||||
 | 
			
		||||
  bool operator==(const ClientStateCounts &other) const {
 | 
			
		||||
    return connecting == other.connecting && discovered == other.discovered && searching == other.searching &&
 | 
			
		||||
           disconnecting == other.disconnecting;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool operator!=(const ClientStateCounts &other) const { return !(*this == other); }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum class ClientState : uint8_t {
 | 
			
		||||
  // Connection is allocated
 | 
			
		||||
  INIT,
 | 
			
		||||
@@ -186,9 +170,6 @@ enum class ScannerState {
 | 
			
		||||
  STOPPING,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Helper function to convert ClientState to string
 | 
			
		||||
const char *client_state_to_string(ClientState state);
 | 
			
		||||
 | 
			
		||||
enum class ConnectionType : uint8_t {
 | 
			
		||||
  // The default connection type, we hold all the services in ram
 | 
			
		||||
  // for the duration of the connection.
 | 
			
		||||
@@ -298,85 +279,38 @@ class ESP32BLETracker : public Component,
 | 
			
		||||
  /// Check if any clients are in connecting or ready to connect state
 | 
			
		||||
  bool has_connecting_clients_() const;
 | 
			
		||||
#endif
 | 
			
		||||
  /// Handle scanner failure states
 | 
			
		||||
  void handle_scanner_failure_();
 | 
			
		||||
  /// Try to promote discovered clients to ready to connect
 | 
			
		||||
  void try_promote_discovered_clients_();
 | 
			
		||||
  /// Convert scanner state enum to string for logging
 | 
			
		||||
  const char *scanner_state_to_string_(ScannerState state) const;
 | 
			
		||||
  /// Log an unexpected scanner state
 | 
			
		||||
  void log_unexpected_state_(const char *operation, ScannerState expected_state) const;
 | 
			
		||||
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
 | 
			
		||||
  /// Update BLE coexistence preference
 | 
			
		||||
  void update_coex_preference_(bool force_ble);
 | 
			
		||||
#endif
 | 
			
		||||
  /// Count clients in each state
 | 
			
		||||
  ClientStateCounts count_client_states_() const {
 | 
			
		||||
    ClientStateCounts counts;
 | 
			
		||||
    for (auto *client : this->clients_) {
 | 
			
		||||
      switch (client->state()) {
 | 
			
		||||
        case ClientState::DISCONNECTING:
 | 
			
		||||
          counts.disconnecting++;
 | 
			
		||||
          break;
 | 
			
		||||
        case ClientState::DISCOVERED:
 | 
			
		||||
          counts.discovered++;
 | 
			
		||||
          break;
 | 
			
		||||
        case ClientState::SEARCHING:
 | 
			
		||||
          counts.searching++;
 | 
			
		||||
          break;
 | 
			
		||||
        case ClientState::CONNECTING:
 | 
			
		||||
        case ClientState::READY_TO_CONNECT:
 | 
			
		||||
          counts.connecting++;
 | 
			
		||||
          break;
 | 
			
		||||
        default:
 | 
			
		||||
          break;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return counts;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Group 1: Large objects (12+ bytes) - vectors and callback manager
 | 
			
		||||
  std::vector<ESPBTDeviceListener *> listeners_;
 | 
			
		||||
  std::vector<ESPBTClient *> clients_;
 | 
			
		||||
  CallbackManager<void(ScannerState)> scanner_state_callbacks_;
 | 
			
		||||
#ifdef USE_ESP32_BLE_DEVICE
 | 
			
		||||
  uint8_t app_id_{0};
 | 
			
		||||
 | 
			
		||||
  /// Vector of addresses that have already been printed in print_bt_device_info
 | 
			
		||||
  std::vector<uint64_t> already_discovered_;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // Group 2: Structs (aligned to 4 bytes)
 | 
			
		||||
  std::vector<ESPBTDeviceListener *> listeners_;
 | 
			
		||||
  /// Client parameters.
 | 
			
		||||
  std::vector<ESPBTClient *> clients_;
 | 
			
		||||
  /// A structure holding the ESP BLE scan parameters.
 | 
			
		||||
  esp_ble_scan_params_t scan_params_;
 | 
			
		||||
  ClientStateCounts client_state_counts_;
 | 
			
		||||
 | 
			
		||||
  // Group 3: 4-byte types
 | 
			
		||||
  /// The interval in seconds to perform scans.
 | 
			
		||||
  uint32_t scan_duration_;
 | 
			
		||||
  uint32_t scan_interval_;
 | 
			
		||||
  uint32_t scan_window_;
 | 
			
		||||
  esp_bt_status_t scan_start_failed_{ESP_BT_STATUS_SUCCESS};
 | 
			
		||||
  esp_bt_status_t scan_set_param_failed_{ESP_BT_STATUS_SUCCESS};
 | 
			
		||||
 | 
			
		||||
  // Group 4: 1-byte types (enums, uint8_t, bool)
 | 
			
		||||
  uint8_t app_id_{0};
 | 
			
		||||
  uint8_t scan_start_fail_count_{0};
 | 
			
		||||
  ScannerState scanner_state_{ScannerState::IDLE};
 | 
			
		||||
  bool scan_continuous_;
 | 
			
		||||
  bool scan_active_;
 | 
			
		||||
  ScannerState scanner_state_{ScannerState::IDLE};
 | 
			
		||||
  CallbackManager<void(ScannerState)> scanner_state_callbacks_;
 | 
			
		||||
  bool ble_was_disabled_{true};
 | 
			
		||||
  bool raw_advertisements_{false};
 | 
			
		||||
  bool parse_advertisements_{false};
 | 
			
		||||
 | 
			
		||||
  esp_bt_status_t scan_start_failed_{ESP_BT_STATUS_SUCCESS};
 | 
			
		||||
  esp_bt_status_t scan_set_param_failed_{ESP_BT_STATUS_SUCCESS};
 | 
			
		||||
  int connecting_{0};
 | 
			
		||||
  int discovered_{0};
 | 
			
		||||
  int searching_{0};
 | 
			
		||||
  int disconnecting_{0};
 | 
			
		||||
#ifdef USE_ESP32_BLE_SOFTWARE_COEXISTENCE
 | 
			
		||||
  bool coex_prefer_ble_{false};
 | 
			
		||||
#endif
 | 
			
		||||
  // Scan timeout state machine
 | 
			
		||||
  enum class ScanTimeoutState : uint8_t {
 | 
			
		||||
    INACTIVE,       // No timeout monitoring
 | 
			
		||||
    MONITORING,     // Actively monitoring for timeout
 | 
			
		||||
    EXCEEDED_WAIT,  // Timeout exceeded, waiting one loop before reboot
 | 
			
		||||
  };
 | 
			
		||||
  uint32_t scan_start_time_{0};
 | 
			
		||||
  ScanTimeoutState scan_timeout_state_{ScanTimeoutState::INACTIVE};
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// NOLINTNEXTLINE
 | 
			
		||||
 
 | 
			
		||||
@@ -345,7 +345,7 @@ async def to_code(config):
 | 
			
		||||
    cg.add_define("USE_CAMERA")
 | 
			
		||||
 | 
			
		||||
    if CORE.using_esp_idf:
 | 
			
		||||
        add_idf_component(name="espressif/esp32-camera", ref="2.1.1")
 | 
			
		||||
        add_idf_component(name="espressif/esp32-camera", ref="2.1.0")
 | 
			
		||||
 | 
			
		||||
    for conf in config.get(CONF_ON_STREAM_START, []):
 | 
			
		||||
        trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
 | 
			
		||||
 
 | 
			
		||||
@@ -42,6 +42,9 @@ static size_t IRAM_ATTR HOT encoder_callback(const void *data, size_t size, size
 | 
			
		||||
        symbols[i] = params->bit0;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    if ((index + 1) >= size && params->reset.duration0 == 0 && params->reset.duration1 == 0) {
 | 
			
		||||
      *done = true;
 | 
			
		||||
    }
 | 
			
		||||
    return RMT_SYMBOLS_PER_BYTE;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -107,7 +110,7 @@ void ESP32RMTLEDStripLightOutput::setup() {
 | 
			
		||||
  memset(&encoder, 0, sizeof(encoder));
 | 
			
		||||
  encoder.callback = encoder_callback;
 | 
			
		||||
  encoder.arg = &this->params_;
 | 
			
		||||
  encoder.min_chunk_size = RMT_SYMBOLS_PER_BYTE;
 | 
			
		||||
  encoder.min_chunk_size = 8;
 | 
			
		||||
  if (rmt_new_simple_encoder(&encoder, &this->encoder_) != ESP_OK) {
 | 
			
		||||
    ESP_LOGE(TAG, "Encoder creation failed");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
 
 | 
			
		||||
@@ -171,8 +171,8 @@ class ESP32TouchComponent : public Component {
 | 
			
		||||
  // based on the filter configuration
 | 
			
		||||
  uint32_t read_touch_value(touch_pad_t pad) const;
 | 
			
		||||
 | 
			
		||||
  // Helper to update touch state with a known state and value
 | 
			
		||||
  void update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched, uint32_t value);
 | 
			
		||||
  // Helper to update touch state with a known state
 | 
			
		||||
  void update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched);
 | 
			
		||||
 | 
			
		||||
  // Helper to read touch value and update state for a given child
 | 
			
		||||
  bool check_and_update_touch_state_(ESP32TouchBinarySensor *child);
 | 
			
		||||
@@ -234,13 +234,9 @@ class ESP32TouchBinarySensor : public binary_sensor::BinarySensor {
 | 
			
		||||
  touch_pad_t get_touch_pad() const { return this->touch_pad_; }
 | 
			
		||||
  uint32_t get_threshold() const { return this->threshold_; }
 | 
			
		||||
  void set_threshold(uint32_t threshold) { this->threshold_ = threshold; }
 | 
			
		||||
 | 
			
		||||
  /// Get the raw touch measurement value.
 | 
			
		||||
  /// @note Although this method may appear unused within the component, it is a public API
 | 
			
		||||
  /// used by lambdas in user configurations for custom touch value processing.
 | 
			
		||||
  /// @return The current raw touch sensor reading
 | 
			
		||||
#ifdef USE_ESP32_VARIANT_ESP32
 | 
			
		||||
  uint32_t get_value() const { return this->value_; }
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
  uint32_t get_wakeup_threshold() const { return this->wakeup_threshold_; }
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
@@ -249,8 +245,9 @@ class ESP32TouchBinarySensor : public binary_sensor::BinarySensor {
 | 
			
		||||
  touch_pad_t touch_pad_{TOUCH_PAD_MAX};
 | 
			
		||||
  uint32_t threshold_{0};
 | 
			
		||||
  uint32_t benchmark_{};
 | 
			
		||||
  /// Stores the last raw touch measurement value.
 | 
			
		||||
#ifdef USE_ESP32_VARIANT_ESP32
 | 
			
		||||
  uint32_t value_{0};
 | 
			
		||||
#endif
 | 
			
		||||
  bool last_state_{false};
 | 
			
		||||
  const uint32_t wakeup_threshold_{0};
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -100,8 +100,6 @@ void ESP32TouchComponent::process_setup_mode_logging_(uint32_t now) {
 | 
			
		||||
#else
 | 
			
		||||
      // Read the value being used for touch detection
 | 
			
		||||
      uint32_t value = this->read_touch_value(child->get_touch_pad());
 | 
			
		||||
      // Store the value for get_value() access in lambdas
 | 
			
		||||
      child->value_ = value;
 | 
			
		||||
      ESP_LOGD(TAG, "Touch Pad '%s' (T%d): %d", child->get_name().c_str(), child->get_touch_pad(), value);
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -10,11 +10,8 @@ namespace esp32_touch {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "esp32_touch";
 | 
			
		||||
 | 
			
		||||
// Helper to update touch state with a known state and value
 | 
			
		||||
void ESP32TouchComponent::update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched, uint32_t value) {
 | 
			
		||||
  // Store the value for get_value() access in lambdas
 | 
			
		||||
  child->value_ = value;
 | 
			
		||||
 | 
			
		||||
// Helper to update touch state with a known state
 | 
			
		||||
void ESP32TouchComponent::update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched) {
 | 
			
		||||
  // Always update timer when touched
 | 
			
		||||
  if (is_touched) {
 | 
			
		||||
    child->last_touch_time_ = App.get_loop_component_start_time();
 | 
			
		||||
@@ -24,8 +21,9 @@ void ESP32TouchComponent::update_touch_state_(ESP32TouchBinarySensor *child, boo
 | 
			
		||||
    child->last_state_ = is_touched;
 | 
			
		||||
    child->publish_state(is_touched);
 | 
			
		||||
    if (is_touched) {
 | 
			
		||||
      // ESP32-S2/S3 v2: touched when value > threshold
 | 
			
		||||
      ESP_LOGV(TAG, "Touch Pad '%s' state: ON (value: %" PRIu32 " > threshold: %" PRIu32 ")", child->get_name().c_str(),
 | 
			
		||||
               value, child->threshold_ + child->benchmark_);
 | 
			
		||||
               this->read_touch_value(child->touch_pad_), child->threshold_ + child->benchmark_);
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGV(TAG, "Touch Pad '%s' state: OFF", child->get_name().c_str());
 | 
			
		||||
    }
 | 
			
		||||
@@ -43,7 +41,7 @@ bool ESP32TouchComponent::check_and_update_touch_state_(ESP32TouchBinarySensor *
 | 
			
		||||
           child->get_name().c_str(), child->touch_pad_, value, child->threshold_, child->benchmark_);
 | 
			
		||||
  bool is_touched = value > child->benchmark_ + child->threshold_;
 | 
			
		||||
 | 
			
		||||
  this->update_touch_state_(child, is_touched, value);
 | 
			
		||||
  this->update_touch_state_(child, is_touched);
 | 
			
		||||
  return is_touched;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -298,9 +296,7 @@ void ESP32TouchComponent::loop() {
 | 
			
		||||
          this->check_and_update_touch_state_(child);
 | 
			
		||||
        } else if (event.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
 | 
			
		||||
          // We only get ACTIVE interrupts now, releases are detected by timeout
 | 
			
		||||
          // Read the current value
 | 
			
		||||
          uint32_t value = this->read_touch_value(child->touch_pad_);
 | 
			
		||||
          this->update_touch_state_(child, true, value);  // Always touched for ACTIVE interrupts
 | 
			
		||||
          this->update_touch_state_(child, true);  // Always touched for ACTIVE interrupts
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
      }
 | 
			
		||||
 
 | 
			
		||||
@@ -19,9 +19,7 @@
 | 
			
		||||
namespace esphome {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "esphome.ota";
 | 
			
		||||
static constexpr uint16_t OTA_BLOCK_SIZE = 8192;
 | 
			
		||||
static constexpr uint32_t OTA_SOCKET_TIMEOUT_HANDSHAKE = 10000;  // milliseconds for initial handshake
 | 
			
		||||
static constexpr uint32_t OTA_SOCKET_TIMEOUT_DATA = 90000;       // milliseconds for data transfer
 | 
			
		||||
static constexpr u_int16_t OTA_BLOCK_SIZE = 8192;
 | 
			
		||||
 | 
			
		||||
void ESPHomeOTAComponent::setup() {
 | 
			
		||||
#ifdef USE_OTA_STATE_CALLBACK
 | 
			
		||||
@@ -30,19 +28,19 @@ void ESPHomeOTAComponent::setup() {
 | 
			
		||||
 | 
			
		||||
  this->server_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0);  // monitored for incoming connections
 | 
			
		||||
  if (this->server_ == nullptr) {
 | 
			
		||||
    this->log_socket_error_("creation");
 | 
			
		||||
    ESP_LOGW(TAG, "Could not create socket");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  int enable = 1;
 | 
			
		||||
  int err = this->server_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
 | 
			
		||||
  if (err != 0) {
 | 
			
		||||
    this->log_socket_error_("reuseaddr");
 | 
			
		||||
    ESP_LOGW(TAG, "Socket unable to set reuseaddr: errno %d", err);
 | 
			
		||||
    // we can still continue
 | 
			
		||||
  }
 | 
			
		||||
  err = this->server_->setblocking(false);
 | 
			
		||||
  if (err != 0) {
 | 
			
		||||
    this->log_socket_error_("non-blocking");
 | 
			
		||||
    ESP_LOGW(TAG, "Socket unable to set nonblocking mode: errno %d", err);
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
@@ -51,21 +49,21 @@ void ESPHomeOTAComponent::setup() {
 | 
			
		||||
 | 
			
		||||
  socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), this->port_);
 | 
			
		||||
  if (sl == 0) {
 | 
			
		||||
    this->log_socket_error_("set sockaddr");
 | 
			
		||||
    ESP_LOGW(TAG, "Socket unable to set sockaddr: errno %d", errno);
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  err = this->server_->bind((struct sockaddr *) &server, sizeof(server));
 | 
			
		||||
  if (err != 0) {
 | 
			
		||||
    this->log_socket_error_("bind");
 | 
			
		||||
    ESP_LOGW(TAG, "Socket unable to bind: errno %d", errno);
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  err = this->server_->listen(4);
 | 
			
		||||
  if (err != 0) {
 | 
			
		||||
    this->log_socket_error_("listen");
 | 
			
		||||
    ESP_LOGW(TAG, "Socket unable to listen: errno %d", errno);
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
@@ -85,93 +83,17 @@ void ESPHomeOTAComponent::dump_config() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ESPHomeOTAComponent::loop() {
 | 
			
		||||
  // Skip handle_handshake_() call if no client connected and no incoming connections
 | 
			
		||||
  // Skip handle_() call if no client connected and no incoming connections
 | 
			
		||||
  // This optimization reduces idle loop overhead when OTA is not active
 | 
			
		||||
  // Note: No need to check server_ for null as the component is marked failed in setup()
 | 
			
		||||
  // if server_ creation fails
 | 
			
		||||
  // Note: No need to check server_ for null as the component is marked failed in setup() if server_ creation fails
 | 
			
		||||
  if (this->client_ != nullptr || this->server_->ready()) {
 | 
			
		||||
    this->handle_handshake_();
 | 
			
		||||
    this->handle_();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static const uint8_t FEATURE_SUPPORTS_COMPRESSION = 0x01;
 | 
			
		||||
 | 
			
		||||
void ESPHomeOTAComponent::handle_handshake_() {
 | 
			
		||||
  /// Handle the initial OTA handshake.
 | 
			
		||||
  ///
 | 
			
		||||
  /// This method is non-blocking and will return immediately if no data is available.
 | 
			
		||||
  /// It waits for the first magic byte (0x6C) before proceeding to handle_data_().
 | 
			
		||||
  /// A 10-second timeout is enforced from initial connection.
 | 
			
		||||
 | 
			
		||||
  if (this->client_ == nullptr) {
 | 
			
		||||
    // We already checked server_->ready() in loop(), so we can accept directly
 | 
			
		||||
    struct sockaddr_storage source_addr;
 | 
			
		||||
    socklen_t addr_len = sizeof(source_addr);
 | 
			
		||||
    int enable = 1;
 | 
			
		||||
 | 
			
		||||
    this->client_ = this->server_->accept_loop_monitored((struct sockaddr *) &source_addr, &addr_len);
 | 
			
		||||
    if (this->client_ == nullptr)
 | 
			
		||||
      return;
 | 
			
		||||
    int err = this->client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
 | 
			
		||||
    if (err != 0) {
 | 
			
		||||
      this->log_socket_error_("nodelay");
 | 
			
		||||
      this->cleanup_connection_();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    err = this->client_->setblocking(false);
 | 
			
		||||
    if (err != 0) {
 | 
			
		||||
      this->log_socket_error_("non-blocking");
 | 
			
		||||
      this->cleanup_connection_();
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    this->log_start_("handshake");
 | 
			
		||||
    this->client_connect_time_ = App.get_loop_component_start_time();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Check for handshake timeout
 | 
			
		||||
  uint32_t now = App.get_loop_component_start_time();
 | 
			
		||||
  if (now - this->client_connect_time_ > OTA_SOCKET_TIMEOUT_HANDSHAKE) {
 | 
			
		||||
    ESP_LOGW(TAG, "Handshake timeout");
 | 
			
		||||
    this->cleanup_connection_();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Try to read first byte of magic bytes
 | 
			
		||||
  uint8_t first_byte;
 | 
			
		||||
  ssize_t read = this->client_->read(&first_byte, 1);
 | 
			
		||||
 | 
			
		||||
  if (read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
 | 
			
		||||
    return;  // No data yet, try again next loop
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (read <= 0) {
 | 
			
		||||
    // Error or connection closed
 | 
			
		||||
    if (read == -1) {
 | 
			
		||||
      this->log_socket_error_("reading first byte");
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGW(TAG, "Remote closed during handshake");
 | 
			
		||||
    }
 | 
			
		||||
    this->cleanup_connection_();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Got first byte, check if it's the magic byte
 | 
			
		||||
  if (first_byte != 0x6C) {
 | 
			
		||||
    ESP_LOGW(TAG, "Invalid initial byte: 0x%02X", first_byte);
 | 
			
		||||
    this->cleanup_connection_();
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // First byte is valid, continue with data handling
 | 
			
		||||
  this->handle_data_();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
  /// Handle the OTA data transfer and update process.
 | 
			
		||||
  ///
 | 
			
		||||
  /// This method is blocking and will not return until the OTA update completes,
 | 
			
		||||
  /// fails, or times out. It handles authentication, receives the firmware data,
 | 
			
		||||
  /// writes it to flash, and reboots on success.
 | 
			
		||||
void ESPHomeOTAComponent::handle_() {
 | 
			
		||||
  ota::OTAResponseTypes error_code = ota::OTA_RESPONSE_ERROR_UNKNOWN;
 | 
			
		||||
  bool update_started = false;
 | 
			
		||||
  size_t total = 0;
 | 
			
		||||
@@ -186,14 +108,38 @@ void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
  size_t size_acknowledged = 0;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // Read remaining 4 bytes of magic (we already read the first byte 0x6C in handle_handshake_)
 | 
			
		||||
  if (!this->readall_(buf, 4)) {
 | 
			
		||||
    this->log_read_error_("magic bytes");
 | 
			
		||||
  if (this->client_ == nullptr) {
 | 
			
		||||
    // We already checked server_->ready() in loop(), so we can accept directly
 | 
			
		||||
    struct sockaddr_storage source_addr;
 | 
			
		||||
    socklen_t addr_len = sizeof(source_addr);
 | 
			
		||||
    this->client_ = this->server_->accept((struct sockaddr *) &source_addr, &addr_len);
 | 
			
		||||
    if (this->client_ == nullptr)
 | 
			
		||||
      return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  int enable = 1;
 | 
			
		||||
  int err = this->client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
 | 
			
		||||
  if (err != 0) {
 | 
			
		||||
    ESP_LOGW(TAG, "Socket could not enable TCP nodelay, errno %d", errno);
 | 
			
		||||
    this->client_->close();
 | 
			
		||||
    this->client_ = nullptr;
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  ESP_LOGD(TAG, "Starting update from %s", this->client_->getpeername().c_str());
 | 
			
		||||
  this->status_set_warning();
 | 
			
		||||
#ifdef USE_OTA_STATE_CALLBACK
 | 
			
		||||
  this->state_callback_.call(ota::OTA_STARTED, 0.0f, 0);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  if (!this->readall_(buf, 5)) {
 | 
			
		||||
    ESP_LOGW(TAG, "Reading magic bytes failed");
 | 
			
		||||
    goto error;  // NOLINT(cppcoreguidelines-avoid-goto)
 | 
			
		||||
  }
 | 
			
		||||
  // Check remaining magic bytes: 0x26, 0xF7, 0x5C, 0x45
 | 
			
		||||
  if (buf[0] != 0x26 || buf[1] != 0xF7 || buf[2] != 0x5C || buf[3] != 0x45) {
 | 
			
		||||
    ESP_LOGW(TAG, "Magic bytes mismatch! 0x6C-0x%02X-0x%02X-0x%02X-0x%02X", buf[0], buf[1], buf[2], buf[3]);
 | 
			
		||||
  // 0x6C, 0x26, 0xF7, 0x5C, 0x45
 | 
			
		||||
  if (buf[0] != 0x6C || buf[1] != 0x26 || buf[2] != 0xF7 || buf[3] != 0x5C || buf[4] != 0x45) {
 | 
			
		||||
    ESP_LOGW(TAG, "Magic bytes do not match! 0x%02X-0x%02X-0x%02X-0x%02X-0x%02X", buf[0], buf[1], buf[2], buf[3],
 | 
			
		||||
             buf[4]);
 | 
			
		||||
    error_code = ota::OTA_RESPONSE_ERROR_MAGIC;
 | 
			
		||||
    goto error;  // NOLINT(cppcoreguidelines-avoid-goto)
 | 
			
		||||
  }
 | 
			
		||||
@@ -207,7 +153,7 @@ void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
 | 
			
		||||
  // Read features - 1 byte
 | 
			
		||||
  if (!this->readall_(buf, 1)) {
 | 
			
		||||
    this->log_read_error_("features");
 | 
			
		||||
    ESP_LOGW(TAG, "Reading features failed");
 | 
			
		||||
    goto error;  // NOLINT(cppcoreguidelines-avoid-goto)
 | 
			
		||||
  }
 | 
			
		||||
  ota_features = buf[0];  // NOLINT
 | 
			
		||||
@@ -286,7 +232,7 @@ void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
 | 
			
		||||
  // Read size, 4 bytes MSB first
 | 
			
		||||
  if (!this->readall_(buf, 4)) {
 | 
			
		||||
    this->log_read_error_("size");
 | 
			
		||||
    ESP_LOGW(TAG, "Reading size failed");
 | 
			
		||||
    goto error;  // NOLINT(cppcoreguidelines-avoid-goto)
 | 
			
		||||
  }
 | 
			
		||||
  ota_size = 0;
 | 
			
		||||
@@ -296,17 +242,6 @@ void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
  }
 | 
			
		||||
  ESP_LOGV(TAG, "Size is %u bytes", ota_size);
 | 
			
		||||
 | 
			
		||||
  // Now that we've passed authentication and are actually
 | 
			
		||||
  // starting the update, set the warning status and notify
 | 
			
		||||
  // listeners. This ensures that port scanners do not
 | 
			
		||||
  // accidentally trigger the update process.
 | 
			
		||||
  this->log_start_("update");
 | 
			
		||||
  this->status_set_warning();
 | 
			
		||||
#ifdef USE_OTA_STATE_CALLBACK
 | 
			
		||||
  this->state_callback_.call(ota::OTA_STARTED, 0.0f, 0);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // This will block for a few seconds as it locks flash
 | 
			
		||||
  error_code = backend->begin(ota_size);
 | 
			
		||||
  if (error_code != ota::OTA_RESPONSE_OK)
 | 
			
		||||
    goto error;  // NOLINT(cppcoreguidelines-avoid-goto)
 | 
			
		||||
@@ -318,7 +253,7 @@ void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
 | 
			
		||||
  // Read binary MD5, 32 bytes
 | 
			
		||||
  if (!this->readall_(buf, 32)) {
 | 
			
		||||
    this->log_read_error_("MD5 checksum");
 | 
			
		||||
    ESP_LOGW(TAG, "Reading binary MD5 checksum failed");
 | 
			
		||||
    goto error;  // NOLINT(cppcoreguidelines-avoid-goto)
 | 
			
		||||
  }
 | 
			
		||||
  sbuf[32] = '\0';
 | 
			
		||||
@@ -335,22 +270,23 @@ void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
    ssize_t read = this->client_->read(buf, requested);
 | 
			
		||||
    if (read == -1) {
 | 
			
		||||
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
 | 
			
		||||
        this->yield_and_feed_watchdog_();
 | 
			
		||||
        App.feed_wdt();
 | 
			
		||||
        delay(1);
 | 
			
		||||
        continue;
 | 
			
		||||
      }
 | 
			
		||||
      ESP_LOGW(TAG, "Read error, errno %d", errno);
 | 
			
		||||
      ESP_LOGW(TAG, "Error receiving data for update, errno %d", errno);
 | 
			
		||||
      goto error;  // NOLINT(cppcoreguidelines-avoid-goto)
 | 
			
		||||
    } else if (read == 0) {
 | 
			
		||||
      // $ man recv
 | 
			
		||||
      // "When  a  stream socket peer has performed an orderly shutdown, the return value will
 | 
			
		||||
      // be 0 (the traditional "end-of-file" return)."
 | 
			
		||||
      ESP_LOGW(TAG, "Remote closed connection");
 | 
			
		||||
      ESP_LOGW(TAG, "Remote end closed connection");
 | 
			
		||||
      goto error;  // NOLINT(cppcoreguidelines-avoid-goto)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    error_code = backend->write(buf, read);
 | 
			
		||||
    if (error_code != ota::OTA_RESPONSE_OK) {
 | 
			
		||||
      ESP_LOGW(TAG, "Flash write error, code: %d", error_code);
 | 
			
		||||
      ESP_LOGW(TAG, "Error writing binary data to flash!, error_code: %d", error_code);
 | 
			
		||||
      goto error;  // NOLINT(cppcoreguidelines-avoid-goto)
 | 
			
		||||
    }
 | 
			
		||||
    total += read;
 | 
			
		||||
@@ -371,7 +307,8 @@ void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
      this->state_callback_.call(ota::OTA_IN_PROGRESS, percentage, 0);
 | 
			
		||||
#endif
 | 
			
		||||
      // feed watchdog and give other tasks a chance to run
 | 
			
		||||
      this->yield_and_feed_watchdog_();
 | 
			
		||||
      App.feed_wdt();
 | 
			
		||||
      yield();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -381,7 +318,7 @@ void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
 | 
			
		||||
  error_code = backend->end();
 | 
			
		||||
  if (error_code != ota::OTA_RESPONSE_OK) {
 | 
			
		||||
    ESP_LOGW(TAG, "Error ending update! code: %d", error_code);
 | 
			
		||||
    ESP_LOGW(TAG, "Error ending update! error_code: %d", error_code);
 | 
			
		||||
    goto error;  // NOLINT(cppcoreguidelines-avoid-goto)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
@@ -391,11 +328,12 @@ void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
 | 
			
		||||
  // Read ACK
 | 
			
		||||
  if (!this->readall_(buf, 1) || buf[0] != ota::OTA_RESPONSE_OK) {
 | 
			
		||||
    this->log_read_error_("ack");
 | 
			
		||||
    ESP_LOGW(TAG, "Reading back acknowledgement failed");
 | 
			
		||||
    // do not go to error, this is not fatal
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  this->cleanup_connection_();
 | 
			
		||||
  this->client_->close();
 | 
			
		||||
  this->client_ = nullptr;
 | 
			
		||||
  delay(10);
 | 
			
		||||
  ESP_LOGI(TAG, "Update complete");
 | 
			
		||||
  this->status_clear_warning();
 | 
			
		||||
@@ -408,7 +346,8 @@ void ESPHomeOTAComponent::handle_data_() {
 | 
			
		||||
error:
 | 
			
		||||
  buf[0] = static_cast<uint8_t>(error_code);
 | 
			
		||||
  this->writeall_(buf, 1);
 | 
			
		||||
  this->cleanup_connection_();
 | 
			
		||||
  this->client_->close();
 | 
			
		||||
  this->client_ = nullptr;
 | 
			
		||||
 | 
			
		||||
  if (backend != nullptr && update_started) {
 | 
			
		||||
    backend->abort();
 | 
			
		||||
@@ -425,24 +364,28 @@ bool ESPHomeOTAComponent::readall_(uint8_t *buf, size_t len) {
 | 
			
		||||
  uint32_t at = 0;
 | 
			
		||||
  while (len - at > 0) {
 | 
			
		||||
    uint32_t now = millis();
 | 
			
		||||
    if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
 | 
			
		||||
      ESP_LOGW(TAG, "Timeout reading %d bytes", len);
 | 
			
		||||
    if (now - start > 1000) {
 | 
			
		||||
      ESP_LOGW(TAG, "Timed out reading %d bytes of data", len);
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ssize_t read = this->client_->read(buf + at, len - at);
 | 
			
		||||
    if (read == -1) {
 | 
			
		||||
      if (errno != EAGAIN && errno != EWOULDBLOCK) {
 | 
			
		||||
        ESP_LOGW(TAG, "Error reading %d bytes, errno %d", len, errno);
 | 
			
		||||
        return false;
 | 
			
		||||
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
 | 
			
		||||
        App.feed_wdt();
 | 
			
		||||
        delay(1);
 | 
			
		||||
        continue;
 | 
			
		||||
      }
 | 
			
		||||
      ESP_LOGW(TAG, "Failed to read %d bytes of data, errno %d", len, errno);
 | 
			
		||||
      return false;
 | 
			
		||||
    } else if (read == 0) {
 | 
			
		||||
      ESP_LOGW(TAG, "Remote closed connection");
 | 
			
		||||
      return false;
 | 
			
		||||
    } else {
 | 
			
		||||
      at += read;
 | 
			
		||||
    }
 | 
			
		||||
    this->yield_and_feed_watchdog_();
 | 
			
		||||
    App.feed_wdt();
 | 
			
		||||
    delay(1);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return true;
 | 
			
		||||
@@ -452,21 +395,25 @@ bool ESPHomeOTAComponent::writeall_(const uint8_t *buf, size_t len) {
 | 
			
		||||
  uint32_t at = 0;
 | 
			
		||||
  while (len - at > 0) {
 | 
			
		||||
    uint32_t now = millis();
 | 
			
		||||
    if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
 | 
			
		||||
      ESP_LOGW(TAG, "Timeout writing %d bytes", len);
 | 
			
		||||
    if (now - start > 1000) {
 | 
			
		||||
      ESP_LOGW(TAG, "Timed out writing %d bytes of data", len);
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ssize_t written = this->client_->write(buf + at, len - at);
 | 
			
		||||
    if (written == -1) {
 | 
			
		||||
      if (errno != EAGAIN && errno != EWOULDBLOCK) {
 | 
			
		||||
        ESP_LOGW(TAG, "Error writing %d bytes, errno %d", len, errno);
 | 
			
		||||
        return false;
 | 
			
		||||
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
 | 
			
		||||
        App.feed_wdt();
 | 
			
		||||
        delay(1);
 | 
			
		||||
        continue;
 | 
			
		||||
      }
 | 
			
		||||
      ESP_LOGW(TAG, "Failed to write %d bytes of data, errno %d", len, errno);
 | 
			
		||||
      return false;
 | 
			
		||||
    } else {
 | 
			
		||||
      at += written;
 | 
			
		||||
    }
 | 
			
		||||
    this->yield_and_feed_watchdog_();
 | 
			
		||||
    App.feed_wdt();
 | 
			
		||||
    delay(1);
 | 
			
		||||
  }
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
@@ -474,25 +421,5 @@ bool ESPHomeOTAComponent::writeall_(const uint8_t *buf, size_t len) {
 | 
			
		||||
float ESPHomeOTAComponent::get_setup_priority() const { return setup_priority::AFTER_WIFI; }
 | 
			
		||||
uint16_t ESPHomeOTAComponent::get_port() const { return this->port_; }
 | 
			
		||||
void ESPHomeOTAComponent::set_port(uint16_t port) { this->port_ = port; }
 | 
			
		||||
 | 
			
		||||
void ESPHomeOTAComponent::log_socket_error_(const char *msg) { ESP_LOGW(TAG, "Socket %s: errno %d", msg, errno); }
 | 
			
		||||
 | 
			
		||||
void ESPHomeOTAComponent::log_read_error_(const char *what) { ESP_LOGW(TAG, "Read %s failed", what); }
 | 
			
		||||
 | 
			
		||||
void ESPHomeOTAComponent::log_start_(const char *phase) {
 | 
			
		||||
  ESP_LOGD(TAG, "Starting %s from %s", phase, this->client_->getpeername().c_str());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ESPHomeOTAComponent::cleanup_connection_() {
 | 
			
		||||
  this->client_->close();
 | 
			
		||||
  this->client_ = nullptr;
 | 
			
		||||
  this->client_connect_time_ = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ESPHomeOTAComponent::yield_and_feed_watchdog_() {
 | 
			
		||||
  App.feed_wdt();
 | 
			
		||||
  delay(1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
#endif
 | 
			
		||||
 
 | 
			
		||||
@@ -27,22 +27,15 @@ class ESPHomeOTAComponent : public ota::OTAComponent {
 | 
			
		||||
  uint16_t get_port() const;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void handle_handshake_();
 | 
			
		||||
  void handle_data_();
 | 
			
		||||
  void handle_();
 | 
			
		||||
  bool readall_(uint8_t *buf, size_t len);
 | 
			
		||||
  bool writeall_(const uint8_t *buf, size_t len);
 | 
			
		||||
  void log_socket_error_(const char *msg);
 | 
			
		||||
  void log_read_error_(const char *what);
 | 
			
		||||
  void log_start_(const char *phase);
 | 
			
		||||
  void cleanup_connection_();
 | 
			
		||||
  void yield_and_feed_watchdog_();
 | 
			
		||||
 | 
			
		||||
#ifdef USE_OTA_PASSWORD
 | 
			
		||||
  std::string password_;
 | 
			
		||||
#endif  // USE_OTA_PASSWORD
 | 
			
		||||
 | 
			
		||||
  uint16_t port_;
 | 
			
		||||
  uint32_t client_connect_time_{0};
 | 
			
		||||
 | 
			
		||||
  std::unique_ptr<socket::Socket> server_;
 | 
			
		||||
  std::unique_ptr<socket::Socket> client_;
 | 
			
		||||
 
 | 
			
		||||
@@ -65,6 +65,15 @@ CONF_WAIT_FOR_SENT = "wait_for_sent"
 | 
			
		||||
MAX_ESPNOW_PACKET_SIZE = 250  # Maximum size of the payload in bytes
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def _validate_unknown_peer(config):
 | 
			
		||||
    if config[CONF_AUTO_ADD_PEER] and config.get(CONF_ON_UNKNOWN_PEER):
 | 
			
		||||
        raise cv.Invalid(
 | 
			
		||||
            f"'{CONF_ON_UNKNOWN_PEER}' cannot be used when '{CONF_AUTO_ADD_PEER}' is enabled.",
 | 
			
		||||
            path=[CONF_ON_UNKNOWN_PEER],
 | 
			
		||||
        )
 | 
			
		||||
    return config
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.All(
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
@@ -94,6 +103,7 @@ CONFIG_SCHEMA = cv.All(
 | 
			
		||||
        },
 | 
			
		||||
    ).extend(cv.COMPONENT_SCHEMA),
 | 
			
		||||
    cv.only_on_esp32,
 | 
			
		||||
    _validate_unknown_peer,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -114,6 +124,7 @@ async def _trigger_to_code(config):
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    print(config)
 | 
			
		||||
    var = cg.new_Pvariable(config[CONF_ID])
 | 
			
		||||
    await cg.register_component(var, config)
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -40,20 +40,20 @@ template<typename... Ts> class SendAction : public Action<Ts...>, public Parente
 | 
			
		||||
    this->num_running_++;
 | 
			
		||||
    send_callback_t send_callback = [this, x...](esp_err_t status) {
 | 
			
		||||
      if (status == ESP_OK) {
 | 
			
		||||
        if (!this->sent_.empty()) {
 | 
			
		||||
          this->sent_.play(x...);
 | 
			
		||||
        } else if (this->flags_.wait_for_sent) {
 | 
			
		||||
        if (this->sent_.empty() && this->flags_.wait_for_sent) {
 | 
			
		||||
          this->play_next_(x...);
 | 
			
		||||
        } else if (!this->sent_.empty()) {
 | 
			
		||||
          this->sent_.play(x...);
 | 
			
		||||
        }
 | 
			
		||||
      } else {
 | 
			
		||||
        if (!this->error_.empty()) {
 | 
			
		||||
          this->error_.play(x...);
 | 
			
		||||
        } else if (this->flags_.wait_for_sent) {
 | 
			
		||||
        if (this->error_.empty() && this->flags_.wait_for_sent) {
 | 
			
		||||
          if (this->flags_.continue_on_error) {
 | 
			
		||||
            this->play_next_(x...);
 | 
			
		||||
          } else {
 | 
			
		||||
            this->stop_complex();
 | 
			
		||||
          }
 | 
			
		||||
        } else if (!this->error_.empty()) {
 | 
			
		||||
          this->error_.play(x...);
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    };
 | 
			
		||||
 
 | 
			
		||||
@@ -154,7 +154,7 @@ void ESPNowComponent::setup() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ESPNowComponent::enable() {
 | 
			
		||||
  if (this->state_ == ESPNOW_STATE_ENABLED)
 | 
			
		||||
  if (this->state_ != ESPNOW_STATE_ENABLED)
 | 
			
		||||
    return;
 | 
			
		||||
 | 
			
		||||
  ESP_LOGD(TAG, "Enabling");
 | 
			
		||||
@@ -178,7 +178,11 @@ void ESPNowComponent::enable_() {
 | 
			
		||||
 | 
			
		||||
    this->apply_wifi_channel();
 | 
			
		||||
  }
 | 
			
		||||
  this->get_wifi_channel();
 | 
			
		||||
#ifdef USE_WIFI
 | 
			
		||||
  else {
 | 
			
		||||
    this->wifi_channel_ = wifi::global_wifi_component->get_wifi_channel();
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  esp_err_t err = esp_now_init();
 | 
			
		||||
  if (err != ESP_OK) {
 | 
			
		||||
@@ -208,11 +212,10 @@ void ESPNowComponent::enable_() {
 | 
			
		||||
  esp_wifi_connectionless_module_set_wake_interval(CONFIG_ESPNOW_WAKE_INTERVAL);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  this->state_ = ESPNOW_STATE_ENABLED;
 | 
			
		||||
 | 
			
		||||
  for (auto peer : this->peers_) {
 | 
			
		||||
    this->add_peer(peer.address);
 | 
			
		||||
  }
 | 
			
		||||
  this->state_ = ESPNOW_STATE_ENABLED;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void ESPNowComponent::disable() {
 | 
			
		||||
@@ -225,6 +228,10 @@ void ESPNowComponent::disable() {
 | 
			
		||||
  esp_now_unregister_recv_cb();
 | 
			
		||||
  esp_now_unregister_send_cb();
 | 
			
		||||
 | 
			
		||||
  for (auto peer : this->peers_) {
 | 
			
		||||
    this->del_peer(peer.address);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  esp_err_t err = esp_now_deinit();
 | 
			
		||||
  if (err != ESP_OK) {
 | 
			
		||||
    ESP_LOGE(TAG, "esp_now_deinit failed! 0x%x", err);
 | 
			
		||||
@@ -260,6 +267,7 @@ void ESPNowComponent::loop() {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  // Process received packets
 | 
			
		||||
  ESPNowPacket *packet = this->receive_packet_queue_.pop();
 | 
			
		||||
  while (packet != nullptr) {
 | 
			
		||||
@@ -267,15 +275,13 @@ void ESPNowComponent::loop() {
 | 
			
		||||
      case ESPNowPacket::RECEIVED: {
 | 
			
		||||
        const ESPNowRecvInfo info = packet->get_receive_info();
 | 
			
		||||
        if (!esp_now_is_peer_exist(info.src_addr)) {
 | 
			
		||||
          bool handled = false;
 | 
			
		||||
          for (auto *handler : this->unknown_peer_handlers_) {
 | 
			
		||||
            if (handler->on_unknown_peer(info, packet->packet_.receive.data, packet->packet_.receive.size)) {
 | 
			
		||||
              handled = true;
 | 
			
		||||
              break;  // If a handler returns true, stop processing further handlers
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
          if (!handled && this->auto_add_peer_) {
 | 
			
		||||
          if (this->auto_add_peer_) {
 | 
			
		||||
            this->add_peer(info.src_addr);
 | 
			
		||||
          } else {
 | 
			
		||||
            for (auto *handler : this->unknown_peer_handlers_) {
 | 
			
		||||
              if (handler->on_unknown_peer(info, packet->packet_.receive.data, packet->packet_.receive.size))
 | 
			
		||||
                break;  // If a handler returns true, stop processing further handlers
 | 
			
		||||
            }
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        // Intentionally left as if instead of else in case the peer is added above
 | 
			
		||||
@@ -337,12 +343,6 @@ void ESPNowComponent::loop() {
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t ESPNowComponent::get_wifi_channel() {
 | 
			
		||||
  wifi_second_chan_t dummy;
 | 
			
		||||
  esp_wifi_get_channel(&this->wifi_channel_, &dummy);
 | 
			
		||||
  return this->wifi_channel_;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
esp_err_t ESPNowComponent::send(const uint8_t *peer_address, const uint8_t *payload, size_t size,
 | 
			
		||||
                                const send_callback_t &callback) {
 | 
			
		||||
  if (this->state_ != ESPNOW_STATE_ENABLED) {
 | 
			
		||||
@@ -407,7 +407,7 @@ esp_err_t ESPNowComponent::add_peer(const uint8_t *peer) {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (memcmp(peer, this->own_address_, ESP_NOW_ETH_ALEN) == 0) {
 | 
			
		||||
    this->status_momentary_warning("peer-add-failed");
 | 
			
		||||
    this->mark_failed();
 | 
			
		||||
    return ESP_ERR_INVALID_MAC;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -110,7 +110,6 @@ class ESPNowComponent : public Component {
 | 
			
		||||
 | 
			
		||||
  void set_wifi_channel(uint8_t channel) { this->wifi_channel_ = channel; }
 | 
			
		||||
  void apply_wifi_channel();
 | 
			
		||||
  uint8_t get_wifi_channel();
 | 
			
		||||
 | 
			
		||||
  void set_auto_add_peer(bool value) { this->auto_add_peer_ = value; }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -14,16 +14,18 @@ ld2410_ns = cg.esphome_ns.namespace("ld2410")
 | 
			
		||||
LD2410Component = ld2410_ns.class_("LD2410Component", cg.Component, uart.UARTDevice)
 | 
			
		||||
 | 
			
		||||
CONF_LD2410_ID = "ld2410_id"
 | 
			
		||||
 | 
			
		||||
CONF_MAX_MOVE_DISTANCE = "max_move_distance"
 | 
			
		||||
CONF_MAX_STILL_DISTANCE = "max_still_distance"
 | 
			
		||||
CONF_MOVE_THRESHOLDS = [f"g{x}_move_threshold" for x in range(9)]
 | 
			
		||||
CONF_STILL_THRESHOLDS = [f"g{x}_still_threshold" for x in range(9)]
 | 
			
		||||
CONF_MOVE_THRESHOLDS = [f"g{x}_move_threshold" for x in range(9)]
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(): cv.declare_id(LD2410Component),
 | 
			
		||||
        cv.Optional(CONF_THROTTLE): cv.invalid(
 | 
			
		||||
            f"{CONF_THROTTLE} has been removed; use per-sensor filters, instead"
 | 
			
		||||
        cv.Optional(CONF_THROTTLE, default="1000ms"): cv.All(
 | 
			
		||||
            cv.positive_time_period_milliseconds,
 | 
			
		||||
            cv.Range(min=cv.TimePeriod(milliseconds=1)),
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_MAX_MOVE_DISTANCE): cv.invalid(
 | 
			
		||||
            f"The '{CONF_MAX_MOVE_DISTANCE}' option has been moved to the '{CONF_MAX_MOVE_DISTANCE}'"
 | 
			
		||||
@@ -73,6 +75,7 @@ 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]))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CALIBRATION_ACTION_SCHEMA = maybe_simple_id(
 | 
			
		||||
 
 | 
			
		||||
@@ -22,23 +22,19 @@ CONFIG_SCHEMA = {
 | 
			
		||||
    cv.GenerateID(CONF_LD2410_ID): cv.use_id(LD2410Component),
 | 
			
		||||
    cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema(
 | 
			
		||||
        device_class=DEVICE_CLASS_OCCUPANCY,
 | 
			
		||||
        filters=[{"settle": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
        icon=ICON_ACCOUNT,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_HAS_MOVING_TARGET): binary_sensor.binary_sensor_schema(
 | 
			
		||||
        device_class=DEVICE_CLASS_MOTION,
 | 
			
		||||
        filters=[{"settle": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
        icon=ICON_MOTION_SENSOR,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_HAS_STILL_TARGET): binary_sensor.binary_sensor_schema(
 | 
			
		||||
        device_class=DEVICE_CLASS_OCCUPANCY,
 | 
			
		||||
        filters=[{"settle": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
        icon=ICON_MOTION_SENSOR,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_OUT_PIN_PRESENCE_STATUS): binary_sensor.binary_sensor_schema(
 | 
			
		||||
        device_class=DEVICE_CLASS_PRESENCE,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
        filters=[{"settle": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
        icon=ICON_ACCOUNT,
 | 
			
		||||
    ),
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -188,8 +188,9 @@ void LD2410Component::dump_config() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG,
 | 
			
		||||
                "LD2410:\n"
 | 
			
		||||
                "  Firmware version: %s\n"
 | 
			
		||||
                "  MAC address: %s",
 | 
			
		||||
                version.c_str(), mac_str.c_str());
 | 
			
		||||
                "  MAC address: %s\n"
 | 
			
		||||
                "  Throttle: %u ms",
 | 
			
		||||
                version.c_str(), mac_str.c_str(), this->throttle_);
 | 
			
		||||
#ifdef USE_BINARY_SENSOR
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Binary Sensors:");
 | 
			
		||||
  LOG_BINARY_SENSOR("  ", "Target", this->target_binary_sensor_);
 | 
			
		||||
@@ -305,6 +306,11 @@ void LD2410Component::send_command_(uint8_t command, const uint8_t *command_valu
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2410Component::handle_periodic_data_() {
 | 
			
		||||
  // Reduce data update rate to reduce home assistant database growth
 | 
			
		||||
  // Check this first to prevent unnecessary processing done in later checks/parsing
 | 
			
		||||
  if (App.get_loop_component_start_time() - this->last_periodic_millis_ < this->throttle_) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  // 4 frame header bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame footer bytes
 | 
			
		||||
  // data header=0xAA, data footer=0x55, crc=0x00
 | 
			
		||||
  if (this->buffer_pos_ < 12 || !ld2410::validate_header_footer(DATA_FRAME_HEADER, this->buffer_data_) ||
 | 
			
		||||
@@ -312,6 +318,9 @@ void LD2410Component::handle_periodic_data_() {
 | 
			
		||||
      this->buffer_data_[this->buffer_pos_ - 5] != CHECK) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  // Save the timestamp after validating the frame so, if invalid, we'll take the next frame immediately
 | 
			
		||||
  this->last_periodic_millis_ = App.get_loop_component_start_time();
 | 
			
		||||
 | 
			
		||||
  /*
 | 
			
		||||
    Data Type: 7th
 | 
			
		||||
    0x01: Engineering mode
 | 
			
		||||
 
 | 
			
		||||
@@ -93,6 +93,7 @@ class LD2410Component : public Component, public uart::UARTDevice {
 | 
			
		||||
  void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s);
 | 
			
		||||
  void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s);
 | 
			
		||||
#endif
 | 
			
		||||
  void set_throttle(uint16_t value) { this->throttle_ = value; };
 | 
			
		||||
  void set_bluetooth_password(const std::string &password);
 | 
			
		||||
  void set_engineering_mode(bool enable);
 | 
			
		||||
  void read_all_info();
 | 
			
		||||
@@ -115,6 +116,8 @@ class LD2410Component : public Component, public uart::UARTDevice {
 | 
			
		||||
  void query_light_control_();
 | 
			
		||||
  void restart_();
 | 
			
		||||
 | 
			
		||||
  uint32_t last_periodic_millis_ = 0;
 | 
			
		||||
  uint16_t throttle_ = 0;
 | 
			
		||||
  uint8_t light_function_ = 0;
 | 
			
		||||
  uint8_t light_threshold_ = 0;
 | 
			
		||||
  uint8_t out_pin_level_ = 0;
 | 
			
		||||
 
 | 
			
		||||
@@ -18,50 +18,42 @@ from esphome.const import (
 | 
			
		||||
from . import CONF_LD2410_ID, LD2410Component
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["ld2410"]
 | 
			
		||||
 | 
			
		||||
CONF_STILL_DISTANCE = "still_distance"
 | 
			
		||||
CONF_MOVING_ENERGY = "moving_energy"
 | 
			
		||||
CONF_STILL_ENERGY = "still_energy"
 | 
			
		||||
CONF_DETECTION_DISTANCE = "detection_distance"
 | 
			
		||||
CONF_MOVE_ENERGY = "move_energy"
 | 
			
		||||
CONF_MOVING_ENERGY = "moving_energy"
 | 
			
		||||
CONF_STILL_DISTANCE = "still_distance"
 | 
			
		||||
CONF_STILL_ENERGY = "still_energy"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(CONF_LD2410_ID): cv.use_id(LD2410Component),
 | 
			
		||||
        cv.Optional(CONF_MOVING_DISTANCE): sensor.sensor_schema(
 | 
			
		||||
            device_class=DEVICE_CLASS_DISTANCE,
 | 
			
		||||
            filters=[{"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
            icon=ICON_SIGNAL,
 | 
			
		||||
            unit_of_measurement=UNIT_CENTIMETER,
 | 
			
		||||
            icon=ICON_SIGNAL,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_STILL_DISTANCE): sensor.sensor_schema(
 | 
			
		||||
            device_class=DEVICE_CLASS_DISTANCE,
 | 
			
		||||
            filters=[{"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
            icon=ICON_SIGNAL,
 | 
			
		||||
            unit_of_measurement=UNIT_CENTIMETER,
 | 
			
		||||
            icon=ICON_SIGNAL,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_MOVING_ENERGY): sensor.sensor_schema(
 | 
			
		||||
            filters=[{"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
            icon=ICON_MOTION_SENSOR,
 | 
			
		||||
            unit_of_measurement=UNIT_PERCENT,
 | 
			
		||||
            icon=ICON_MOTION_SENSOR,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_STILL_ENERGY): sensor.sensor_schema(
 | 
			
		||||
            filters=[{"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
            icon=ICON_FLASH,
 | 
			
		||||
            unit_of_measurement=UNIT_PERCENT,
 | 
			
		||||
            icon=ICON_FLASH,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_LIGHT): sensor.sensor_schema(
 | 
			
		||||
            device_class=DEVICE_CLASS_ILLUMINANCE,
 | 
			
		||||
            entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
            filters=[{"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
            icon=ICON_LIGHTBULB,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_DETECTION_DISTANCE): sensor.sensor_schema(
 | 
			
		||||
            device_class=DEVICE_CLASS_DISTANCE,
 | 
			
		||||
            filters=[{"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
            icon=ICON_SIGNAL,
 | 
			
		||||
            unit_of_measurement=UNIT_CENTIMETER,
 | 
			
		||||
            icon=ICON_SIGNAL,
 | 
			
		||||
        ),
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
@@ -71,20 +63,14 @@ CONFIG_SCHEMA = CONFIG_SCHEMA.extend(
 | 
			
		||||
        cv.Optional(f"g{x}"): cv.Schema(
 | 
			
		||||
            {
 | 
			
		||||
                cv.Optional(CONF_MOVE_ENERGY): sensor.sensor_schema(
 | 
			
		||||
                    entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
                    filters=[
 | 
			
		||||
                        {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}
 | 
			
		||||
                    ],
 | 
			
		||||
                    icon=ICON_MOTION_SENSOR,
 | 
			
		||||
                    unit_of_measurement=UNIT_PERCENT,
 | 
			
		||||
                    entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
                    icon=ICON_MOTION_SENSOR,
 | 
			
		||||
                ),
 | 
			
		||||
                cv.Optional(CONF_STILL_ENERGY): sensor.sensor_schema(
 | 
			
		||||
                    entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
                    filters=[
 | 
			
		||||
                        {"throttle_with_priority": cv.TimePeriod(milliseconds=1000)}
 | 
			
		||||
                    ],
 | 
			
		||||
                    icon=ICON_FLASH,
 | 
			
		||||
                    unit_of_measurement=UNIT_PERCENT,
 | 
			
		||||
                    entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
                    icon=ICON_FLASH,
 | 
			
		||||
                ),
 | 
			
		||||
            }
 | 
			
		||||
        )
 | 
			
		||||
 
 | 
			
		||||
@@ -1,46 +0,0 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.components import uart
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.const import CONF_ID, CONF_THROTTLE
 | 
			
		||||
 | 
			
		||||
AUTO_LOAD = ["ld24xx"]
 | 
			
		||||
CODEOWNERS = ["@Rihan9"]
 | 
			
		||||
DEPENDENCIES = ["uart"]
 | 
			
		||||
MULTI_CONF = True
 | 
			
		||||
 | 
			
		||||
LD2412_ns = cg.esphome_ns.namespace("ld2412")
 | 
			
		||||
LD2412Component = LD2412_ns.class_("LD2412Component", cg.Component, uart.UARTDevice)
 | 
			
		||||
 | 
			
		||||
CONF_LD2412_ID = "ld2412_id"
 | 
			
		||||
 | 
			
		||||
CONF_MAX_MOVE_DISTANCE = "max_move_distance"
 | 
			
		||||
CONF_MAX_STILL_DISTANCE = "max_still_distance"
 | 
			
		||||
CONF_MOVE_THRESHOLDS = [f"g{x}_move_threshold" for x in range(9)]
 | 
			
		||||
CONF_STILL_THRESHOLDS = [f"g{x}_still_threshold" for x in range(9)]
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = (
 | 
			
		||||
    cv.Schema(
 | 
			
		||||
        {
 | 
			
		||||
            cv.GenerateID(): cv.declare_id(LD2412Component),
 | 
			
		||||
            cv.Optional(CONF_THROTTLE): cv.invalid(
 | 
			
		||||
                f"{CONF_THROTTLE} has been removed; use per-sensor filters, instead"
 | 
			
		||||
            ),
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
    .extend(uart.UART_DEVICE_SCHEMA)
 | 
			
		||||
    .extend(cv.COMPONENT_SCHEMA)
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
 | 
			
		||||
    "ld2412",
 | 
			
		||||
    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)
 | 
			
		||||
@@ -1,70 +0,0 @@
 | 
			
		||||
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,
 | 
			
		||||
    DEVICE_CLASS_RUNNING,
 | 
			
		||||
    ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
    ICON_ACCOUNT,
 | 
			
		||||
    ICON_MOTION_SENSOR,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
from . import CONF_LD2412_ID, LD2412Component
 | 
			
		||||
 | 
			
		||||
DEPENDENCIES = ["ld2412"]
 | 
			
		||||
 | 
			
		||||
CONF_DYNAMIC_BACKGROUND_CORRECTION_STATUS = "dynamic_background_correction_status"
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = {
 | 
			
		||||
    cv.GenerateID(CONF_LD2412_ID): cv.use_id(LD2412Component),
 | 
			
		||||
    cv.Optional(
 | 
			
		||||
        CONF_DYNAMIC_BACKGROUND_CORRECTION_STATUS
 | 
			
		||||
    ): binary_sensor.binary_sensor_schema(
 | 
			
		||||
        device_class=DEVICE_CLASS_RUNNING,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
        icon=ICON_ACCOUNT,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema(
 | 
			
		||||
        device_class=DEVICE_CLASS_OCCUPANCY,
 | 
			
		||||
        filters=[{"settle": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
        icon=ICON_ACCOUNT,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_HAS_MOVING_TARGET): binary_sensor.binary_sensor_schema(
 | 
			
		||||
        device_class=DEVICE_CLASS_MOTION,
 | 
			
		||||
        filters=[{"settle": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
        icon=ICON_MOTION_SENSOR,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_HAS_STILL_TARGET): binary_sensor.binary_sensor_schema(
 | 
			
		||||
        device_class=DEVICE_CLASS_OCCUPANCY,
 | 
			
		||||
        filters=[{"settle": cv.TimePeriod(milliseconds=1000)}],
 | 
			
		||||
        icon=ICON_MOTION_SENSOR,
 | 
			
		||||
    ),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    LD2412_component = await cg.get_variable(config[CONF_LD2412_ID])
 | 
			
		||||
    if dynamic_background_correction_status_config := config.get(
 | 
			
		||||
        CONF_DYNAMIC_BACKGROUND_CORRECTION_STATUS
 | 
			
		||||
    ):
 | 
			
		||||
        sens = await binary_sensor.new_binary_sensor(
 | 
			
		||||
            dynamic_background_correction_status_config
 | 
			
		||||
        )
 | 
			
		||||
        cg.add(
 | 
			
		||||
            LD2412_component.set_dynamic_background_correction_status_binary_sensor(
 | 
			
		||||
                sens
 | 
			
		||||
            )
 | 
			
		||||
        )
 | 
			
		||||
    if has_target_config := config.get(CONF_HAS_TARGET):
 | 
			
		||||
        sens = await binary_sensor.new_binary_sensor(has_target_config)
 | 
			
		||||
        cg.add(LD2412_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(LD2412_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(LD2412_component.set_still_target_binary_sensor(sens))
 | 
			
		||||
@@ -1,74 +0,0 @@
 | 
			
		||||
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_DATABASE,
 | 
			
		||||
    ICON_PULSE,
 | 
			
		||||
    ICON_RESTART,
 | 
			
		||||
    ICON_RESTART_ALERT,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
from .. import CONF_LD2412_ID, LD2412_ns, LD2412Component
 | 
			
		||||
 | 
			
		||||
FactoryResetButton = LD2412_ns.class_("FactoryResetButton", button.Button)
 | 
			
		||||
QueryButton = LD2412_ns.class_("QueryButton", button.Button)
 | 
			
		||||
RestartButton = LD2412_ns.class_("RestartButton", button.Button)
 | 
			
		||||
StartDynamicBackgroundCorrectionButton = LD2412_ns.class_(
 | 
			
		||||
    "StartDynamicBackgroundCorrectionButton", button.Button
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONF_QUERY_PARAMS = "query_params"
 | 
			
		||||
CONF_START_DYNAMIC_BACKGROUND_CORRECTION = "start_dynamic_background_correction"
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = {
 | 
			
		||||
    cv.GenerateID(CONF_LD2412_ID): cv.use_id(LD2412Component),
 | 
			
		||||
    cv.Optional(CONF_FACTORY_RESET): button.button_schema(
 | 
			
		||||
        FactoryResetButton,
 | 
			
		||||
        device_class=DEVICE_CLASS_RESTART,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
        icon=ICON_RESTART_ALERT,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_QUERY_PARAMS): button.button_schema(
 | 
			
		||||
        QueryButton,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
        icon=ICON_DATABASE,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_RESTART): button.button_schema(
 | 
			
		||||
        RestartButton,
 | 
			
		||||
        device_class=DEVICE_CLASS_RESTART,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
 | 
			
		||||
        icon=ICON_RESTART,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_START_DYNAMIC_BACKGROUND_CORRECTION): button.button_schema(
 | 
			
		||||
        StartDynamicBackgroundCorrectionButton,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
        icon=ICON_PULSE,
 | 
			
		||||
    ),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    LD2412_component = await cg.get_variable(config[CONF_LD2412_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_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_factory_reset_button(b))
 | 
			
		||||
    if query_params_config := config.get(CONF_QUERY_PARAMS):
 | 
			
		||||
        b = await button.new_button(query_params_config)
 | 
			
		||||
        await cg.register_parented(b, config[CONF_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_query_button(b))
 | 
			
		||||
    if restart_config := config.get(CONF_RESTART):
 | 
			
		||||
        b = await button.new_button(restart_config)
 | 
			
		||||
        await cg.register_parented(b, config[CONF_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_restart_button(b))
 | 
			
		||||
    if start_dynamic_background_correction_config := config.get(
 | 
			
		||||
        CONF_START_DYNAMIC_BACKGROUND_CORRECTION
 | 
			
		||||
    ):
 | 
			
		||||
        b = await button.new_button(start_dynamic_background_correction_config)
 | 
			
		||||
        await cg.register_parented(b, config[CONF_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_start_dynamic_background_correction_button(b))
 | 
			
		||||
@@ -1,9 +0,0 @@
 | 
			
		||||
#include "factory_reset_button.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
void FactoryResetButton::press_action() { this->parent_->factory_reset(); }
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,18 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/button/button.h"
 | 
			
		||||
#include "../ld2412.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
class FactoryResetButton : public button::Button, public Parented<LD2412Component> {
 | 
			
		||||
 public:
 | 
			
		||||
  FactoryResetButton() = default;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void press_action() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,9 +0,0 @@
 | 
			
		||||
#include "query_button.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
void QueryButton::press_action() { this->parent_->read_all_info(); }
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,18 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/button/button.h"
 | 
			
		||||
#include "../ld2412.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
class QueryButton : public button::Button, public Parented<LD2412Component> {
 | 
			
		||||
 public:
 | 
			
		||||
  QueryButton() = default;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void press_action() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,9 +0,0 @@
 | 
			
		||||
#include "restart_button.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
void RestartButton::press_action() { this->parent_->restart_and_read_all_info(); }
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,18 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/button/button.h"
 | 
			
		||||
#include "../ld2412.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
class RestartButton : public button::Button, public Parented<LD2412Component> {
 | 
			
		||||
 public:
 | 
			
		||||
  RestartButton() = default;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void press_action() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,11 +0,0 @@
 | 
			
		||||
#include "start_dynamic_background_correction_button.h"
 | 
			
		||||
 | 
			
		||||
#include "restart_button.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
void StartDynamicBackgroundCorrectionButton::press_action() { this->parent_->start_dynamic_background_correction(); }
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,18 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/button/button.h"
 | 
			
		||||
#include "../ld2412.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
class StartDynamicBackgroundCorrectionButton : public button::Button, public Parented<LD2412Component> {
 | 
			
		||||
 public:
 | 
			
		||||
  StartDynamicBackgroundCorrectionButton() = default;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void press_action() override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,861 +0,0 @@
 | 
			
		||||
#include "ld2412.h"
 | 
			
		||||
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
#include "esphome/components/number/number.h"
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SENSOR
 | 
			
		||||
#include "esphome/components/sensor/sensor.h"
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#include "esphome/core/application.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
static const char *const TAG = "ld2412";
 | 
			
		||||
static const char *const UNKNOWN_MAC = "unknown";
 | 
			
		||||
static const char *const VERSION_FMT = "%u.%02X.%02X%02X%02X%02X";
 | 
			
		||||
 | 
			
		||||
enum BaudRate : 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,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum DistanceResolution : uint8_t {
 | 
			
		||||
  DISTANCE_RESOLUTION_0_2 = 0x03,
 | 
			
		||||
  DISTANCE_RESOLUTION_0_5 = 0x01,
 | 
			
		||||
  DISTANCE_RESOLUTION_0_75 = 0x00,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum LightFunction : uint8_t {
 | 
			
		||||
  LIGHT_FUNCTION_OFF = 0x00,
 | 
			
		||||
  LIGHT_FUNCTION_BELOW = 0x01,
 | 
			
		||||
  LIGHT_FUNCTION_ABOVE = 0x02,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum OutPinLevel : uint8_t {
 | 
			
		||||
  OUT_PIN_LEVEL_LOW = 0x01,
 | 
			
		||||
  OUT_PIN_LEVEL_HIGH = 0x00,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
Data Type: 6th byte
 | 
			
		||||
Target states: 9th byte
 | 
			
		||||
    Moving target distance: 10~11th bytes
 | 
			
		||||
    Moving target energy: 12th byte
 | 
			
		||||
    Still target distance: 13~14th bytes
 | 
			
		||||
    Still target energy: 15th byte
 | 
			
		||||
    Detect distance: 16~17th bytes
 | 
			
		||||
*/
 | 
			
		||||
enum PeriodicData : uint8_t {
 | 
			
		||||
  DATA_TYPES = 6,
 | 
			
		||||
  TARGET_STATES = 8,
 | 
			
		||||
  MOVING_TARGET_LOW = 9,
 | 
			
		||||
  MOVING_TARGET_HIGH = 10,
 | 
			
		||||
  MOVING_ENERGY = 11,
 | 
			
		||||
  STILL_TARGET_LOW = 12,
 | 
			
		||||
  STILL_TARGET_HIGH = 13,
 | 
			
		||||
  STILL_ENERGY = 14,
 | 
			
		||||
  MOVING_SENSOR_START = 17,
 | 
			
		||||
  STILL_SENSOR_START = 31,
 | 
			
		||||
  LIGHT_SENSOR = 45,
 | 
			
		||||
  OUT_PIN_SENSOR = 38,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum PeriodicDataValue : uint8_t {
 | 
			
		||||
  HEADER = 0XAA,
 | 
			
		||||
  FOOTER = 0x55,
 | 
			
		||||
  CHECK = 0x00,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
enum AckData : uint8_t {
 | 
			
		||||
  COMMAND = 6,
 | 
			
		||||
  COMMAND_STATUS = 7,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Memory-efficient lookup tables
 | 
			
		||||
struct StringToUint8 {
 | 
			
		||||
  const char *str;
 | 
			
		||||
  const uint8_t value;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
struct Uint8ToString {
 | 
			
		||||
  const uint8_t value;
 | 
			
		||||
  const char *str;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
constexpr StringToUint8 BAUD_RATES_BY_STR[] = {
 | 
			
		||||
    {"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},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
constexpr StringToUint8 DISTANCE_RESOLUTIONS_BY_STR[] = {
 | 
			
		||||
    {"0.2m", DISTANCE_RESOLUTION_0_2},
 | 
			
		||||
    {"0.5m", DISTANCE_RESOLUTION_0_5},
 | 
			
		||||
    {"0.75m", DISTANCE_RESOLUTION_0_75},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
constexpr Uint8ToString DISTANCE_RESOLUTIONS_BY_UINT[] = {
 | 
			
		||||
    {DISTANCE_RESOLUTION_0_2, "0.2m"},
 | 
			
		||||
    {DISTANCE_RESOLUTION_0_5, "0.5m"},
 | 
			
		||||
    {DISTANCE_RESOLUTION_0_75, "0.75m"},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
constexpr StringToUint8 LIGHT_FUNCTIONS_BY_STR[] = {
 | 
			
		||||
    {"off", LIGHT_FUNCTION_OFF},
 | 
			
		||||
    {"below", LIGHT_FUNCTION_BELOW},
 | 
			
		||||
    {"above", LIGHT_FUNCTION_ABOVE},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
constexpr Uint8ToString LIGHT_FUNCTIONS_BY_UINT[] = {
 | 
			
		||||
    {LIGHT_FUNCTION_OFF, "off"},
 | 
			
		||||
    {LIGHT_FUNCTION_BELOW, "below"},
 | 
			
		||||
    {LIGHT_FUNCTION_ABOVE, "above"},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
constexpr StringToUint8 OUT_PIN_LEVELS_BY_STR[] = {
 | 
			
		||||
    {"low", OUT_PIN_LEVEL_LOW},
 | 
			
		||||
    {"high", OUT_PIN_LEVEL_HIGH},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
constexpr Uint8ToString OUT_PIN_LEVELS_BY_UINT[] = {
 | 
			
		||||
    {OUT_PIN_LEVEL_LOW, "low"},
 | 
			
		||||
    {OUT_PIN_LEVEL_HIGH, "high"},
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
// Helper functions for lookups
 | 
			
		||||
template<size_t N> uint8_t find_uint8(const StringToUint8 (&arr)[N], const std::string &str) {
 | 
			
		||||
  for (const auto &entry : arr) {
 | 
			
		||||
    if (str == entry.str) {
 | 
			
		||||
      return entry.value;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return 0xFF;  // Not found
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
template<size_t N> const char *find_str(const Uint8ToString (&arr)[N], uint8_t value) {
 | 
			
		||||
  for (const auto &entry : arr) {
 | 
			
		||||
    if (value == entry.value) {
 | 
			
		||||
      return entry.str;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return "";  // Not found
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static constexpr uint8_t DEFAULT_PRESENCE_TIMEOUT = 5;  // Default used when number component is not defined
 | 
			
		||||
// Commands
 | 
			
		||||
static constexpr uint8_t CMD_ENABLE_CONF = 0xFF;
 | 
			
		||||
static constexpr uint8_t CMD_DISABLE_CONF = 0xFE;
 | 
			
		||||
static constexpr uint8_t CMD_ENABLE_ENG = 0x62;
 | 
			
		||||
static constexpr uint8_t CMD_DISABLE_ENG = 0x63;
 | 
			
		||||
static constexpr uint8_t CMD_QUERY_BASIC_CONF = 0x12;
 | 
			
		||||
static constexpr uint8_t CMD_BASIC_CONF = 0x02;
 | 
			
		||||
static constexpr uint8_t CMD_QUERY_VERSION = 0xA0;
 | 
			
		||||
static constexpr uint8_t CMD_QUERY_DISTANCE_RESOLUTION = 0x11;
 | 
			
		||||
static constexpr uint8_t CMD_SET_DISTANCE_RESOLUTION = 0x01;
 | 
			
		||||
static constexpr uint8_t CMD_QUERY_LIGHT_CONTROL = 0x1C;
 | 
			
		||||
static constexpr uint8_t CMD_SET_LIGHT_CONTROL = 0x0C;
 | 
			
		||||
static constexpr uint8_t CMD_SET_BAUD_RATE = 0xA1;
 | 
			
		||||
static constexpr uint8_t CMD_QUERY_MAC_ADDRESS = 0xA5;
 | 
			
		||||
static constexpr uint8_t CMD_FACTORY_RESET = 0xA2;
 | 
			
		||||
static constexpr uint8_t CMD_RESTART = 0xA3;
 | 
			
		||||
static constexpr uint8_t CMD_BLUETOOTH = 0xA4;
 | 
			
		||||
static constexpr uint8_t CMD_DYNAMIC_BACKGROUND_CORRECTION = 0x0B;
 | 
			
		||||
static constexpr uint8_t CMD_QUERY_DYNAMIC_BACKGROUND_CORRECTION = 0x1B;
 | 
			
		||||
static constexpr uint8_t CMD_MOTION_GATE_SENS = 0x03;
 | 
			
		||||
static constexpr uint8_t CMD_QUERY_MOTION_GATE_SENS = 0x13;
 | 
			
		||||
static constexpr uint8_t CMD_STATIC_GATE_SENS = 0x04;
 | 
			
		||||
static constexpr uint8_t CMD_QUERY_STATIC_GATE_SENS = 0x14;
 | 
			
		||||
static constexpr uint8_t CMD_NONE = 0x00;
 | 
			
		||||
// Commands values
 | 
			
		||||
static constexpr uint8_t CMD_MAX_MOVE_VALUE = 0x00;
 | 
			
		||||
static constexpr uint8_t CMD_MAX_STILL_VALUE = 0x01;
 | 
			
		||||
static constexpr uint8_t CMD_DURATION_VALUE = 0x02;
 | 
			
		||||
// Bitmasks for target states
 | 
			
		||||
static constexpr uint8_t MOVE_BITMASK = 0x01;
 | 
			
		||||
static constexpr uint8_t STILL_BITMASK = 0x02;
 | 
			
		||||
// Header & Footer size
 | 
			
		||||
static constexpr uint8_t HEADER_FOOTER_SIZE = 4;
 | 
			
		||||
// Command Header & Footer
 | 
			
		||||
static constexpr uint8_t CMD_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xFD, 0xFC, 0xFB, 0xFA};
 | 
			
		||||
static constexpr uint8_t CMD_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0x04, 0x03, 0x02, 0x01};
 | 
			
		||||
// Data Header & Footer
 | 
			
		||||
static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xF4, 0xF3, 0xF2, 0xF1};
 | 
			
		||||
static constexpr uint8_t DATA_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0xF8, 0xF7, 0xF6, 0xF5};
 | 
			
		||||
// MAC address the module uses when Bluetooth is disabled
 | 
			
		||||
static constexpr uint8_t NO_MAC[] = {0x08, 0x05, 0x04, 0x03, 0x02, 0x01};
 | 
			
		||||
 | 
			
		||||
static inline int two_byte_to_int(char firstbyte, char secondbyte) { return (int16_t) (secondbyte << 8) + firstbyte; }
 | 
			
		||||
 | 
			
		||||
static inline bool validate_header_footer(const uint8_t *header_footer, const uint8_t *buffer) {
 | 
			
		||||
  return std::memcmp(header_footer, buffer, HEADER_FOOTER_SIZE) == 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::dump_config() {
 | 
			
		||||
  std::string mac_str =
 | 
			
		||||
      mac_address_is_valid(this->mac_address_) ? format_mac_address_pretty(this->mac_address_) : UNKNOWN_MAC;
 | 
			
		||||
  std::string version = str_sprintf(VERSION_FMT, this->version_[1], this->version_[0], this->version_[5],
 | 
			
		||||
                                    this->version_[4], this->version_[3], this->version_[2]);
 | 
			
		||||
  ESP_LOGCONFIG(TAG,
 | 
			
		||||
                "LD2412:\n"
 | 
			
		||||
                "  Firmware version: %s\n"
 | 
			
		||||
                "  MAC address: %s",
 | 
			
		||||
                version.c_str(), mac_str.c_str());
 | 
			
		||||
#ifdef USE_BINARY_SENSOR
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Binary Sensors:");
 | 
			
		||||
  LOG_BINARY_SENSOR("  ", "DynamicBackgroundCorrectionStatus",
 | 
			
		||||
                    this->dynamic_background_correction_status_binary_sensor_);
 | 
			
		||||
  LOG_BINARY_SENSOR("  ", "MovingTarget", this->moving_target_binary_sensor_);
 | 
			
		||||
  LOG_BINARY_SENSOR("  ", "StillTarget", this->still_target_binary_sensor_);
 | 
			
		||||
  LOG_BINARY_SENSOR("  ", "Target", this->target_binary_sensor_);
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SENSOR
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Sensors:");
 | 
			
		||||
  LOG_SENSOR_WITH_DEDUP_SAFE("  ", "Light", this->light_sensor_);
 | 
			
		||||
  LOG_SENSOR_WITH_DEDUP_SAFE("  ", "DetectionDistance", this->detection_distance_sensor_);
 | 
			
		||||
  LOG_SENSOR_WITH_DEDUP_SAFE("  ", "MovingTargetDistance", this->moving_target_distance_sensor_);
 | 
			
		||||
  LOG_SENSOR_WITH_DEDUP_SAFE("  ", "MovingTargetEnergy", this->moving_target_energy_sensor_);
 | 
			
		||||
  LOG_SENSOR_WITH_DEDUP_SAFE("  ", "StillTargetDistance", this->still_target_distance_sensor_);
 | 
			
		||||
  LOG_SENSOR_WITH_DEDUP_SAFE("  ", "StillTargetEnergy", this->still_target_energy_sensor_);
 | 
			
		||||
  for (auto &s : this->gate_still_sensors_) {
 | 
			
		||||
    LOG_SENSOR_WITH_DEDUP_SAFE("  ", "GateStill", s);
 | 
			
		||||
  }
 | 
			
		||||
  for (auto &s : this->gate_move_sensors_) {
 | 
			
		||||
    LOG_SENSOR_WITH_DEDUP_SAFE("  ", "GateMove", s);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Text Sensors:");
 | 
			
		||||
  LOG_TEXT_SENSOR("  ", "MAC address", this->mac_text_sensor_);
 | 
			
		||||
  LOG_TEXT_SENSOR("  ", "Version", this->version_text_sensor_);
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Numbers:");
 | 
			
		||||
  LOG_NUMBER("  ", "LightThreshold", this->light_threshold_number_);
 | 
			
		||||
  LOG_NUMBER("  ", "MaxDistanceGate", this->max_distance_gate_number_);
 | 
			
		||||
  LOG_NUMBER("  ", "MinDistanceGate", this->min_distance_gate_number_);
 | 
			
		||||
  LOG_NUMBER("  ", "Timeout", this->timeout_number_);
 | 
			
		||||
  for (number::Number *n : this->gate_move_threshold_numbers_) {
 | 
			
		||||
    LOG_NUMBER("  ", "Move Thresholds", n);
 | 
			
		||||
  }
 | 
			
		||||
  for (number::Number *n : this->gate_still_threshold_numbers_) {
 | 
			
		||||
    LOG_NUMBER("  ", "Still Thresholds", n);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Selects:");
 | 
			
		||||
  LOG_SELECT("  ", "BaudRate", this->baud_rate_select_);
 | 
			
		||||
  LOG_SELECT("  ", "DistanceResolution", this->distance_resolution_select_);
 | 
			
		||||
  LOG_SELECT("  ", "LightFunction", this->light_function_select_);
 | 
			
		||||
  LOG_SELECT("  ", "OutPinLevel", this->out_pin_level_select_);
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SWITCH
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Switches:");
 | 
			
		||||
  LOG_SWITCH("  ", "Bluetooth", this->bluetooth_switch_);
 | 
			
		||||
  LOG_SWITCH("  ", "EngineeringMode", this->engineering_mode_switch_);
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_BUTTON
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Buttons:");
 | 
			
		||||
  LOG_BUTTON("  ", "FactoryReset", this->factory_reset_button_);
 | 
			
		||||
  LOG_BUTTON("  ", "Query", this->query_button_);
 | 
			
		||||
  LOG_BUTTON("  ", "Restart", this->restart_button_);
 | 
			
		||||
  LOG_BUTTON("  ", "StartDynamicBackgroundCorrection", this->start_dynamic_background_correction_button_);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::setup() {
 | 
			
		||||
  ESP_LOGCONFIG(TAG, "Running setup");
 | 
			
		||||
  this->read_all_info();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::read_all_info() {
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  this->get_version_();
 | 
			
		||||
  delay(10);  // NOLINT
 | 
			
		||||
  this->get_mac_();
 | 
			
		||||
  delay(10);  // NOLINT
 | 
			
		||||
  this->get_distance_resolution_();
 | 
			
		||||
  delay(10);  // NOLINT
 | 
			
		||||
  this->query_parameters_();
 | 
			
		||||
  delay(10);  // NOLINT
 | 
			
		||||
  this->query_dynamic_background_correction_();
 | 
			
		||||
  delay(10);  // NOLINT
 | 
			
		||||
  this->query_light_control_();
 | 
			
		||||
  delay(10);  // NOLINT
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  this->get_gate_threshold();
 | 
			
		||||
  delay(10);  // NOLINT
 | 
			
		||||
#endif
 | 
			
		||||
  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_->publish_state(baud_rate);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::restart_and_read_all_info() {
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  this->restart_();
 | 
			
		||||
  this->set_timeout(1000, [this]() { this->read_all_info(); });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::loop() {
 | 
			
		||||
  while (this->available()) {
 | 
			
		||||
    this->readline_(this->read());
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) {
 | 
			
		||||
  ESP_LOGV(TAG, "Sending COMMAND %02X", command);
 | 
			
		||||
  // frame header bytes
 | 
			
		||||
  this->write_array(CMD_FRAME_HEADER, HEADER_FOOTER_SIZE);
 | 
			
		||||
  // length bytes
 | 
			
		||||
  uint8_t len = 2;
 | 
			
		||||
  if (command_value != nullptr) {
 | 
			
		||||
    len += command_value_len;
 | 
			
		||||
  }
 | 
			
		||||
  // 2 length bytes (low, high) + 2 command bytes (low, high)
 | 
			
		||||
  uint8_t len_cmd[] = {len, 0x00, command, 0x00};
 | 
			
		||||
  this->write_array(len_cmd, sizeof(len_cmd));
 | 
			
		||||
 | 
			
		||||
  // command value bytes
 | 
			
		||||
  if (command_value != nullptr) {
 | 
			
		||||
    this->write_array(command_value, command_value_len);
 | 
			
		||||
  }
 | 
			
		||||
  // frame footer bytes
 | 
			
		||||
  this->write_array(CMD_FRAME_FOOTER, HEADER_FOOTER_SIZE);
 | 
			
		||||
 | 
			
		||||
  if (command != CMD_ENABLE_CONF && command != CMD_DISABLE_CONF) {
 | 
			
		||||
    delay(30);  // NOLINT
 | 
			
		||||
  }
 | 
			
		||||
  delay(20);  // NOLINT
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::handle_periodic_data_() {
 | 
			
		||||
  // 4 frame header bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame footer bytes
 | 
			
		||||
  // data header=0xAA, data footer=0x55, crc=0x00
 | 
			
		||||
  if (this->buffer_pos_ < 12 || !ld2412::validate_header_footer(DATA_FRAME_HEADER, this->buffer_data_) ||
 | 
			
		||||
      this->buffer_data_[7] != HEADER || this->buffer_data_[this->buffer_pos_ - 6] != FOOTER) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  /*
 | 
			
		||||
    Data Type: 7th
 | 
			
		||||
    0x01: Engineering mode
 | 
			
		||||
    0x02: Normal mode
 | 
			
		||||
  */
 | 
			
		||||
  bool engineering_mode = this->buffer_data_[DATA_TYPES] == 0x01;
 | 
			
		||||
#ifdef USE_SWITCH
 | 
			
		||||
  if (this->engineering_mode_switch_ != nullptr) {
 | 
			
		||||
    this->engineering_mode_switch_->publish_state(engineering_mode);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
#ifdef USE_BINARY_SENSOR
 | 
			
		||||
  /*
 | 
			
		||||
    Target states: 9th
 | 
			
		||||
    0x00 = No target
 | 
			
		||||
    0x01 = Moving targets
 | 
			
		||||
    0x02 = Still targets
 | 
			
		||||
    0x03 = Moving+Still targets
 | 
			
		||||
  */
 | 
			
		||||
  char target_state = this->buffer_data_[TARGET_STATES];
 | 
			
		||||
  if (this->target_binary_sensor_ != nullptr) {
 | 
			
		||||
    this->target_binary_sensor_->publish_state(target_state != 0x00);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->moving_target_binary_sensor_ != nullptr) {
 | 
			
		||||
    this->moving_target_binary_sensor_->publish_state(target_state & MOVE_BITMASK);
 | 
			
		||||
  }
 | 
			
		||||
  if (this->still_target_binary_sensor_ != nullptr) {
 | 
			
		||||
    this->still_target_binary_sensor_->publish_state(target_state & STILL_BITMASK);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
  /*
 | 
			
		||||
    Moving target distance: 10~11th bytes
 | 
			
		||||
    Moving target energy: 12th byte
 | 
			
		||||
    Still target distance: 13~14th bytes
 | 
			
		||||
    Still target energy: 15th byte
 | 
			
		||||
    Detect distance: 16~17th bytes
 | 
			
		||||
  */
 | 
			
		||||
#ifdef USE_SENSOR
 | 
			
		||||
  SAFE_PUBLISH_SENSOR(
 | 
			
		||||
      this->moving_target_distance_sensor_,
 | 
			
		||||
      ld2412::two_byte_to_int(this->buffer_data_[MOVING_TARGET_LOW], this->buffer_data_[MOVING_TARGET_HIGH]))
 | 
			
		||||
  SAFE_PUBLISH_SENSOR(this->moving_target_energy_sensor_, this->buffer_data_[MOVING_ENERGY])
 | 
			
		||||
  SAFE_PUBLISH_SENSOR(
 | 
			
		||||
      this->still_target_distance_sensor_,
 | 
			
		||||
      ld2412::two_byte_to_int(this->buffer_data_[STILL_TARGET_LOW], this->buffer_data_[STILL_TARGET_HIGH]))
 | 
			
		||||
  SAFE_PUBLISH_SENSOR(this->still_target_energy_sensor_, this->buffer_data_[STILL_ENERGY])
 | 
			
		||||
  if (this->detection_distance_sensor_ != nullptr) {
 | 
			
		||||
    int new_detect_distance = 0;
 | 
			
		||||
    if (target_state != 0x00 && (target_state & MOVE_BITMASK)) {
 | 
			
		||||
      new_detect_distance =
 | 
			
		||||
          ld2412::two_byte_to_int(this->buffer_data_[MOVING_TARGET_LOW], this->buffer_data_[MOVING_TARGET_HIGH]);
 | 
			
		||||
    } else if (target_state != 0x00) {
 | 
			
		||||
      new_detect_distance =
 | 
			
		||||
          ld2412::two_byte_to_int(this->buffer_data_[STILL_TARGET_LOW], this->buffer_data_[STILL_TARGET_HIGH]);
 | 
			
		||||
    }
 | 
			
		||||
    this->detection_distance_sensor_->publish_state_if_not_dup(new_detect_distance);
 | 
			
		||||
  }
 | 
			
		||||
  if (engineering_mode) {
 | 
			
		||||
    /*
 | 
			
		||||
      Moving distance range: 18th byte
 | 
			
		||||
      Still distance range: 19th byte
 | 
			
		||||
      Moving energy: 20~28th bytes
 | 
			
		||||
    */
 | 
			
		||||
    for (uint8_t i = 0; i < TOTAL_GATES; i++) {
 | 
			
		||||
      SAFE_PUBLISH_SENSOR(this->gate_move_sensors_[i], this->buffer_data_[MOVING_SENSOR_START + i])
 | 
			
		||||
    }
 | 
			
		||||
    /*
 | 
			
		||||
      Still energy: 29~37th bytes
 | 
			
		||||
    */
 | 
			
		||||
    for (uint8_t i = 0; i < TOTAL_GATES; i++) {
 | 
			
		||||
      SAFE_PUBLISH_SENSOR(this->gate_still_sensors_[i], this->buffer_data_[STILL_SENSOR_START + i])
 | 
			
		||||
    }
 | 
			
		||||
    /*
 | 
			
		||||
      Light sensor: 38th bytes
 | 
			
		||||
    */
 | 
			
		||||
    SAFE_PUBLISH_SENSOR(this->light_sensor_, this->buffer_data_[LIGHT_SENSOR])
 | 
			
		||||
  } else {
 | 
			
		||||
    for (auto &gate_move_sensor : this->gate_move_sensors_) {
 | 
			
		||||
      SAFE_PUBLISH_SENSOR_UNKNOWN(gate_move_sensor)
 | 
			
		||||
    }
 | 
			
		||||
    for (auto &gate_still_sensor : this->gate_still_sensors_) {
 | 
			
		||||
      SAFE_PUBLISH_SENSOR_UNKNOWN(gate_still_sensor)
 | 
			
		||||
    }
 | 
			
		||||
    SAFE_PUBLISH_SENSOR_UNKNOWN(this->light_sensor_)
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
  // the radar module won't tell us when it's done, so we just have to keep polling...
 | 
			
		||||
  if (this->dynamic_background_correction_active_) {
 | 
			
		||||
    this->set_config_mode_(true);
 | 
			
		||||
    this->query_dynamic_background_correction_();
 | 
			
		||||
    this->set_config_mode_(false);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
std::function<void(void)> set_number_value(number::Number *n, float value) {
 | 
			
		||||
  if (n != nullptr && (!n->has_state() || n->state != value)) {
 | 
			
		||||
    n->state = value;
 | 
			
		||||
    return [n, value]() { n->publish_state(value); };
 | 
			
		||||
  }
 | 
			
		||||
  return []() {};
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
bool LD2412Component::handle_ack_data_() {
 | 
			
		||||
  ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", this->buffer_data_[COMMAND]);
 | 
			
		||||
  if (this->buffer_pos_ < 10) {
 | 
			
		||||
    ESP_LOGW(TAG, "Invalid length");
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  if (!ld2412::validate_header_footer(CMD_FRAME_HEADER, this->buffer_data_)) {
 | 
			
		||||
    ESP_LOGW(TAG, "Invalid header: %s", format_hex_pretty(this->buffer_data_, HEADER_FOOTER_SIZE).c_str());
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->buffer_data_[COMMAND_STATUS] != 0x01) {
 | 
			
		||||
    ESP_LOGW(TAG, "Invalid status");
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->buffer_data_[8] || this->buffer_data_[9]) {
 | 
			
		||||
    ESP_LOGW(TAG, "Invalid command: %02X, %02X", this->buffer_data_[8], this->buffer_data_[9]);
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  switch (this->buffer_data_[COMMAND]) {
 | 
			
		||||
    case CMD_ENABLE_CONF:
 | 
			
		||||
      ESP_LOGV(TAG, "Enable conf");
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    case CMD_DISABLE_CONF:
 | 
			
		||||
      ESP_LOGV(TAG, "Disabled conf");
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    case CMD_SET_BAUD_RATE:
 | 
			
		||||
      ESP_LOGV(TAG, "Baud rate change");
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
      if (this->baud_rate_select_ != nullptr) {
 | 
			
		||||
        ESP_LOGW(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str());
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    case CMD_QUERY_VERSION: {
 | 
			
		||||
      std::memcpy(this->version_, &this->buffer_data_[12], sizeof(this->version_));
 | 
			
		||||
      std::string version = str_sprintf(VERSION_FMT, this->version_[1], this->version_[0], this->version_[5],
 | 
			
		||||
                                        this->version_[4], this->version_[3], this->version_[2]);
 | 
			
		||||
      ESP_LOGV(TAG, "Firmware version: %s", version.c_str());
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
      if (this->version_text_sensor_ != nullptr) {
 | 
			
		||||
        this->version_text_sensor_->publish_state(version);
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
    case CMD_QUERY_DISTANCE_RESOLUTION: {
 | 
			
		||||
      const auto *distance_resolution = find_str(DISTANCE_RESOLUTIONS_BY_UINT, this->buffer_data_[10]);
 | 
			
		||||
      ESP_LOGV(TAG, "Distance resolution: %s", distance_resolution);
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
      if (this->distance_resolution_select_ != nullptr) {
 | 
			
		||||
        this->distance_resolution_select_->publish_state(distance_resolution);
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case CMD_QUERY_LIGHT_CONTROL: {
 | 
			
		||||
      this->light_function_ = this->buffer_data_[10];
 | 
			
		||||
      this->light_threshold_ = this->buffer_data_[11];
 | 
			
		||||
      const auto *light_function_str = find_str(LIGHT_FUNCTIONS_BY_UINT, this->light_function_);
 | 
			
		||||
      ESP_LOGV(TAG,
 | 
			
		||||
               "Light function: %s\n"
 | 
			
		||||
               "Light threshold: %u",
 | 
			
		||||
               light_function_str, this->light_threshold_);
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
      if (this->light_function_select_ != nullptr) {
 | 
			
		||||
        this->light_function_select_->publish_state(light_function_str);
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
      if (this->light_threshold_number_ != nullptr) {
 | 
			
		||||
        this->light_threshold_number_->publish_state(static_cast<float>(this->light_threshold_));
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case CMD_QUERY_MAC_ADDRESS: {
 | 
			
		||||
      if (this->buffer_pos_ < 20) {
 | 
			
		||||
        return false;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      this->bluetooth_on_ = std::memcmp(&this->buffer_data_[10], NO_MAC, sizeof(NO_MAC)) != 0;
 | 
			
		||||
      if (this->bluetooth_on_) {
 | 
			
		||||
        std::memcpy(this->mac_address_, &this->buffer_data_[10], sizeof(this->mac_address_));
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      std::string mac_str =
 | 
			
		||||
          mac_address_is_valid(this->mac_address_) ? format_mac_address_pretty(this->mac_address_) : UNKNOWN_MAC;
 | 
			
		||||
      ESP_LOGV(TAG, "MAC address: %s", mac_str.c_str());
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
      if (this->mac_text_sensor_ != nullptr) {
 | 
			
		||||
        this->mac_text_sensor_->publish_state(mac_str);
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SWITCH
 | 
			
		||||
      if (this->bluetooth_switch_ != nullptr) {
 | 
			
		||||
        this->bluetooth_switch_->publish_state(this->bluetooth_on_);
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case CMD_SET_DISTANCE_RESOLUTION:
 | 
			
		||||
      ESP_LOGV(TAG, "Handled set distance resolution command");
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    case CMD_QUERY_DYNAMIC_BACKGROUND_CORRECTION: {
 | 
			
		||||
      ESP_LOGV(TAG, "Handled query dynamic background correction");
 | 
			
		||||
      bool dynamic_background_correction_active = (this->buffer_data_[10] != 0x00);
 | 
			
		||||
#ifdef USE_BINARY_SENSOR
 | 
			
		||||
      if (this->dynamic_background_correction_status_binary_sensor_ != nullptr) {
 | 
			
		||||
        this->dynamic_background_correction_status_binary_sensor_->publish_state(dynamic_background_correction_active);
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      this->dynamic_background_correction_active_ = dynamic_background_correction_active;
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case CMD_BLUETOOTH:
 | 
			
		||||
      ESP_LOGV(TAG, "Handled bluetooth command");
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    case CMD_SET_LIGHT_CONTROL:
 | 
			
		||||
      ESP_LOGV(TAG, "Handled set light control command");
 | 
			
		||||
      break;
 | 
			
		||||
 | 
			
		||||
    case CMD_QUERY_MOTION_GATE_SENS: {
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
      std::vector<std::function<void(void)>> updates;
 | 
			
		||||
      updates.reserve(this->gate_still_threshold_numbers_.size());
 | 
			
		||||
      for (size_t i = 0; i < this->gate_still_threshold_numbers_.size(); i++) {
 | 
			
		||||
        updates.push_back(set_number_value(this->gate_move_threshold_numbers_[i], this->buffer_data_[10 + i]));
 | 
			
		||||
      }
 | 
			
		||||
      for (auto &update : updates) {
 | 
			
		||||
        update();
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case CMD_QUERY_STATIC_GATE_SENS: {
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
      std::vector<std::function<void(void)>> updates;
 | 
			
		||||
      updates.reserve(this->gate_still_threshold_numbers_.size());
 | 
			
		||||
      for (size_t i = 0; i < this->gate_still_threshold_numbers_.size(); i++) {
 | 
			
		||||
        updates.push_back(set_number_value(this->gate_still_threshold_numbers_[i], this->buffer_data_[10 + i]));
 | 
			
		||||
      }
 | 
			
		||||
      for (auto &update : updates) {
 | 
			
		||||
        update();
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    case CMD_QUERY_BASIC_CONF:  // Query parameters response
 | 
			
		||||
    {
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
      /*
 | 
			
		||||
        Moving distance range: 9th byte
 | 
			
		||||
        Still distance range: 10th byte
 | 
			
		||||
      */
 | 
			
		||||
      std::vector<std::function<void(void)>> updates;
 | 
			
		||||
      updates.push_back(set_number_value(this->min_distance_gate_number_, this->buffer_data_[10]));
 | 
			
		||||
      updates.push_back(set_number_value(this->max_distance_gate_number_, this->buffer_data_[11] - 1));
 | 
			
		||||
      ESP_LOGV(TAG, "min_distance_gate_number_: %u, max_distance_gate_number_ %u", this->buffer_data_[10],
 | 
			
		||||
               this->buffer_data_[11]);
 | 
			
		||||
      /*
 | 
			
		||||
        None Duration: 11~12th bytes
 | 
			
		||||
      */
 | 
			
		||||
      updates.push_back(set_number_value(this->timeout_number_,
 | 
			
		||||
                                         ld2412::two_byte_to_int(this->buffer_data_[12], this->buffer_data_[13])));
 | 
			
		||||
      ESP_LOGV(TAG, "timeout_number_: %u", ld2412::two_byte_to_int(this->buffer_data_[12], this->buffer_data_[13]));
 | 
			
		||||
      /*
 | 
			
		||||
        Output pin configuration: 13th bytes
 | 
			
		||||
      */
 | 
			
		||||
      this->out_pin_level_ = this->buffer_data_[14];
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
      const auto *out_pin_level_str = find_str(OUT_PIN_LEVELS_BY_UINT, this->out_pin_level_);
 | 
			
		||||
      if (this->out_pin_level_select_ != nullptr) {
 | 
			
		||||
        this->out_pin_level_select_->publish_state(out_pin_level_str);
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
      for (auto &update : updates) {
 | 
			
		||||
        update();
 | 
			
		||||
      }
 | 
			
		||||
#endif
 | 
			
		||||
    } break;
 | 
			
		||||
    default:
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::readline_(int readch) {
 | 
			
		||||
  if (readch < 0) {
 | 
			
		||||
    return;  // No data available
 | 
			
		||||
  }
 | 
			
		||||
  if (this->buffer_pos_ < HEADER_FOOTER_SIZE && readch != DATA_FRAME_HEADER[this->buffer_pos_] &&
 | 
			
		||||
      readch != CMD_FRAME_HEADER[this->buffer_pos_]) {
 | 
			
		||||
    this->buffer_pos_ = 0;
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->buffer_pos_ < MAX_LINE_LENGTH - 1) {
 | 
			
		||||
    this->buffer_data_[this->buffer_pos_++] = readch;
 | 
			
		||||
    this->buffer_data_[this->buffer_pos_] = 0;
 | 
			
		||||
  } else {
 | 
			
		||||
    // We should never get here, but just in case...
 | 
			
		||||
    ESP_LOGW(TAG, "Max command length exceeded; ignoring");
 | 
			
		||||
    this->buffer_pos_ = 0;
 | 
			
		||||
  }
 | 
			
		||||
  if (this->buffer_pos_ < 4) {
 | 
			
		||||
    return;  // Not enough data to process yet
 | 
			
		||||
  }
 | 
			
		||||
  if (ld2412::validate_header_footer(DATA_FRAME_FOOTER, &this->buffer_data_[this->buffer_pos_ - 4])) {
 | 
			
		||||
    ESP_LOGV(TAG, "Handling Periodic Data: %s", format_hex_pretty(this->buffer_data_, this->buffer_pos_).c_str());
 | 
			
		||||
    this->handle_periodic_data_();
 | 
			
		||||
    this->buffer_pos_ = 0;  // Reset position index for next message
 | 
			
		||||
  } else if (ld2412::validate_header_footer(CMD_FRAME_FOOTER, &this->buffer_data_[this->buffer_pos_ - 4])) {
 | 
			
		||||
    ESP_LOGV(TAG, "Handling Ack Data: %s", format_hex_pretty(this->buffer_data_, this->buffer_pos_).c_str());
 | 
			
		||||
    if (this->handle_ack_data_()) {
 | 
			
		||||
      this->buffer_pos_ = 0;  // Reset position index for next message
 | 
			
		||||
    } else {
 | 
			
		||||
      ESP_LOGV(TAG, "Ack Data incomplete");
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::set_config_mode_(bool enable) {
 | 
			
		||||
  const uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
 | 
			
		||||
  const uint8_t cmd_value[2] = {0x01, 0x00};
 | 
			
		||||
  this->send_command_(cmd, enable ? cmd_value : nullptr, sizeof(cmd_value));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::set_bluetooth(bool enable) {
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  const uint8_t cmd_value[2] = {enable ? (uint8_t) 0x01 : (uint8_t) 0x00, 0x00};
 | 
			
		||||
  this->send_command_(CMD_BLUETOOTH, cmd_value, sizeof(cmd_value));
 | 
			
		||||
  this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::set_distance_resolution(const std::string &state) {
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  const uint8_t cmd_value[6] = {find_uint8(DISTANCE_RESOLUTIONS_BY_STR, state), 0x00, 0x00, 0x00, 0x00, 0x00};
 | 
			
		||||
  this->send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value, sizeof(cmd_value));
 | 
			
		||||
  this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::set_baud_rate(const std::string &state) {
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  const uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00};
 | 
			
		||||
  this->send_command_(CMD_SET_BAUD_RATE, cmd_value, sizeof(cmd_value));
 | 
			
		||||
  this->set_timeout(200, [this]() { this->restart_(); });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::query_dynamic_background_correction_() {
 | 
			
		||||
  this->send_command_(CMD_QUERY_DYNAMIC_BACKGROUND_CORRECTION, nullptr, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::start_dynamic_background_correction() {
 | 
			
		||||
  if (this->dynamic_background_correction_active_) {
 | 
			
		||||
    return;  // Already in progress
 | 
			
		||||
  }
 | 
			
		||||
#ifdef USE_BINARY_SENSOR
 | 
			
		||||
  if (this->dynamic_background_correction_status_binary_sensor_ != nullptr) {
 | 
			
		||||
    this->dynamic_background_correction_status_binary_sensor_->publish_state(true);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
  this->dynamic_background_correction_active_ = true;
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  this->send_command_(CMD_DYNAMIC_BACKGROUND_CORRECTION, nullptr, 0);
 | 
			
		||||
  this->set_config_mode_(false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::set_engineering_mode(bool enable) {
 | 
			
		||||
  const uint8_t cmd = enable ? CMD_ENABLE_ENG : CMD_DISABLE_ENG;
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  this->send_command_(cmd, nullptr, 0);
 | 
			
		||||
  this->set_config_mode_(false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::factory_reset() {
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  this->send_command_(CMD_FACTORY_RESET, nullptr, 0);
 | 
			
		||||
  this->set_timeout(2000, [this]() { this->restart_and_read_all_info(); });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::restart_() { this->send_command_(CMD_RESTART, nullptr, 0); }
 | 
			
		||||
 | 
			
		||||
void LD2412Component::query_parameters_() { this->send_command_(CMD_QUERY_BASIC_CONF, nullptr, 0); }
 | 
			
		||||
 | 
			
		||||
void LD2412Component::get_version_() { this->send_command_(CMD_QUERY_VERSION, nullptr, 0); }
 | 
			
		||||
 | 
			
		||||
void LD2412Component::get_mac_() {
 | 
			
		||||
  const uint8_t cmd_value[2] = {0x01, 0x00};
 | 
			
		||||
  this->send_command_(CMD_QUERY_MAC_ADDRESS, cmd_value, sizeof(cmd_value));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::get_distance_resolution_() { this->send_command_(CMD_QUERY_DISTANCE_RESOLUTION, nullptr, 0); }
 | 
			
		||||
 | 
			
		||||
void LD2412Component::query_light_control_() { this->send_command_(CMD_QUERY_LIGHT_CONTROL, nullptr, 0); }
 | 
			
		||||
 | 
			
		||||
void LD2412Component::set_basic_config() {
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  if (!this->min_distance_gate_number_->has_state() || !this->max_distance_gate_number_->has_state() ||
 | 
			
		||||
      !this->timeout_number_->has_state()) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
  if (!this->out_pin_level_select_->has_state()) {
 | 
			
		||||
    return;
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
  uint8_t value[5] = {
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
      lowbyte(static_cast<int>(this->min_distance_gate_number_->state)),
 | 
			
		||||
      lowbyte(static_cast<int>(this->max_distance_gate_number_->state) + 1),
 | 
			
		||||
      lowbyte(static_cast<int>(this->timeout_number_->state)),
 | 
			
		||||
      highbyte(static_cast<int>(this->timeout_number_->state)),
 | 
			
		||||
#else
 | 
			
		||||
      1,    TOTAL_GATES, DEFAULT_PRESENCE_TIMEOUT, 0,
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
      find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->state),
 | 
			
		||||
#else
 | 
			
		||||
      0x01,  // Default value if not using select
 | 
			
		||||
#endif
 | 
			
		||||
  };
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  this->send_command_(CMD_BASIC_CONF, value, sizeof(value));
 | 
			
		||||
  this->set_config_mode_(false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
void LD2412Component::set_gate_threshold() {
 | 
			
		||||
  if (this->gate_move_threshold_numbers_.empty() && this->gate_still_threshold_numbers_.empty()) {
 | 
			
		||||
    return;  // No gate threshold numbers set; nothing to do here
 | 
			
		||||
  }
 | 
			
		||||
  uint8_t value[TOTAL_GATES] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  if (!this->gate_move_threshold_numbers_.empty()) {
 | 
			
		||||
    for (size_t i = 0; i < this->gate_move_threshold_numbers_.size(); i++) {
 | 
			
		||||
      value[i] = lowbyte(static_cast<int>(this->gate_move_threshold_numbers_[i]->state));
 | 
			
		||||
    }
 | 
			
		||||
    this->send_command_(CMD_MOTION_GATE_SENS, value, sizeof(value));
 | 
			
		||||
  }
 | 
			
		||||
  if (!this->gate_still_threshold_numbers_.empty()) {
 | 
			
		||||
    for (size_t i = 0; i < this->gate_still_threshold_numbers_.size(); i++) {
 | 
			
		||||
      value[i] = lowbyte(static_cast<int>(this->gate_still_threshold_numbers_[i]->state));
 | 
			
		||||
    }
 | 
			
		||||
    this->send_command_(CMD_STATIC_GATE_SENS, value, sizeof(value));
 | 
			
		||||
  }
 | 
			
		||||
  this->set_config_mode_(false);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::get_gate_threshold() {
 | 
			
		||||
  this->send_command_(CMD_QUERY_MOTION_GATE_SENS, nullptr, 0);
 | 
			
		||||
  this->send_command_(CMD_QUERY_STATIC_GATE_SENS, nullptr, 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::set_gate_still_threshold_number(uint8_t gate, number::Number *n) {
 | 
			
		||||
  this->gate_still_threshold_numbers_[gate] = n;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void LD2412Component::set_gate_move_threshold_number(uint8_t gate, number::Number *n) {
 | 
			
		||||
  this->gate_move_threshold_numbers_[gate] = n;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void LD2412Component::set_light_out_control() {
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  if (this->light_threshold_number_ != nullptr && this->light_threshold_number_->has_state()) {
 | 
			
		||||
    this->light_threshold_ = static_cast<uint8_t>(this->light_threshold_number_->state);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
  if (this->light_function_select_ != nullptr && this->light_function_select_->has_state()) {
 | 
			
		||||
    this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->state);
 | 
			
		||||
  }
 | 
			
		||||
#endif
 | 
			
		||||
  uint8_t value[2] = {this->light_function_, this->light_threshold_};
 | 
			
		||||
  this->set_config_mode_(true);
 | 
			
		||||
  this->send_command_(CMD_SET_LIGHT_CONTROL, value, sizeof(value));
 | 
			
		||||
  this->query_light_control_();
 | 
			
		||||
  this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#ifdef USE_SENSOR
 | 
			
		||||
// These could leak memory, but they are only set once prior to 'setup()' and should never be used again.
 | 
			
		||||
void LD2412Component::set_gate_move_sensor(uint8_t gate, sensor::Sensor *s) {
 | 
			
		||||
  this->gate_move_sensors_[gate] = new SensorWithDedup<uint8_t>(s);
 | 
			
		||||
}
 | 
			
		||||
void LD2412Component::set_gate_still_sensor(uint8_t gate, sensor::Sensor *s) {
 | 
			
		||||
  this->gate_still_sensors_[gate] = new SensorWithDedup<uint8_t>(s);
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,141 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "esphome/core/defines.h"
 | 
			
		||||
#include "esphome/core/component.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_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
 | 
			
		||||
#include "esphome/components/ld24xx/ld24xx.h"
 | 
			
		||||
#include "esphome/components/uart/uart.h"
 | 
			
		||||
#include "esphome/core/automation.h"
 | 
			
		||||
#include "esphome/core/helpers.h"
 | 
			
		||||
 | 
			
		||||
#include <array>
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
using namespace ld24xx;
 | 
			
		||||
 | 
			
		||||
static constexpr uint8_t MAX_LINE_LENGTH = 54;  // Max characters for serial buffer
 | 
			
		||||
static constexpr uint8_t TOTAL_GATES = 14;      // Total number of gates supported by the LD2412
 | 
			
		||||
 | 
			
		||||
class LD2412Component : public Component, public uart::UARTDevice {
 | 
			
		||||
#ifdef USE_BINARY_SENSOR
 | 
			
		||||
  SUB_BINARY_SENSOR(dynamic_background_correction_status)
 | 
			
		||||
  SUB_BINARY_SENSOR(moving_target)
 | 
			
		||||
  SUB_BINARY_SENSOR(still_target)
 | 
			
		||||
  SUB_BINARY_SENSOR(target)
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SENSOR
 | 
			
		||||
  SUB_SENSOR_WITH_DEDUP(light, uint8_t)
 | 
			
		||||
  SUB_SENSOR_WITH_DEDUP(detection_distance, int)
 | 
			
		||||
  SUB_SENSOR_WITH_DEDUP(moving_target_distance, int)
 | 
			
		||||
  SUB_SENSOR_WITH_DEDUP(moving_target_energy, uint8_t)
 | 
			
		||||
  SUB_SENSOR_WITH_DEDUP(still_target_distance, int)
 | 
			
		||||
  SUB_SENSOR_WITH_DEDUP(still_target_energy, uint8_t)
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_TEXT_SENSOR
 | 
			
		||||
  SUB_TEXT_SENSOR(mac)
 | 
			
		||||
  SUB_TEXT_SENSOR(version)
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  SUB_NUMBER(light_threshold)
 | 
			
		||||
  SUB_NUMBER(max_distance_gate)
 | 
			
		||||
  SUB_NUMBER(min_distance_gate)
 | 
			
		||||
  SUB_NUMBER(timeout)
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SELECT
 | 
			
		||||
  SUB_SELECT(baud_rate)
 | 
			
		||||
  SUB_SELECT(distance_resolution)
 | 
			
		||||
  SUB_SELECT(light_function)
 | 
			
		||||
  SUB_SELECT(out_pin_level)
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SWITCH
 | 
			
		||||
  SUB_SWITCH(bluetooth)
 | 
			
		||||
  SUB_SWITCH(engineering_mode)
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_BUTTON
 | 
			
		||||
  SUB_BUTTON(factory_reset)
 | 
			
		||||
  SUB_BUTTON(query)
 | 
			
		||||
  SUB_BUTTON(restart)
 | 
			
		||||
  SUB_BUTTON(start_dynamic_background_correction)
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
 public:
 | 
			
		||||
  void setup() override;
 | 
			
		||||
  void dump_config() override;
 | 
			
		||||
  void loop() override;
 | 
			
		||||
  void set_light_out_control();
 | 
			
		||||
  void set_basic_config();
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  void set_gate_move_threshold_number(uint8_t gate, number::Number *n);
 | 
			
		||||
  void set_gate_still_threshold_number(uint8_t gate, number::Number *n);
 | 
			
		||||
  void set_gate_threshold();
 | 
			
		||||
  void get_gate_threshold();
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SENSOR
 | 
			
		||||
  void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s);
 | 
			
		||||
  void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s);
 | 
			
		||||
#endif
 | 
			
		||||
  void set_engineering_mode(bool enable);
 | 
			
		||||
  void read_all_info();
 | 
			
		||||
  void restart_and_read_all_info();
 | 
			
		||||
  void set_bluetooth(bool enable);
 | 
			
		||||
  void set_distance_resolution(const std::string &state);
 | 
			
		||||
  void set_baud_rate(const std::string &state);
 | 
			
		||||
  void factory_reset();
 | 
			
		||||
  void start_dynamic_background_correction();
 | 
			
		||||
 | 
			
		||||
 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_();
 | 
			
		||||
  bool handle_ack_data_();
 | 
			
		||||
  void readline_(int readch);
 | 
			
		||||
  void query_parameters_();
 | 
			
		||||
  void get_version_();
 | 
			
		||||
  void get_mac_();
 | 
			
		||||
  void get_distance_resolution_();
 | 
			
		||||
  void query_light_control_();
 | 
			
		||||
  void restart_();
 | 
			
		||||
  void query_dynamic_background_correction_();
 | 
			
		||||
 | 
			
		||||
  uint8_t light_function_ = 0;
 | 
			
		||||
  uint8_t light_threshold_ = 0;
 | 
			
		||||
  uint8_t out_pin_level_ = 0;
 | 
			
		||||
  uint8_t buffer_pos_ = 0;  // where to resume processing/populating buffer
 | 
			
		||||
  uint8_t buffer_data_[MAX_LINE_LENGTH];
 | 
			
		||||
  uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0};
 | 
			
		||||
  uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
 | 
			
		||||
  bool bluetooth_on_{false};
 | 
			
		||||
  bool dynamic_background_correction_active_{false};
 | 
			
		||||
#ifdef USE_NUMBER
 | 
			
		||||
  std::array<number::Number *, TOTAL_GATES> gate_move_threshold_numbers_{};
 | 
			
		||||
  std::array<number::Number *, TOTAL_GATES> gate_still_threshold_numbers_{};
 | 
			
		||||
#endif
 | 
			
		||||
#ifdef USE_SENSOR
 | 
			
		||||
  std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_move_sensors_{};
 | 
			
		||||
  std::array<SensorWithDedup<uint8_t> *, TOTAL_GATES> gate_still_sensors_{};
 | 
			
		||||
#endif
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,126 +0,0 @@
 | 
			
		||||
import esphome.codegen as cg
 | 
			
		||||
from esphome.components import number
 | 
			
		||||
import esphome.config_validation as cv
 | 
			
		||||
from esphome.const import (
 | 
			
		||||
    CONF_ID,
 | 
			
		||||
    CONF_MOVE_THRESHOLD,
 | 
			
		||||
    CONF_STILL_THRESHOLD,
 | 
			
		||||
    CONF_TIMEOUT,
 | 
			
		||||
    DEVICE_CLASS_DISTANCE,
 | 
			
		||||
    DEVICE_CLASS_ILLUMINANCE,
 | 
			
		||||
    DEVICE_CLASS_SIGNAL_STRENGTH,
 | 
			
		||||
    ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
    ICON_LIGHTBULB,
 | 
			
		||||
    ICON_MOTION_SENSOR,
 | 
			
		||||
    ICON_TIMELAPSE,
 | 
			
		||||
    UNIT_PERCENT,
 | 
			
		||||
    UNIT_SECOND,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
from .. import CONF_LD2412_ID, LD2412_ns, LD2412Component
 | 
			
		||||
 | 
			
		||||
GateThresholdNumber = LD2412_ns.class_("GateThresholdNumber", number.Number)
 | 
			
		||||
LightThresholdNumber = LD2412_ns.class_("LightThresholdNumber", number.Number)
 | 
			
		||||
MaxDistanceTimeoutNumber = LD2412_ns.class_("MaxDistanceTimeoutNumber", number.Number)
 | 
			
		||||
 | 
			
		||||
CONF_LIGHT_THRESHOLD = "light_threshold"
 | 
			
		||||
CONF_MAX_DISTANCE_GATE = "max_distance_gate"
 | 
			
		||||
CONF_MIN_DISTANCE_GATE = "min_distance_gate"
 | 
			
		||||
 | 
			
		||||
TIMEOUT_GROUP = "timeout"
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = cv.Schema(
 | 
			
		||||
    {
 | 
			
		||||
        cv.GenerateID(CONF_LD2412_ID): cv.use_id(LD2412Component),
 | 
			
		||||
        cv.Optional(CONF_LIGHT_THRESHOLD): number.number_schema(
 | 
			
		||||
            LightThresholdNumber,
 | 
			
		||||
            device_class=DEVICE_CLASS_ILLUMINANCE,
 | 
			
		||||
            entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
            icon=ICON_LIGHTBULB,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_MAX_DISTANCE_GATE): number.number_schema(
 | 
			
		||||
            MaxDistanceTimeoutNumber,
 | 
			
		||||
            device_class=DEVICE_CLASS_DISTANCE,
 | 
			
		||||
            entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
            icon=ICON_MOTION_SENSOR,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_MIN_DISTANCE_GATE): number.number_schema(
 | 
			
		||||
            MaxDistanceTimeoutNumber,
 | 
			
		||||
            device_class=DEVICE_CLASS_DISTANCE,
 | 
			
		||||
            entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
            icon=ICON_MOTION_SENSOR,
 | 
			
		||||
        ),
 | 
			
		||||
        cv.Optional(CONF_TIMEOUT): number.number_schema(
 | 
			
		||||
            MaxDistanceTimeoutNumber,
 | 
			
		||||
            entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
            icon=ICON_TIMELAPSE,
 | 
			
		||||
            unit_of_measurement=UNIT_SECOND,
 | 
			
		||||
        ),
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = CONFIG_SCHEMA.extend(
 | 
			
		||||
    {
 | 
			
		||||
        cv.Optional(f"gate_{x}"): (
 | 
			
		||||
            {
 | 
			
		||||
                cv.Required(CONF_MOVE_THRESHOLD): number.number_schema(
 | 
			
		||||
                    GateThresholdNumber,
 | 
			
		||||
                    device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
 | 
			
		||||
                    entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
                    icon=ICON_MOTION_SENSOR,
 | 
			
		||||
                    unit_of_measurement=UNIT_PERCENT,
 | 
			
		||||
                ),
 | 
			
		||||
                cv.Required(CONF_STILL_THRESHOLD): number.number_schema(
 | 
			
		||||
                    GateThresholdNumber,
 | 
			
		||||
                    device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
 | 
			
		||||
                    entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
                    icon=ICON_MOTION_SENSOR,
 | 
			
		||||
                    unit_of_measurement=UNIT_PERCENT,
 | 
			
		||||
                ),
 | 
			
		||||
            }
 | 
			
		||||
        )
 | 
			
		||||
        for x in range(14)
 | 
			
		||||
    }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    LD2412_component = await cg.get_variable(config[CONF_LD2412_ID])
 | 
			
		||||
    if light_threshold_config := config.get(CONF_LIGHT_THRESHOLD):
 | 
			
		||||
        n = await number.new_number(
 | 
			
		||||
            light_threshold_config, min_value=0, max_value=255, step=1
 | 
			
		||||
        )
 | 
			
		||||
        await cg.register_parented(n, config[CONF_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_light_threshold_number(n))
 | 
			
		||||
    if max_distance_gate_config := config.get(CONF_MAX_DISTANCE_GATE):
 | 
			
		||||
        n = await number.new_number(
 | 
			
		||||
            max_distance_gate_config, min_value=2, max_value=13, step=1
 | 
			
		||||
        )
 | 
			
		||||
        await cg.register_parented(n, config[CONF_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_max_distance_gate_number(n))
 | 
			
		||||
    if min_distance_gate_config := config.get(CONF_MIN_DISTANCE_GATE):
 | 
			
		||||
        n = await number.new_number(
 | 
			
		||||
            min_distance_gate_config, min_value=1, max_value=12, step=1
 | 
			
		||||
        )
 | 
			
		||||
        await cg.register_parented(n, config[CONF_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_min_distance_gate_number(n))
 | 
			
		||||
    for x in range(14):
 | 
			
		||||
        if gate_conf := config.get(f"gate_{x}"):
 | 
			
		||||
            move_config = gate_conf[CONF_MOVE_THRESHOLD]
 | 
			
		||||
            n = cg.new_Pvariable(move_config[CONF_ID], x)
 | 
			
		||||
            await number.register_number(
 | 
			
		||||
                n, move_config, min_value=0, max_value=100, step=1
 | 
			
		||||
            )
 | 
			
		||||
            await cg.register_parented(n, config[CONF_LD2412_ID])
 | 
			
		||||
            cg.add(LD2412_component.set_gate_move_threshold_number(x, n))
 | 
			
		||||
            still_config = gate_conf[CONF_STILL_THRESHOLD]
 | 
			
		||||
            n = cg.new_Pvariable(still_config[CONF_ID], x)
 | 
			
		||||
            await number.register_number(
 | 
			
		||||
                n, still_config, min_value=0, max_value=100, step=1
 | 
			
		||||
            )
 | 
			
		||||
            await cg.register_parented(n, config[CONF_LD2412_ID])
 | 
			
		||||
            cg.add(LD2412_component.set_gate_still_threshold_number(x, n))
 | 
			
		||||
    if timeout_config := config.get(CONF_TIMEOUT):
 | 
			
		||||
        n = await number.new_number(timeout_config, min_value=0, max_value=900, step=1)
 | 
			
		||||
        await cg.register_parented(n, config[CONF_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_timeout_number(n))
 | 
			
		||||
@@ -1,14 +0,0 @@
 | 
			
		||||
#include "gate_threshold_number.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
GateThresholdNumber::GateThresholdNumber(uint8_t gate) : gate_(gate) {}
 | 
			
		||||
 | 
			
		||||
void GateThresholdNumber::control(float value) {
 | 
			
		||||
  this->publish_state(value);
 | 
			
		||||
  this->parent_->set_gate_threshold();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,19 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/number/number.h"
 | 
			
		||||
#include "../ld2412.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
class GateThresholdNumber : public number::Number, public Parented<LD2412Component> {
 | 
			
		||||
 public:
 | 
			
		||||
  GateThresholdNumber(uint8_t gate);
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  uint8_t gate_;
 | 
			
		||||
  void control(float value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,12 +0,0 @@
 | 
			
		||||
#include "light_threshold_number.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
void LightThresholdNumber::control(float value) {
 | 
			
		||||
  this->publish_state(value);
 | 
			
		||||
  this->parent_->set_light_out_control();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,18 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/number/number.h"
 | 
			
		||||
#include "../ld2412.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
class LightThresholdNumber : public number::Number, public Parented<LD2412Component> {
 | 
			
		||||
 public:
 | 
			
		||||
  LightThresholdNumber() = default;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void control(float value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,12 +0,0 @@
 | 
			
		||||
#include "max_distance_timeout_number.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
void MaxDistanceTimeoutNumber::control(float value) {
 | 
			
		||||
  this->publish_state(value);
 | 
			
		||||
  this->parent_->set_basic_config();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,18 +0,0 @@
 | 
			
		||||
#pragma once
 | 
			
		||||
 | 
			
		||||
#include "esphome/components/number/number.h"
 | 
			
		||||
#include "../ld2412.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
class MaxDistanceTimeoutNumber : public number::Number, public Parented<LD2412Component> {
 | 
			
		||||
 public:
 | 
			
		||||
  MaxDistanceTimeoutNumber() = default;
 | 
			
		||||
 | 
			
		||||
 protected:
 | 
			
		||||
  void control(float value) override;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
@@ -1,82 +0,0 @@
 | 
			
		||||
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_LIGHTBULB,
 | 
			
		||||
    ICON_RULER,
 | 
			
		||||
    ICON_SCALE,
 | 
			
		||||
    ICON_THERMOMETER,
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
from .. import CONF_LD2412_ID, LD2412_ns, LD2412Component
 | 
			
		||||
 | 
			
		||||
BaudRateSelect = LD2412_ns.class_("BaudRateSelect", select.Select)
 | 
			
		||||
DistanceResolutionSelect = LD2412_ns.class_("DistanceResolutionSelect", select.Select)
 | 
			
		||||
LightOutControlSelect = LD2412_ns.class_("LightOutControlSelect", select.Select)
 | 
			
		||||
 | 
			
		||||
CONF_DISTANCE_RESOLUTION = "distance_resolution"
 | 
			
		||||
CONF_LIGHT_FUNCTION = "light_function"
 | 
			
		||||
CONF_OUT_PIN_LEVEL = "out_pin_level"
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
CONFIG_SCHEMA = {
 | 
			
		||||
    cv.GenerateID(CONF_LD2412_ID): cv.use_id(LD2412Component),
 | 
			
		||||
    cv.Optional(CONF_BAUD_RATE): select.select_schema(
 | 
			
		||||
        BaudRateSelect,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
        icon=ICON_THERMOMETER,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_DISTANCE_RESOLUTION): select.select_schema(
 | 
			
		||||
        DistanceResolutionSelect,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
        icon=ICON_RULER,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_LIGHT_FUNCTION): select.select_schema(
 | 
			
		||||
        LightOutControlSelect,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
        icon=ICON_LIGHTBULB,
 | 
			
		||||
    ),
 | 
			
		||||
    cv.Optional(CONF_OUT_PIN_LEVEL): select.select_schema(
 | 
			
		||||
        LightOutControlSelect,
 | 
			
		||||
        entity_category=ENTITY_CATEGORY_CONFIG,
 | 
			
		||||
        icon=ICON_SCALE,
 | 
			
		||||
    ),
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
async def to_code(config):
 | 
			
		||||
    LD2412_component = await cg.get_variable(config[CONF_LD2412_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_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_baud_rate_select(s))
 | 
			
		||||
    if distance_resolution_config := config.get(CONF_DISTANCE_RESOLUTION):
 | 
			
		||||
        s = await select.new_select(
 | 
			
		||||
            distance_resolution_config, options=["0.2m", "0.5m", "0.75m"]
 | 
			
		||||
        )
 | 
			
		||||
        await cg.register_parented(s, config[CONF_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_distance_resolution_select(s))
 | 
			
		||||
    if light_function_config := config.get(CONF_LIGHT_FUNCTION):
 | 
			
		||||
        s = await select.new_select(
 | 
			
		||||
            light_function_config, options=["off", "below", "above"]
 | 
			
		||||
        )
 | 
			
		||||
        await cg.register_parented(s, config[CONF_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_light_function_select(s))
 | 
			
		||||
    if out_pin_level_config := config.get(CONF_OUT_PIN_LEVEL):
 | 
			
		||||
        s = await select.new_select(out_pin_level_config, options=["low", "high"])
 | 
			
		||||
        await cg.register_parented(s, config[CONF_LD2412_ID])
 | 
			
		||||
        cg.add(LD2412_component.set_out_pin_level_select(s))
 | 
			
		||||
@@ -1,12 +0,0 @@
 | 
			
		||||
#include "baud_rate_select.h"
 | 
			
		||||
 | 
			
		||||
namespace esphome {
 | 
			
		||||
namespace ld2412 {
 | 
			
		||||
 | 
			
		||||
void BaudRateSelect::control(const std::string &value) {
 | 
			
		||||
  this->publish_state(value);
 | 
			
		||||
  this->parent_->set_baud_rate(state);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ld2412
 | 
			
		||||
}  // namespace esphome
 | 
			
		||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user